siyuan-note/siyuan · critical

truncate file [ ] failed

Error message

truncate file [%s] failed: %s

What it means

WriteFileByMmap writes a file by truncating it to the new length and memory-mapping it. If f.Truncate fails (I/O error, full disk, unsupported filesystem, permission problem), the function logs and returns 'truncate file [...] failed: <os error>'. Used by WriteTree and saveAttributeView to persist .sy files and attribute views efficiently.

Solutions

  1. Free disk space or resolve quota limits on the workspace volume
  2. Verify the file and volume are writable (check read-only mount, file locks, antivirus)
  3. Check the embedded os error in the message: 'permission denied' -> fix ACLs; 'disk quota'/'no space' -> free space; 'invalid argument' -> filesystem may not support truncate, move workspace to a local disk
  4. Retry saving after resolving; SiYuan will rewrite the file on the next save operation
Defensive patterns

Strategy: try-catch

Validate before calling

func canWriteFile(path string) error {
    if st, err := os.Stat(path); err == nil && st.Mode()&0200 == 0 {
        return errors.New("file is read-only")
    }
    return checkDiskSpace(filepath.Dir(path))
}

Try / catch

if err := util.WriteFileByMmap(path, data); err != nil {
    if strings.Contains(err.Error(), "truncate file") {
        log.Errorf("persist failed (disk/lock/permission?): %v", err)
        // inspect wrapped OS error and alert the user before data loss
    }
    return err
}

Prevention

When it happens

Trigger: Calling WriteFileByMmap (via WriteTree for a .sy document or saveAttributeView for an .av database) when the target filesystem rejects Truncate: disk full, read-only mount, file locked by another process, or a filesystem (some network mounts) that does not support ftruncate.

Common situations: Workspace on a full disk or quota-exceeded volume, sync client holding the file open on Windows, workspace on a network share (SMB/NFS) with poor ftruncate support, read-only mount after a crash.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/b17997e11a480548. Report an issue: GitHub.

Appendix: source

Thrown at kernel/util/mmap.go:47

// WriteFileByMmap 使用内存映射将 data 原地覆写到 filePath。
//
// 流程:OpenFile(O_RDWR|O_CREATE) → Truncate 到精确长度 → mmap.Map(RDWR) →
// copy 写入 → Flush → Unmap,全程持有 filelock 的进程内互斥锁,避免并发写冲突。
//
// 相比 filelock.WriteFile(临时文件 + rename + fsync),此路径在进程级 I/O
// 计数(IO Write Bytes)上几乎不计——copy 是纯内存写,不经过 I/O 子系统,
// 只有 Flush 会产生极少量计入。出错时由调用方回退到 filelock.WriteFile。
func WriteFileByMmap(filePath string, data []byte) (err error) {
	f, err := filelock.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0644)
	if err != nil {
		return
	}
	defer filelock.CloseFile(f)

	if err = f.Truncate(int64(len(data))); err != nil {
		msg := fmt.Sprintf("truncate file [%s] failed: %s", filePath, err)
		logging.LogError(msg)
		err = errors.New(msg)
		return
	}

	m, err := mmap.Map(f, mmap.RDWR, 0)
	if err != nil {
		msg := fmt.Sprintf("map file [%s] failed: %s", filePath, err)
		logging.LogError(msg)
		err = errors.New(msg)
		return
	}
	defer m.Unmap()

	copy(m, data)
	if err = m.Flush(); err != nil {
		msg := fmt.Sprintf("flush data [%s] failed: %s", filePath, err)
		logging.LogError(msg)
		err = errors.New(msg)
		return

View on GitHub (pinned to 9f775e8a12)