slimtoolkit/slim · error
cannot write %s file: %w
Error message
cannot write %s file: %w
What it means
ArchiveFiles wraps any error that occurs while copying a file's contents into the tar archive writer (io.CopyN of th.Size bytes). The %w keeps the underlying cause (e.g. disk full, read failure) so callers can unwrap it. It means the tarball could not be completed for this entry and the whole ArchiveFiles call aborts.
Source
Thrown at pkg/util/fsutil/fsutil.go:1421
th, err := tar.FileInfoHeader(info, "")
if err != nil {
return err
}
th.Name = fpRewrite(fname, true, addPrefix)
if err := tw.WriteHeader(th); err != nil {
return err
}
f, err := os.Open(fname)
if err != nil {
return err
}
defer close(f)
if _, err := io.CopyN(tw, f, th.Size); err != nil {
return fmt.Errorf("cannot write %s file: %w", fname, err)
}
} else {
log.Errorf("fsutil.ArchiveFiles: bad file - %s", fname)
return fmt.Errorf("bad file - %s", fname)
}
}
return nil
}
func ArchiveDir(afname string,
d2aname string,
trimPrefix string,
addPrefix string) error {
dirInfo, err := os.Stat(d2aname)
if err != nil {
return err
}View on GitHub (pinned to 81940d17fa)
Solutions
- Check disk space on the archive destination (df -h) and free space if io.CopyN failed writing.
- Stop or quiesce processes modifying the source files before archiving (snapshot first, e.g. LVM/ZFS snapshot).
- Unwrap the error (errors.Unwrap) to see the underlying cause and address it (read error vs write error).
- Verify the file is a regular readable file before calling ArchiveFiles.
Example fix
// before
cmd.Archive("/var/log/live.log", out)
// after
// snapshot or copy the live file first to a stable path
cmd.Archive("/var/log/live.log.snapshot", out) Defensive patterns
Strategy: try-catch
Validate before calling
info, err := os.Stat(fname)
if err != nil || !info.Mode().IsRegular() {
return fmt.Errorf("skipping %s: not a regular file", fname)
}
// ensure free disk space on destination before archiving Try / catch
err := fsutil.ArchiveFiles(tw, files)
if err != nil {
var wrapped error
if errors.As(err, &wrapped) && errors.Is(wrapped, syscall.ENOSPC) {
// free disk space and retry
}
return fmt.Errorf("archive failed: %w", err)
} Prevention
- Monitor free disk space on the archive destination volume.
- Snapshot or copy live files before archiving so they don't change mid-read.
- Always unwrap and log the underlying cause for diagnosis.
When it happens
Trigger: Calling fsutil.ArchiveFiles (directly or via fsutil.Archive / OnCommand) on a file whose size shrinks below the recorded header size mid-read (io.CopyN returns io.EOF), an I/O read error on the source file, or a write error on the underlying tar/file writer (disk full, permissions).
Common situations: File modified/truncated while being archived (e.g. live log or DB file), out-of-disk-space on the destination volume, or a file on flaky storage that returns read errors.
Related errors
- failed to read file into the tar: %w
- bad output tar - %s
- failed to calculate relative path: %w
- not implemented archiving file type %s (%s)
- failed to scan files: %w
AI-assisted analysis of slimtoolkit/slim@81940d17fa (2026-08-31).
Data as JSON: /api/errors/7754266718708c93.
Report an issue: GitHub.