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
2 changes: 0 additions & 2 deletions filesystem/ext4/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ func directoryChecksummer(seed, inodeNumber, inodeGeneration uint32) checksummer
// directoryChecksumAppender returns a function that implements checksumAppender for a directory entries block
// original calculations can be seen for e2fsprogs https://git.kernel.org/pub/scm/fs/ext2/e2fsprogs.git/tree/lib/ext2fs/csum.c#n301
// and in the linux tree https://github.com/torvalds/linux/blob/master/fs/ext4/namei.c#L376-L384
//
//nolint:unparam // inodeGeneration is always 0
func directoryChecksumAppender(seed, inodeNumber, inodeGeneration uint32) checksumAppender {
fn := directoryChecksummer(seed, inodeNumber, inodeGeneration)
return func(b []byte) []byte {
Expand Down
33 changes: 27 additions & 6 deletions filesystem/ext4/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Directory struct {
}

// toBytes convert our entries to raw bytes. Provides checksum as well. Final returned byte slice will be a multiple of bytesPerBlock.
func (d *Directory) toBytes(bytesPerBlock uint32, checksumFunc checksumAppender) []byte {
func (d *Directory) toBytes(bytesPerBlock uint32, checksumFunc checksumAppender, withChecksums bool) []byte {
b := make([]byte, 0)
var (
previousLength int
Expand All @@ -30,26 +30,47 @@ func (d *Directory) toBytes(bytesPerBlock uint32, checksumFunc checksumAppender)
if len(d.entries) == 0 {
return b
}
checksumSize := 0
if withChecksums {
checksumSize = minDirEntryLength
}
blockLimit := int(bytesPerBlock) - checksumSize
lastEntryCount = len(d.entries) - 1
for i, de := range d.entries {
b2 := de.toBytes(0)
switch {
case len(block)+len(b2) > int(bytesPerBlock)-minDirEntryLength:
case len(block)+len(b2) > blockLimit:
// if adding this one will go past the end of the block, pad out the previous
block = block[:len(block)-previousLength]
previousB := previousEntry.toBytes(uint16(int(bytesPerBlock) - len(block) - minDirEntryLength))
previousB := previousEntry.toBytes(uint16(blockLimit - len(block)))
block = append(block, previousB...)
// add the checksum
block = checksumFunc(block)
if withChecksums {
block = checksumFunc(block)
}
b = append(b, block...)
// start a new block
block = make([]byte, 0)
// add current entry to the new block
if i == lastEntryCount {
b2 = de.toBytes(uint16(blockLimit - len(block)))
block = append(block, b2...)
if withChecksums {
block = checksumFunc(block)
}
b = append(b, block...)
block = make([]byte, 0)
} else {
block = append(block, b2...)
}
case i == lastEntryCount:
// if this is the last one, pad it out
b2 = de.toBytes(uint16(int(bytesPerBlock) - len(block) - minDirEntryLength))
b2 = de.toBytes(uint16(blockLimit - len(block)))
block = append(block, b2...)
// add the checksum
block = checksumFunc(block)
if withChecksums {
block = checksumFunc(block)
}
b = append(b, block...)
// start a new block
block = make([]byte, 0)
Expand Down
2 changes: 1 addition & 1 deletion filesystem/ext4/directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestDirectoryToBytes(t *testing.T) {
if err != nil {
t.Fatal(err)
}
b := dir.toBytes(bytesPerBlock, directoryChecksumAppender(sb.checksumSeed, 2, 0))
b := dir.toBytes(bytesPerBlock, directoryChecksumAppender(sb.checksumSeed, 2, 0), true)

// read the bytes from the disk
diff, diffString := testhelper.DumpByteSlicesWithDiffs(b, expected, 32, false, true, true)
Expand Down
19 changes: 16 additions & 3 deletions filesystem/ext4/directoryentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,32 @@ type directoryEntryInfo struct {
}

func (de *directoryEntryInfo) Info() (iofs.FileInfo, error) {
mode := iofs.FileMode(0)
isDir := de.directoryEntry.fileType == dirFileTypeDirectory
if isDir {
mode |= iofs.ModeDir
}
if de.inode != nil && de.inode.fileType == fileTypeSymbolicLink {
mode |= iofs.ModeSymlink
}
return &FileInfo{
modTime: de.modifyTime,
name: de.filename,
size: int64(de.size),
isDir: de.directoryEntry.fileType == dirFileTypeDirectory,
isDir: isDir,
mode: mode,
}, nil
}

func (de *directoryEntryInfo) Type() iofs.FileMode {
mode := iofs.FileMode(0)
if de.directoryEntry.fileType == dirFileTypeDirectory {
return iofs.ModeDir
mode |= iofs.ModeDir
}
if de.inode != nil && de.inode.fileType == fileTypeSymbolicLink {
mode |= iofs.ModeSymlink
}
return 0
return mode
}

func (de *directoryEntryInfo) IsDir() bool {
Expand Down
Loading