14 incompleteFileSuffix = ".incomplete"
15 infoForkNameTemplate = ".info_%s" // template string for info fork filenames
16 rsrcForkNameTemplate = ".rsrc_%s" // template string for resource fork filenames
19 // fileWrapper encapsulates the data, info, and resource forks of a Hotline file and provides methods to manage the files.
20 type fileWrapper struct {
22 name string // name of the file
23 path string // path to file directory
24 dataPath string // path to the file data fork
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
29 ffo *flattenedFileObject
32 func newFileWrapper(fs FileStore, path string, dataOffset int64) (*fileWrapper, error) {
33 dir := filepath.Dir(path)
34 fName := filepath.Base(path)
40 dataOffset: dataOffset,
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),
44 ffo: &flattenedFileObject{},
48 f.ffo, err = f.flattenedFileObject()
56 func (f *fileWrapper) totalSize() []byte {
58 size := make([]byte, 4)
60 info, err := f.fs.Stat(f.dataPath)
62 s += info.Size() - f.dataOffset
65 info, err = f.fs.Stat(f.rsrcPath)
70 binary.BigEndian.PutUint32(size, uint32(s))
75 func (f *fileWrapper) rsrcForkSize() (s [4]byte) {
76 info, err := f.fs.Stat(f.rsrcPath)
81 binary.BigEndian.PutUint32(s[:], uint32(info.Size()))
85 func (f *fileWrapper) rsrcForkHeader() FlatFileForkHeader {
86 return FlatFileForkHeader{
87 ForkType: [4]byte{0x4D, 0x41, 0x43, 0x52}, // "MACR"
88 CompressionType: [4]byte{},
90 DataSize: f.rsrcForkSize(),
94 func (f *fileWrapper) incompleteDataName() string {
95 return f.name + incompleteFileSuffix
98 func (f *fileWrapper) rsrcForkName() string {
99 return fmt.Sprintf(rsrcForkNameTemplate, f.name)
102 func (f *fileWrapper) infoForkName() string {
103 return fmt.Sprintf(infoForkNameTemplate, f.name)
106 func (f *fileWrapper) rsrcForkWriter() (io.WriteCloser, error) {
107 file, err := os.OpenFile(f.rsrcPath, os.O_CREATE|os.O_WRONLY, 0644)
115 func (f *fileWrapper) infoForkWriter() (io.WriteCloser, error) {
116 file, err := os.OpenFile(f.infoPath, os.O_CREATE|os.O_WRONLY, 0644)
124 func (f *fileWrapper) incFileWriter() (io.WriteCloser, error) {
125 file, err := os.OpenFile(f.incompletePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
133 func (f *fileWrapper) dataForkReader() (io.Reader, error) {
134 return f.fs.Open(f.dataPath)
137 func (f *fileWrapper) rsrcForkFile() (*os.File, error) {
138 return f.fs.Open(f.rsrcPath)
141 func (f *fileWrapper) dataFile() (os.FileInfo, error) {
142 if fi, err := f.fs.Stat(f.dataPath); err == nil {
145 if fi, err := f.fs.Stat(f.incompletePath); err == nil {
149 return nil, errors.New("file or directory not found")
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.
158 func (f *fileWrapper) move(newPath string) error {
159 err := f.fs.Rename(f.dataPath, filepath.Join(newPath, f.name))
164 err = f.fs.Rename(f.incompletePath, filepath.Join(newPath, f.incompleteDataName()))
165 if err != nil && !errors.Is(err, os.ErrNotExist) {
169 err = f.fs.Rename(f.rsrcPath, filepath.Join(newPath, f.rsrcForkName()))
170 if err != nil && !errors.Is(err, os.ErrNotExist) {
174 err = f.fs.Rename(f.infoPath, filepath.Join(newPath, f.infoForkName()))
175 if err != nil && !errors.Is(err, os.ErrNotExist) {
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)
189 err = f.fs.Remove(f.incompletePath)
190 if err != nil && !errors.Is(err, os.ErrNotExist) {
194 err = f.fs.Remove(f.rsrcPath)
195 if err != nil && !errors.Is(err, os.ErrNotExist) {
199 err = f.fs.Remove(f.infoPath)
200 if err != nil && !errors.Is(err, os.ErrNotExist) {
207 func (f *fileWrapper) flattenedFileObject() (*flattenedFileObject, error) {
208 dataSize := make([]byte, 4)
209 mTime := make([]byte, 8)
211 ft := defaultFileType
213 fileInfo, err := f.fs.Stat(f.dataPath)
214 if err != nil && !errors.Is(err, fs.ErrNotExist) {
217 if errors.Is(err, fs.ErrNotExist) {
218 fileInfo, err = f.fs.Stat(f.incompletePath)
220 mTime = toHotlineTime(fileInfo.ModTime())
221 binary.BigEndian.PutUint32(dataSize, uint32(fileInfo.Size()-f.dataOffset))
222 ft, _ = fileTypeFromInfo(fileInfo)
225 mTime = toHotlineTime(fileInfo.ModTime())
226 binary.BigEndian.PutUint32(dataSize, uint32(fileInfo.Size()-f.dataOffset))
227 ft, _ = fileTypeFromInfo(fileInfo)
230 f.ffo.FlatFileHeader = FlatFileHeader{
231 Format: [4]byte{0x46, 0x49, 0x4c, 0x50}, // "FILP"
232 Version: [2]byte{0, 1},
234 ForkCount: [2]byte{0, 2},
237 _, err = f.fs.Stat(f.infoPath)
239 b, err := f.fs.ReadFile(f.infoPath)
244 f.ffo.FlatFileHeader.ForkCount[1] = 3
246 if err := f.ffo.FlatFileInformationFork.UnmarshalBinary(b); err != nil {
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
259 NameScript: []byte{0, 0},
260 Name: []byte(f.name),
261 NameSize: []byte{0, 0},
262 CommentSize: []byte{0, 0},
265 binary.BigEndian.PutUint16(f.ffo.FlatFileInformationFork.NameSize, uint16(len(f.name)))
268 f.ffo.FlatFileInformationForkHeader = FlatFileForkHeader{
269 ForkType: [4]byte{0x49, 0x4E, 0x46, 0x4F}, // "INFO"
270 CompressionType: [4]byte{},
272 DataSize: f.ffo.FlatFileInformationFork.Size(),
275 f.ffo.FlatFileDataForkHeader = FlatFileForkHeader{
276 ForkType: [4]byte{0x44, 0x41, 0x54, 0x41}, // "DATA"
277 CompressionType: [4]byte{},
279 DataSize: [4]byte{dataSize[0], dataSize[1], dataSize[2], dataSize[3]},
281 f.ffo.FlatFileResForkHeader = f.rsrcForkHeader()