{"record":{"id":"2723fb0eea9e79f0","repo":"kopia/kopia","slug":"cannot-create-temporary-file","errorCode":null,"errorMessage":"cannot create temporary file","messagePattern":"cannot create temporary file","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"repo/blob/filesystem/filesystem_storage.go","lineNumber":227,"sourceCode":"\t}), fs.isRetriable)\n\n\treturn err\n}\n\n// createTempFileWithData creates a temporary file, writes data to it, syncs and closes it.\n// Returns the name of the temporary file and an error.\n// If there is an error writing, syncing, or closing the file, the temporary file is removed.\nfunc (fs *fsImpl) createTempFileWithData(path string, data blob.Bytes) (name string, err error) {\n\trandSuffix := make([]byte, tempFileRandomSuffixLen)\n\tif _, err := rand.Read(randSuffix); err != nil {\n\t\treturn \"\", errors.Wrap(err, \"can't get random bytes for temporary filename\")\n\t}\n\n\ttempFile := fmt.Sprintf(\"%s.tmp.%x\", path, randSuffix)\n\n\tf, err := fs.createTempFileAndDir(tempFile)\n\tif err != nil {\n\t\treturn \"\", errors.Wrap(err, \"cannot create temporary file\")\n\t}\n\n\tdefer func() {\n\t\tif closeErr := f.Close(); closeErr != nil {\n\t\t\terr = stderrors.Join(err, errors.Wrap(closeErr, \"can't close temporary file\"))\n\t\t}\n\n\t\t// remove temp file when any of the operations fail\n\t\tif err != nil {\n\t\t\tname = \"\"\n\n\t\t\tif removeErr := fs.osi.Remove(tempFile); removeErr != nil {\n\t\t\t\terr = stderrors.Join(err, errors.Wrap(removeErr, \"can't remove temp file after error\"))\n\t\t\t}\n\t\t}\n\t}()\n\n\tif _, err = data.WriteTo(f); err != nil {","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/kopia/kopia/blob/82495e54b584c1ef6073c9e1be048f57f8aef078/repo/blob/filesystem/filesystem_storage.go#L209-L245","documentation":"createTempFileWithData calls createTempFileAndDir to create a uniquely named '<path>.tmp.<hex>' file (creating intermediate directories as needed); this error wraps any failure to create it, such as permission errors or filesystem exhaustion. The temp-file approach ensures atomic puts via later rename.","triggerScenarios":"PutBlob/PutBlobInPath where creating the temp file fails: read-only filesystem, missing write permission in the target directory, disk full, too many open files, or inability to create missing parent directories.","commonSituations":"Read-only container filesystems; disk-quota or inode exhaustion; wrong ownership on the storage directory; SELinux/AppArmor denying writes; EMFILE after fd leaks.","solutions":["Read the wrapped OS error (EACCES/ENOSPC/EMFILE etc.) and address it specifically","Verify the storage directory is writable by the running user (ls -ld, try touch)","Free disk space / inodes if the error indicates exhaustion (df -h, df -i)","Raise the open-file limit (ulimit -n) if EMFILE, and check for fd leaks"],"exampleFix":"// before (read-only mount)\nmount -o ro /mnt/storage\n// after\nmount -o rw /mnt/storage && chown app:app /mnt/storage/dir","handlingStrategy":"validation","validationCode":"dir := filepath.Dir(targetPath)\nif info, err := os.Stat(dir); err != nil || !info.IsDir() { return fmt.Errorf(\"storage dir missing: %s\", dir) }\nif err := os.WriteFile(filepath.Join(dir, \".write-test\"), nil, 0o600); err != nil { return fmt.Errorf(\"storage dir not writable: %w\", err) }\nos.Remove(filepath.Join(dir, \".write-test\"))","typeGuard":null,"tryCatchPattern":"err := st.PutBlob(ctx, blobID, data, blob.PutOptions{})\nif err != nil && strings.Contains(err.Error(), \"cannot create temporary file\") {\n    var perr *fs.PathError\n    if errors.As(err, &perr) {\n        switch perr.Err {\n        case syscall.ENOSPC: freeDiskSpace()\n        case syscall.EMFILE: raiseFdLimit()\n        case syscall.EACCES: fixPermissions(perr.Path)\n        }\n    }\n    return err\n}","preventionTips":["Check df -h / df -i for disk and inode headroom","Verify storage dir ownership/permissions for the running user","Raise ulimit -n where many files are open","Keep mounts read-write and monitor for ENOSPC/EACCES"],"tags":["filesystem","temp-file","permissions"],"backgroundTag":"file-write-failed","analyzedSha":"82495e54b584c1ef6073c9e1be048f57f8aef078","analyzedAt":"2026-09-07T20:35:21.689Z","contentChangedAt":"2026-09-07T20:35:21.689Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}