@@ -24,14 +24,12 @@ type RWFileHandle struct {
24
24
* os.File
25
25
mu sync.Mutex
26
26
closed bool // set if handle has been closed
27
- remote string
28
27
file * File
29
28
d * Dir
30
29
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
35
33
}
36
34
37
35
// Check interfaces
@@ -44,26 +42,25 @@ var (
44
42
_ io.Closer = (* RWFileHandle )(nil )
45
43
)
46
44
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 ) {
48
46
// if O_CREATE and O_EXCL are set and if path already exists, then return EEXIST
49
47
if flags & (os .O_CREATE | os .O_EXCL ) == os .O_CREATE | os .O_EXCL && f .exists () {
50
48
return nil , EEXIST
51
49
}
52
50
53
51
fh = & RWFileHandle {
54
- file : f ,
55
- d : d ,
56
- remote : remote ,
57
- flags : flags ,
52
+ file : f ,
53
+ d : d ,
54
+ flags : flags ,
58
55
}
59
56
60
57
// 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 () )
62
59
63
60
// Make a place for the file
64
- fh . osPath , err = d .vfs .cache .mkdir (remote )
61
+ _ , err = d .vfs .cache .mkdir (fh . file . Path () )
65
62
if err != nil {
66
- fh .d .vfs .cache .close (fh .remote )
63
+ fh .d .vfs .cache .close (fh .file . Path () )
67
64
return nil , errors .Wrap (err , "open RW handle failed to make cache directory" )
68
65
}
69
66
@@ -113,22 +110,22 @@ func (fh *RWFileHandle) openPending(truncate bool) (err error) {
113
110
// If the remote object exists AND its cached file exists locally AND there are no
114
111
// other RW handles with it open, then attempt to update it.
115
112
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 () )
117
114
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 )
119
116
if err != nil {
120
117
return errors .Wrap (err , "open RW handle failed to update cached file" )
121
118
}
122
119
}
123
120
}
124
121
125
122
// 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 )
127
124
if os .IsNotExist (err ) {
128
125
// cache file does not exist, so need to fetch it if we have an object to fetch
129
126
// it from
130
127
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 )
132
129
if err != nil {
133
130
cause := errors .Cause (err )
134
131
if cause != fs .ErrorObjectNotFound && cause != fs .ErrorDirNotFound {
@@ -162,7 +159,7 @@ func (fh *RWFileHandle) openPending(truncate bool) (err error) {
162
159
fh .changed = true
163
160
if fh .flags & os .O_CREATE == 0 && fh .file .exists () {
164
161
// 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 )
166
163
if err != nil {
167
164
return errors .Wrap (err , "cache open failed to create zero length file" )
168
165
}
@@ -172,9 +169,9 @@ func (fh *RWFileHandle) openPending(truncate bool) (err error) {
172
169
// exists in these cases.
173
170
if runtime .GOOS == "windows" && fh .flags & os .O_APPEND != 0 {
174
171
cacheFileOpenFlags &^= os .O_TRUNC
175
- _ , err = os .Stat (fh .osPath )
172
+ _ , err = os .Stat (fh .file . osPath () )
176
173
if err == nil {
177
- err = os .Truncate (fh .osPath , 0 )
174
+ err = os .Truncate (fh .file . osPath () , 0 )
178
175
if err != nil {
179
176
return errors .Wrap (err , "cache open failed to truncate" )
180
177
}
@@ -184,7 +181,7 @@ func (fh *RWFileHandle) openPending(truncate bool) (err error) {
184
181
185
182
if fd == nil {
186
183
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 )
188
185
if err != nil {
189
186
return errors .Wrap (err , "cache open file failed" )
190
187
}
@@ -280,14 +277,14 @@ func (fh *RWFileHandle) flushWrites(closeFile bool) error {
280
277
281
278
if isCopied {
282
279
// 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 () )
284
281
if err != nil {
285
282
err = errors .Wrap (err , "failed to find cache file" )
286
283
fs .Errorf (fh .logPrefix (), "%v" , err )
287
284
return err
288
285
}
289
286
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 )
291
288
if err != nil {
292
289
err = errors .Wrap (err , "failed to transfer file from cache to remote" )
293
290
fs .Errorf (fh .logPrefix (), "%v" , err )
@@ -320,7 +317,7 @@ func (fh *RWFileHandle) close() (err error) {
320
317
if fh .opened {
321
318
fh .file .delRWOpen ()
322
319
}
323
- fh .d .vfs .cache .close (fh .remote )
320
+ fh .d .vfs .cache .close (fh .file . Path () )
324
321
}()
325
322
326
323
return fh .flushWrites (true )
@@ -549,5 +546,5 @@ func (fh *RWFileHandle) Sync() error {
549
546
}
550
547
551
548
func (fh * RWFileHandle ) logPrefix () string {
552
- return fmt .Sprintf ("%s(%p)" , fh .remote , fh )
549
+ return fmt .Sprintf ("%s(%p)" , fh .file . Path () , fh )
553
550
}
0 commit comments