fix(修复问题): 修复监听文件变化
/ build (push) Successful in 12s

This commit is contained in:
2026-02-28 23:19:42 +08:00
parent e174d81262
commit 93817f7114
2 changed files with 35 additions and 4 deletions
+1
View File
@@ -25,6 +25,7 @@ type SymFS struct {
fuse.FileSystemBase fuse.FileSystemBase
root string root string
host *fuse.FileSystemHost host *fuse.FileSystemHost
done chan struct{}
} }
// NewSymFS 创建 SymFS 实例 // NewSymFS 创建 SymFS 实例
+34 -4
View File
@@ -48,11 +48,13 @@ func errno(err error) int {
// Init 初始化文件系统 // Init 初始化文件系统
func (s *SymFS) Init() { func (s *SymFS) Init() {
s.done = make(chan struct{})
go s.watch() go s.watch()
} }
// Destroy 销毁文件系统 // Destroy 销毁文件系统
func (s *SymFS) Destroy() { func (s *SymFS) Destroy() {
close(s.done)
} }
// Statfs 获取文件系统统计信息 // Statfs 获取文件系统统计信息
@@ -412,7 +414,7 @@ func (s *SymFS) open(path string, flags int, mode uint32) (int, uint64) {
return errno(err), ^uint64(0) return errno(err), ^uint64(0)
} }
if needTruncate { if needTruncate {
err := windows.SetEndOfFile(h) err = windows.SetEndOfFile(h)
if err != nil { if err != nil {
windows.CloseHandle(h) windows.CloseHandle(h)
h, err = windows.CreateFile(pathPtr, access, shareMode, nil, windows.TRUNCATE_EXISTING, attrs, 0) h, err = windows.CreateFile(pathPtr, access, shareMode, nil, windows.TRUNCATE_EXISTING, attrs, 0)
@@ -436,16 +438,27 @@ func (s *SymFS) watch() {
windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE, windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE,
nil, nil,
windows.OPEN_EXISTING, windows.OPEN_EXISTING,
windows.FILE_FLAG_BACKUP_SEMANTICS, windows.FILE_FLAG_BACKUP_SEMANTICS|windows.FILE_FLAG_OVERLAPPED,
0, 0,
) )
if err != nil { if err != nil {
return return
} }
defer windows.CloseHandle(h) defer windows.CloseHandle(h)
event, err := windows.CreateEvent(nil, 0, 0, nil)
if err != nil {
return
}
defer windows.CloseHandle(event)
buf := make([]byte, 16384) buf := make([]byte, 16384)
for { for {
var bytesReturned uint32 select {
case <-s.done:
return
default:
}
var overlapped windows.Overlapped
overlapped.HEvent = event
err = windows.ReadDirectoryChanges( err = windows.ReadDirectoryChanges(
h, h,
&buf[0], &buf[0],
@@ -458,10 +471,27 @@ func (s *SymFS) watch() {
windows.FILE_NOTIFY_CHANGE_LAST_WRITE| windows.FILE_NOTIFY_CHANGE_LAST_WRITE|
windows.FILE_NOTIFY_CHANGE_CREATION| windows.FILE_NOTIFY_CHANGE_CREATION|
windows.FILE_NOTIFY_CHANGE_SECURITY, windows.FILE_NOTIFY_CHANGE_SECURITY,
&bytesReturned,
nil, nil,
(*windows.Overlapped)(unsafe.Pointer(&overlapped)),
0, 0,
) )
if err != nil && err != windows.ERROR_IO_PENDING {
return
}
for {
result, _ := windows.WaitForSingleObject(event, 100)
if result == uint32(windows.WAIT_OBJECT_0) {
break
}
select {
case <-s.done:
windows.CancelIo(h)
return
default:
}
}
var bytesReturned uint32
err = windows.GetOverlappedResult(h, (*windows.Overlapped)(unsafe.Pointer(&overlapped)), &bytesReturned, false)
if err != nil { if err != nil {
return return
} }