| 1 | package reversesshfs |
| 2 | |
| 3 | import ( |
| 4 | "github.com/pkg/sftp" |
| 5 | "golang.org/x/sys/unix" |
| 6 | ) |
| 7 | |
| 8 | func statVFS(st *unix.Statfs_t) *sftp.StatVFS { |
| 9 | return &sftp.StatVFS{ |
| 10 | Bsize: uint64(st.Bsize), |
| 11 | Frsize: uint64(st.Bsize), |
| 12 | Blocks: st.Blocks, |
| 13 | Bfree: st.Bfree, |
| 14 | Bavail: st.Bavail, |
| 15 | Files: st.Files, |
| 16 | Ffree: st.Ffree, |
| 17 | Favail: st.Ffree, |
| 18 | Flag: uint64(st.Flags), |
| 19 | Namemax: 255, |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | |
| 24 | func renameNoReplace(oldfd int, oldBase string, newfd int, newBase string) error { |
| 25 | return unix.RenameatxNp(oldfd, oldBase, newfd, newBase, unix.RENAME_EXCL) |
| 26 | } |
| 27 | |
| 28 | |
| 29 | |
| 30 | func fchmodatNoFollow(dirfd int, base string, mode uint32) error { |
| 31 | var st unix.Stat_t |
| 32 | if err := unix.Fstatat(dirfd, base, &st, unix.AT_SYMLINK_NOFOLLOW); err != nil { |
| 33 | return err |
| 34 | } |
| 35 | if st.Mode&unix.S_IFMT == unix.S_IFLNK { |
| 36 | return unix.ELOOP |
| 37 | } |
| 38 | return unix.Fchmodat(dirfd, base, mode, unix.AT_SYMLINK_NOFOLLOW) |
| 39 | } |
| 40 | |
| 41 | |
| 42 | |
| 43 | func cloudOpenat(fd int, name string, flags int, mode uint32) (int, error) { |
| 44 | next, err := unix.Openat(fd, name, flags, mode) |
| 45 | if err != nil { |
| 46 | return -1, err |
| 47 | } |
| 48 | var parent, child unix.Stat_t |
| 49 | if err = unix.Fstat(fd, &parent); err == nil { |
| 50 | err = unix.Fstat(next, &child) |
| 51 | } |
| 52 | if err == nil && parent.Dev != child.Dev { |
| 53 | err = unix.EXDEV |
| 54 | } |
| 55 | if err != nil { |
| 56 | unix.Close(next) |
| 57 | return -1, err |
| 58 | } |
| 59 | return next, nil |
| 60 | } |