|
| 1 | +//go:build forknative |
| 2 | + |
| 3 | +package native |
| 4 | + |
| 5 | +import ( |
| 6 | + "errors" |
| 7 | + stdfs "io/fs" |
| 8 | + "os" |
| 9 | + |
| 10 | + corefs "github.com/ObsoleteMadness/ClassicStack/core/fs" |
| 11 | +) |
| 12 | + |
| 13 | +// nativeForkEngine serves the resource fork and Finder info from the HOST file's native |
| 14 | +// facilities: on macOS/HFS+ the resource fork is the "<file>/..namedfork/rsrc" stream |
| 15 | +// and the Finder info is the com.apple.FinderInfo xattr. The data fork is the plain host |
| 16 | +// file, reached through the base FileSystem like the AppleDouble engine. Finder-info |
| 17 | +// access is per-OS (finderinfo_*.go); the resource-fork stream path is opened directly |
| 18 | +// on the host path, which simply does not exist on a host without native forks (treated |
| 19 | +// as "no resource fork"). |
| 20 | +type nativeForkEngine struct { |
| 21 | + base corefs.FileSystem |
| 22 | + host corefs.HostPather |
| 23 | +} |
| 24 | + |
| 25 | +func newNativeForkEngine(base corefs.FileSystem, host corefs.HostPather) *nativeForkEngine { |
| 26 | + return &nativeForkEngine{base: base, host: host} |
| 27 | +} |
| 28 | + |
| 29 | +// rsrcStreamPath is the host path of the native resource-fork stream for a store path, |
| 30 | +// or ok=false when the store path cannot be resolved to a host path. |
| 31 | +func (e *nativeForkEngine) rsrcStreamPath(storePath string) (string, bool) { |
| 32 | + hp, ok := e.host.HostPath(storePath) |
| 33 | + if !ok { |
| 34 | + return "", false |
| 35 | + } |
| 36 | + return hp + "/..namedfork/rsrc", true |
| 37 | +} |
| 38 | + |
| 39 | +func (e *nativeForkEngine) OpenFork(path string, fork corefs.ForkType, flag int) (corefs.File, error) { |
| 40 | + if fork == corefs.DataFork { |
| 41 | + // The data fork is the plain host file; defer to the base FileSystem. |
| 42 | + return e.base.OpenFile(path, flag) |
| 43 | + } |
| 44 | + sp, ok := e.rsrcStreamPath(path) |
| 45 | + if !ok { |
| 46 | + return nil, stdfs.ErrNotExist |
| 47 | + } |
| 48 | + f, err := os.OpenFile(sp, flag, 0o644) |
| 49 | + if err != nil { |
| 50 | + if errors.Is(err, stdfs.ErrNotExist) && flag&os.O_CREATE == 0 { |
| 51 | + return nil, stdfs.ErrNotExist |
| 52 | + } |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + return f, nil // *os.File satisfies fs.File (ReadAt/WriteAt/Truncate/Stat/Sync/Close) |
| 56 | +} |
| 57 | + |
| 58 | +func (e *nativeForkEngine) ForkLen(path string, fork corefs.ForkType) (int64, error) { |
| 59 | + if fork == corefs.DataFork { |
| 60 | + info, err := e.base.Stat(path) |
| 61 | + if err != nil { |
| 62 | + return 0, err |
| 63 | + } |
| 64 | + return info.Size(), nil |
| 65 | + } |
| 66 | + sp, ok := e.rsrcStreamPath(path) |
| 67 | + if !ok { |
| 68 | + return 0, nil |
| 69 | + } |
| 70 | + info, err := os.Stat(sp) |
| 71 | + if err != nil { |
| 72 | + if errors.Is(err, stdfs.ErrNotExist) { |
| 73 | + return 0, nil |
| 74 | + } |
| 75 | + return 0, err |
| 76 | + } |
| 77 | + return info.Size(), nil |
| 78 | +} |
| 79 | + |
| 80 | +// ReadFinderInfo / WriteFinderInfo are per-OS (finderinfo_darwin.go uses the |
| 81 | +// com.apple.FinderInfo xattr; finderinfo_other.go reports absent / no-op). |
| 82 | + |
| 83 | +func (e *nativeForkEngine) ReadComment(path string) ([]byte, bool) { _ = path; return nil, false } |
| 84 | +func (e *nativeForkEngine) WriteComment(path string, c []byte) error { |
| 85 | + _ = path |
| 86 | + _ = c |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +// MoveMetadata / DeleteMetadata are no-ops: the resource fork and Finder info are host |
| 91 | +// attributes of the file itself, so the base FileSystem's Rename/Remove of the data path |
| 92 | +// carries them automatically. |
| 93 | +func (e *nativeForkEngine) MoveMetadata(old, new string) error { _ = old; _ = new; return nil } |
| 94 | +func (e *nativeForkEngine) DeleteMetadata(path string) error { _ = path; return nil } |
| 95 | + |
| 96 | +// MetadataPaths returns nil: native forks ride with the host file, so there is no |
| 97 | +// separate container to coordinate on a rename/delete. |
| 98 | +func (e *nativeForkEngine) MetadataPaths(storePath string) []string { _ = storePath; return nil } |
| 99 | + |
| 100 | +// hostPathOf resolves the host path for a store path (used by the per-OS Finder-info |
| 101 | +// code), or ok=false. |
| 102 | +func (e *nativeForkEngine) hostPathOf(storePath string) (string, bool) { |
| 103 | + return e.host.HostPath(storePath) |
| 104 | +} |
0 commit comments