Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create sparse files on backup import and migration receive #773

Merged
merged 3 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/server/storage/drivers/generic_vfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func genericVFSCreateVolumeFromMigration(d Driver, initVolume func(vol Volume) (
d.Logger().Debug("Receiving block volume started", logger.Ctx{"volName": volName, "path": path})
defer d.Logger().Debug("Receiving block volume stopped", logger.Ctx{"volName": volName, "path": path})

_, err = io.Copy(to, fromPipe)
_, err = io.Copy(NewSparseFileWrapper(to), fromPipe)
if err != nil {
return fmt.Errorf("Error copying from migration connection to %q: %w", path, err)
}
Expand Down Expand Up @@ -786,7 +786,7 @@ func genericVFSBackupUnpack(d Driver, sysOS *sys.OS, vol Volume, snapshots []str
}

d.Logger().Debug(logMsg, logger.Ctx{"source": srcFile, "target": targetPath})
_, err = io.Copy(to, tr)
_, err = io.Copy(NewSparseFileWrapper(to), tr)
if err != nil {
return err
}
Expand Down
46 changes: 46 additions & 0 deletions internal/server/storage/drivers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,3 +880,49 @@ func wipeBlockHeaders(path string) error {
func IsContentBlock(contentType ContentType) bool {
return contentType == ContentTypeBlock || contentType == ContentTypeISO
}

// NewSparseFileWrapper returns a SparseFileWrapper for the provided io.File.
func NewSparseFileWrapper(w *os.File) *SparseFileWrapper {
return &SparseFileWrapper{w: w}
}

// SparseFileWrapper wraps os.File to create sparse Files.
type SparseFileWrapper struct {
w *os.File
}

// Write performs the write but skips null bytes.
func (sfw *SparseFileWrapper) Write(p []byte) (n int, err error) {
originalLength := len(p)
start := 0

for start < len(p) {
end := start
if p[start] == 0 {
for end < len(p) && p[end] == 0 {
end++
}

_, err := sfw.w.Seek(int64(end-start), io.SeekCurrent)
if err != nil {
return start, err
}

start = end
} else {
// Write non-zero bytes
for end < len(p) && p[end] != 0 {
end++
}

written, err := sfw.w.Write(p[start:end])
if err != nil {
return start + written, err
}

start = end
}
}

return originalLength, nil
}
Loading