-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/chunked: add support for sparse files
automatically detect holes in sparse files (the threshold is hardcoded at 1kb for now) and add this information to the manifest file. The receiver will create a hole (using unix.Seek and unix.Ftruncate) instead of writing the actual zeros. Closes: #1091 Signed-off-by: Giuseppe Scrivano <[email protected]>
- Loading branch information
Showing
5 changed files
with
384 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package compressor | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"io" | ||
"testing" | ||
) | ||
|
||
func TestHole(t *testing.T) { | ||
data := []byte("\x00\x00\x00\x00\x00") | ||
|
||
hf := &holesFinder{ | ||
threshold: 1, | ||
reader: bufio.NewReader(bytes.NewReader(data)), | ||
} | ||
|
||
hole, _, err := hf.ReadByte() | ||
if err != nil { | ||
t.Errorf("got error: %w", err) | ||
} | ||
if hole != 5 { | ||
t.Error("expected hole not found") | ||
} | ||
|
||
if _, _, err := hf.ReadByte(); err != io.EOF { | ||
t.Errorf("EOF not found") | ||
} | ||
|
||
hf = &holesFinder{ | ||
threshold: 1000, | ||
reader: bufio.NewReader(bytes.NewReader(data)), | ||
} | ||
for i := 0; i < 5; i++ { | ||
hole, byte, err := hf.ReadByte() | ||
if err != nil { | ||
t.Errorf("got error: %w", err) | ||
} | ||
if hole != 0 { | ||
t.Error("hole found") | ||
} | ||
if byte != 0 { | ||
t.Error("wrong read") | ||
} | ||
} | ||
if _, _, err := hf.ReadByte(); err != io.EOF { | ||
t.Error("didn't receive EOF") | ||
} | ||
} | ||
|
||
func TestTwoHoles(t *testing.T) { | ||
data := []byte("\x00\x00\x00\x00\x00FOO\x00\x00\x00\x00\x00") | ||
|
||
hf := &holesFinder{ | ||
threshold: 2, | ||
reader: bufio.NewReader(bytes.NewReader(data)), | ||
} | ||
|
||
hole, _, err := hf.ReadByte() | ||
if err != nil { | ||
t.Errorf("got error: %w", err) | ||
} | ||
if hole != 5 { | ||
t.Error("hole not found") | ||
} | ||
|
||
for _, e := range []byte("FOO") { | ||
hole, c, err := hf.ReadByte() | ||
if err != nil { | ||
t.Errorf("got error: %w", err) | ||
} | ||
if hole != 0 { | ||
t.Error("hole found") | ||
} | ||
if c != e { | ||
t.Errorf("wrong byte read %v instead of %v", c, e) | ||
} | ||
} | ||
hole, _, err = hf.ReadByte() | ||
if err != nil { | ||
t.Errorf("got error: %w", err) | ||
} | ||
if hole != 5 { | ||
t.Error("expected hole not found") | ||
} | ||
|
||
if _, _, err := hf.ReadByte(); err != io.EOF { | ||
t.Error("didn't receive EOF") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.