MCPcopy
hub / github.com/rclone/rclone / newRWFileHandle

Function newRWFileHandle

vfs/read_write.go:43–77  ·  view source on GitHub ↗
(d *Dir, f *File, flags int)

Source from the content-addressed store, hash-verified

41}
42
43func newRWFileHandle(d *Dir, f *File, flags int) (fh *RWFileHandle, err error) {
44 defer log.Trace(f.Path(), "")("err=%v", &err)
45 // get an item to represent this from the cache
46 item := d.vfs.cache.Item(f.CachePath())
47
48 exists := f.exists() || (item.Exists() && !item.WrittenBack())
49
50 // if O_CREATE and O_EXCL are set and if path already exists, then return EEXIST
51 if flags&(os.O_CREATE|os.O_EXCL) == os.O_CREATE|os.O_EXCL && exists {
52 return nil, EEXIST
53 }
54
55 fh = &RWFileHandle{
56 file: f,
57 d: d,
58 flags: flags,
59 item: item,
60 }
61
62 // truncate immediately if O_TRUNC is set or O_CREATE is set and file doesn't exist
63 if !fh.readOnly() && (fh.flags&os.O_TRUNC != 0 || (fh.flags&os.O_CREATE != 0 && !exists)) {
64 err = fh.Truncate(0)
65 if err != nil {
66 return nil, fmt.Errorf("cache open with O_TRUNC: failed to truncate: %w", err)
67 }
68 // we definitely need to write back the item even if we don't write to it
69 item.Dirty()
70 }
71
72 if !fh.readOnly() {
73 fh.file.addWriter(fh)
74 }
75
76 return fh, nil
77}
78
79// readOnly returns whether flags say fh is read only
80func (fh *RWFileHandle) readOnly() bool {

Callers 1

openRWMethod · 0.85

Calls 12

TraceFunction · 0.92
ItemMethod · 0.80
CachePathMethod · 0.80
existsMethod · 0.80
WrittenBackMethod · 0.80
readOnlyMethod · 0.80
DirtyMethod · 0.80
addWriterMethod · 0.80
PathMethod · 0.65
TruncateMethod · 0.65
ExistsMethod · 0.45
ErrorfMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…