Skip to content

Commit

Permalink
feat(sparse): Allow for sparse file uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
sylus authored and voyvodov committed Sep 18, 2024
1 parent 2389e0b commit 3e88a28
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion internal/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func (fh *FileHandle) WriteFile(offset int64, data []byte) (err error) {
return fh.lastWriteError
}

if offset != fh.nextWriteOffset {
if offset < fh.nextWriteOffset {
fh.inode.errFuse("WriteFile: only sequential writes supported", fh.nextWriteOffset, offset)
fh.lastWriteError = syscall.ENOTSUP
return fh.lastWriteError
Expand All @@ -264,6 +264,37 @@ func (fh *FileHandle) WriteFile(offset int64, data []byte) (err error) {
fh.inode.mu.Unlock()
}

// fill the hole with zero
if offset > fh.nextWriteOffset {
toSkip := offset - fh.nextWriteOffset
n := 10240
data := make([]byte, n)

for {
if fh.buf == nil {
fh.buf = MBuf{}.Init(fh.poolHandle, fh.partSize(), true)
}

if toSkip < int64(n) {
n = int(toSkip)
}
nCopied, _ := fh.buf.Write(data[:n])
toSkip -= int64(nCopied)
fh.nextWriteOffset += int64(nCopied)

if fh.buf.Full() {
err = fh.uploadCurrentBuf(!fh.cloud.Capabilities().NoParallelMultipart)
if err != nil {
return
}
}

if toSkip == 0 {
break
}
}
}

for {
if fh.buf == nil {
fh.buf = MBuf{}.Init(fh.poolHandle, fh.partSize(), true)
Expand Down

0 comments on commit 3e88a28

Please sign in to comment.