File: loopback_freebsd.go

package info (click to toggle)
golang-github-hanwen-go-fuse 2.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,584 kB
  • sloc: cpp: 78; sh: 47; makefile: 16
file content (80 lines) | stat: -rw-r--r-- 2,620 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2024 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package fs

import (
	"context"
	"syscall"

	"github.com/hanwen/go-fuse/v2/internal/xattr"
	"golang.org/x/sys/unix"
)

const unix_UTIME_OMIT = unix.UTIME_OMIT

// FreeBSD has added copy_file_range(2) since FreeBSD 12. However,
// golang.org/x/sys/unix hasn't add corresponding syscall constant or
// wrap function. Here we define the syscall constant until sys/unix
// provides.
const sys_COPY_FILE_RANGE = 569

// TODO: replace the manual syscall when sys/unix provides CopyFileRange
// for FreeBSD
func doCopyFileRange(fdIn int, offIn int64, fdOut int, offOut int64,
	len int, flags int) (uint32, syscall.Errno) {
	count, _, errno := unix.Syscall6(sys_COPY_FILE_RANGE,
		uintptr(fdIn), uintptr(offIn), uintptr(fdOut), uintptr(offOut),
		uintptr(len), uintptr(flags),
	)
	return uint32(count), errno
}

func intDev(dev uint32) uint64 {
	return uint64(dev)
}

// Since FUSE on FreeBSD expect Linux flavor data format of
// listxattr, we should reconstruct it with data returned by
// FreeBSD's syscall. And here we have added a "user." prefix
// to put them under "user" namespace, which is readable and
// writable for normal user, for a userspace implemented FS.
func rebuildAttrBuf(attrList [][]byte) []byte {
	ret := make([]byte, 0)
	for _, attrName := range attrList {
		nsAttrName := append([]byte("user."), attrName...)
		ret = append(ret, nsAttrName...)
		ret = append(ret, 0x0)
	}
	return ret
}

var _ = (NodeListxattrer)((*LoopbackNode)(nil))

func (n *LoopbackNode) Listxattr(ctx context.Context, dest []byte) (uint32, syscall.Errno) {
	// In order to simulate same data format as Linux does,
	// and the size of returned buf is required to match, we must
	// call unix.Llistxattr twice.
	sz, err := unix.Llistxattr(n.path(), nil)
	if err != nil {
		return uint32(sz), ToErrno(err)
	}
	rawBuf := make([]byte, sz)
	sz, err = unix.Llistxattr(n.path(), rawBuf)
	if err != nil {
		return uint32(sz), ToErrno(err)
	}
	attrList := xattr.ParseAttrNames(rawBuf)
	rebuiltBuf := rebuildAttrBuf(attrList)
	sz = len(rebuiltBuf)
	if len(dest) != 0 {
		// When len(dest) is 0, which means that caller wants to get
		// the size. If len(dest) is less than len(rebuiltBuf), but greater
		// than 0 dest will be also filled with data from rebuiltBuf,
		// but truncated to len(dest). copy() function will do the same.
		// And this behaviour is same as FreeBSD's syscall extattr_list_file(2).
		sz = copy(dest, rebuiltBuf)
	}
	return uint32(sz), ToErrno(err)
}