Skip to content

Commit 241921c

Browse files
committed
vfs: don't cache the path in RW file objects to fix renaming
1 parent a186284 commit 241921c

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

vfs/file.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ func (f *File) Path() string {
9595
return path.Join(f.d.path, f.leaf)
9696
}
9797

98+
// osPath returns the full path of the file in the cache in OS format
99+
func (f *File) osPath() string {
100+
return f.d.vfs.cache.toOSPath(f.Path())
101+
}
102+
98103
// Sys returns underlying data source (can be nil) - satisfies Node interface
99104
func (f *File) Sys() interface{} {
100105
return nil
@@ -473,7 +478,7 @@ func (f *File) openRW(flags int) (fh *RWFileHandle, err error) {
473478
}
474479
// fs.Debugf(o, "File.openRW")
475480

476-
fh, err = newRWFileHandle(d, f, f.Path(), flags)
481+
fh, err = newRWFileHandle(d, f, flags)
477482
if err != nil {
478483
fs.Errorf(f, "File.openRW failed: %v", err)
479484
return nil, err

vfs/read_write.go

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ type RWFileHandle struct {
2424
*os.File
2525
mu sync.Mutex
2626
closed bool // set if handle has been closed
27-
remote string
2827
file *File
2928
d *Dir
3029
opened bool
31-
flags int // open flags
32-
osPath string // path to the file in the cache
33-
writeCalled bool // if any Write() methods have been called
34-
changed bool // file contents was changed in any other way
30+
flags int // open flags
31+
writeCalled bool // if any Write() methods have been called
32+
changed bool // file contents was changed in any other way
3533
}
3634

3735
// Check interfaces
@@ -44,26 +42,25 @@ var (
4442
_ io.Closer = (*RWFileHandle)(nil)
4543
)
4644

47-
func newRWFileHandle(d *Dir, f *File, remote string, flags int) (fh *RWFileHandle, err error) {
45+
func newRWFileHandle(d *Dir, f *File, flags int) (fh *RWFileHandle, err error) {
4846
// if O_CREATE and O_EXCL are set and if path already exists, then return EEXIST
4947
if flags&(os.O_CREATE|os.O_EXCL) == os.O_CREATE|os.O_EXCL && f.exists() {
5048
return nil, EEXIST
5149
}
5250

5351
fh = &RWFileHandle{
54-
file: f,
55-
d: d,
56-
remote: remote,
57-
flags: flags,
52+
file: f,
53+
d: d,
54+
flags: flags,
5855
}
5956

6057
// mark the file as open in the cache - must be done before the mkdir
61-
fh.d.vfs.cache.open(fh.remote)
58+
fh.d.vfs.cache.open(fh.file.Path())
6259

6360
// Make a place for the file
64-
fh.osPath, err = d.vfs.cache.mkdir(remote)
61+
_, err = d.vfs.cache.mkdir(fh.file.Path())
6562
if err != nil {
66-
fh.d.vfs.cache.close(fh.remote)
63+
fh.d.vfs.cache.close(fh.file.Path())
6764
return nil, errors.Wrap(err, "open RW handle failed to make cache directory")
6865
}
6966

@@ -113,22 +110,22 @@ func (fh *RWFileHandle) openPending(truncate bool) (err error) {
113110
// If the remote object exists AND its cached file exists locally AND there are no
114111
// other RW handles with it open, then attempt to update it.
115112
if o != nil && fh.file.rwOpens() == 0 {
116-
cacheObj, err := fh.d.vfs.cache.f.NewObject(context.TODO(), fh.remote)
113+
cacheObj, err := fh.d.vfs.cache.f.NewObject(context.TODO(), fh.file.Path())
117114
if err == nil && cacheObj != nil {
118-
_, err = copyObj(fh.d.vfs.cache.f, cacheObj, fh.remote, o)
115+
_, err = copyObj(fh.d.vfs.cache.f, cacheObj, fh.file.Path(), o)
119116
if err != nil {
120117
return errors.Wrap(err, "open RW handle failed to update cached file")
121118
}
122119
}
123120
}
124121

125122
// try to open a exising cache file
126-
fd, err = file.OpenFile(fh.osPath, cacheFileOpenFlags&^os.O_CREATE, 0600)
123+
fd, err = file.OpenFile(fh.file.osPath(), cacheFileOpenFlags&^os.O_CREATE, 0600)
127124
if os.IsNotExist(err) {
128125
// cache file does not exist, so need to fetch it if we have an object to fetch
129126
// it from
130127
if o != nil {
131-
_, err = copyObj(fh.d.vfs.cache.f, nil, fh.remote, o)
128+
_, err = copyObj(fh.d.vfs.cache.f, nil, fh.file.Path(), o)
132129
if err != nil {
133130
cause := errors.Cause(err)
134131
if cause != fs.ErrorObjectNotFound && cause != fs.ErrorDirNotFound {
@@ -162,7 +159,7 @@ func (fh *RWFileHandle) openPending(truncate bool) (err error) {
162159
fh.changed = true
163160
if fh.flags&os.O_CREATE == 0 && fh.file.exists() {
164161
// create an empty file if it exists on the source
165-
err = ioutil.WriteFile(fh.osPath, []byte{}, 0600)
162+
err = ioutil.WriteFile(fh.file.osPath(), []byte{}, 0600)
166163
if err != nil {
167164
return errors.Wrap(err, "cache open failed to create zero length file")
168165
}
@@ -172,9 +169,9 @@ func (fh *RWFileHandle) openPending(truncate bool) (err error) {
172169
// exists in these cases.
173170
if runtime.GOOS == "windows" && fh.flags&os.O_APPEND != 0 {
174171
cacheFileOpenFlags &^= os.O_TRUNC
175-
_, err = os.Stat(fh.osPath)
172+
_, err = os.Stat(fh.file.osPath())
176173
if err == nil {
177-
err = os.Truncate(fh.osPath, 0)
174+
err = os.Truncate(fh.file.osPath(), 0)
178175
if err != nil {
179176
return errors.Wrap(err, "cache open failed to truncate")
180177
}
@@ -184,7 +181,7 @@ func (fh *RWFileHandle) openPending(truncate bool) (err error) {
184181

185182
if fd == nil {
186183
fs.Debugf(fh.logPrefix(), "Opening cached copy with flags=%s", decodeOpenFlags(fh.flags))
187-
fd, err = file.OpenFile(fh.osPath, cacheFileOpenFlags, 0600)
184+
fd, err = file.OpenFile(fh.file.osPath(), cacheFileOpenFlags, 0600)
188185
if err != nil {
189186
return errors.Wrap(err, "cache open file failed")
190187
}
@@ -280,14 +277,14 @@ func (fh *RWFileHandle) flushWrites(closeFile bool) error {
280277

281278
if isCopied {
282279
// Transfer the temp file to the remote
283-
cacheObj, err := fh.d.vfs.cache.f.NewObject(context.TODO(), fh.remote)
280+
cacheObj, err := fh.d.vfs.cache.f.NewObject(context.TODO(), fh.file.Path())
284281
if err != nil {
285282
err = errors.Wrap(err, "failed to find cache file")
286283
fs.Errorf(fh.logPrefix(), "%v", err)
287284
return err
288285
}
289286

290-
o, err := copyObj(fh.d.vfs.f, fh.file.getObject(), fh.remote, cacheObj)
287+
o, err := copyObj(fh.d.vfs.f, fh.file.getObject(), fh.file.Path(), cacheObj)
291288
if err != nil {
292289
err = errors.Wrap(err, "failed to transfer file from cache to remote")
293290
fs.Errorf(fh.logPrefix(), "%v", err)
@@ -320,7 +317,7 @@ func (fh *RWFileHandle) close() (err error) {
320317
if fh.opened {
321318
fh.file.delRWOpen()
322319
}
323-
fh.d.vfs.cache.close(fh.remote)
320+
fh.d.vfs.cache.close(fh.file.Path())
324321
}()
325322

326323
return fh.flushWrites(true)
@@ -549,5 +546,5 @@ func (fh *RWFileHandle) Sync() error {
549546
}
550547

551548
func (fh *RWFileHandle) logPrefix() string {
552-
return fmt.Sprintf("%s(%p)", fh.remote, fh)
549+
return fmt.Sprintf("%s(%p)", fh.file.Path(), fh)
553550
}

0 commit comments

Comments
 (0)