-
Notifications
You must be signed in to change notification settings - Fork 6
/
gptroot.go
78 lines (62 loc) 路 1.93 KB
/
gptroot.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
package gpt
import (
"io"
"io/fs"
"syscall"
"time"
)
// Root is a pseudo root directory containing the partitions.
type Root struct {
gpt *GptPartitionTable
dirOffset int
}
func (r *Root) Read([]byte) (int, error) {
return 0, syscall.EPERM
}
// Name always returns '/' for GPT roots.
func (r *Root) Name() string { return "." }
// ReadDir lists all partitions in the GPT.
func (r *Root) ReadDir(n int) ([]fs.DirEntry, error) {
var partitionInfos []fs.DirEntry
partitions := r.gpt.Primary().Entries()
for index, partition := range partitions {
if partition.FirstLba() != 0 || partition.LastLba() != 0 {
p := NewPartition(index, &partitions[index])
partitionInfos = append(partitionInfos, p)
}
}
// directory already exhausted
if n <= 0 && r.dirOffset >= len(partitionInfos) {
return nil, nil
}
var err error
// read till end
if n > 0 && r.dirOffset+n > len(partitionInfos) {
err = io.EOF
if r.dirOffset > len(partitionInfos) {
return nil, err
}
}
if n > 0 && r.dirOffset+n <= len(partitionInfos) {
partitionInfos = partitionInfos[r.dirOffset : r.dirOffset+n]
r.dirOffset += n
} else {
partitionInfos = partitionInfos[r.dirOffset:]
r.dirOffset += len(partitionInfos)
}
return partitionInfos, err
}
// Size returns 0 for GPT pseudo roots.
func (r *Root) Size() int64 { return 0 }
// Mode returns fs.ModeDir for GPT pseudo roots.
func (r *Root) Mode() fs.FileMode { return fs.ModeDir }
// ModTime returns the zero time (0001-01-01 00:00) for GPT pseudo roots.
func (r *Root) ModTime() time.Time { return time.Time{} }
// IsDir returns true for GPT pseudo roots.
func (r *Root) IsDir() bool { return true }
// Sys returns nil for GPT pseudo roots.
func (r *Root) Sys() interface{} { return nil }
// Close does not do anything for GPT pseudo roots.
func (r *Root) Close() error { return nil }
// Stat returns the GPT pseudo roots itself as fs.FileMode.
func (r *Root) Stat() (fs.FileInfo, error) { return r, nil }