Skip to content

Commit

Permalink
Fix rename directory will lose directory contents on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
mstmdev committed Dec 11, 2021
1 parent 569e9f2 commit 6f838d4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
8 changes: 2 additions & 6 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package daemon
import (
"bufio"
"fmt"
"github.com/no-src/gofs/util"
"github.com/no-src/log"
"os"
"runtime"
"time"
)

Expand Down Expand Up @@ -37,7 +37,7 @@ func startSubprocess() (*os.Process, error) {
attr := &os.ProcAttr{Files: []*os.File{os.Stdin, os.Stdout, os.Stderr}}
// try to check stdin
// if compile with [-ldflags="-H windowsgui"] on Windows system, stdin will get error
if isWindows() {
if util.IsWindows() {
_, stdInErr := os.Stdin.Stat()
if stdInErr != nil {
attr = &os.ProcAttr{Files: []*os.File{nil, nil, nil}}
Expand Down Expand Up @@ -111,10 +111,6 @@ func writePidFile(ppid, pid, subPid int) error {
return err
}

func isWindows() bool {
return runtime.GOOS == "windows"
}

// KillPPid kill parent process
func KillPPid() {
ppid := os.Getppid()
Expand Down
6 changes: 4 additions & 2 deletions monitor/fsnotify_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/no-src/gofs/core"
"github.com/no-src/gofs/retry"
"github.com/no-src/gofs/sync"
"github.com/no-src/gofs/util"
"github.com/no-src/log"
"io/fs"
"path/filepath"
Expand Down Expand Up @@ -139,8 +140,9 @@ func (m *fsNotifyMonitor) processEvents() error {
log.Error(err, "Create event execute monitor error => [%s]", event.Name)
}
}
if err == nil && !isDir {
// rename a file, will not trigger Write event
if err == nil && (!isDir || (isDir && !util.IsWindows())) {
// rename a file, will not trigger the Write event
// rename a dir, will not trigger the Write event on Linux, but it will trigger the Write event for parent dir on Windows
// send a Write event manually
go func() {
log.Debug("prepare to send a Write event after Create event [%s]", event.Name)
Expand Down
7 changes: 7 additions & 0 deletions util/os.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package util

import "runtime"

func IsWindows() bool {
return runtime.GOOS == "windows"
}

0 comments on commit 6f838d4

Please sign in to comment.