-
Notifications
You must be signed in to change notification settings - Fork 0
/
iso.go
214 lines (189 loc) · 4.34 KB
/
iso.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package joint
import (
"io"
"io/fs"
"strings"
iso "github.com/kdomanski/iso9660"
"golang.org/x/text/encoding/charmap"
)
// IsoJoint opens file with ISO9660 disk and prepares disk-structure
// to access to nested files.
// Key is external path, to ISO9660-file disk image at local filesystem.
type IsoJoint struct {
Base Joint
img *iso.Image
cache map[string]*iso.File
*iso.File
*io.SectionReader
rdn int
}
func (j *IsoJoint) Make(base Joint, isopath string) (err error) {
if base == nil {
base = &SysJoint{}
}
if _, err = base.Open(isopath); err != nil {
return
}
j.Base = base
if j.img, err = iso.OpenImage(j.Base); err != nil {
return
}
j.cache = map[string]*iso.File{}
if j.cache[""], err = j.img.RootDir(); err != nil {
return
}
return
}
func (j *IsoJoint) Cleanup() (err error) {
if j.Busy() {
j.Close()
}
if j.Base != nil {
err = j.Base.Cleanup()
j.Base = nil
}
return err
}
func (j *IsoJoint) Busy() bool {
return j.File != nil
}
func (j *IsoJoint) Open(fpath string) (file fs.File, err error) {
if j.Busy() {
return nil, fs.ErrExist
}
if fpath == "." { // dot folder does not accepted
fpath = ""
}
if j.File, err = j.OpenFile(fpath); err != nil {
return
}
if fpath == "" { // open base ISO-disk to read
var size int64
if size, err = j.Base.Size(); err != nil {
return
}
j.SectionReader = io.NewSectionReader(j.Base, 0, size)
} else if sr := j.File.Reader(); sr != nil {
j.SectionReader = sr.(*io.SectionReader)
}
j.rdn = 0 // start new sequence
return j, nil
}
func (j *IsoJoint) Close() error {
j.File = nil
j.SectionReader = nil
return nil
}
func (j *IsoJoint) OpenFile(fpath string) (*iso.File, error) {
if file, ok := j.cache[fpath]; ok {
return file, nil
}
if !fs.ValidPath(fpath) {
return nil, fs.ErrInvalid
}
var dec = charmap.Windows1251.NewDecoder()
var curdir string
var chunks = strings.Split(fpath, "/")
var file = j.cache[curdir] // get root directory
for _, chunk := range chunks {
if !file.IsDir() {
return nil, fs.ErrNotExist
}
var curpath = JoinPath(curdir, chunk)
if f, ok := j.cache[curpath]; ok {
file = f // the file must be unchanged otherwise
} else {
var list, err = file.GetChildren()
if err != nil {
return nil, err
}
var found = false
for _, file = range list {
var name, _ = dec.String(file.Name())
j.cache[JoinPath(curdir, name)] = file
if name == chunk {
found = true
break
}
}
if !found {
return nil, fs.ErrNotExist
}
}
curdir = curpath
}
return file, nil
}
func (j *IsoJoint) Size() (int64, error) {
return j.File.Size(), nil
}
func (j *IsoJoint) ReadDir(n int) (list []fs.DirEntry, err error) {
var files []*iso.File // children entries cached by previous calls
if files, err = j.File.GetChildren(); err != nil {
return
}
if n < 0 {
n = len(files) - j.rdn
} else if n > len(files)-j.rdn {
n = len(files) - j.rdn
err = io.EOF
}
if n <= 0 { // on case all files readed or some deleted
return
}
list = make([]fs.DirEntry, n)
for i := 0; i < n; i++ {
list[i] = IsoFileInfo{files[j.rdn+i]}
}
j.rdn += n
return
}
func (j *IsoJoint) Stat() (fs.FileInfo, error) {
if j.File.IsDir() && j.SectionReader != nil { // base ISO-disk
return j.Base.Stat()
}
return IsoFileInfo{j.File}, nil
}
func (j *IsoJoint) Info(fpath string) (fs.FileInfo, error) {
var file, err = j.OpenFile(fpath)
if err != nil {
return nil, err
}
return IsoFileInfo{file}, nil
}
type IsoFileInfo struct {
*iso.File
}
// Name returns filename translated from cyrillic to unicode.
func (fi IsoFileInfo) Name() string {
var dec = charmap.Windows1251.NewDecoder()
var name, _ = dec.String(fi.File.Name())
return name
}
func (fi IsoFileInfo) Mode() fs.FileMode {
var mode = fi.File.Mode()
if mode.IsRegular() && IsTypeIso(fi.File.Name()) {
mode |= fs.ModeDir
}
return mode
}
func (fi IsoFileInfo) IsDir() bool {
return fi.File.IsDir() || IsTypeIso(fi.File.Name())
}
func (fi IsoFileInfo) IsRealDir() bool {
return fi.File.IsDir()
}
func (fi IsoFileInfo) Type() fs.FileMode {
return fi.Mode().Type()
}
// Info provided for fs.DirEntry compatibility and returns object itself.
func (fi IsoFileInfo) Info() (fs.FileInfo, error) {
return fi, nil
}
// Sys returns IsoFileInfo itself.
func (fi IsoFileInfo) Sys() interface{} {
return fi
}
func (fi IsoFileInfo) String() string {
return fs.FormatDirEntry(fi)
}