]> ruderich.org/simon Gitweb - safcm/safcm.git/commitdiff
sync: refactor temporary file creation into WriteTemp()
authorSimon Ruderich <simon@ruderich.org>
Sun, 4 Apr 2021 08:50:05 +0000 (10:50 +0200)
committerSimon Ruderich <simon@ruderich.org>
Mon, 5 Apr 2021 07:41:12 +0000 (09:41 +0200)
Make it public because it will be used by other packages in the near
future.

cmd/safcm-remote/sync/files.go

index 158b5539274f3b54fb1f3671190594a51976e58d..6e001a8186d14675864b4d90794d77694269cee0 100644 (file)
@@ -337,43 +337,11 @@ reopen:
        case 0: // regular file
                debugf("creating temporary file %q",
                        filepath.Join(dir, base+"*"))
        case 0: // regular file
                debugf("creating temporary file %q",
                        filepath.Join(dir, base+"*"))
-               newFh, err := os.CreateTemp(dir, base)
+               tmpPath, err = WriteTemp(dir, base, file.Data,
+                       file.Uid, file.Gid, file.Mode)
                if err != nil {
                        return err
                }
                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
 
        case fs.ModeSymlink:
                i := 0
@@ -505,6 +473,50 @@ func OpenFileNoFollow(path string) (*os.File, error) {
                os.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0)
 }
 
                os.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0)
 }
 
+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.
 // 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.