(d *Dir, f *File, flags int)
| 41 | } |
| 42 | |
| 43 | func 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 |
| 80 | func (fh *RWFileHandle) readOnly() bool { |
no test coverage detected
searching dependent graphs…