Skip to content

Commit

Permalink
use temp file for redownload
Browse files Browse the repository at this point in the history
  • Loading branch information
jstaf committed Oct 18, 2023
1 parent 22f68aa commit 14e7047
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions fs/fs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fs

import (
"io"
"math"
"os"
"path/filepath"
Expand Down Expand Up @@ -513,14 +514,25 @@ 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 || !inode.VerifyChecksum(graph.QuickXORHashStream(fd)) {

// write to tempfile first to ensure our download is good
tempID := "temp-" + id
temp, err := f.content.Open(tempID)
if err != nil {
ctx.Error().Err(err).Msg("Failed to create tempfile for download.")
return fuse.EIO
}
defer f.content.Delete(tempID)

// replace content only on a match
size, err := graph.GetItemContentStream(id, f.auth, temp)
if err != nil || !inode.VerifyChecksum(graph.QuickXORHashStream(temp)) {
ctx.Error().Err(err).Msg("Failed to fetch remote content.")
return fuse.EREMOTEIO
}
fd.Seek(0, 0)
fd.Truncate(0)
io.Copy(fd, temp)
inode.DriveItem.Size = size
return fuse.OK
}
Expand Down

0 comments on commit 14e7047

Please sign in to comment.