Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/internal/archive/iso9660/utils.go
2348 views
1
package iso9660
2
3
import (
4
"io"
5
"os"
6
"path/filepath"
7
"strings"
8
9
"github.com/alist-org/alist/v3/internal/archive/tool"
10
"github.com/alist-org/alist/v3/internal/errs"
11
"github.com/alist-org/alist/v3/internal/model"
12
"github.com/alist-org/alist/v3/internal/stream"
13
"github.com/alist-org/alist/v3/pkg/utils"
14
"github.com/kdomanski/iso9660"
15
)
16
17
func getImage(ss *stream.SeekableStream) (*iso9660.Image, error) {
18
reader, err := stream.NewReadAtSeeker(ss, 0)
19
if err != nil {
20
return nil, err
21
}
22
return iso9660.OpenImage(reader)
23
}
24
25
func getObj(img *iso9660.Image, path string) (*iso9660.File, error) {
26
obj, err := img.RootDir()
27
if err != nil {
28
return nil, err
29
}
30
if path == "/" {
31
return obj, nil
32
}
33
paths := strings.Split(strings.TrimPrefix(path, "/"), "/")
34
for _, p := range paths {
35
if !obj.IsDir() {
36
return nil, errs.ObjectNotFound
37
}
38
children, err := obj.GetChildren()
39
if err != nil {
40
return nil, err
41
}
42
exist := false
43
for _, child := range children {
44
if child.Name() == p {
45
obj = child
46
exist = true
47
break
48
}
49
}
50
if !exist {
51
return nil, errs.ObjectNotFound
52
}
53
}
54
return obj, nil
55
}
56
57
func toModelObj(file *iso9660.File) model.Obj {
58
return &model.Object{
59
Name: file.Name(),
60
Size: file.Size(),
61
Modified: file.ModTime(),
62
IsFolder: file.IsDir(),
63
}
64
}
65
66
func decompress(f *iso9660.File, path string, up model.UpdateProgress) error {
67
return decompressEntry(f.Reader(), f.Size(), path, f.Name(), up)
68
}
69
70
func decompressEntry(reader io.Reader, size int64, path, entryName string, up model.UpdateProgress) error {
71
dstPath, err := tool.SecureJoin(path, entryName)
72
if err != nil {
73
return err
74
}
75
if err = os.MkdirAll(filepath.Dir(dstPath), 0700); err != nil {
76
return err
77
}
78
file, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
79
if err != nil {
80
return err
81
}
82
defer file.Close()
83
_, err = utils.CopyWithBuffer(file, &stream.ReaderUpdatingProgress{
84
Reader: &stream.SimpleReaderWithSize{
85
Reader: reader,
86
Size: size,
87
},
88
UpdateProgress: up,
89
})
90
return err
91
}
92
93
func decompressAll(children []*iso9660.File, path string) error {
94
for _, child := range children {
95
if child.IsDir() {
96
nextChildren, err := child.GetChildren()
97
if err != nil {
98
return err
99
}
100
nextPath, err := tool.SecureJoin(path, child.Name())
101
if err != nil {
102
return err
103
}
104
if err = os.MkdirAll(nextPath, 0700); err != nil {
105
return err
106
}
107
if err = decompressAll(nextChildren, nextPath); err != nil {
108
return err
109
}
110
} else {
111
if err := decompress(child, path, func(_ float64) {}); err != nil {
112
return err
113
}
114
}
115
}
116
return nil
117
}
118
119