Skip to content

Commit

Permalink
fix #1
Browse files Browse the repository at this point in the history
  • Loading branch information
deadc0de6 committed Feb 25, 2024
1 parent a9e6ae6 commit 3e4e2b9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
23 changes: 15 additions & 8 deletions internal/walker/archives/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package archives

import (
"context"
"fmt"
"gocatcli/internal/log"
"io/fs"
"os"
Expand Down Expand Up @@ -42,7 +43,7 @@ func GetFiles(path string) ([]*ArchivedFile, error) {
}
defer fd.Close()

format, _, err := archiver.Identify(path, fd)
format, stream, err := archiver.Identify(path, fd)
if err == archiver.ErrNoMatch {
log.Debugf("file \"%s\" is not an archive", path)
return names, nil
Expand All @@ -61,13 +62,19 @@ func GetFiles(path string) ([]*ArchivedFile, error) {
return nil
}

ext := format.(archiver.Extractor)
ctx := context.Background()
err = ext.Extract(ctx, fd, nil, handler)
if err != nil {
return names, err
switch archive := format.(type) {
case archiver.Extractor:
ctx := context.Background()
err = archive.Extract(ctx, stream, nil, handler)
if err != nil {
return names, err
}
log.Debugf("got %d file(s) inside %s", len(names), path)
return names, nil
case archiver.Decompressor:
// no children
return names, nil
}

log.Debugf("got %d file(s) inside %s", len(names), path)
return names, nil
return nil, fmt.Errorf("cannot read archive content for %s", path)
}
31 changes: 21 additions & 10 deletions internal/walker/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,7 @@ func (w *Walker) walk(storageID int, walkPath string, storagePath string, parent
// handle archives
if w.withArchive && archives.IsArchive(pathUnderRoot) {
log.Debugf("%s is archive", pathUnderRoot)
child.Type = node.FileTypeArchive
archived, _ := archives.GetFiles(pathUnderRoot)
for _, arc := range archived {
fpath, err := filepath.Rel(storagePath, pathUnderRoot)
if err != nil {
return err
}
sub := node.NewArchivedFileNode(storageID, fpath, arc.FileInfo, arc.Path)
child.AddChild(sub)
}
processArchive(pathUnderRoot, storageID, storagePath, child)
}
}
return nil
Expand All @@ -133,6 +124,26 @@ func (w *Walker) walk(storageID int, walkPath string, storagePath string, parent
return cnt, err
}

func processArchive(path string, storageID int, storagePath string, child *node.FileNode) {
//defer func() {
// r := recover()
// if r != nil {
// log.Errorf("archive indexing failed for %s", path)
// }
//}()
archived, _ := archives.GetFiles(path)
for _, arc := range archived {
fpath, err := filepath.Rel(storagePath, path)
if err != nil {
log.Errorf("archive indexing failed for %s: %v", path, err)
return
}
sub := node.NewArchivedFileNode(storageID, fpath, arc.FileInfo, arc.Path)
child.AddChild(sub)
}
child.Type = node.FileTypeArchive
}

// Walk walks the filesystem hierarchy
func (w *Walker) Walk(storageID int, walkPath string, storage *node.StorageNode) (int64, error) {
cnt, err := w.walk(storageID, walkPath, walkPath, storage)
Expand Down
13 changes: 9 additions & 4 deletions tests-ng/test-archive.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,35 @@ out="${tmpd}/output.txt"
arcdir="${tmpd}/archives"
mkdir -p "${arcdir}"

# create archive
# create archives
tar_arc="${arcdir}/archive1.tar.gz"
tar -czf "${tar_arc}" "${cur}/../internal"

zip_arc="${arcdir}/archive2.zip"
zip -r "${zip_arc}" "${cur}/../internal"

echo "test" > "${tmpd}/gzipped-original"
gzip -c "${tmpd}/gzipped-original" > "${arcdir}/gzipped.gz"

# index
"${bin}" --debug index -a -C -c "${catalog}" "${arcdir}" arcdir
[ ! -e "${catalog}" ] && echo "catalog not created" && exit 1

cnt=$(find "${cur}/../internal" | wc -l)
# +1 storage entry
# +2 each of the archive file header
total="$(("${cnt}" + "${cnt}" + 1 + 2))"
# +1 for gzipped
total="$(("${cnt}" + "${cnt}" + 1 + 2 + 1))"

echo ">>> test archive ls <<<"
"${bin}" --debug ls -r -a -c "${catalog}" | sed -e 's/\x1b\[[0-9;]*m//g' > "${out}"
cat_file "${out}"
cnt=$(wc -l "${out}" | awk '{print $1}')
[ "${cnt}" != "${total}" ] && echo "expecting ${total} line (got ${cnt})" && exit 1
grep '^storage arcdir 0B' "${out}" && (echo "empty storage" && exit 1)
grep '^archive1.tar.gz' "${out}" || (echo "empty storage" && exit 1)
grep '^archive2.zip' "${out}" || (echo "empty storage" && exit 1)
grep '^archive1.tar.gz' "${out}" || (echo "no archive1" && exit 1)
grep '^archive2.zip' "${out}" || (echo "archive2" && exit 1)
grep '^gzipped.gz' "${out}" || (echo "no gzipped" && exit 1)

echo "test $(basename "${0}") OK!"
exit 0

0 comments on commit 3e4e2b9

Please sign in to comment.