{"record":{"id":"de75e496f729ee34","repo":"yorukot/superfile","slug":"failed-to-open-source-file-w","errorCode":null,"errorMessage":"failed to open source file: %w","messagePattern":"failed to open source file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/internal/file_operations.go","lineNumber":140,"sourceCode":"\n// is an equivalent of \"cp -P\" command\nfunc copyLinkFile(src, dst string) error {\n\ttarget, err := os.Readlink(src)\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn os.Symlink(target, dst)\n}\n\nfunc isSymlink(info os.FileInfo) bool {\n\treturn info.Mode()&os.ModeSymlink != 0\n}\n\n// copyFile copies a single file\nfunc copyFile(src, dst string, srcInfo os.FileInfo) error {\n\tsrcFile, err := os.Open(src)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to open source file: %w\", err)\n\t}\n\tdefer srcFile.Close()\n\n\tdstFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, srcInfo.Mode())\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to create destination file: %w\", err)\n\t}\n\tdefer dstFile.Close()\n\n\tif _, err := io.Copy(dstFile, srcFile); err != nil {\n\t\treturn fmt.Errorf(\"failed to copy file contents: %w\", err)\n\t}\n\treturn nil\n}\n\n// pasteDir handles directory copying with progress tracking\nfunc pasteDir(src, dst string, p *processbar.Process, cut bool, processBarModel *processbar.Model) error {\n\tdst, err := renameIfDuplicate(dst)","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/yorukot/superfile/blob/b72f550bc6e75913f48eb49fdd258e1a2df2fe88/src/internal/file_operations.go#L122-L158","documentation":"copyFile wraps os.Open(src) failures when opening the source file for reading during a copy. Typical wrapped causes are ENOENT (file gone) and EACCES (no read permission). The copy of that single file fails immediately.","triggerScenarios":"copyElement, copyDir or actualPasteOperation calls copyFile with a src path that was deleted after the directory listing, or a file the user cannot read (mode 0600 owned by another user, or read permission removed).","commonSituations":"Pasting files after the original was deleted or renamed; copying files with restrictive permissions; reading from a mount whose contents changed; a file locked/deleted by another process.","solutions":["Verify the source file exists (ls or os.Stat) and re-select it if it was moved or deleted.","Fix read permissions: chmod u+r (or chown) so the running user can read the file.","If running with elevated context lost (e.g. sudo copy earlier), re-copy with correct ownership.","Retry the paste; if it recurs, check for processes deleting files concurrently."],"exampleFix":"// before\nerr := ops.CopyFile(srcPath, dstPath)\n// after\nif _, statErr := os.Stat(srcPath); errors.Is(statErr, fs.ErrNotExist) {\n    return fmt.Errorf(\"source %s no longer exists; refresh selection and retry\", srcPath)\n}\nif err := ops.CopyFile(srcPath, dstPath); err != nil {\n    var pe *fs.PathError\n    if errors.As(err, &pe) && errors.Is(pe.Err, fs.ErrPermission) {\n        return fmt.Errorf(\"cannot read %s: fix permissions (%w)\", srcPath, err)\n    }\n    return err\n}","handlingStrategy":"validation","validationCode":"info, err := os.Stat(src)\nif err != nil {\n    return fmt.Errorf(\"source unavailable: %w\", err)\n}\nif info.IsDir() {\n    return fmt.Errorf(\"%s is a directory, not a file\", src)\n}\nf, err := os.OpenFile(src, os.O_RDONLY, 0)\nif err != nil {\n    return fmt.Errorf(\"source not readable: %w\", err)\n}\nf.Close()","typeGuard":null,"tryCatchPattern":"if err := ops.CopyFile(src, dst); err != nil {\n    if errors.Is(err, fs.ErrNotExist) { /* refresh selection */ }\n    if errors.Is(err, fs.ErrPermission) { /* fix chmod */ }\n    return err\n}","preventionTips":["Refresh file selections before every paste to drop deleted sources.","Check read permission bits on files you plan to copy.","Avoid copying files owned by other users without adjusting permissions first."],"tags":["go","filesystem","copy","file-open"],"backgroundTag":"file-open-failed","analyzedSha":"b72f550bc6e75913f48eb49fdd258e1a2df2fe88","analyzedAt":"2026-09-01T04:38:08.254Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}