{"record":{"id":"2919efc6eea7df90","repo":"Tencent/WeKnora","slug":"failed-to-create-file-w","errorCode":null,"errorMessage":"failed to create file: %w","messagePattern":"failed to create file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/application/service/file/local.go","lineNumber":90,"sourceCode":"\tfilename := fmt.Sprintf(\"%d%s\", time.Now().UnixNano(), ext)\n\tfilePath := filepath.Join(dir, filename)\n\tlogger.Infof(ctx, \"Generated file path: %s\", filePath)\n\n\t// Open source file for reading\n\tlogger.Info(ctx, \"Opening source file\")\n\tsrc, err := file.Open()\n\tif err != nil {\n\t\tlogger.Errorf(ctx, \"Failed to open source file: %v\", err)\n\t\treturn \"\", fmt.Errorf(\"failed to open file: %w\", err)\n\t}\n\tdefer src.Close()\n\n\t// Create destination file for writing\n\tlogger.Info(ctx, \"Creating destination file\")\n\tdst, err := os.Create(filePath)\n\tif err != nil {\n\t\tlogger.Errorf(ctx, \"Failed to create destination file: %v\", err)\n\t\treturn \"\", fmt.Errorf(\"failed to create file: %w\", err)\n\t}\n\tdefer dst.Close()\n\n\t// Copy content from source to destination\n\tlogger.Info(ctx, \"Copying file content\")\n\tif _, err := io.Copy(dst, src); err != nil {\n\t\tlogger.Errorf(ctx, \"Failed to copy file content: %v\", err)\n\t\treturn \"\", fmt.Errorf(\"failed to save file: %w\", err)\n\t}\n\n\tlogger.Infof(ctx, \"File saved successfully: %s\", filePath)\n\t// Return provider:// path format: local://{relative_path}\n\trelPath, _ := filepath.Rel(s.baseDir, filePath)\n\treturn localScheme + filepath.ToSlash(relPath), nil\n}\n\n// GetFile retrieves a file from the local file system by its path\n// Returns a ReadCloser for reading the file content","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/application/service/file/local.go#L72-L108","documentation":"SaveFile wraps the error from os.Create(filePath) when the destination file under the storage base directory cannot be created. This is a write-side filesystem error: the wrapped error carries the exact path and reason. The destination is never created in this case.","triggerScenarios":"os.Create fails because the directory under baseDir does not exist, the process lacks write permission on it, the disk is full, the path component is invalid, or baseDir itself was misconfigured to a non-existent/unwritable location.","commonSituations":"LOCAL_STORAGE_BASE_DIR pointing at a read-only volume or path that was never mkdir'd; container running as non-root user without ownership of the storage volume; disk quota/full-disk in production; SELinux/apparmor blocking writes.","solutions":["Ensure the storage directory exists and is writable by the service user (mkdir -p and chown the baseDir)","Verify LOCAL_STORAGE_BASE_DIR is correct and mounted (not read-only) in the deployment environment","Check disk space with df and clean up or expand the volume if full","Inspect the wrapped *fs.PathError path to confirm which directory is the problem"],"exampleFix":"// before service construction\nsvc := file.NewLocalService(cfg.BaseDir)\n// after: fail fast at startup if the base dir is unusable\nif err := os.MkdirAll(cfg.BaseDir, 0o755); err != nil {\n\tlog.Fatalf(\"storage base dir unusable: %v\", err)\n}","handlingStrategy":"validation","validationCode":"info, err := os.Stat(baseDir)\nif err != nil || !info.IsDir() {\n\treturn fmt.Errorf(\"storage base dir %q missing\", baseDir)\n}\nif info.Mode().Perm()&0o200 == 0 {\n\treturn fmt.Errorf(\"storage base dir %q not writable\", baseDir)\n}","typeGuard":null,"tryCatchPattern":"dst, err := os.Create(filePath)\nif err != nil {\n\treturn fmt.Errorf(\"failed to create file: %w\", err)\n}","preventionTips":["MkdirAll the baseDir at service startup and fail fast if it fails","Verify the storage volume is mounted read-write in container/deploy config","Run the service as a user that owns the storage directory","Monitor disk space and quotas on the storage volume"],"tags":["go","file-io","filesystem","permissions"],"backgroundTag":"file-create-failed","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}