]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
1 | package hotline |
2 | ||
3 | import ( | |
4 | "encoding/binary" | |
16a4ad70 JH |
5 | "errors" |
6 | "io/fs" | |
6988a057 JH |
7 | "io/ioutil" |
8 | "os" | |
9 | "path/filepath" | |
10 | "strings" | |
11 | ) | |
12 | ||
aeec1015 JH |
13 | const incompleteFileSuffix = ".incomplete" |
14 | ||
2728d12b JH |
15 | func downcaseFileExtension(filename string) string { |
16 | splitStr := strings.Split(filename, ".") | |
17 | ext := strings.ToLower( | |
18 | splitStr[len(splitStr)-1], | |
19 | ) | |
6988a057 | 20 | |
2728d12b | 21 | return ext |
6988a057 JH |
22 | } |
23 | ||
2728d12b JH |
24 | func fileTypeFromFilename(fn string) fileType { |
25 | ft, ok := fileTypes[downcaseFileExtension(fn)] | |
26 | if ok { | |
27 | return ft | |
6988a057 | 28 | } |
2728d12b | 29 | return defaultFileType |
6988a057 JH |
30 | } |
31 | ||
2d52424e JH |
32 | func fileTypeFromInfo(info os.FileInfo) (ft fileType, err error) { |
33 | if info.IsDir() { | |
34 | ft.CreatorCode = "n/a " | |
35 | ft.TypeCode = "fldr" | |
36 | } else { | |
37 | ft = fileTypeFromFilename(info.Name()) | |
38 | } | |
39 | ||
40 | return ft, nil | |
41 | } | |
42 | ||
c5d9af5a | 43 | func getFileNameList(filePath string) (fields []Field, err error) { |
6988a057 JH |
44 | files, err := ioutil.ReadDir(filePath) |
45 | if err != nil { | |
46 | return fields, nil | |
47 | } | |
48 | ||
49 | for _, file := range files { | |
72dd37f1 | 50 | var fnwi FileNameWithInfo |
d7548c16 | 51 | |
43ecc0f4 | 52 | fileCreator := make([]byte, 4) |
43ecc0f4 | 53 | |
d7548c16 JH |
54 | if file.Mode()&os.ModeSymlink != 0 { |
55 | resolvedPath, err := os.Readlink(filePath + "/" + file.Name()) | |
56 | if err != nil { | |
57 | return fields, err | |
58 | } | |
6988a057 | 59 | |
d7548c16 | 60 | rFile, err := os.Stat(filePath + "/" + resolvedPath) |
dd7aadfa JH |
61 | if errors.Is(err, os.ErrNotExist) { |
62 | continue | |
63 | } | |
d7548c16 JH |
64 | if err != nil { |
65 | return fields, err | |
66 | } | |
67 | ||
68 | if rFile.IsDir() { | |
69 | dir, err := ioutil.ReadDir(filePath + "/" + file.Name()) | |
70 | if err != nil { | |
71 | return fields, err | |
72 | } | |
73 | binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir))) | |
74 | copy(fnwi.Type[:], []byte("fldr")[:]) | |
75 | copy(fnwi.Creator[:], fileCreator[:]) | |
76 | } else { | |
77 | binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(rFile.Size())) | |
78 | copy(fnwi.Type[:], []byte(fileTypeFromFilename(rFile.Name()).TypeCode)[:]) | |
79 | copy(fnwi.Creator[:], []byte(fileTypeFromFilename(rFile.Name()).CreatorCode)[:]) | |
80 | } | |
81 | ||
82 | } else if file.IsDir() { | |
6988a057 JH |
83 | dir, err := ioutil.ReadDir(filePath + "/" + file.Name()) |
84 | if err != nil { | |
85 | return fields, err | |
86 | } | |
72dd37f1 | 87 | binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir))) |
d7548c16 | 88 | copy(fnwi.Type[:], []byte("fldr")[:]) |
72dd37f1 | 89 | copy(fnwi.Creator[:], fileCreator[:]) |
d7548c16 | 90 | } else { |
fca1ebde JH |
91 | // the Hotline protocol does not support file sizes > 4GiB due to the 4 byte field size, so skip them |
92 | if file.Size() > 4294967296 { | |
93 | continue | |
94 | } | |
d7548c16 JH |
95 | binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(file.Size())) |
96 | copy(fnwi.Type[:], []byte(fileTypeFromFilename(file.Name()).TypeCode)[:]) | |
97 | copy(fnwi.Creator[:], []byte(fileTypeFromFilename(file.Name()).CreatorCode)[:]) | |
6988a057 JH |
98 | } |
99 | ||
16a4ad70 JH |
100 | strippedName := strings.Replace(file.Name(), ".incomplete", "", -1) |
101 | ||
72dd37f1 | 102 | nameSize := make([]byte, 2) |
16a4ad70 | 103 | binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName))) |
72dd37f1 JH |
104 | copy(fnwi.NameSize[:], nameSize[:]) |
105 | ||
16a4ad70 | 106 | fnwi.name = []byte(strippedName) |
72dd37f1 JH |
107 | |
108 | b, err := fnwi.MarshalBinary() | |
109 | if err != nil { | |
110 | return nil, err | |
111 | } | |
112 | fields = append(fields, NewField(fieldFileNameWithInfo, b)) | |
6988a057 JH |
113 | } |
114 | ||
115 | return fields, nil | |
116 | } | |
117 | ||
118 | func CalcTotalSize(filePath string) ([]byte, error) { | |
119 | var totalSize uint32 | |
120 | err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error { | |
121 | if err != nil { | |
122 | return err | |
123 | } | |
124 | ||
125 | if info.IsDir() { | |
126 | return nil | |
127 | } | |
128 | ||
129 | totalSize += uint32(info.Size()) | |
130 | ||
131 | return nil | |
132 | }) | |
133 | if err != nil { | |
134 | return nil, err | |
135 | } | |
136 | ||
137 | bs := make([]byte, 4) | |
138 | binary.BigEndian.PutUint32(bs, totalSize) | |
139 | ||
140 | return bs, nil | |
141 | } | |
142 | ||
143 | func CalcItemCount(filePath string) ([]byte, error) { | |
144 | var itemcount uint16 | |
145 | err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error { | |
146 | itemcount += 1 | |
147 | ||
148 | if err != nil { | |
149 | return err | |
150 | } | |
151 | ||
152 | return nil | |
153 | }) | |
154 | if err != nil { | |
155 | return nil, err | |
156 | } | |
157 | ||
158 | bs := make([]byte, 2) | |
159 | binary.BigEndian.PutUint16(bs, itemcount-1) | |
160 | ||
161 | return bs, nil | |
162 | } | |
163 | ||
164 | func EncodeFilePath(filePath string) []byte { | |
165 | pathSections := strings.Split(filePath, "/") | |
166 | pathItemCount := make([]byte, 2) | |
167 | binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections))) | |
168 | ||
169 | bytes := pathItemCount | |
170 | ||
171 | for _, section := range pathSections { | |
172 | bytes = append(bytes, []byte{0, 0}...) | |
173 | ||
174 | pathStr := []byte(section) | |
175 | bytes = append(bytes, byte(len(pathStr))) | |
176 | bytes = append(bytes, pathStr...) | |
177 | } | |
178 | ||
179 | return bytes | |
180 | } | |
16a4ad70 | 181 | |
16a4ad70 JH |
182 | // effectiveFile wraps os.Open to check for the presence of a partial file transfer as a fallback |
183 | func effectiveFile(filePath string) (*os.File, error) { | |
184 | file, err := os.Open(filePath) | |
185 | if err != nil && !errors.Is(err, fs.ErrNotExist) { | |
186 | return nil, err | |
187 | } | |
188 | ||
189 | if errors.Is(err, fs.ErrNotExist) { | |
c26c20e3 | 190 | file, err = os.OpenFile(filePath+incompleteFileSuffix, os.O_APPEND|os.O_WRONLY, 0644) |
16a4ad70 JH |
191 | if err != nil { |
192 | return nil, err | |
193 | } | |
194 | } | |
195 | return file, nil | |
196 | } |