{"record":{"id":"fba452f7b2595e8a","repo":"Tencent/WeKnora","slug":"failed-to-create-destination-file-w","errorCode":null,"errorMessage":"failed to create destination file: %w","messagePattern":"failed to create destination file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/application/service/file/local.go","lineNumber":201,"sourceCode":"\t\treturn \"\", fmt.Errorf(\"invalid path: %w\", err)\n\t}\n\tif err := os.MkdirAll(dir, 0o755); err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to create directory: %w\", err)\n\t}\n\n\text := filepath.Ext(srcPath)\n\tfilename := fmt.Sprintf(\"%d%s\", time.Now().UnixNano(), ext)\n\tdstPath := filepath.Join(dir, filename)\n\n\tsrc, err := os.Open(srcResolved)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to open source file: %w\", err)\n\t}\n\tdefer src.Close()\n\n\tdst, err := os.Create(dstPath)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to create destination file: %w\", err)\n\t}\n\tdefer dst.Close()\n\n\tif _, err := io.Copy(dst, src); err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to copy file content: %w\", err)\n\t}\n\n\trelPath, _ := filepath.Rel(s.baseDir, dstPath)\n\tnewPath := localScheme + filepath.ToSlash(relPath)\n\tlogger.Infof(ctx, \"Copied local file %s to %s\", srcPath, newPath)\n\treturn newPath, nil\n}\n\n// SaveBytes saves bytes data to a file and returns the file path\n// temp parameter is ignored for local storage (no auto-expiration support)\n// fileName 仅允许安全文件名，禁止路径遍历（如 ../../）\nfunc (s *localFileService) SaveBytes(ctx context.Context, data []byte, tenantID uint64, fileName string, temp bool) (string, error) {\n\tlogger.Infof(ctx, \"Saving bytes data: fileName=%s, size=%d, tenantID=%d, temp=%v\", fileName, len(data), tenantID, temp)","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/application/service/file/local.go#L183-L219","documentation":"CopyFile fails to create the destination file at dstPath (<dir>/<unixNano><ext>) via os.Create. The directory was successfully created, but creating the file inside it failed — typically due to permissions, disk-full, or a name/path problem (e.g. dstPath became too long).","triggerScenarios":"Destination directory lacks write permission for the process; filesystem is full or read-only (quota exceeded); dstPath exceeds the filesystem NAME_MAX because a very long file extension inflates the generated name; SELinux/AppArmor policy blocks creation.","commonSituations":"Disk quota exhausted on the storage volume (common in CI and containers); read-only root filesystem with baseDir inside it; long knowledgeID plus long extension pushing path over 255-byte component limit; misconfigured security profiles blocking writes.","solutions":["Read the wrapped error: EDQUOT/ENOSPC → free space or raise quota on the volume; EACCES → fix directory write permissions.","Check disk usage (`df -h`) on the volume backing s.baseDir and clean up or expand storage.","Sanitize/limit the file extension (filepath.Ext of srcPath) so the generated filename stays within NAME_MAX.","If baseDir is on a read-only mount, reconfigure storage to a writable persistent volume."],"exampleFix":"// before\next := filepath.Ext(srcPath)\nfilename := fmt.Sprintf(\"%d%s\", time.Now().UnixNano(), ext)\n// after\next := filepath.Ext(srcPath)\nif len(ext) > 16 {\n    ext = ext[:16]\n}\nfilename := fmt.Sprintf(\"%d%s\", time.Now().UnixNano(), ext)","handlingStrategy":"validation","validationCode":"func ensureDestWritable(dir string) error {\n    if fi, err := os.Stat(dir); err != nil || !fi.IsDir() {\n        return fmt.Errorf(\"dest dir invalid: %w\", err)\n    }\n    probe := filepath.Join(dir, \".probe\")\n    if err := os.WriteFile(probe, []byte{0}, 0o600); err != nil {\n        return fmt.Errorf(\"dest not writable: %w\", err)\n    }\n    os.Remove(probe)\n    return nil\n}\n// also check disk space: syscall.Statfs(dir) → Bavail* Bsize > needed bytes","typeGuard":null,"tryCatchPattern":"newPath, err := svc.CopyFile(ctx, srcPath, tenantID, knowledgeID)\nif err != nil {\n    var perr *fs.PathError\n    if errors.As(err, &perr) {\n        switch {\n        case errors.Is(perr.Err, syscall.ENOSPC), errors.Is(perr.Err, syscall.EDQUOT):\n            return fmt.Errorf(\"storage full: %w\", err) // trigger cleanup/expand volume\n        case errors.Is(perr.Err, syscall.EACCES):\n            return fmt.Errorf(\"permission denied on storage: %w\", err)\n        }\n    }\n    return fmt.Errorf(\"copy failed: %w\", err)\n}","preventionTips":["Monitor free space and inodes on the storage volume; alert before hitting limits.","Configure baseDir on a persistent, writable volume — never the container's read-only root layer.","Sanitize/limit stored file extensions to keep generated names under NAME_MAX.","Test SELinux/AppArmor policies for write access to the storage path in staging."],"tags":["filesystem","disk-full","permissions","io"],"backgroundTag":"disk-full","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}