-
Notifications
You must be signed in to change notification settings - Fork 7
/
os.go
97 lines (88 loc) · 1.65 KB
/
os.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
package crx3
import (
"encoding/binary"
"fmt"
"net/http"
"os"
)
func isDir(filename string) bool {
file, err := os.Open(filename)
if err != nil {
return false
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
return false
}
return stat.IsDir()
}
func isCRX(filename string) bool {
size := 12
file, err := os.Open(filename)
if err != nil {
return false
}
defer file.Close()
buf := make([]byte, size)
if _, err := file.Read(buf); err != nil {
return false
}
if len(buf) < size {
return false
}
if string(buf[0:4]) != "Cr24" {
return false
}
if binary.LittleEndian.Uint32(buf[4:8]) != 3 {
return false
}
return true
}
func openCrxFile(filename string) (*os.File, error) {
if err := crxFileExists(filename); err != nil {
return nil, err
}
fd, err := os.Open(filename)
if err != nil {
return nil, err
}
return fd, nil
}
func crxFileExists(filename string) error {
if !isCRX(filename) {
return fmt.Errorf("%v got %s", ErrUnknownFileExtension, filename)
}
return nil
}
func isZip(filename string) bool {
file, err := os.Open(filename)
if err != nil {
return false
}
defer file.Close()
buf := make([]byte, 512)
if _, err := file.Read(buf); err != nil {
return false
}
fileType := http.DetectContentType(buf)
switch fileType {
case "application/x-gzip", "application/zip":
return true
}
return false
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) || err != nil {
return false
}
return !info.IsDir()
}
func dirExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) || err != nil {
return false
}
return info.IsDir()
}