Co-authored-by: sneak <sneak@sneak.berlin> Reviewed-on: #5
This commit was merged in pull request #5.
This commit is contained in:
1 parent
7a8a1b4a4a
commit
7df558d8d0
29 files changed
+923
-220
No files matched your search
+142
-83
@@ -1,35 +1,42 @@
|
||||
package mfer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.eeqj.de/sneak/mfer/internal/log"
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
|
||||
type ManifestFile struct {
|
||||
Path string
|
||||
FileInfo fs.FileInfo
|
||||
type manifestFile struct {
|
||||
path string
|
||||
info fs.FileInfo
|
||||
}
|
||||
|
||||
func (m *ManifestFile) String() string {
|
||||
return fmt.Sprintf("<File \"%s\">", m.Path)
|
||||
func (m *manifestFile) String() string {
|
||||
return fmt.Sprintf("<File \"%s\">", m.path)
|
||||
}
|
||||
|
||||
type Manifest struct {
|
||||
SourceFS afero.Fs
|
||||
SourceFSRoot string
|
||||
Files []*ManifestFile
|
||||
ScanOptions *ManifestScanOptions
|
||||
TotalFileSize int64
|
||||
type manifest struct {
|
||||
sourceFS []afero.Fs
|
||||
files []*manifestFile
|
||||
scanOptions *ManifestScanOptions
|
||||
totalFileSize int64
|
||||
pbInner *MFFile
|
||||
pbOuter *MFFileOuter
|
||||
output *bytes.Buffer
|
||||
ctx context.Context
|
||||
errors []*error
|
||||
}
|
||||
|
||||
func (m *Manifest) String() string {
|
||||
return fmt.Sprintf("<Manifest count=%d totalSize=%d>", len(m.Files), m.TotalFileSize)
|
||||
func (m *manifest) String() string {
|
||||
return fmt.Sprintf("<Manifest count=%d totalSize=%d>", len(m.files), m.totalFileSize)
|
||||
}
|
||||
|
||||
type ManifestScanOptions struct {
|
||||
@@ -37,78 +44,130 @@ type ManifestScanOptions struct {
|
||||
FollowSymLinks bool
|
||||
}
|
||||
|
||||
func NewFromPath(inputPath string, options *ManifestScanOptions) (*Manifest, error) {
|
||||
func (m *manifest) HasError() bool {
|
||||
return len(m.errors) > 0
|
||||
}
|
||||
|
||||
func (m *manifest) AddError(e error) *manifest {
|
||||
m.errors = append(m.errors, &e)
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *manifest) WithContext(c context.Context) *manifest {
|
||||
m.ctx = c
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *manifest) addInputPath(inputPath string) error {
|
||||
abs, err := filepath.Abs(inputPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
afs := afero.NewBasePathFs(afero.NewOsFs(), abs)
|
||||
m, err := NewFromFS(afs, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m.SourceFSRoot = abs
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func NewFromFS(fs afero.Fs, options *ManifestScanOptions) (*Manifest, error) {
|
||||
m := &Manifest{
|
||||
SourceFS: fs,
|
||||
ScanOptions: options,
|
||||
}
|
||||
err := m.Scan()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m *Manifest) Scan() error {
|
||||
// FIXME scan and whatever function does the hashing should take ctx
|
||||
oe := afero.Walk(m.SourceFS, "./", func(path string, info fs.FileInfo, err error) error {
|
||||
if m.ScanOptions.IgnoreDotfiles && strings.HasPrefix(path, ".") {
|
||||
// FIXME make this check all path components BUG
|
||||
return nil
|
||||
}
|
||||
|
||||
if info != nil && info.IsDir() {
|
||||
// manifests contain only files, directories are implied.
|
||||
return nil
|
||||
}
|
||||
|
||||
fileinfo, staterr := m.SourceFS.Stat(path)
|
||||
if staterr != nil {
|
||||
panic(staterr)
|
||||
}
|
||||
|
||||
nf := &ManifestFile{
|
||||
Path: path,
|
||||
FileInfo: fileinfo,
|
||||
}
|
||||
m.Files = append(m.Files, nf)
|
||||
m.TotalFileSize = m.TotalFileSize + info.Size()
|
||||
return nil
|
||||
})
|
||||
if oe != nil {
|
||||
return oe
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manifest) WriteToFile(path string) error {
|
||||
// FIXME refuse to overwrite without -f if file exists
|
||||
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
return m.Write(f)
|
||||
// FIXME check to make sure inputPath/abs exists maybe
|
||||
afs := afero.NewReadOnlyFs(afero.NewBasePathFs(afero.NewOsFs(), abs))
|
||||
return m.addInputFS(afs)
|
||||
}
|
||||
|
||||
func (m *Manifest) Write(output io.Writer) error {
|
||||
// FIXME implement
|
||||
panic("nope")
|
||||
return nil // nolint:all
|
||||
func (m *manifest) addInputFS(f afero.Fs) error {
|
||||
if m.sourceFS == nil {
|
||||
m.sourceFS = make([]afero.Fs, 0)
|
||||
}
|
||||
m.sourceFS = append(m.sourceFS, f)
|
||||
// FIXME do some sort of check on f here?
|
||||
return nil
|
||||
}
|
||||
|
||||
func New() *manifest {
|
||||
m := &manifest{}
|
||||
return m
|
||||
}
|
||||
|
||||
func NewFromPaths(options *ManifestScanOptions, inputPaths ...string) (*manifest, error) {
|
||||
log.Dump(inputPaths)
|
||||
m := New()
|
||||
m.scanOptions = options
|
||||
for _, p := range inputPaths {
|
||||
err := m.addInputPath(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func NewFromFS(options *ManifestScanOptions, fs afero.Fs) (*manifest, error) {
|
||||
m := New()
|
||||
m.scanOptions = options
|
||||
err := m.addInputFS(fs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m *manifest) GetFileCount() int64 {
|
||||
return int64(len(m.files))
|
||||
}
|
||||
|
||||
func (m *manifest) GetTotalFileSize() int64 {
|
||||
return m.totalFileSize
|
||||
}
|
||||
|
||||
func pathIsHidden(p string) bool {
|
||||
tp := path.Clean(p)
|
||||
if strings.HasPrefix(tp, ".") {
|
||||
return true
|
||||
}
|
||||
for {
|
||||
d, f := path.Split(tp)
|
||||
if strings.HasPrefix(f, ".") {
|
||||
return true
|
||||
}
|
||||
if d == "" {
|
||||
return false
|
||||
}
|
||||
tp = d[0 : len(d)-1] // trim trailing slash from dir
|
||||
}
|
||||
}
|
||||
|
||||
func (m *manifest) addFile(p string, fi fs.FileInfo, sfsIndex int) error {
|
||||
if m.scanOptions.IgnoreDotfiles && pathIsHidden(p) {
|
||||
return nil
|
||||
}
|
||||
if fi != nil && fi.IsDir() {
|
||||
// manifests contain only files, directories are implied.
|
||||
return nil
|
||||
}
|
||||
// FIXME test if 'fi' is already result of stat
|
||||
fileinfo, staterr := m.sourceFS[sfsIndex].Stat(p)
|
||||
if staterr != nil {
|
||||
return staterr
|
||||
}
|
||||
cleanPath := p
|
||||
if cleanPath[0:1] == "/" {
|
||||
cleanPath = cleanPath[1:]
|
||||
}
|
||||
nf := &manifestFile{
|
||||
path: cleanPath,
|
||||
info: fileinfo,
|
||||
}
|
||||
m.files = append(m.files, nf)
|
||||
m.totalFileSize = m.totalFileSize + fi.Size()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *manifest) Scan() error {
|
||||
// FIXME scan and whatever function does the hashing should take ctx
|
||||
for idx, sfs := range m.sourceFS {
|
||||
if sfs == nil {
|
||||
return errors.New("invalid source fs")
|
||||
}
|
||||
e := afero.Walk(sfs, "/", func(p string, info fs.FileInfo, err error) error {
|
||||
return m.addFile(p, info, idx)
|
||||
})
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user