Skip to content
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
19 changes: 10 additions & 9 deletions filesystem/squashfs/metadatablock.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,27 +106,28 @@ func (fs *FileSystem) readMetaBlock(r io.ReaderAt, c Compressor, location int64)
// readMetadata read as many bytes of metadata as required for the given size, with the byteOffset provided as a starting
// point into the first block. Can read multiple blocks if necessary, e.g. if a block is 8192 bytes (standard), and
// requests to read 500 bytes beginning at offset 8000 into the first block.
// it always returns to the end of the block, even if that is greater than the given size. This makes it easy to use more
// data than expected on first read. The consumer is expected to cut it down, if needed
// It returns exactly size bytes, in a new slice the caller may keep or modify: copying up to the end of the block
// instead would cost up to a whole block per call, e.g. for every inode of a directory being listed.
func (fs *FileSystem) readMetadata(r io.ReaderAt, c Compressor, firstBlock int64, initialBlockOffset uint32, byteOffset uint16, size int) ([]byte, error) {
var (
b []byte
blockOffset = int(initialBlockOffset)
)
// we know how many blocks, so read them all in
blockOffset := int(initialBlockOffset)
m, read, err := fs.readMetaBlock(r, c, firstBlock+int64(blockOffset))
if err != nil {
return nil, err
}
b = append(b, m[byteOffset:]...)
if int(byteOffset) > len(m) {
return nil, fmt.Errorf("offset %d is beyond the %d bytes of metadata block at %d", byteOffset, len(m), firstBlock+int64(blockOffset))
}
m = m[byteOffset:]
b := make([]byte, 0, size)
b = append(b, m[:min(len(m), size)]...)
// do we have any more to read?
for len(b) < size {
blockOffset += int(read)
m, read, err = fs.readMetaBlock(r, c, firstBlock+int64(blockOffset))
if err != nil {
return nil, err
}
b = append(b, m...)
b = append(b, m[:min(len(m), size-len(b))]...)
}
return b, nil
}
59 changes: 59 additions & 0 deletions filesystem/squashfs/metadatablock_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,62 @@ func TestReadMetaBlockLimit(t *testing.T) {
t.Errorf("%d bytes: expected %v, got %v", metadataBlockSize+1, ErrDecompressedTooLarge, err)
}
}

func TestReadMetadata(t *testing.T) {
// three uncompressed metadata blocks, of 5, 4 and 3 bytes, at 0, 7 and 13
table := []byte{
0x5, 0x80, 1, 2, 3, 4, 5,
0x4, 0x80, 6, 7, 8, 9,
0x3, 0x80, 10, 11, 12,
}
tests := []struct {
name string
blockOffset uint32
byteOffset uint16
size int
out []byte
err string
}{
{"within the first block", 0, 1, 2, []byte{2, 3}, ""},
{"to the end of the first block", 0, 0, 5, []byte{1, 2, 3, 4, 5}, ""},
{"across two blocks", 0, 3, 4, []byte{4, 5, 6, 7}, ""},
{"across three blocks", 0, 4, 6, []byte{5, 6, 7, 8, 9, 10}, ""},
{"from a later block", 7, 1, 2, []byte{7, 8}, ""},
{"from the end of a block", 0, 5, 2, []byte{6, 7}, ""},
{"nothing", 0, 2, 0, []byte{}, ""},
{"offset beyond the block", 0, 6, 1, nil, "offset 6 is beyond the 5 bytes of metadata block at 0"},
{"beyond the end of the table", 7, 0, 20, nil, "metadata block at 18 compressed, but no compressor provided"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fs := &FileSystem{cache: newLRU(10)}
b, err := fs.readMetadata(bytes.NewReader(table), nil, 0, tt.blockOffset, tt.byteOffset, tt.size)
switch {
case tt.err != "" && (err == nil || !strings.Contains(err.Error(), tt.err)):
t.Errorf("expected error %q, got %v", tt.err, err)
case tt.err == "" && err != nil:
t.Errorf("unexpected error: %v", err)
case !bytes.Equal(b, tt.out):
t.Errorf("got % x, expected % x", b, tt.out)
}
})
}

// the bytes returned are the caller's own: changing them does not change what the cache returns next
t.Run("returns a copy", func(t *testing.T) {
fs := &FileSystem{cache: newLRU(10)}
b, err := fs.readMetadata(bytes.NewReader(table), nil, 0, 0, 0, 5)
if err != nil {
t.Fatal(err)
}
b[0] = 0xff
b, err = fs.readMetadata(bytes.NewReader(table), nil, 0, 0, 0, 5)
if err != nil {
t.Fatal(err)
}
if b[0] != 1 {
t.Errorf("cached block changed through a returned slice: got % x", b)
}
})
}
4 changes: 0 additions & 4 deletions filesystem/squashfs/squashfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,10 +754,6 @@ func (fs *FileSystem) getDirectory(blockOffset uint32, byteOffset uint16, size i
if err != nil {
return nil, fmt.Errorf("error reading block at position %d: %v", blockOffset, err)
}
// for parseDirectory, we only want to use precisely the right number of bytes
if len(uncompressed) > size {
uncompressed = uncompressed[:size]
}
// get the inode from the offset into the uncompressed block
return parseDirectory(uncompressed)
}
Expand Down
Loading