Skip to content

Commit

Permalink
🎨 Saving doc "Access is denied" siyuan-note/insider#552
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed May 28, 2021
1 parent 445d9ec commit e202bec
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion file.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"os"
"path/filepath"
"strings"
"time"
)

// WriteFileSafer writes the data to a temp file and atomically move if everything else succeeds.
Expand All @@ -41,7 +42,18 @@ func (GuluFile) WriteFileSafer(writePath string, data []byte, perm os.FileMode)
}

if nil == err {
err = os.Rename(f.Name(), writePath) // Not atomic on Windows
for i := 0; i < 3; i++ {
err = os.Rename(f.Name(), writePath) // Windows 上重命名是非原子的
if nil == err {
break
}

if strings.Contains(err.Error(), "Access is denied") { // 文件可能是被锁定
time.Sleep(100 * time.Millisecond)
continue
}
break
}
}

if nil != err {
Expand Down

0 comments on commit e202bec

Please sign in to comment.