This commit is contained in:
2026-02-09 00:42:06 -08:00
commit e6e85b6e11
12 changed files with 708 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
package bsdaily
import (
"fmt"
"io"
"log/slog"
"os"
)
func CopyFile(src, dst string) (err error) {
slog.Info("copying file", "src", src, "dst", dst)
srcFile, err := os.Open(src)
if err != nil {
return fmt.Errorf("opening source %s: %w", src, err)
}
defer srcFile.Close()
srcInfo, err := srcFile.Stat()
if err != nil {
return fmt.Errorf("stat source %s: %w", src, err)
}
dstFile, err := os.Create(dst)
if err != nil {
return fmt.Errorf("creating destination %s: %w", dst, err)
}
defer func() {
if cerr := dstFile.Close(); cerr != nil && err == nil {
err = fmt.Errorf("closing destination %s: %w", dst, cerr)
}
}()
written, err := io.Copy(dstFile, srcFile)
if err != nil {
return fmt.Errorf("copying data: %w", err)
}
if written != srcInfo.Size() {
return fmt.Errorf("short copy: wrote %d bytes, expected %d", written, srcInfo.Size())
}
if err := dstFile.Sync(); err != nil {
return fmt.Errorf("syncing destination %s: %w", dst, err)
}
slog.Info("file copied", "dst", dst, "bytes", written)
return nil
}