15 incompleteFileSuffix = ".incomplete"
16 infoForkNameTemplate = ".info_%s" // template string for info fork filenames
17 rsrcForkNameTemplate = ".rsrc_%s" // template string for resource fork filenames
20 // fileWrapper encapsulates the data, info, and resource forks of a Hotline file and provides methods to manage the files.
21 type fileWrapper struct {
23 name string // name of the file
24 path string // path to file directory
25 dataPath string // path to the file data fork
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
33 func newFileWrapper(fs FileStore, path string, dataOffset int64) (*fileWrapper, error) {
34 dir := filepath.Dir(path)
35 fName := filepath.Base(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{},
49 f.ffo, err = f.flattenedFileObject()
57 func (f *fileWrapper) totalSize() []byte {
59 size := make([]byte, 4)
61 info, err := f.fs.Stat(f.dataPath)
63 s += info.Size() - f.dataOffset
66 info, err = f.fs.Stat(f.rsrcPath)
71 binary.BigEndian.PutUint32(size, uint32(s))
76 func (f *fileWrapper) rsrcForkSize() (s [4]byte) {
77 info, err := f.fs.Stat(f.rsrcPath)
82 binary.BigEndian.PutUint32(s[:], uint32(info.Size()))
86 func (f *fileWrapper) rsrcForkHeader() FlatFileForkHeader {
87 return FlatFileForkHeader{
88 ForkType: [4]byte{0x4D, 0x41, 0x43, 0x52}, // "MACR"
89 CompressionType: [4]byte{},
91 DataSize: f.rsrcForkSize(),
95 func (f *fileWrapper) incompleteDataName() string {
96 return f.name + incompleteFileSuffix
99 func (f *fileWrapper) rsrcForkName() string {
100 return fmt.Sprintf(rsrcForkNameTemplate, f.name)
103 func (f *fileWrapper) infoForkName() string {
104 return fmt.Sprintf(infoForkNameTemplate, f.name)
107 func (f *fileWrapper) rsrcForkWriter() (io.WriteCloser, error) {
108 file, err := os.OpenFile(f.rsrcPath, os.O_CREATE|os.O_WRONLY, 0644)
116 func (f *fileWrapper) infoForkWriter() (io.WriteCloser, error) {
117 file, err := os.OpenFile(f.infoPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
125 func (f *fileWrapper) incFileWriter() (io.WriteCloser, error) {
126 file, err := os.OpenFile(f.incompletePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
134 func (f *fileWrapper) dataForkReader() (io.Reader, error) {
135 return f.fs.Open(f.dataPath)
138 func (f *fileWrapper) rsrcForkFile() (*os.File, error) {
139 return f.fs.Open(f.rsrcPath)
142 func (f *fileWrapper) dataFile() (os.FileInfo, error) {
143 if fi, err := f.fs.Stat(f.dataPath); err == nil {
146 if fi, err := f.fs.Stat(f.incompletePath); err == nil {
150 return nil, errors.New("file or directory not found")
153 // move a fileWrapper and its associated meta files to newPath.
154 // Meta files include:
155 // * Partially uploaded file ending with .incomplete
156 // * Resource fork starting with .rsrc_
157 // * Info fork starting with .info
158 // During move of the meta files, os.ErrNotExist is ignored as these files may legitimately not exist.
159 func (f *fileWrapper) move(newPath string) error {
160 err := f.fs.Rename(f.dataPath, filepath.Join(newPath, f.name))
165 err = f.fs.Rename(f.incompletePath, filepath.Join(newPath, f.incompleteDataName()))
166 if err != nil && !errors.Is(err, os.ErrNotExist) {
170 err = f.fs.Rename(f.rsrcPath, filepath.Join(newPath, f.rsrcForkName()))
171 if err != nil && !errors.Is(err, os.ErrNotExist) {
175 err = f.fs.Rename(f.infoPath, filepath.Join(newPath, f.infoForkName()))
176 if err != nil && !errors.Is(err, os.ErrNotExist) {
183 // delete a fileWrapper and its associated metadata files if they exist
184 func (f *fileWrapper) delete() error {
185 err := f.fs.RemoveAll(f.dataPath)
190 err = f.fs.Remove(f.incompletePath)
191 if err != nil && !errors.Is(err, os.ErrNotExist) {
195 err = f.fs.Remove(f.rsrcPath)
196 if err != nil && !errors.Is(err, os.ErrNotExist) {
200 err = f.fs.Remove(f.infoPath)
201 if err != nil && !errors.Is(err, os.ErrNotExist) {
208 func (f *fileWrapper) flattenedFileObject() (*flattenedFileObject, error) {
209 dataSize := make([]byte, 4)
212 ft := defaultFileType
214 fileInfo, err := f.fs.Stat(f.dataPath)
215 if err != nil && !errors.Is(err, fs.ErrNotExist) {
218 if errors.Is(err, fs.ErrNotExist) {
219 fileInfo, err = f.fs.Stat(f.incompletePath)
221 mTime = toHotlineTime(fileInfo.ModTime())
222 binary.BigEndian.PutUint32(dataSize, uint32(fileInfo.Size()-f.dataOffset))
223 ft, _ = fileTypeFromInfo(fileInfo)
226 mTime = toHotlineTime(fileInfo.ModTime())
227 binary.BigEndian.PutUint32(dataSize, uint32(fileInfo.Size()-f.dataOffset))
228 ft, _ = fileTypeFromInfo(fileInfo)
231 f.ffo.FlatFileHeader = FlatFileHeader{
232 Format: [4]byte{0x46, 0x49, 0x4c, 0x50}, // "FILP"
233 Version: [2]byte{0, 1},
235 ForkCount: [2]byte{0, 2},
238 _, err = f.fs.Stat(f.infoPath)
240 b, err := f.fs.ReadFile(f.infoPath)
245 f.ffo.FlatFileHeader.ForkCount[1] = 3
247 _, err = io.Copy(&f.ffo.FlatFileInformationFork, bytes.NewReader(b))
249 return nil, fmt.Errorf("error copying FlatFileInformationFork: %w", err)
252 f.ffo.FlatFileInformationFork = FlatFileInformationFork{
253 Platform: [4]byte{0x41, 0x4D, 0x41, 0x43}, // "AMAC" TODO: Remove hardcode to support "AWIN" Platform (maybe?)
254 TypeSignature: [4]byte([]byte(ft.TypeCode)),
255 CreatorSignature: [4]byte([]byte(ft.CreatorCode)),
256 PlatformFlags: [4]byte{0, 0, 1, 0}, // TODO: What is this?
257 CreateDate: mTime, // some filesystems don't support createTime
259 Name: []byte(f.name),
263 ns := make([]byte, 2)
264 binary.BigEndian.PutUint16(ns, uint16(len(f.name)))
265 f.ffo.FlatFileInformationFork.NameSize = [2]byte(ns[:])
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()