/
filesystem.go
57 lines (48 loc) · 1.03 KB
/
filesystem.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package fat
import (
"github.com/mitchellh/go-fs"
)
// FileSystem is the implementation of fs.FileSystem that can read a
// FAT filesystem.
type FileSystem struct {
bs *BootSectorCommon
device fs.BlockDevice
fat *FAT
rootDir *DirectoryCluster
}
// New returns a new FileSystem for accessing a previously created
// FAT filesystem.
func New(device fs.BlockDevice) (*FileSystem, error) {
bs, err := DecodeBootSector(device)
if err != nil {
return nil, err
}
fat, err := DecodeFAT(device, bs, 0)
if err != nil {
return nil, err
}
var rootDir *DirectoryCluster
if bs.FATType() == FAT32 {
panic("FAT32 not implemented yet")
} else {
rootDir, err = DecodeFAT16RootDirectoryCluster(device, bs)
if err != nil {
return nil, err
}
}
result := &FileSystem{
bs: bs,
device: device,
fat: fat,
rootDir: rootDir,
}
return result, nil
}
func (f *FileSystem) RootDir() (fs.Directory, error) {
dir := &Directory{
device: f.device,
dirCluster: f.rootDir,
fat: f.fat,
}
return dir, nil
}