{"record":{"id":"4000de52346a6849","repo":"siyuan-note/siyuan","slug":"create-import-dir-failed","errorCode":null,"errorMessage":"create import dir failed: ","messagePattern":"create import dir failed: ","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/httprequest.go","lineNumber":363,"sourceCode":"\tmaxReadBytes := int64(maxHTTPRequestBytes)\n\tif !isTextContentType(contentType) {\n\t\tmaxReadBytes = maxHTTPRequestFileBytes\n\t}\n\t// ContentLength 为 -1（chunked）时跳过大小预检，交由 LimitReader 兜底截断。\n\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","sourceCodeStart":345,"sourceCodeEnd":381,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/8641553a1f07374001902d3ce773285db1292b2d/kernel/util/httprequest.go#L345-L381","documentation":"When HTTPRequest receives a non-text response (binary content), it writes the body into <TempDir>/import. Before writing, it creates that directory with os.MkdirAll(importDir, 0755); if directory creation fails, the error is wrapped as \"create import dir failed: <cause>\". This is a local filesystem failure, not an HTTP problem.","triggerScenarios":"HTTPRequest() with a binary Content-Type response while os.MkdirAll on <TempDir>/import fails — read-only filesystem, full disk, permission denied on the temp directory, or TempDir pointing at a path that exists as a regular file.","commonSituations":"Running SiYuan with a read-only or full workspace/temp volume, restrictive umask or sandboxed environments where the temp directory is not writable, or a corrupted workspace where 'import' exists as a file.","solutions":["Check the wrapped cause after the prefix: permission denied vs no space left vs not a directory, and fix accordingly","Verify the workspace temp directory exists and is writable by the SiYuan process user (check disk space with df, permissions with ls -ld)","If <TempDir>/import exists as a regular file, remove or rename it so MkdirAll can create the directory","On sandboxed/managed systems, grant the process write access to its temp directory or relocate the workspace to a writable volume"],"exampleFix":"// diagnosis example\n$ ls -ld <workspace>/temp\n// if 'import' is a file:\n$ mv <workspace>/temp/import <workspace>/temp/import.bak\n$ mkdir <workspace>/temp/import","handlingStrategy":"validation","validationCode":"importDir := filepath.Join(util.TempDir, \"import\")\nif info, err := os.Stat(importDir); err == nil && !info.IsDir() {\n    return errors.New(\"import path exists but is not a directory\")\n}\nif err := os.MkdirAll(importDir, 0755); err != nil {\n    return fmt.Errorf(\"temp dir not writable: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"status, ct, text, err := util.HTTPRequest(\"GET\", binaryURL, nil, \"\")\nif err != nil && strings.Contains(err.Error(), \"create import dir failed\") {\n    // check disk space and permissions on the workspace temp dir before retrying\n    return fmt.Errorf(\"local storage problem, not HTTP: %w\", err)\n}","preventionTips":["Ensure the workspace volume has free space and the process user owns/holds write permission on the temp directory","Check that <TempDir>/import is a directory, not a stale regular file","On sandboxed systems, confirm the temp path is inside the writable sandbox"],"tags":["filesystem","mkdir","permissions","go"],"backgroundTag":"mkdir-permission-denied","analyzedSha":"8641553a1f07374001902d3ce773285db1292b2d","analyzedAt":"2026-09-11T16:08:28.414Z","contentChangedAt":"2026-09-11T16:08:28.414Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}