Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When listing local files use prefix to limit walk. #149

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions local/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/url"
"os"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -76,7 +77,7 @@ func (c *container) Put(name string, r io.Reader, size int64, metadata map[strin
}

func (c *container) Items(prefix, cursor string, count int) ([]stow.Item, string, error) {
files, err := flatdirs(c.path)
files, err := flatdirs(c.path, prefix)
if err != nil {
return nil, "", err
}
Expand Down Expand Up @@ -138,16 +139,20 @@ func (c *container) Item(id string) (stow.Item, error) {

// flatdirs walks the entire tree returning a list of
// os.FileInfo for all items encountered.
func flatdirs(path string) ([]os.FileInfo, error) {
func flatdirs(dir, prefix string) ([]os.FileInfo, error) {
searchDir := path.Dir(path.Join(dir, prefix))
var list []os.FileInfo
err := filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
err := filepath.Walk(searchDir, func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
flatname, err := filepath.Rel(path, p)
flatname, err := filepath.Rel(dir, p)
if !strings.HasPrefix(flatname, prefix) {
return nil
}
if err != nil {
return err
}
Expand Down