X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=cmd%2Fsafcm-remote%2Fsync%2Ffiles.go;h=3e3c7ec18357e8a01e21e22906e6a5e79cb8257f;hb=77197f737073b551e216e55e30c41ebac53a0219;hp=12508ce08c2f7e7d2c17b74dca980285ec01f334;hpb=713cde4bd701dd53ed46d01f43c9e9e7b82dc514;p=safcm%2Fsafcm.git diff --git a/cmd/safcm-remote/sync/files.go b/cmd/safcm-remote/sync/files.go index 12508ce..3e3c7ec 100644 --- a/cmd/safcm-remote/sync/files.go +++ b/cmd/safcm-remote/sync/files.go @@ -328,52 +328,20 @@ reopen: } dir := filepath.Dir(file.Path) - base := filepath.Base(file.Path) + // Create hidden file which should be ignored by most other tools and + // thus not affect anything during creation + base := "." + filepath.Base(file.Path) var tmpPath string switch file.Mode.Type() { case 0: // regular file debugf("creating temporary file %q", - filepath.Join(dir, "."+base+"*")) - // Create hidden file which should be ignored by most other - // tools and thus not affect anything during creation - newFh, err := os.CreateTemp(dir, "."+base) + filepath.Join(dir, base+"*")) + tmpPath, err = WriteTemp(dir, base, file.Data, + file.Uid, file.Gid, file.Mode) if err != nil { return err } - tmpPath = newFh.Name() - - _, err = newFh.Write(file.Data) - if err != nil { - newFh.Close() - os.Remove(tmpPath) - return err - } - // CreateTemp() creates the file with 0600 - err = newFh.Chown(file.Uid, file.Gid) - if err != nil { - newFh.Close() - os.Remove(tmpPath) - return err - } - err = newFh.Chmod(file.Mode) - if err != nil { - newFh.Close() - os.Remove(tmpPath) - return err - } - err = newFh.Sync() - if err != nil { - newFh.Close() - os.Remove(tmpPath) - return err - } - err = newFh.Close() - if err != nil { - newFh.Close() - os.Remove(tmpPath) - return err - } case fs.ModeSymlink: i := 0 @@ -381,7 +349,7 @@ reopen: // Similar to os.CreateTemp() but for symlinks which we cannot // open as file tmpPath = filepath.Join(dir, - "."+base+strconv.Itoa(rand.Int())) + base+strconv.Itoa(rand.Int())) debugf("creating temporary symlink %q", tmpPath) err := os.Symlink(string(file.Data), tmpPath) if err != nil { @@ -408,7 +376,7 @@ reopen: os.Remove(tmpPath) return err } - err = syncPath(dir) + err = SyncPath(dir) if err != nil { return err } @@ -505,7 +473,51 @@ func OpenFileNoFollow(path string) (*os.File, error) { os.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0) } -// syncPath syncs path, which should be a directory. To guarantee durability +func WriteTemp(dir, base string, data []byte, uid, gid int, mode fs.FileMode) ( + string, error) { + + fh, err := os.CreateTemp(dir, base) + if err != nil { + return "", err + } + tmpPath := fh.Name() + + _, err = fh.Write(data) + if err != nil { + fh.Close() + os.Remove(tmpPath) + return "", err + } + // CreateTemp() creates the file with 0600 + err = fh.Chown(uid, gid) + if err != nil { + fh.Close() + os.Remove(tmpPath) + return "", err + } + err = fh.Chmod(mode) + if err != nil { + fh.Close() + os.Remove(tmpPath) + return "", err + } + err = fh.Sync() + if err != nil { + fh.Close() + os.Remove(tmpPath) + return "", err + } + err = fh.Close() + if err != nil { + fh.Close() + os.Remove(tmpPath) + return "", err + } + + return tmpPath, nil +} + +// SyncPath syncs path, which should be a directory. To guarantee durability // it must be called on a parent directory after adding, renaming or removing // files therein. // @@ -513,7 +525,7 @@ func OpenFileNoFollow(path string) (*os.File, error) { // fsync: "Calling fsync() does not necessarily ensure that the entry in the // directory containing the file has also reached disk. For that an explicit // fsync() on a file descriptor for the directory is also needed." -func syncPath(path string) error { +func SyncPath(path string) error { x, err := os.Open(path) if err != nil { return err