Skip to content

Commit d857a9f

Browse files
committed
Added mutex to guard file handle
1 parent 3415f48 commit d857a9f

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

File.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ type FileINode struct {
2525
Attrs Attrs // Cache of file attributes // TODO: implement TTL
2626
Parent *DirINode // Pointer to the parent directory (allows computing fully-qualified paths on demand)
2727

28-
activeHandles []*FileHandle // list of opened file handles
29-
fileMutex sync.Mutex // mutex for activeHandles
30-
handle FileProxy // handle to the temp file in staging dir
28+
activeHandles []*FileHandle // list of opened file handles
29+
fileMutex sync.Mutex // mutex for file operation such as open, delete
30+
handle FileProxy // handle to the temp file in staging dir
31+
fileHandleMutex sync.Mutex // mutex for file handle
3132
}
3233

3334
// Verify that *File implements necesary FUSE interfaces
@@ -225,7 +226,7 @@ func (file *FileINode) createStagingFile(operation string, existsInDFS bool) err
225226
return err
226227
}
227228
}
228-
file.handle = &LocalFileProxy{localFile: stagingFile}
229+
file.handle = &LocalFileProxy{localFile: stagingFile, file: file}
229230
return nil
230231
}
231232

@@ -252,6 +253,9 @@ func (file *FileINode) downloadToStaging(stagingFile *os.File, operation string)
252253

253254
// Creates new file handle
254255
func (file *FileINode) NewFileHandle(existsInDFS bool, flags fuse.OpenFlags) (*FileHandle, error) {
256+
file.lockFileHandle()
257+
defer file.unLockFileHandle()
258+
255259
operation := Create
256260
if existsInDFS {
257261
operation = Open
@@ -293,3 +297,11 @@ func (file *FileINode) logInfo(fields Fields) Fields {
293297
}
294298
return f
295299
}
300+
301+
func (file *FileINode) lockFileHandle() {
302+
file.fileHandleMutex.Lock()
303+
}
304+
305+
func (file *FileINode) unLockFileHandle() {
306+
file.fileHandleMutex.Unlock()
307+
}

LocalFileProxy.go

+15
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,49 @@ import "os"
44

55
type LocalFileProxy struct {
66
localFile *os.File // handle to the temp file in staging dir
7+
file *FileINode
78
}
89

910
var _ FileProxy = (*LocalFileProxy)(nil)
1011

1112
func (p *LocalFileProxy) Truncate(size int64) error {
13+
p.file.lockFileHandle()
14+
defer p.file.unLockFileHandle()
1215
return p.localFile.Truncate(size)
1316
}
1417

1518
func (p *LocalFileProxy) WriteAt(b []byte, off int64) (n int, err error) {
19+
p.file.lockFileHandle()
20+
defer p.file.unLockFileHandle()
1621
return p.localFile.WriteAt(b, off)
1722
}
1823

1924
func (p *LocalFileProxy) ReadAt(b []byte, off int64) (n int, err error) {
25+
p.file.lockFileHandle()
26+
defer p.file.unLockFileHandle()
2027
return p.localFile.ReadAt(b, off)
2128
}
2229

2330
func (p *LocalFileProxy) Seek(offset int64, whence int) (ret int64, err error) {
31+
p.file.lockFileHandle()
32+
defer p.file.unLockFileHandle()
2433
return p.localFile.Seek(offset, whence)
2534
}
2635

2736
func (p *LocalFileProxy) Read(b []byte) (n int, err error) {
37+
p.file.lockFileHandle()
38+
defer p.file.unLockFileHandle()
2839
return p.localFile.Read(b)
2940
}
3041

3142
func (p *LocalFileProxy) Close() error {
43+
p.file.lockFileHandle()
44+
defer p.file.unLockFileHandle()
3245
return p.localFile.Close()
3346
}
3447

3548
func (p *LocalFileProxy) Sync() error {
49+
p.file.lockFileHandle()
50+
defer p.file.unLockFileHandle()
3651
return p.localFile.Sync()
3752
}

0 commit comments

Comments
 (0)