| 1 | package sender |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "io/fs" |
| 7 | "os" |
| 8 | "path" |
| 9 | ) |
| 10 | |
| 11 | // FileSource is the interface which the gokrazy rsync sender uses |
| 12 | // to discover and read files. This allows working with rsync modules |
| 13 | // that are backed by an actual file system (*os.Root) or fs.FS. |
| 14 | type FileSource interface { |
| 15 | // FS returns the underlying fs.FS for use with fs.WalkDir. |
| 16 | FS() fs.FS |
| 17 | |
| 18 | // Open opens a file. Returns error if file does not implement io.Seeker. |
| 19 | Open(name string) (File, error) |
| 20 | |
| 21 | // Readlink reads a symlink target. Needs fs.ReadLinkFS. |
| 22 | Readlink(name string) (string, error) |
| 23 | |
| 24 | Close() error |
| 25 | } |
| 26 | |
| 27 | type File interface { |
| 28 | fs.File |
| 29 | io.Seeker |
| 30 | } |
| 31 | |
| 32 | type osRootSource struct { |
| 33 | root *os.Root |
| 34 | } |
| 35 | |
| 36 | func newOSRootSource(root *os.Root) FileSource { |
| 37 | return &osRootSource{root: root} |
| 38 | } |
| 39 | |
| 40 | func (s *osRootSource) FS() fs.FS { return s.root.FS() } |
| 41 | func (s *osRootSource) Open(name string) (File, error) { return s.root.Open(name) } |
| 42 | func (s *osRootSource) Readlink(name string) (string, error) { return s.root.Readlink(name) } |
| 43 | func (s *osRootSource) Close() error { return s.root.Close() } |
| 44 | |
| 45 | // singleFileSource implements FileSource. |
| 46 | type singleFileSource struct { |
| 47 | path string |
| 48 | info fs.FileInfo |
| 49 | } |
| 50 | |
| 51 | func newSingleFileSource(path string, info fs.FileInfo) *singleFileSource { |
| 52 | return &singleFileSource{ |
| 53 | path: path, |
| 54 | info: info, |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func (s *singleFileSource) FS() fs.FS { return &singleFileFS{s} } |
| 59 | func (s *singleFileSource) Open(_ string) (File, error) { return os.Open(s.path) } |
| 60 | func (s *singleFileSource) Readlink(_ string) (string, error) { return os.Readlink(s.path) } |
| 61 | func (s *singleFileSource) Close() error { return nil } |
| 62 | |
| 63 | type singleFileFS struct { |
| 64 | sfs *singleFileSource |
| 65 | } |
| 66 | |
| 67 | func (f *singleFileFS) Open(name string) (fs.File, error) { return os.Open(f.sfs.path) } |
| 68 | func (f *singleFileFS) Stat(name string) (fs.FileInfo, error) { return f.sfs.info, nil } |
| 69 | |
| 70 | // fsSource wraps an fs.FS to implement FileSource. |
| 71 | type fsSource struct { |
| 72 | fsys fs.FS |
| 73 | } |
| 74 | |
| 75 | // NewFSSource creates a FileSource from an fs.FS. |
| 76 | // |
| 77 | // Files returned by the fs.FS must implement io.Seeker, |
| 78 | // otherwise Open will fail. |
| 79 | // |
| 80 | // The fs.FS should implement ReadLinkFS, |
| 81 | // otherwise working with symlinks will fail. |
| 82 | func NewFSSource(fsys fs.FS) FileSource { |
| 83 | return &fsSource{fsys: fsys} |
| 84 | } |
| 85 | |
| 86 | func (s *fsSource) FS() fs.FS { return s.fsys } |
| 87 | |
| 88 | func (s *fsSource) Open(name string) (File, error) { |
| 89 | f, err := s.fsys.Open(name) |
| 90 | if err != nil { |
| 91 | return nil, err |
| 92 | } |
| 93 | sf, ok := f.(File) // checks for io.Seeker |
| 94 | if !ok { |
| 95 | f.Close() |
| 96 | return nil, fmt.Errorf("open %s: fs.File must implement io.Seeker", name) |
| 97 | } |
| 98 | return sf, nil |
| 99 | } |
| 100 | |
| 101 | func (s *fsSource) Readlink(name string) (string, error) { |
| 102 | if rl, ok := s.fsys.(fs.ReadLinkFS); ok { |
| 103 | return rl.ReadLink(name) |
| 104 | } |
| 105 | return "", fmt.Errorf("readlink %s: fs.FS does not implement fs.ReadLinkFS", name) |
| 106 | } |
| 107 | |
| 108 | func (s *fsSource) Close() error { return nil } |
| 109 | |
| 110 | // subSource wraps a FileSource and serves the specified subtree, |
| 111 | // like fs.Sub() does for fs.FS. |
| 112 | type subSource struct { |
| 113 | underlying FileSource |
| 114 | subdir string |
| 115 | fsys fs.FS |
| 116 | } |
| 117 | |
| 118 | func newSubSource(underlying FileSource, subdir string) (FileSource, error) { |
| 119 | fsys, err := fs.Sub(underlying.FS(), subdir) |
| 120 | if err != nil { |
| 121 | return nil, err |
| 122 | } |
| 123 | return &subSource{ |
| 124 | underlying: underlying, |
| 125 | subdir: subdir, |
| 126 | fsys: fsys, |
| 127 | }, nil |
| 128 | } |
| 129 | |
| 130 | func (s *subSource) FS() fs.FS { return s.fsys } |
| 131 | |
| 132 | func (s *subSource) Open(name string) (File, error) { |
| 133 | return s.underlying.Open(path.Join(s.subdir, name)) |
| 134 | } |
| 135 | |
| 136 | func (s *subSource) Readlink(name string) (string, error) { |
| 137 | return s.underlying.Readlink(path.Join(s.subdir, name)) |
| 138 | } |
| 139 | |
| 140 | func (s *subSource) Close() error { return nil } |