-
Notifications
You must be signed in to change notification settings - Fork 4
/
partitions.go
80 lines (63 loc) · 1.56 KB
/
partitions.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright © 2018 Platina Systems, Inc. All rights reserved.
// Use of this source code is governed by the GPL-2 license described in the
// LICENSE file.
package partitions
import (
"errors"
"os"
"github.com/platinasystems/goes/internal/magic"
"github.com/satori/go.uuid"
)
var ErrNotFilesystem = errors.New("not a filesystem")
var ErrNotSupported = errors.New("filesystem feature not supported")
type superBlock interface {
UUID() (uuid.UUID, error)
Kind() string
}
type unknownSB struct {
kind string
}
func (sb *unknownSB) UUID() (uuid.UUID, error) {
return uuid.Nil, ErrNotSupported
}
func (sb *unknownSB) Kind() string {
return sb.kind
}
type ext234 struct {
sUUID uuid.UUID
kind string
}
const (
ext234SUUIDOff = 0x468
ext234SUUIDLen = 16
)
func (sb *ext234) UUID() (uuid.UUID, error) {
return sb.sUUID, nil
}
func (sb *ext234) Kind() string {
return sb.kind
}
func ReadSuperBlock(dev string) (superBlock, error) {
f, err := os.Open(dev)
if err != nil {
return nil, err
}
defer f.Close()
fsHeader := make([]byte, 0x10000)
_, err = f.Read(fsHeader)
if err != nil {
return nil, err
}
partitionMapType := magic.IdentifyPartitionMap(fsHeader)
partitionType := magic.IdentifyPartition(fsHeader)
if partitionMapType != "" {
return nil, ErrNotFilesystem
}
if partitionType == "ext2" || partitionType == "ext3" || partitionType == "ext4" {
sb := &ext234{}
sb.sUUID = uuid.FromBytesOrNil(fsHeader[ext234SUUIDOff : ext234SUUIDOff+ext234SUUIDLen])
sb.kind = partitionType
return sb, nil
}
return &unknownSB{kind: partitionType}, nil
}