| 1 | package reversesshfs |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strconv" |
| 6 | |
| 7 | "github.com/pkg/sftp" |
| 8 | "golang.org/x/sys/unix" |
| 9 | ) |
| 10 | |
| 11 | func statVFS(st *unix.Statfs_t) *sftp.StatVFS { |
| 12 | return &sftp.StatVFS{ |
| 13 | Bsize: uint64(st.Bsize), |
| 14 | Frsize: uint64(st.Frsize), |
| 15 | Blocks: st.Blocks, |
| 16 | Bfree: st.Bfree, |
| 17 | Bavail: st.Bavail, |
| 18 | Files: st.Files, |
| 19 | Ffree: st.Ffree, |
| 20 | Favail: st.Ffree, |
| 21 | Flag: uint64(st.Flags), |
| 22 | Namemax: uint64(st.Namelen), |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | |
| 27 | func renameNoReplace(oldfd int, oldBase string, newfd int, newBase string) error { |
| 28 | return unix.Renameat2(oldfd, oldBase, newfd, newBase, unix.RENAME_NOREPLACE) |
| 29 | } |
| 30 | |
| 31 | |
| 32 | |
| 33 | func fchmodatNoFollow(dirfd int, base string, mode uint32) error { |
| 34 | err := unix.Fchmodat(dirfd, base, mode, unix.AT_SYMLINK_NOFOLLOW) |
| 35 | if !errors.Is(err, unix.EOPNOTSUPP) && !errors.Is(err, unix.ENOSYS) { |
| 36 | return err |
| 37 | } |
| 38 | return fchmodatOPath(dirfd, base, mode) |
| 39 | } |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | func fchmodatOPath(dirfd int, base string, mode uint32) error { |
| 45 | fd, err := unix.Openat(dirfd, base, unix.O_PATH|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0) |
| 46 | if err != nil { |
| 47 | return err |
| 48 | } |
| 49 | defer unix.Close(fd) |
| 50 | var st unix.Stat_t |
| 51 | if err := unix.Fstat(fd, &st); err != nil { |
| 52 | return err |
| 53 | } |
| 54 | if st.Mode&unix.S_IFMT == unix.S_IFLNK { |
| 55 | return unix.ELOOP |
| 56 | } |
| 57 | return unix.Chmod("/proc/self/fd/"+strconv.Itoa(fd), mode) |
| 58 | } |
| 59 | |
| 60 | |
| 61 | |
| 62 | func cloudOpenat(fd int, name string, flags int, mode uint32) (int, error) { |
| 63 | if flags&unix.O_CREAT == 0 { |
| 64 | mode = 0 |
| 65 | } |
| 66 | return unix.Openat2(fd, name, &unix.OpenHow{Flags: uint64(flags), Mode: uint64(mode), Resolve: unix.RESOLVE_NO_XDEV | unix.RESOLVE_NO_SYMLINKS | unix.RESOLVE_BENEATH}) |
| 67 | } |