{"record":{"id":"7754266718708c93","repo":"slimtoolkit/slim","slug":"cannot-write-s-file-w","errorCode":null,"errorMessage":"cannot write %s file: %w","messagePattern":"cannot write (.+?) file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/util/fsutil/fsutil.go","lineNumber":1421,"sourceCode":"\n\t\t\tth, err := tar.FileInfoHeader(info, \"\")\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\tth.Name = fpRewrite(fname, true, addPrefix)\n\t\t\tif err := tw.WriteHeader(th); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\tf, err := os.Open(fname)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\tdefer close(f)\n\t\t\tif _, err := io.CopyN(tw, f, th.Size); err != nil {\n\t\t\t\treturn fmt.Errorf(\"cannot write %s file: %w\", fname, err)\n\t\t\t}\n\t\t} else {\n\t\t\tlog.Errorf(\"fsutil.ArchiveFiles: bad file - %s\", fname)\n\t\t\treturn fmt.Errorf(\"bad file - %s\", fname)\n\t\t}\n\t}\n\n\treturn nil\n}\n\nfunc ArchiveDir(afname string,\n\td2aname string,\n\ttrimPrefix string,\n\taddPrefix string) error {\n\tdirInfo, err := os.Stat(d2aname)\n\tif err != nil {\n\t\treturn err\n\t}","sourceCodeStart":1403,"sourceCodeEnd":1439,"githubUrl":"https://github.com/slimtoolkit/slim/blob/81940d17fa112cc678e32209214bcb2355cb3004/pkg/util/fsutil/fsutil.go#L1403-L1439","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\ncmd.Archive(\"/var/log/live.log\", out)\n// after\n// snapshot or copy the live file first to a stable path\ncmd.Archive(\"/var/log/live.log.snapshot\", out)","handlingStrategy":"try-catch","validationCode":"info, err := os.Stat(fname)\nif err != nil || !info.Mode().IsRegular() {\n    return fmt.Errorf(\"skipping %s: not a regular file\", fname)\n}\n// ensure free disk space on destination before archiving","typeGuard":null,"tryCatchPattern":"err := fsutil.ArchiveFiles(tw, files)\nif err != nil {\n    var wrapped error\n    if errors.As(err, &wrapped) && errors.Is(wrapped, syscall.ENOSPC) {\n        // free disk space and retry\n    }\n    return fmt.Errorf(\"archive failed: %w\", err)\n}","preventionTips":["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."],"tags":["filesystem","archive","tar","io"],"backgroundTag":"archive-write-failed","analyzedSha":"81940d17fa112cc678e32209214bcb2355cb3004","analyzedAt":"2026-08-31T23:06:12.682Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}