mirror of
https://github.com/gogs/gogs.git
synced 2026-02-18 12:27:01 +01:00
Co-authored-by: JSS <jss@unknwon.dev> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
48 lines
890 B
Go
48 lines
890 B
Go
package iox
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/cockroachdb/errors"
|
|
)
|
|
|
|
// CopyFile copies the file at src to dst, preserving file mode and
|
|
// modification time.
|
|
func CopyFile(src, dst string) error {
|
|
si, err := os.Stat(src)
|
|
if err != nil {
|
|
return errors.Wrap(err, "stat source")
|
|
}
|
|
|
|
in, err := os.Open(src)
|
|
if err != nil {
|
|
return errors.Wrap(err, "open source")
|
|
}
|
|
defer in.Close()
|
|
|
|
out, err := os.Create(dst)
|
|
if err != nil {
|
|
return errors.Wrap(err, "create target")
|
|
}
|
|
defer out.Close()
|
|
|
|
if _, err = io.Copy(out, in); err != nil {
|
|
return errors.Wrap(err, "copy")
|
|
}
|
|
|
|
if err = out.Sync(); err != nil {
|
|
return errors.Wrap(err, "sync target")
|
|
}
|
|
|
|
if err = os.Chmod(dst, si.Mode()); err != nil {
|
|
return errors.Wrap(err, "chmod target")
|
|
}
|
|
|
|
if err = os.Chtimes(dst, si.ModTime(), si.ModTime()); err != nil {
|
|
return errors.Wrap(err, "chtimes target")
|
|
}
|
|
|
|
return nil
|
|
}
|