libgocryptfs/common_ops.go

97 lines
2.7 KiB
Go
Raw Normal View History

2021-06-08 21:25:50 +02:00
package main
import (
"C"
"syscall"
"golang.org/x/sys/unix"
2021-10-24 10:48:44 +02:00
"libgocryptfs/v2/internal/nametransform"
"libgocryptfs/v2/internal/syscallcompat"
2021-06-08 21:25:50 +02:00
)
//export gcf_get_attrs
2022-06-23 12:04:36 +02:00
func gcf_get_attrs(sessionID int, relPath string) (uint32, uint64, uint64, bool) {
2022-04-20 21:17:32 +02:00
value, ok := OpenedVolumes.Load(sessionID)
if !ok {
2022-06-23 12:04:36 +02:00
return 0, 0, 0, false
}
2022-04-20 21:17:32 +02:00
volume := value.(*Volume)
2021-06-08 21:25:50 +02:00
dirfd, cName, err := volume.prepareAtSyscall(relPath)
if err != nil {
2022-06-23 12:04:36 +02:00
return 0, 0, 0, false
2021-06-08 21:25:50 +02:00
}
defer syscall.Close(dirfd)
st, err := syscallcompat.Fstatat2(dirfd, cName, unix.AT_SYMLINK_NOFOLLOW)
if err != nil {
2022-06-23 12:04:36 +02:00
return 0, 0, 0, false
2021-06-08 21:25:50 +02:00
}
// Translate ciphertext size to plaintext size
size := volume.translateSize(dirfd, cName, st)
2022-06-23 12:04:36 +02:00
return st.Mode, size, uint64(st.Mtim.Sec), true
2021-06-08 21:25:50 +02:00
}
2021-12-18 14:50:55 +01:00
// libgocryptfs: using Renameat instead of Renameat2 to support older kernels
2021-06-08 21:25:50 +02:00
//export gcf_rename
func gcf_rename(sessionID int, oldPath string, newPath string) bool {
2022-04-20 21:17:32 +02:00
value, ok := OpenedVolumes.Load(sessionID)
if !ok {
return false
}
2022-04-20 21:17:32 +02:00
volume := value.(*Volume)
2021-06-08 21:25:50 +02:00
dirfd, cName, err := volume.prepareAtSyscall(oldPath)
if err != nil {
return false
}
defer syscall.Close(dirfd)
dirfd2, cName2, err := volume.prepareAtSyscall(newPath)
if err != nil {
return false
}
defer syscall.Close(dirfd2)
// Easy case.
if volume.plainTextNames {
2021-12-18 14:50:55 +01:00
return errToBool(syscallcompat.Renameat(dirfd, cName, dirfd2, cName2))
2021-06-08 21:25:50 +02:00
}
// Long destination file name: create .name file
nameFileAlreadyThere := false
if nametransform.IsLongContent(cName2) {
err = volume.nameTransform.WriteLongNameAt(dirfd2, cName2, newPath)
// Failure to write the .name file is expected when the target path already
// exists. Since hashes are pretty unique, there is no need to modify the
// .name file in this case, and we ignore the error.
if err == syscall.EEXIST {
nameFileAlreadyThere = true
} else if err != nil {
return false
}
}
// Actual rename
2021-12-18 14:50:55 +01:00
err = syscallcompat.Renameat(dirfd, cName, dirfd2, cName2)
2021-06-08 21:25:50 +02:00
if err == syscall.ENOTEMPTY || err == syscall.EEXIST {
// If an empty directory is overwritten we will always get an error as
// the "empty" directory will still contain gocryptfs.diriv.
// Interestingly, ext4 returns ENOTEMPTY while xfs returns EEXIST.
// We handle that by trying to fs.Rmdir() the target directory and trying
// again.
if gcf_rmdir(sessionID, newPath) {
2021-12-18 14:50:55 +01:00
err = syscallcompat.Renameat(dirfd, cName, dirfd2, cName2)
2021-06-08 21:25:50 +02:00
}
}
if err != nil {
if nametransform.IsLongContent(cName2) && !nameFileAlreadyThere {
// Roll back .name creation unless the .name file was already there
nametransform.DeleteLongNameAt(dirfd2, cName2)
}
return false
}
if nametransform.IsLongContent(cName) {
nametransform.DeleteLongNameAt(dirfd, cName)
}
return true
}