-
Notifications
You must be signed in to change notification settings - Fork 11
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
Implement block addressing format #16
Changes from all commits
244bb7d
2c9facf
5ddd59b
e6bb180
8a13874
19e93c0
f51c46b
9a727bc
657dae6
043e013
9921e7a
33e25d1
4aebf88
200a98b
6874c80
22dd741
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
package ext4 | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
|
||
"golang.org/x/xerrors" | ||
) | ||
|
||
// ExtentHeader is ... | ||
type ExtentHeader struct { | ||
Magic uint16 `struc:"uint16,little"` | ||
|
@@ -66,6 +73,13 @@ type Inode struct { | |
Reserved [96]uint8 `struc:"[96]uint32,little"` | ||
} | ||
|
||
type BlockAddressing struct { | ||
DirectBlock [12]uint32 `struc:"[12]uint32,little"` | ||
SingleIndirectBlock uint32 `struc:"uint32,little"` | ||
DoubleIndirectBlock uint32 `struc:"uint32,little"` | ||
TripleIndirectBlock uint32 `struc:"uint32,little"` | ||
} | ||
|
||
func (i Inode) IsDir() bool { | ||
return i.Mode&0x4000 != 0 && i.Mode&0x8000 == 0 | ||
} | ||
|
@@ -97,6 +111,130 @@ func (i *Inode) GetSize() int64 { | |
return (int64(i.SizeHigh) << 32) | int64(i.SizeLo) | ||
} | ||
|
||
func resolveSingleIndirectBlockAddress(ext4 *FileSystem, singleIndirectBlockAddress uint32) ([]uint32, error) { | ||
var blockAddresses []uint32 | ||
|
||
_, err := ext4.r.Seek(int64(singleIndirectBlockAddress)*ext4.sb.GetBlockSize(), 0) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to seek: %w", err) | ||
} | ||
|
||
singleIndirectBlockAddresses, err := readBlock(ext4.r, ext4.sb.GetBlockSize()) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to read directory entry at block address %#x: %w", singleIndirectBlockAddress, err) | ||
} | ||
|
||
for singleIndirectBlockAddresses.Len() > 0 { | ||
address := binary.LittleEndian.Uint32(singleIndirectBlockAddresses.Next(4)) | ||
if address == 0 { | ||
break | ||
} | ||
blockAddresses = append(blockAddresses, address) | ||
} | ||
|
||
return blockAddresses, nil | ||
} | ||
|
||
func resolveDoubleIndirectBlockAddress(ext4 *FileSystem, doubleIndirectBlockAddress uint32) ([]uint32, error) { | ||
var blockAddresses []uint32 | ||
|
||
_, err := ext4.r.Seek(int64(doubleIndirectBlockAddress)*ext4.sb.GetBlockSize(), 0) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to seek: %w", err) | ||
} | ||
|
||
doubleIndirectBlockAddresses, err := readBlock(ext4.r, ext4.sb.GetBlockSize()) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to read directory entry at block address %#x: %w", doubleIndirectBlockAddress, err) | ||
} | ||
|
||
for doubleIndirectBlockAddresses.Len() > 0 { | ||
singleIndirectBlockAddress := binary.LittleEndian.Uint32(doubleIndirectBlockAddresses.Next(4)) | ||
if singleIndirectBlockAddress == 0 { | ||
break | ||
} | ||
|
||
singleIndirectBlockAddresses, err := resolveSingleIndirectBlockAddress(ext4, singleIndirectBlockAddress) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to read single indirect block addressing: %w", err) | ||
} | ||
blockAddresses = append(blockAddresses, singleIndirectBlockAddresses...) | ||
} | ||
|
||
return blockAddresses, nil | ||
} | ||
|
||
func resolveTripleIndirectBlockAddress(ext4 *FileSystem, tripleIndirectBlockAddress uint32) ([]uint32, error) { | ||
var blockAddresses []uint32 | ||
|
||
_, err := ext4.r.Seek(int64(tripleIndirectBlockAddress)*ext4.sb.GetBlockSize(), 0) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to seek: %w", err) | ||
} | ||
|
||
tripleIndirectBlockAddresses, err := readBlock(ext4.r, ext4.sb.GetBlockSize()) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to read directory entry at block address %#x: %w", tripleIndirectBlockAddress, err) | ||
} | ||
|
||
for tripleIndirectBlockAddresses.Len() > 0 { | ||
doubleIndirectBlockAddress := binary.LittleEndian.Uint32(tripleIndirectBlockAddresses.Next(4)) | ||
if doubleIndirectBlockAddress == 0 { | ||
break | ||
} | ||
|
||
doubleIndirectBlockAddresses, err := resolveDoubleIndirectBlockAddress(ext4, doubleIndirectBlockAddress) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to read double indirect block addressing: %w", err) | ||
} | ||
blockAddresses = append(blockAddresses, doubleIndirectBlockAddresses...) | ||
} | ||
|
||
return blockAddresses, nil | ||
} | ||
|
||
func (i *Inode) GetBlockAddresses(ext4 *FileSystem) ([]uint32, error) { | ||
addresses := BlockAddressing{} | ||
err := binary.Read(bytes.NewReader(i.BlockOrExtents[:]), binary.LittleEndian, &addresses) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to read block addressing: %w", err) | ||
} | ||
|
||
var blockAddresses []uint32 | ||
for _, blockAddress := range addresses.DirectBlock { | ||
if blockAddress == 0 { | ||
break | ||
} | ||
blockAddresses = append(blockAddresses, blockAddress) | ||
} | ||
|
||
if addresses.SingleIndirectBlock != 0 { | ||
singleIndirectBlockAddresses, err := resolveSingleIndirectBlockAddress(ext4, addresses.SingleIndirectBlock) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to read single indirect block addressing: %w", err) | ||
} | ||
blockAddresses = append(blockAddresses, singleIndirectBlockAddresses...) | ||
} | ||
|
||
if addresses.DoubleIndirectBlock != 0 { | ||
doubleIndirectBlockAddresses, err := resolveDoubleIndirectBlockAddress(ext4, addresses.DoubleIndirectBlock) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to read double indirect block addressing: %w", err) | ||
} | ||
blockAddresses = append(blockAddresses, doubleIndirectBlockAddresses...) | ||
} | ||
|
||
if addresses.TripleIndirectBlock != 0 { | ||
tripleIndirectBlockAddresses, err := resolveTripleIndirectBlockAddress(ext4, addresses.TripleIndirectBlock) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to read triple indirect block addressing: %w", err) | ||
} | ||
blockAddresses = append(blockAddresses, tripleIndirectBlockAddresses...) | ||
} | ||
Comment on lines
+211
to
+233
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. これって全て存在するパターンや一部存在するパターンなどあるんですかね? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Block Addressingの制約で、1ファイルのサイズが1Blockのサイズを超えたときのためにこのような管理方法が取られているようです。 https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout#Direct.2FIndirect_Block_Addressing ここからサイズを計算すると、ファイルサイズが4GBを超えるとTripleまで全て存在するようになります。4MBを超えるとDoubleまで、4KBを超えるとSingleまでになるはずです。少ない方から順番に詰められていくので、途中で飛ばしたりという組み合わせはなさそうです。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. なるほどです。拡張されていく仕様ですか。 |
||
|
||
return blockAddresses, nil | ||
} | ||
|
||
// ExtentInternal | ||
type ExtentInternal struct { | ||
Block uint32 `struc:"uint32,little"` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここ全体的にですが、エラーが発生したアドレスがわかるようにしたいです。ブロックのアドレスなどdebugの時にxxd とかで参照できると嬉しい(他のところが十分にできてないので心苦しいですが)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ひとまず single/double/triple の block addresses を解決するところで、エラーメッセージ中にブロックアドレスを表示するようにしてみました。
22dd741