This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
forked from jdel/go-syno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packages_io.go
181 lines (152 loc) · 4.05 KB
/
packages_io.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
package syno // import jdel.org/go-syno/syno
import (
"archive/tar"
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
ini "gopkg.in/ini.v1"
)
func (p *Package) containsFiles(fileNamePattern string) (bool, error) {
var err error
var spkFile *os.File
spkFile, err = os.Open(filepath.Join(o.PackagesDir, p.fileName))
defer spkFile.Close()
if err != nil {
return false, err
}
tarReader := tar.NewReader(spkFile)
for {
fileHeader, eofErr := tarReader.Next()
if eofErr == io.EOF {
break
} else if eofErr != nil {
return false, eofErr
}
if match, _ := regexp.MatchString(fileNamePattern, fileHeader.Name); match {
return true, nil
}
}
return false, nil
}
func (p *Package) extractFiles(fileNamePattern string) ([]string, error) {
var err error
var spkFile *os.File
var extractedFiles []string
spkFile, err = os.Open(filepath.Join(o.PackagesDir, p.fileName))
defer spkFile.Close()
if err != nil {
return extractedFiles, err
}
tarReader := tar.NewReader(spkFile)
for {
fileHeader, eofErr := tarReader.Next()
if eofErr == io.EOF {
break
} else if eofErr != nil {
return extractedFiles, eofErr
}
if match, _ := regexp.MatchString(fileNamePattern, fileHeader.Name); match && fileHeader.Typeflag == tar.TypeReg {
fileName := trimDotSlash(fileHeader.Name)
extractedFiles = append(extractedFiles, fileName)
outputPath := filepath.Join(o.CacheDir, p.fileName, fileName)
err = writeToFile(tarReader, outputPath, 0755)
}
}
return extractedFiles, err
}
func (p *Package) getSize() (string, error) {
var size int
var err error
spkFileInfo, err := os.Stat(filepath.Join(o.PackagesDir, p.fileName))
if err != nil {
return "", err
}
size = int(spkFileInfo.Size())
return strconv.Itoa(size), err
}
func (p *Package) getMD5() (string, error) {
var calculatedMD5 string
var err error
spkFile, err := os.Open(filepath.Join(o.PackagesDir, p.fileName))
if err != nil {
return "", err
}
defer spkFile.Close()
md5Hash := md5.New()
if _, err := io.Copy(md5Hash, spkFile); err != nil {
return "", err
}
calculatedMD5 = hex.EncodeToString(md5Hash.Sum(nil))
return calculatedMD5, err
}
func (p *Package) extractInfo() error {
extractedFiles, err := p.extractFiles("INFO")
if err != nil || len(extractedFiles) == 0 {
if err := os.Rename(filepath.Join(o.PackagesDir, p.fileName), fmt.Sprintf("%s.ignored", filepath.Join(o.PackagesDir, p.fileName))); err != nil {
return err
}
return fmt.Errorf("Quatantined %s: No INFO or not a tar file", p.fileName)
}
return nil
}
// Returns the info file path and an eventual error
func (p *Package) getOrExtractInfo() (string, error) {
var err error
infoINIPath := filepath.Join(o.CacheDir, p.fileName, "INFO")
if _, err = os.Stat(infoINIPath); err != nil {
if err = p.extractInfo(); err != nil {
return "", err
}
}
return infoINIPath, err
}
func (p *Package) parseInfo() (*ini.File, error) {
spkinfoFilePath, err := p.getOrExtractInfo()
if err != nil {
return nil, fmt.Errorf("Cannot extract INFO for %s: %s", p.fileName, err)
}
infoINI, err := ini.InsensitiveLoad(spkinfoFilePath)
if err != nil {
return nil, fmt.Errorf("Cannot read INFO for %s: %s", p.fileName, err)
}
infoINI.BlockMode = false
return infoINI, nil
}
func (p *Package) extractImages() ([]string, error) {
var err error
var images []string
if images, err = p.extractFiles("(?i)[^/]*png"); err != nil {
return images, err
}
return images, err
}
func (p *Package) getImages() ([]string, error) {
var err error
var files []os.FileInfo
var images []string
files, err = ioutil.ReadDir(filepath.Join(o.CacheDir, p.fileName))
if err != nil {
return nil, err
}
for _, f := range files {
filename := f.Name()
if filepath.Ext(strings.ToLower(filename)) == ".png" {
images = append(images, filename)
}
}
return images, err
}
func (p *Package) getOrExtractImages() ([]string, error) {
extractedImages, err := p.getImages()
if len(extractedImages) == 0 {
extractedImages, err = p.extractImages()
}
return extractedImages, err
}