Skip to content

Commit

Permalink
🔥 Remove TestWriteFileSaferWithoutChangeTime func siyuan-note/siyuan#…
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Dec 9, 2023
1 parent dafc64f commit b7b6994
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 92 deletions.
52 changes: 0 additions & 52 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,58 +114,6 @@ func (GuluFile) WriteFileSaferByReader(writePath string, reader io.Reader, perm
return
}

// WriteFileSaferWithoutChangeTime writes the data to a temp file and atomically move if everything else succeeds, do not change the file modification time.
func (GuluFile) WriteFileSaferWithoutChangeTime(writePath string, data []byte, perm os.FileMode) (err error) {
// credits: https://github.com/vitessio/vitess/blob/master/go/ioutil2/ioutil.go

dir, name := filepath.Split(writePath)
tmp := filepath.Join(dir, name+Rand.String(7)+".tmp")
f, err := os.OpenFile(tmp, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if nil != err {
return
}

if _, err = f.Write(data); nil != err {
return
}

if err = f.Sync(); nil != err {
return
}

if err = f.Close(); nil != err {
return
}

if err = os.Chmod(f.Name(), perm); nil != err {
return
}

// 保持文件修改时间不变
info, _ := os.Stat(writePath)
if nil != info {
t := info.ModTime()
if err = os.Chtimes(f.Name(), t, t); nil != err {
return
}
}

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

if errMsg := strings.ToLower(err.Error()); strings.Contains(errMsg, "access is denied") || strings.Contains(errMsg, "used by another process") { // 文件可能是被锁定
time.Sleep(200 * time.Millisecond)
continue
}
break
}
return
}

// WriteFileSafer writes the data to a temp file and atomically move if everything else succeeds.
func (GuluFile) WriteFileSafer(writePath string, data []byte, perm os.FileMode) (err error) {
// credits: https://github.com/vitessio/vitess/blob/master/go/ioutil2/ioutil.go
Expand Down
40 changes: 0 additions & 40 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,46 +100,6 @@ func TestWriteFileSaferByReader(t *testing.T) {
}
}

func TestWriteFileSaferWithoutChangeTime(t *testing.T) {
writePath := "testdata/filewrite.go"
defer os.RemoveAll(writePath)

if err := os.WriteFile(writePath, []byte("0"), 0644); nil != err {
t.Fatalf("write file [%s] failed: %s", writePath, err)
}

info, err := os.Stat(writePath)
if nil != err {
t.Fatalf("stat file [%s] failed: %s", writePath, err)
}
modTime1 := info.ModTime()

if err = File.WriteFileSaferWithoutChangeTime(writePath, []byte("test"), 0644); nil != err {
t.Errorf("write file [%s] failed: %s", writePath, err)
}

info, err = os.Stat(writePath)
if nil != err {
t.Fatalf("stat file [%s] failed: %s", writePath, err)
}
modTime2 := info.ModTime()
if modTime1 != modTime2 {
t.Errorf("mod time should not be changed")
}

writePath1 := "testdata/filewrite1.go"
defer os.RemoveAll(writePath1)
if err = File.WriteFileSaferWithoutChangeTime(writePath1, []byte("test"), 0644); nil != err {
t.Errorf("write file [%s] failed: %s", writePath, err)
}
info, err = os.Stat(writePath1)
if nil != err {
t.Fatalf("stat file [%s] failed: %s", writePath1, err)
}
modTime3 := info.ModTime()
t.Logf("file mod time [%v]", modTime3)
}

func TestWriteFileSafer(t *testing.T) {
writePath := "testdata/filewrite.go"
defer os.RemoveAll(writePath)
Expand Down

0 comments on commit b7b6994

Please sign in to comment.