Apply preserve timestamps for directories (#1979)

* Refactor preserve functionality

* Keep changes minimal
This commit is contained in:
Joe Lim authored and GitHub committed 2025-05-11 10:25:57 +10:00
1 parent 11b0d4e238
commit 84c57cc58a
1 file changed
+25 -19
+25 -19
View File
@@ -5,6 +5,7 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"slices"
"strconv" "strconv"
"strings" "strings"
@@ -54,30 +55,22 @@ func copySize(srcs []string) (int64, error) {
} }
func copyFile(src, dst string, preserve []string, info os.FileInfo, nums chan int64) error { func copyFile(src, dst string, preserve []string, info os.FileInfo, nums chan int64) error {
var dst_mode os.FileMode = 0o666
preserve_timestamps := false
for _, s := range preserve {
switch s {
case "timestamps":
preserve_timestamps = true
case "mode":
dst_mode = info.Mode()
}
}
r, err := os.Open(src) r, err := os.Open(src)
if err != nil { if err != nil {
return err return err
} }
defer r.Close() defer r.Close()
var dst_mode os.FileMode = 0o666
if slices.Contains(preserve, "mode") {
dst_mode = info.Mode()
}
w, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, dst_mode) w, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, dst_mode)
if err != nil { if err != nil {
return err return err
} }
_, err = io.Copy(NewProgressWriter(w, nums), r) if _, err := io.Copy(NewProgressWriter(w, nums), r); err != nil {
if err != nil {
w.Close() w.Close()
os.Remove(dst) os.Remove(dst)
return err return err
@@ -88,10 +81,9 @@ func copyFile(src, dst string, preserve []string, info os.FileInfo, nums chan in
return err return err
} }
if preserve_timestamps { if slices.Contains(preserve, "timestamps") {
ts := times.Get(info) atime := times.Get(info).AccessTime()
mtime := info.ModTime() mtime := info.ModTime()
atime := ts.AccessTime()
if err := os.Chtimes(dst, atime, mtime); err != nil { if err := os.Chtimes(dst, atime, mtime); err != nil {
os.Remove(dst) os.Remove(dst)
return err return err
@@ -106,6 +98,8 @@ func copyAll(srcs []string, dstDir string, preserve []string) (nums chan int64,
errs = make(chan error, 1024) errs = make(chan error, 1024)
go func() { go func() {
dirInfos := make(map[string]os.FileInfo)
for _, src := range srcs { for _, src := range srcs {
file := filepath.Base(src) file := filepath.Base(src)
dst := filepath.Join(dstDir, file) dst := filepath.Join(dstDir, file)
@@ -137,12 +131,16 @@ func copyAll(srcs []string, dstDir string, preserve []string) (nums chan int64,
return nil return nil
} }
newPath := filepath.Join(dst, rel) newPath := filepath.Join(dst, rel)
if info.IsDir() { switch {
case info.IsDir():
if err := os.MkdirAll(newPath, info.Mode()); err != nil { if err := os.MkdirAll(newPath, info.Mode()); err != nil {
errs <- fmt.Errorf("mkdir: %s", err) errs <- fmt.Errorf("mkdir: %s", err)
} }
if slices.Contains(preserve, "timestamps") {
dirInfos[newPath] = info
}
nums <- info.Size() nums <- info.Size()
} else if info.Mode()&os.ModeSymlink != 0 { /* Symlink */ case info.Mode()&os.ModeSymlink != 0:
if rlink, err := os.Readlink(path); err != nil { if rlink, err := os.Readlink(path); err != nil {
errs <- fmt.Errorf("symlink: %s", err) errs <- fmt.Errorf("symlink: %s", err)
} else { } else {
@@ -151,7 +149,7 @@ func copyAll(srcs []string, dstDir string, preserve []string) (nums chan int64,
} }
} }
nums <- info.Size() nums <- info.Size()
} else { default:
if err := copyFile(path, newPath, preserve, info, nums); err != nil { if err := copyFile(path, newPath, preserve, info, nums); err != nil {
errs <- fmt.Errorf("copy: %s", err) errs <- fmt.Errorf("copy: %s", err)
} }
@@ -160,6 +158,14 @@ func copyAll(srcs []string, dstDir string, preserve []string) (nums chan int64,
}) })
} }
for path, info := range dirInfos {
atime := times.Get(info).AccessTime()
mtime := info.ModTime()
if err := os.Chtimes(path, atime, mtime); err != nil {
errs <- fmt.Errorf("chtimes: %s", err)
}
}
close(errs) close(errs)
}() }()