{"record":{"id":"3b8c56f258fdbd5b","repo":"siyuan-note/siyuan","slug":"write-file-failed","errorCode":null,"errorMessage":"write file failed: ","messagePattern":"write file failed: ","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/httprequest.go","lineNumber":368,"sourceCode":"\tif resp.ContentLength > maxReadBytes {\n\t\treturn statusCode, contentType, \"\", errors.New(\"response too large\")\n\t}\n\n\trespBody, rerr := io.ReadAll(io.LimitReader(resp.Body, maxReadBytes))\n\tif rerr != nil {\n\t\treturn statusCode, contentType, \"\", errors.New(\"read body failed: \" + rerr.Error())\n\t}\n\n\t// 二进制响应落盘，返回文件路径，供智能体按需进一步处理。\n\tif !isTextContentType(contentType) {\n\t\timportDir := filepath.Join(TempDir, \"import\")\n\t\tif merr := os.MkdirAll(importDir, 0755); merr != nil {\n\t\t\treturn statusCode, contentType, \"\", errors.New(\"create import dir failed: \" + merr.Error())\n\t\t}\n\t\tfilename := extractFilename(rawURL, contentType)\n\t\tfilePath := filepath.Join(importDir, filename)\n\t\tif werr := os.WriteFile(filePath, respBody, 0644); werr != nil {\n\t\t\treturn statusCode, contentType, \"\", errors.New(\"write file failed: \" + werr.Error())\n\t\t}\n\t\treturn statusCode, contentType, fmt.Sprintf(\"Saved to: %s (%d bytes)\", filePath, len(respBody)), nil\n\t}\n\n\treturn statusCode, contentType, truncateRunes(string(respBody), maxHTTPRequestChars), nil\n}\n\n// isTextContentType 判断 Content-Type 是否为可直接展示给智能体的文本类响应。\n// 覆盖 text/*、application/json、application/xml、application/*+json 等。\nfunc isTextContentType(contentType string) bool {\n\tct := strings.ToLower(strings.TrimSpace(strings.SplitN(contentType, \";\", 2)[0]))\n\tif ct == \"\" {\n\t\treturn false\n\t}\n\tif strings.HasPrefix(ct, \"text/\") {\n\t\treturn true\n\t}\n\tswitch ct {","sourceCodeStart":350,"sourceCodeEnd":386,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/httprequest.go#L350-L386","documentation":"After creating the import directory, HTTPRequest writes the binary response body to <TempDir>/import/<filename> via os.WriteFile. This error wraps any failure of that write — permission denied, disk full, the target path being a directory, or an I/O error — appending the OS message after \"write file failed: \".","triggerScenarios":"Calling HTTPRequest with a binary response when the import directory exists but is not writable, the disk fills mid-write, the derived filename (from extractFilename of URL/Content-Type) resolves to an existing directory or an invalid path, or antivirus/EDR locks the target file.","commonSituations":"Full disk during large binary downloads; a directory already exists with the exact filename the server's Content-Disposition/URL implies; read-only temp volume; security software quarantining the file mid-write; concurrent writes colliding on the same filename.","solutions":["Read the appended OS error (permission denied / no space left on device / is a directory) and fix the concrete cause","Free disk space or raise quota on the temp volume","Correct permissions on <TempDir>/import so the kernel process can create files","If the filename collides with an existing directory, remove that directory or clear old import files","Check antivirus/EDR exclusions for the workspace temp path"],"exampleFix":"// before\nopen /tmp/siyuan/import/report.pdf: is a directory\n// after\nrm -rf /tmp/siyuan/import/report.pdf   # remove the colliding directory, then retry the fetch","handlingStrategy":"validation","validationCode":"importDir := filepath.Join(util.TempDir, \"import\")\nprobe := filepath.Join(importDir, \".write-probe\")\nif err := os.WriteFile(probe, []byte(\"ok\"), 0o644); err != nil {\n    return fmt.Errorf(\"import dir not writable: %w\", err)\n}\nos.Remove(probe)","typeGuard":null,"tryCatchPattern":"if err != nil && strings.HasPrefix(err.Error(), \"write file failed:\") {\n    // free disk space / fix perms / clear filename collisions, then retry\n}","preventionTips":["Monitor free disk space on the temp volume","Periodically clean old files from <TempDir>/import","Exclude the temp path from antivirus interference"],"tags":["filesystem","file-write","permissions","go"],"backgroundTag":"file-write-failed","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}