15 incompleteFileSuffix = ".incomplete"
16 infoForkNameTemplate = "%s.info_%s" // template string for info fork filenames
17 rsrcForkNameTemplate = "%s.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 saveMetaData bool // if true, enables saving of info and resource forks in sidecar files
31 infoFork *FlatFileInformationFork
32 ffo *flattenedFileObject
35 func newFileWrapper(fs FileStore, path string, dataOffset int64) (*fileWrapper, error) {
36 pathSegs := strings.Split(path, pathSeparator)
37 dir := strings.Join(pathSegs[:len(pathSegs)-1], pathSeparator)
38 fName := pathSegs[len(pathSegs)-1]
44 dataOffset: dataOffset,
45 rsrcPath: fmt.Sprintf(rsrcForkNameTemplate, dir+"/", fName),
46 infoPath: fmt.Sprintf(infoForkNameTemplate, dir+"/", fName),
47 incompletePath: dir + "/" + fName + incompleteFileSuffix,
48 ffo: &flattenedFileObject{},
52 f.ffo, err = f.flattenedFileObject()
60 func (f *fileWrapper) totalSize() []byte {
62 size := make([]byte, 4)
64 info, err := f.fs.Stat(f.dataPath)
66 s += info.Size() - f.dataOffset
69 info, err = f.fs.Stat(f.rsrcPath)
74 binary.BigEndian.PutUint32(size, uint32(s))
79 func (f *fileWrapper) rsrcForkSize() (s [4]byte) {
80 info, err := f.fs.Stat(f.rsrcPath)
85 binary.BigEndian.PutUint32(s[:], uint32(info.Size()))
89 func (f *fileWrapper) rsrcForkHeader() FlatFileForkHeader {
90 return FlatFileForkHeader{
91 ForkType: [4]byte{0x4D, 0x41, 0x43, 0x52}, // "MACR"
92 CompressionType: [4]byte{},
94 DataSize: f.rsrcForkSize(),
98 func (f *fileWrapper) incompleteDataName() string {
99 return f.name + incompleteFileSuffix
102 func (f *fileWrapper) rsrcForkName() string {
103 return fmt.Sprintf(rsrcForkNameTemplate, "", f.name)
106 func (f *fileWrapper) infoForkName() string {
107 return fmt.Sprintf(infoForkNameTemplate, "", f.name)
110 func (f *fileWrapper) creatorCode() []byte {
111 if f.ffo.FlatFileInformationFork.CreatorSignature != nil {
112 return f.infoFork.CreatorSignature
114 return []byte(fileTypeFromFilename(f.name).CreatorCode)
117 func (f *fileWrapper) typeCode() []byte {
118 if f.infoFork != nil {
119 return f.infoFork.TypeSignature
121 return []byte(fileTypeFromFilename(f.name).TypeCode)
124 func (f *fileWrapper) rsrcForkWriter() (io.Writer, error) {
125 file, err := os.OpenFile(f.rsrcPath, os.O_CREATE|os.O_WRONLY, 0644)
133 func (f *fileWrapper) infoForkWriter() (io.Writer, error) {
134 file, err := os.OpenFile(f.infoPath, os.O_CREATE|os.O_WRONLY, 0644)
142 func (f *fileWrapper) incFileWriter() (io.Writer, error) {
143 file, err := os.OpenFile(f.incompletePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
151 func (f *fileWrapper) dataForkReader() (io.Reader, error) {
152 return f.fs.Open(f.dataPath)
155 func (f *fileWrapper) rsrcForkFile() (*os.File, error) {
156 return f.fs.Open(f.rsrcPath)
159 func (f *fileWrapper) dataFile() (os.FileInfo, error) {
160 if fi, err := f.fs.Stat(f.dataPath); err == nil {
163 if fi, err := f.fs.Stat(f.incompletePath); err == nil {
167 return nil, errors.New("file or directory not found")
170 // move a fileWrapper and its associated metadata files to newPath
171 func (f *fileWrapper) move(newPath string) error {
172 err := f.fs.Rename(f.dataPath, path.Join(newPath, f.name))
177 err = f.fs.Rename(f.incompletePath, path.Join(newPath, f.incompleteDataName()))
182 err = f.fs.Rename(f.rsrcPath, path.Join(newPath, f.rsrcForkName()))
187 err = f.fs.Rename(f.infoPath, path.Join(newPath, f.infoForkName()))
195 // delete a fileWrapper and its associated metadata files if they exist
196 func (f *fileWrapper) delete() error {
197 err := f.fs.RemoveAll(f.dataPath)
202 err = f.fs.Remove(f.incompletePath)
207 err = f.fs.Remove(f.rsrcPath)
212 err = f.fs.Remove(f.infoPath)
220 func (f *fileWrapper) flattenedFileObject() (*flattenedFileObject, error) {
221 dataSize := make([]byte, 4)
222 mTime := make([]byte, 8)
224 ft := defaultFileType
226 fileInfo, err := f.fs.Stat(f.dataPath)
227 if err != nil && !errors.Is(err, fs.ErrNotExist) {
230 if errors.Is(err, fs.ErrNotExist) {
231 fileInfo, err = f.fs.Stat(f.incompletePath)
233 mTime = toHotlineTime(fileInfo.ModTime())
234 binary.BigEndian.PutUint32(dataSize, uint32(fileInfo.Size()-f.dataOffset))
235 ft, _ = fileTypeFromInfo(fileInfo)
238 mTime = toHotlineTime(fileInfo.ModTime())
239 binary.BigEndian.PutUint32(dataSize, uint32(fileInfo.Size()-f.dataOffset))
240 ft, _ = fileTypeFromInfo(fileInfo)
243 f.ffo.FlatFileHeader = FlatFileHeader{
244 Format: [4]byte{0x46, 0x49, 0x4c, 0x50}, // "FILP"
245 Version: [2]byte{0, 1},
247 ForkCount: [2]byte{0, 2},
250 _, err = f.fs.Stat(f.infoPath)
252 b, err := f.fs.ReadFile(f.infoPath)
257 f.ffo.FlatFileHeader.ForkCount[1] = 3
259 if err := f.ffo.FlatFileInformationFork.UnmarshalBinary(b); err != nil {
263 f.ffo.FlatFileInformationFork = FlatFileInformationFork{
264 Platform: []byte("AMAC"), // TODO: Remove hardcode to support "AWIN" Platform (maybe?)
265 TypeSignature: []byte(ft.TypeCode),
266 CreatorSignature: []byte(ft.CreatorCode),
267 Flags: []byte{0, 0, 0, 0},
268 PlatformFlags: []byte{0, 0, 1, 0}, // TODO: What is this?
269 RSVD: make([]byte, 32),
270 CreateDate: mTime, // some filesystems don't support createTime
272 NameScript: []byte{0, 0},
273 Name: []byte(f.name),
274 NameSize: []byte{0, 0},
275 CommentSize: []byte{0, 0},
278 binary.BigEndian.PutUint16(f.ffo.FlatFileInformationFork.NameSize, uint16(len(f.name)))
281 f.ffo.FlatFileInformationForkHeader = FlatFileForkHeader{
282 ForkType: [4]byte{0x49, 0x4E, 0x46, 0x4F}, // "INFO"
283 CompressionType: [4]byte{},
285 DataSize: f.ffo.FlatFileInformationFork.Size(),
288 f.ffo.FlatFileDataForkHeader = FlatFileForkHeader{
289 ForkType: [4]byte{0x44, 0x41, 0x54, 0x41}, // "DATA"
290 CompressionType: [4]byte{},
292 DataSize: [4]byte{dataSize[0], dataSize[1], dataSize[2], dataSize[3]},
294 f.ffo.FlatFileResForkHeader = f.rsrcForkHeader()