Skip to content

Commit

Permalink
🎨 复制文件时保留修改时间
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed May 25, 2021
1 parent 87119a4 commit a68378f
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ func (*GuluFile) CopyFile(source, dest string) (err error) {
if err != nil {
return err
}

defer sourcefile.Close()

if err = os.MkdirAll(filepath.Dir(dest), 0755); nil != err {
Expand All @@ -137,17 +136,16 @@ func (*GuluFile) CopyFile(source, dest string) (err error) {
if err != nil {
return err
}

defer destfile.Close()

_, err = io.Copy(destfile, sourcefile)
if err == nil {
if nil == err {
sourceinfo, err := os.Stat(source)
if err != nil {
err = os.Chmod(dest, sourceinfo.Mode())
if nil == err {
os.Chmod(dest, sourceinfo.Mode())
os.Chtimes(dest, sourceinfo.ModTime(), sourceinfo.ModTime())
}
}

return nil
}

Expand All @@ -159,16 +157,15 @@ func (*GuluFile) CopyDir(source, dest string) (err error) {
}

// create dest dir
err = os.MkdirAll(dest, sourceinfo.Mode())
if err != nil {
if err = os.MkdirAll(dest, sourceinfo.Mode()); err != nil {
return err
}
os.Chtimes(dest, sourceinfo.ModTime(), sourceinfo.ModTime())

directory, err := os.Open(source)
if err != nil {
return err
}

defer directory.Close()

objects, err := directory.Readdir(-1)
Expand All @@ -193,6 +190,5 @@ func (*GuluFile) CopyDir(source, dest string) (err error) {
}
}
}

return nil
}

0 comments on commit a68378f

Please sign in to comment.