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

Symlink Support #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Binary file modified assets/tiny.ext4
Binary file not shown.
15 changes: 15 additions & 0 deletions extent.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ func (en *ExtentNavigator) Read(offset uint64) (data []byte, err error) {
}
}()

iBlock := en.inode.Data().IBlock

// If the inode is not using extents, its data is stored in inode.i_block.
if en.inode.Flag(InodeFlagExtents) == false {
if en.inode.Size() > uint64(len(iBlock)) {
log.Panicf("inode size is %d bytes but does not use extents", en.inode.Size())
}

length := en.inode.Size() - offset
data = make([]byte, length)

copy(data, iBlock[offset:offset+length])
return
dsoprea marked this conversation as resolved.
Show resolved Hide resolved
}

sb := en.inode.BlockGroupDescriptor().Superblock()

blockSize := uint64(sb.BlockSize())
Expand Down
26 changes: 26 additions & 0 deletions extent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@ func TestExtentNavigator_Read(t *testing.T) {
}
}

func TestExtentNavigator_ReadSymlink(t *testing.T) {
f, inode, err := GetTestInode(TestSymlinkInodeNumber)
log.PanicIf(err)

defer f.Close()

en := NewExtentNavigatorWithReadSeeker(f, inode)

inodeSize := inode.Size()
actualBytes := make([]byte, inodeSize)

for offset := uint64(0); offset < inodeSize; {
data, err := en.Read(offset)
log.PanicIf(err)

copy(actualBytes[offset:], data)
offset += uint64(len(data))
}

expectedBytes := []byte("thejungle.txt")

if bytes.Compare(actualBytes, expectedBytes) != 0 {
t.Fatalf("Bytes not read correctly.")
}
}

func ExampleExtentNavigator_Read() {
f, inode, err := GetTestInode(TestFileInodeNumber)
log.PanicIf(err)
Expand Down
2 changes: 0 additions & 2 deletions inode.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,6 @@ func NewInodeWithReadSeeker(bgd *BlockGroupDescriptor, rs io.ReadSeeker, absolut
if inode.Flag(InodeFlagIndex) == true {
// TODO(dustin): Might be present in large directories. We might need to implement both mechanisms (this and "linear directories").
log.Panicf("hash-tree directories not currently supported")
} else if inode.Flag(InodeFlagExtents) == false {
log.Panicf("only inodes having extent trees are supported")
}

return inode, nil
Expand Down
3 changes: 2 additions & 1 deletion testing_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (
const (
TestDirectoryInodeNumber = 2
TestFileInodeNumber = 12
TestSymlinkInodeNumber = 13
)

var (
assetsPath = path.Join(os.Getenv("GOPATH"), "src", "github.com", "dsoprea", "go-ext4", "assets")
assetsPath = "assets"
)

// GetTestInode returns a test inode struct and `os.File` for the file. It's
Expand Down