]>
Commit | Line | Data |
---|---|---|
7cd900d6 JH |
1 | package hotline |
2 | ||
3 | import ( | |
4 | "encoding/binary" | |
5 | "errors" | |
6 | "fmt" | |
7 | "io" | |
8 | "io/fs" | |
9 | "os" | |
f22acf38 | 10 | "path/filepath" |
7cd900d6 JH |
11 | ) |
12 | ||
13 | const ( | |
14 | incompleteFileSuffix = ".incomplete" | |
f22acf38 JH |
15 | infoForkNameTemplate = ".info_%s" // template string for info fork filenames |
16 | rsrcForkNameTemplate = ".rsrc_%s" // template string for resource fork filenames | |
7cd900d6 JH |
17 | ) |
18 | ||
19 | // fileWrapper encapsulates the data, info, and resource forks of a Hotline file and provides methods to manage the files. | |
20 | type fileWrapper struct { | |
21 | fs FileStore | |
22 | name string // name of the file | |
23 | path string // path to file directory | |
24 | dataPath string // path to the file data fork | |
25 | dataOffset int64 | |
26 | rsrcPath string // path to the file resource fork | |
27 | infoPath string // path to the file information fork | |
28 | incompletePath string // path to partially transferred temp file | |
7cd900d6 JH |
29 | ffo *flattenedFileObject |
30 | } | |
31 | ||
32 | func newFileWrapper(fs FileStore, path string, dataOffset int64) (*fileWrapper, error) { | |
f22acf38 JH |
33 | dir := filepath.Dir(path) |
34 | fName := filepath.Base(path) | |
7cd900d6 JH |
35 | f := fileWrapper{ |
36 | fs: fs, | |
37 | name: fName, | |
38 | path: dir, | |
39 | dataPath: path, | |
40 | dataOffset: dataOffset, | |
f22acf38 JH |
41 | rsrcPath: filepath.Join(dir, fmt.Sprintf(rsrcForkNameTemplate, fName)), |
42 | infoPath: filepath.Join(dir, fmt.Sprintf(infoForkNameTemplate, fName)), | |
43 | incompletePath: filepath.Join(dir, fName+incompleteFileSuffix), | |
7cd900d6 JH |
44 | ffo: &flattenedFileObject{}, |
45 | } | |
46 | ||
47 | var err error | |
48 | f.ffo, err = f.flattenedFileObject() | |
49 | if err != nil { | |
50 | return nil, err | |
51 | } | |
52 | ||
53 | return &f, nil | |
54 | } | |
55 | ||
56 | func (f *fileWrapper) totalSize() []byte { | |
57 | var s int64 | |
58 | size := make([]byte, 4) | |
59 | ||
60 | info, err := f.fs.Stat(f.dataPath) | |
61 | if err == nil { | |
62 | s += info.Size() - f.dataOffset | |
63 | } | |
64 | ||
65 | info, err = f.fs.Stat(f.rsrcPath) | |
66 | if err == nil { | |
67 | s += info.Size() | |
68 | } | |
69 | ||
70 | binary.BigEndian.PutUint32(size, uint32(s)) | |
71 | ||
72 | return size | |
73 | } | |
74 | ||
75 | func (f *fileWrapper) rsrcForkSize() (s [4]byte) { | |
76 | info, err := f.fs.Stat(f.rsrcPath) | |
77 | if err != nil { | |
78 | return s | |
79 | } | |
80 | ||
81 | binary.BigEndian.PutUint32(s[:], uint32(info.Size())) | |
82 | return s | |
83 | } | |
84 | ||
85 | func (f *fileWrapper) rsrcForkHeader() FlatFileForkHeader { | |
86 | return FlatFileForkHeader{ | |
87 | ForkType: [4]byte{0x4D, 0x41, 0x43, 0x52}, // "MACR" | |
88 | CompressionType: [4]byte{}, | |
89 | RSVD: [4]byte{}, | |
90 | DataSize: f.rsrcForkSize(), | |
91 | } | |
92 | } | |
93 | ||
94 | func (f *fileWrapper) incompleteDataName() string { | |
95 | return f.name + incompleteFileSuffix | |
96 | } | |
97 | ||
98 | func (f *fileWrapper) rsrcForkName() string { | |
f22acf38 | 99 | return fmt.Sprintf(rsrcForkNameTemplate, f.name) |
7cd900d6 JH |
100 | } |
101 | ||
102 | func (f *fileWrapper) infoForkName() string { | |
f22acf38 | 103 | return fmt.Sprintf(infoForkNameTemplate, f.name) |
7cd900d6 JH |
104 | } |
105 | ||
df1ade54 | 106 | func (f *fileWrapper) rsrcForkWriter() (io.WriteCloser, error) { |
7cd900d6 JH |
107 | file, err := os.OpenFile(f.rsrcPath, os.O_CREATE|os.O_WRONLY, 0644) |
108 | if err != nil { | |
109 | return nil, err | |
110 | } | |
111 | ||
112 | return file, nil | |
113 | } | |
114 | ||
df1ade54 | 115 | func (f *fileWrapper) infoForkWriter() (io.WriteCloser, error) { |
7cd900d6 JH |
116 | file, err := os.OpenFile(f.infoPath, os.O_CREATE|os.O_WRONLY, 0644) |
117 | if err != nil { | |
118 | return nil, err | |
119 | } | |
120 | ||
121 | return file, nil | |
122 | } | |
123 | ||
df1ade54 | 124 | func (f *fileWrapper) incFileWriter() (io.WriteCloser, error) { |
7cd900d6 JH |
125 | file, err := os.OpenFile(f.incompletePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) |
126 | if err != nil { | |
127 | return nil, err | |
128 | } | |
129 | ||
130 | return file, nil | |
131 | } | |
132 | ||
133 | func (f *fileWrapper) dataForkReader() (io.Reader, error) { | |
134 | return f.fs.Open(f.dataPath) | |
135 | } | |
136 | ||
137 | func (f *fileWrapper) rsrcForkFile() (*os.File, error) { | |
138 | return f.fs.Open(f.rsrcPath) | |
139 | } | |
140 | ||
141 | func (f *fileWrapper) dataFile() (os.FileInfo, error) { | |
142 | if fi, err := f.fs.Stat(f.dataPath); err == nil { | |
143 | return fi, nil | |
144 | } | |
145 | if fi, err := f.fs.Stat(f.incompletePath); err == nil { | |
146 | return fi, nil | |
147 | } | |
148 | ||
149 | return nil, errors.New("file or directory not found") | |
150 | } | |
151 | ||
a8e89ca4 JH |
152 | // move a fileWrapper and its associated meta files to newPath. |
153 | // Meta files include: | |
154 | // * Partially uploaded file ending with .incomplete | |
155 | // * Resource fork starting with .rsrc_ | |
156 | // * Info fork starting with .info | |
157 | // During move of the meta files, os.ErrNotExist is ignored as these files may legitimately not exist. | |
7cd900d6 | 158 | func (f *fileWrapper) move(newPath string) error { |
46b48603 | 159 | err := f.fs.Rename(f.dataPath, filepath.Join(newPath, f.name)) |
7cd900d6 | 160 | if err != nil { |
a8e89ca4 | 161 | return err |
7cd900d6 JH |
162 | } |
163 | ||
46b48603 | 164 | err = f.fs.Rename(f.incompletePath, filepath.Join(newPath, f.incompleteDataName())) |
a8e89ca4 JH |
165 | if err != nil && !errors.Is(err, os.ErrNotExist) { |
166 | return err | |
7cd900d6 JH |
167 | } |
168 | ||
46b48603 | 169 | err = f.fs.Rename(f.rsrcPath, filepath.Join(newPath, f.rsrcForkName())) |
a8e89ca4 JH |
170 | if err != nil && !errors.Is(err, os.ErrNotExist) { |
171 | return err | |
7cd900d6 JH |
172 | } |
173 | ||
46b48603 | 174 | err = f.fs.Rename(f.infoPath, filepath.Join(newPath, f.infoForkName())) |
a8e89ca4 JH |
175 | if err != nil && !errors.Is(err, os.ErrNotExist) { |
176 | return err | |
7cd900d6 JH |
177 | } |
178 | ||
179 | return nil | |
180 | } | |
181 | ||
182 | // delete a fileWrapper and its associated metadata files if they exist | |
183 | func (f *fileWrapper) delete() error { | |
184 | err := f.fs.RemoveAll(f.dataPath) | |
185 | if err != nil { | |
a8e89ca4 | 186 | return err |
7cd900d6 JH |
187 | } |
188 | ||
189 | err = f.fs.Remove(f.incompletePath) | |
a8e89ca4 JH |
190 | if err != nil && !errors.Is(err, os.ErrNotExist) { |
191 | return err | |
7cd900d6 JH |
192 | } |
193 | ||
194 | err = f.fs.Remove(f.rsrcPath) | |
a8e89ca4 JH |
195 | if err != nil && !errors.Is(err, os.ErrNotExist) { |
196 | return err | |
7cd900d6 JH |
197 | } |
198 | ||
199 | err = f.fs.Remove(f.infoPath) | |
a8e89ca4 JH |
200 | if err != nil && !errors.Is(err, os.ErrNotExist) { |
201 | return err | |
7cd900d6 JH |
202 | } |
203 | ||
204 | return nil | |
205 | } | |
206 | ||
207 | func (f *fileWrapper) flattenedFileObject() (*flattenedFileObject, error) { | |
208 | dataSize := make([]byte, 4) | |
209 | mTime := make([]byte, 8) | |
210 | ||
211 | ft := defaultFileType | |
212 | ||
213 | fileInfo, err := f.fs.Stat(f.dataPath) | |
214 | if err != nil && !errors.Is(err, fs.ErrNotExist) { | |
215 | return nil, err | |
216 | } | |
217 | if errors.Is(err, fs.ErrNotExist) { | |
218 | fileInfo, err = f.fs.Stat(f.incompletePath) | |
219 | if err == nil { | |
220 | mTime = toHotlineTime(fileInfo.ModTime()) | |
221 | binary.BigEndian.PutUint32(dataSize, uint32(fileInfo.Size()-f.dataOffset)) | |
222 | ft, _ = fileTypeFromInfo(fileInfo) | |
223 | } | |
224 | } else { | |
225 | mTime = toHotlineTime(fileInfo.ModTime()) | |
226 | binary.BigEndian.PutUint32(dataSize, uint32(fileInfo.Size()-f.dataOffset)) | |
227 | ft, _ = fileTypeFromInfo(fileInfo) | |
228 | } | |
229 | ||
230 | f.ffo.FlatFileHeader = FlatFileHeader{ | |
231 | Format: [4]byte{0x46, 0x49, 0x4c, 0x50}, // "FILP" | |
232 | Version: [2]byte{0, 1}, | |
233 | RSVD: [16]byte{}, | |
234 | ForkCount: [2]byte{0, 2}, | |
235 | } | |
236 | ||
237 | _, err = f.fs.Stat(f.infoPath) | |
238 | if err == nil { | |
239 | b, err := f.fs.ReadFile(f.infoPath) | |
240 | if err != nil { | |
241 | return nil, err | |
242 | } | |
243 | ||
244 | f.ffo.FlatFileHeader.ForkCount[1] = 3 | |
245 | ||
246 | if err := f.ffo.FlatFileInformationFork.UnmarshalBinary(b); err != nil { | |
247 | return nil, err | |
248 | } | |
249 | } else { | |
250 | f.ffo.FlatFileInformationFork = FlatFileInformationFork{ | |
251 | Platform: []byte("AMAC"), // TODO: Remove hardcode to support "AWIN" Platform (maybe?) | |
252 | TypeSignature: []byte(ft.TypeCode), | |
253 | CreatorSignature: []byte(ft.CreatorCode), | |
254 | Flags: []byte{0, 0, 0, 0}, | |
255 | PlatformFlags: []byte{0, 0, 1, 0}, // TODO: What is this? | |
256 | RSVD: make([]byte, 32), | |
257 | CreateDate: mTime, // some filesystems don't support createTime | |
258 | ModifyDate: mTime, | |
259 | NameScript: []byte{0, 0}, | |
260 | Name: []byte(f.name), | |
261 | NameSize: []byte{0, 0}, | |
262 | CommentSize: []byte{0, 0}, | |
263 | Comment: []byte{}, | |
264 | } | |
265 | binary.BigEndian.PutUint16(f.ffo.FlatFileInformationFork.NameSize, uint16(len(f.name))) | |
266 | } | |
267 | ||
268 | f.ffo.FlatFileInformationForkHeader = FlatFileForkHeader{ | |
269 | ForkType: [4]byte{0x49, 0x4E, 0x46, 0x4F}, // "INFO" | |
270 | CompressionType: [4]byte{}, | |
271 | RSVD: [4]byte{}, | |
272 | DataSize: f.ffo.FlatFileInformationFork.Size(), | |
273 | } | |
274 | ||
275 | f.ffo.FlatFileDataForkHeader = FlatFileForkHeader{ | |
276 | ForkType: [4]byte{0x44, 0x41, 0x54, 0x41}, // "DATA" | |
277 | CompressionType: [4]byte{}, | |
278 | RSVD: [4]byte{}, | |
279 | DataSize: [4]byte{dataSize[0], dataSize[1], dataSize[2], dataSize[3]}, | |
280 | } | |
281 | f.ffo.FlatFileResForkHeader = f.rsrcForkHeader() | |
282 | ||
283 | return f.ffo, nil | |
284 | } |