Files
Gogs/internal/iox/iox.go
ᴊᴏᴇ ᴄʜᴇɴ 36d56d5525 all: rename packages ending with "util" to end with "x" (#8182)
Co-authored-by: JSS <jss@unknwon.dev>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-16 13:25:19 -05:00

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
}