Skip to content

Commit

Permalink
fix copySparse for empty qcow2 disk
Browse files Browse the repository at this point in the history
Signed-off-by: Nikita Vasilchenko <n@mintscale.ru>
  • Loading branch information
vasileknik76 committed Mar 19, 2024
1 parent 6587349 commit b8ae1f6
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/nativeimgutil/nativeimgutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func copySparse(w *os.File, r io.Reader, bufSize int64) (int64, error) {
zeroBuf := make([]byte, bufSize)
buf := make([]byte, bufSize)
var eof bool
hasWrites := false
for !eof {
rN, rErr := r.Read(buf)
if rErr != nil {
Expand All @@ -143,6 +144,7 @@ func copySparse(w *os.File, r io.Reader, bufSize int64) (int64, error) {
// no need to ftruncate here
n += int64(rN)
} else {
hasWrites = true
wN, wErr := w.Write(buf)
if wN > 0 {
n += int64(wN)
Expand All @@ -155,6 +157,11 @@ func copySparse(w *os.File, r io.Reader, bufSize int64) (int64, error) {
}
}
}

// Ftruncate must be run if the file contains only zeros
if !hasWrites {
return n, MakeSparse(w, n)
}
return n, nil
}

Expand Down

0 comments on commit b8ae1f6

Please sign in to comment.