mirror of
https://git.sr.ht/~phw/scotty
synced 2025-06-01 19:38:34 +02:00
77 lines
2.1 KiB
Go
77 lines
2.1 KiB
Go
/*
|
|
Copyright © 2025 Philipp Wolfer <phw@uploadedlobster.com>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
|
|
package archive
|
|
|
|
import (
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// An implementation of the [ArchiveReader] interface for directories.
|
|
type dirArchive struct {
|
|
path string
|
|
dirFS fs.FS
|
|
}
|
|
|
|
func (a *dirArchive) OpenArchive(path string) error {
|
|
a.path = filepath.Clean(path)
|
|
a.dirFS = os.DirFS(path)
|
|
return nil
|
|
}
|
|
|
|
func (a *dirArchive) Close() error {
|
|
return nil
|
|
}
|
|
|
|
// Open opens the named file in the archive.
|
|
// [fs.File.Close] must be called to release any associated resources.
|
|
func (a *dirArchive) Open(path string) (fs.File, error) {
|
|
return a.dirFS.Open(path)
|
|
}
|
|
|
|
func (a *dirArchive) Glob(pattern string) ([]FileInfo, error) {
|
|
files, err := fs.Glob(a.dirFS, pattern)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result := make([]FileInfo, 0)
|
|
for _, name := range files {
|
|
stat, err := fs.Stat(a.dirFS, name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if stat.IsDir() {
|
|
continue
|
|
}
|
|
|
|
fullPath := filepath.Join(a.path, name)
|
|
info := FileInfo{
|
|
Name: name,
|
|
File: &filesystemFile{path: fullPath},
|
|
}
|
|
result = append(result, info)
|
|
}
|
|
|
|
return result, nil
|
|
}
|