-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgexto.go
55 lines (42 loc) · 1.07 KB
/
gexto.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
package gexto
import (
"os"
"github.com/lunixbochs/struc"
"fmt"
"syscall"
)
type File struct {
extFile
}
type FileSystem interface {
Open(name string) (*File, error)
Create(name string) (*File, error)
Remove(name string) error
Mkdir(name string, perm os.FileMode) error
Close() error
}
func NewFileSystem(devicePath string) (FileSystem, error) {
f, err := os.OpenFile(devicePath, syscall.O_RDWR, 0755)
if err != nil {
return nil, err
}
ret := fs{}
f.Seek(1024, 0)
ret.dev = f
ret.sb = &Superblock{
address: 1024,
fs: &ret,
}
err = struc.Unpack(f, ret.sb)
if err != nil {
return nil, err
}
//log.Printf("Super:\n%+v\n", *ret.sb)
numBlockGroups := (ret.sb.GetBlockCount() + int64(ret.sb.BlockPer_group) - 1) / int64(ret.sb.BlockPer_group)
numBlockGroups2 := (ret.sb.InodeCount + ret.sb.InodePer_group - 1) / ret.sb.InodePer_group
if numBlockGroups != int64(numBlockGroups2) {
return nil, fmt.Errorf("Block/inode mismatch: %d %d %d", ret.sb.GetBlockCount(), numBlockGroups, numBlockGroups2)
}
ret.sb.numBlockGroups = numBlockGroups
return &ret, nil
}