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