mirror of
https://git.sr.ht/~phw/scotty
synced 2025-06-05 04:58:33 +02:00
Moved archive package to public pkg/
This commit is contained in:
parent
28c618ffce
commit
1244405747
10 changed files with 160 additions and 104 deletions
101
pkg/archive/archive.go
Normal file
101
pkg/archive/archive.go
Normal file
|
@ -0,0 +1,101 @@
|
|||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
// Implements generic access to files inside an archive.
|
||||
//
|
||||
// An archive in this context can be any container that holds files.
|
||||
// In this implementation the archive can be a ZIP file or a directory.
|
||||
package archive
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Generic interface to access files inside an archive.
|
||||
type ArchiveReader interface {
|
||||
io.Closer
|
||||
|
||||
// Open the file inside the archive identified by the given path.
|
||||
// The path is relative to the archive's root.
|
||||
// The caller must call [fs.File.Close] when finished using the file.
|
||||
Open(path string) (fs.File, error)
|
||||
|
||||
// List files inside the archive which satisfy the given glob pattern.
|
||||
// This method only returns files, not directories.
|
||||
Glob(pattern string) ([]FileInfo, error)
|
||||
}
|
||||
|
||||
// Open an archive in path.
|
||||
// The archive can be a ZIP file or a directory. The implementation
|
||||
// will detect the type of archive and return the appropriate
|
||||
// implementation of the Archive interface.
|
||||
func OpenArchive(path string) (ArchiveReader, error) {
|
||||
fi, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch mode := fi.Mode(); {
|
||||
case mode.IsRegular():
|
||||
archive := &zipArchive{}
|
||||
err := archive.OpenArchive(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return archive, nil
|
||||
case mode.IsDir():
|
||||
archive := &dirArchive{}
|
||||
err := archive.OpenArchive(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return archive, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported file mode: %s", mode)
|
||||
}
|
||||
}
|
||||
|
||||
// Interface for a file that can be opened when needed.
|
||||
type OpenableFile interface {
|
||||
// Open the file for reading.
|
||||
// The caller is responsible to call [io.ReadCloser.Close] when
|
||||
// finished reading the file.
|
||||
Open() (io.ReadCloser, error)
|
||||
}
|
||||
|
||||
// Generic information about a file inside an archive.
|
||||
// This provides the filename and allows opening the file for reading.
|
||||
type FileInfo struct {
|
||||
Name string
|
||||
File OpenableFile
|
||||
}
|
||||
|
||||
// A openable file in the filesystem.
|
||||
type filesystemFile struct {
|
||||
path string
|
||||
}
|
||||
|
||||
func (f *filesystemFile) Open() (io.ReadCloser, error) {
|
||||
return os.Open(f.path)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue