fix append bug and overwrite/truncate bug
an alternative fix to the truncate/append problem Update ipns_unix.go
Jeromy committed
Apr 28, 2015 at 19:40 UTC
90a3252d3962a338bba332c1b4ddf909f4c07f9b
1 file changed
+24
-2
fuse/ipns/ipns_unix.go
+24
-2
@@ -337,6 +337,20 @@ func (fi *File) Flush(ctx context.Context, req *fuse.FlushRequest) error {
337
}
338
}
339
340
+func (fi *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
341
+ cursize, err := fi.fi.Size()
342
+ if err != nil {
343
+ return err
344
+ }
345
+ if cursize != int64(req.Size) {
346
+ err := fi.fi.Truncate(int64(req.Size))
347
+ if err != nil {
348
+ return err
349
+ }
350
+ }
351
+ return nil
352
+}
353
+
354
// Fsync flushes the content in the file to disk, but does not
355
// update the dag tree internally
356
func (fi *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
@@ -370,13 +384,21 @@ func (dir *Directory) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Nod
384
385
func (fi *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
386
if req.Flags&fuse.OpenTruncate != 0 {
373
- log.Warning("Need to truncate file!")
387
+ log.Info("Need to truncate file!")
388
err := fi.fi.Truncate(0)
389
if err != nil {
390
return nil, err
391
}
392
} else if req.Flags&fuse.OpenAppend != 0 {
379
- log.Warning("Need to append to file!")
393
+ log.Info("Need to append to file!")
394
+
395
+ // seek(0) essentially resets the file object, this is required for appends to work
396
+ // properly
397
+ _, err := fi.fi.Seek(0, os.SEEK_SET)
398
+ if err != nil {
399
+ log.Error("seek reset failed: ", err)
400
+ return nil, err
401
+ }
402
}
403
return fi, nil
404
}