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