Skip to content

Commit

Permalink
fix new file contents getting appended to end of file
Browse files Browse the repository at this point in the history
  • Loading branch information
jstaf committed Oct 16, 2023
1 parent a753eb6 commit 0260ab0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
15 changes: 9 additions & 6 deletions fs/content_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fs

import (
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand All @@ -27,15 +28,15 @@ func (l *LoopbackCache) contentPath(id string) string {
return filepath.Join(l.directory, id)
}

// GetContent reads a file's content from disk.
// Get reads a file's content from disk.
func (l *LoopbackCache) Get(id string) []byte {
content, _ := os.ReadFile(l.contentPath(id))
content, _ := ioutil.ReadFile(l.contentPath(id))
return content
}

// InsertContent writes file content to disk in a single bulk insert.
func (l *LoopbackCache) Insert(id string, content []byte) error {
return os.WriteFile(l.contentPath(id), content, 0600)
return ioutil.WriteFile(l.contentPath(id), content, 0600)
}

// InsertStream inserts a stream of data
Expand All @@ -47,12 +48,13 @@ func (l *LoopbackCache) InsertStream(id string, reader io.Reader) (int64, error)
return io.Copy(fd, reader)
}

// DeleteContent deletes content from disk.
// Delete closes the fd AND deletes content from disk.
func (l *LoopbackCache) Delete(id string) error {
l.Close(id)
return os.Remove(l.contentPath(id))
}

// MoveContent moves content from one ID to another
// Move moves content from one ID to another
func (l *LoopbackCache) Move(oldID string, newID string) error {
return os.Rename(l.contentPath(oldID), l.contentPath(newID))
}
Expand All @@ -75,7 +77,7 @@ func (l *LoopbackCache) HasContent(id string) bool {
return err == nil
}

// OpenContent returns a filehandle for subsequent access
// Open returns a filehandle for subsequent access
func (l *LoopbackCache) Open(id string) (*os.File, error) {
if fd, ok := l.fds.Load(id); ok {
// already opened, return existing fd
Expand All @@ -96,6 +98,7 @@ func (l *LoopbackCache) Open(id string) (*os.File, error) {
return fd, nil
}

// Close closes the currently open fd
func (l *LoopbackCache) Close(id string) {
if fd, ok := l.fds.Load(id); ok {
file := fd.(*os.File)
Expand Down
6 changes: 5 additions & 1 deletion fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,11 @@ func (f *Filesystem) Open(cancel <-chan struct{}, in *fuse.OpenIn, out *fuse.Ope
ctx.Info().Msg(
"Not using cached item due to file hash mismatch, fetching content from API.",
)
// explicitly purge existing file content
fd.Seek(0, 0)
fd.Truncate(0)
size, err := graph.GetItemContentStream(id, f.auth, fd)
if err != nil {
if err != nil || !inode.VerifyChecksum(graph.QuickXORHashStream(fd)) {
ctx.Error().Err(err).Msg("Failed to fetch remote content.")
return fuse.EREMOTEIO
}
Expand Down Expand Up @@ -761,6 +764,7 @@ func (f *Filesystem) SetAttr(cancel <-chan struct{}, in *fuse.SetAttrIn, out *fu
Uint64("newSize", size).
Msg("")
fd, _ := f.content.Open(i.DriveItem.ID)
// the unix syscall does not update the seek position, so neither should we
fd.Truncate(int64(size))
i.DriveItem.Size = size
i.hasChanges = true
Expand Down
6 changes: 4 additions & 2 deletions fs/graph/drive_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ func GetItemContent(id string, auth *Auth) ([]byte, uint64, error) {
return buf.Bytes(), uint64(n), err
}

// GetItemContentStream is the same as GetItemContent, but writes data to an output
// reader
// GetItemContentStream is the same as GetItemContent, but writes data to an
// output reader. This function assumes a brand-new io.Writer is used, so
// "output" must be truncated if there is content already in the io.Writer
// prior to use.
func GetItemContentStream(id string, auth *Auth, output io.Writer) (uint64, error) {
// determine the size of the item
item, err := GetItem(id, auth)
Expand Down

0 comments on commit 0260ab0

Please sign in to comment.