From d7d3c56a784c37549f16a7dd901d84ffba419849 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Tue, 8 May 2018 18:04:26 +0200 Subject: [PATCH] fixes around fat32 and lfn support --- fat/directory.go | 4 ++-- fat/directory_cluster.go | 16 ++++++++++++++-- fat/filesystem.go | 3 ++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/fat/directory.go b/fat/directory.go index 2dec26a..4cc1854 100644 --- a/fat/directory.go +++ b/fat/directory.go @@ -53,14 +53,14 @@ func DecodeDirectoryEntry(d *Directory, entries []*DirectoryClusterEntry) (*Dire // We have a long entry, so we have to traverse to the point where // we're done. Also, calculate out the name and such. if entries[0].IsLong() { - lfnEntries := make([]*DirectoryClusterEntry, 0, 3) + lfnEntries = make([]*DirectoryClusterEntry, 0, 3) for entries[0].IsLong() { lfnEntries = append(lfnEntries, entries[0]) entries = entries[1:] } var nameBytes []rune - nameBytes = make([]rune, 13*len(lfnEntries)) + nameBytes = make([]rune, 0, 13*len(lfnEntries)) for i := len(lfnEntries) - 1; i >= 0; i-- { for _, char := range lfnEntries[i].longName { nameBytes = append(nameBytes, char) diff --git a/fat/directory_cluster.go b/fat/directory_cluster.go index 6917106..fd10efb 100644 --- a/fat/directory_cluster.go +++ b/fat/directory_cluster.go @@ -80,6 +80,15 @@ func DecodeDirectoryCluster(startCluster uint32, device fs.BlockDevice, fat *FAT return result, nil } +// DecodeFAT32RootDirectory decodes the FAT32 root directory structure +// from the device. +func DecodeFAT32RootDirectoryCluster(device fs.BlockDevice, fat *FAT) (*DirectoryCluster, error) { + // 2 is typcially the root dir for fat32 + // FIXME: read from BootSectorCommon and BPB_RootClus there + return DecodeDirectoryCluster(2, device, fat) + +} + // DecodeFAT16RootDirectory decodes the FAT16 root directory structure // from the device. func DecodeFAT16RootDirectoryCluster(device fs.BlockDevice, bs *BootSectorCommon) (*DirectoryCluster, error) { @@ -304,7 +313,7 @@ func (d *DirectoryClusterEntry) IsLong() bool { } func (d *DirectoryClusterEntry) IsVolumeId() bool { - return (d.attr & AttrVolumeId) == AttrVolumeId + return d.attr == AttrVolumeId } // DecodeDirectoryClusterEntry decodes a single directory entry in the @@ -332,8 +341,11 @@ func DecodeDirectoryClusterEntry(data []byte) (*DirectoryClusterEntry, error) { offset := 28 + (i * 2) chars[i+11] = binary.LittleEndian.Uint16(data[offset : offset+2]) } - result.longName = string(utf16.Decode(chars)) + idx := strings.IndexByte(result.longName, 0) + if idx > 0 { + result.longName = result.longName[:idx] + } result.longChecksum = data[13] } else { result.deleted = data[0] == 0xE5 diff --git a/fat/filesystem.go b/fat/filesystem.go index 3102e0e..6c8999a 100644 --- a/fat/filesystem.go +++ b/fat/filesystem.go @@ -28,7 +28,8 @@ func New(device fs.BlockDevice) (*FileSystem, error) { var rootDir *DirectoryCluster if bs.FATType() == FAT32 { - panic("FAT32 not implemented yet") + // WARNING: very experimental and incomplete + rootDir, err = DecodeFAT32RootDirectoryCluster(device, fat) } else { rootDir, err = DecodeFAT16RootDirectoryCluster(device, bs) if err != nil {