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

Glob support #38

Merged
merged 5 commits into from
Dec 13, 2017
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ Nvgd accepts path in these like format:
Nvgd supports these `protocol`s:

* `file` - `/file:///path/to/source`
* support [glob][globspec] like `*`

```
/files:///var/log/access*.log
```

* `command` - result of pre-defined commands
* `s3obj`
* get object: `/s3obj://bucket-name/key/to/object`
Expand Down Expand Up @@ -574,3 +580,4 @@ works same as below URL:
* [koron/night][night] previous impl in NodeJS.

[night]:https://github.com/koron/night
[globspec]:https://golang.org/pkg/path/filepath/#Match
107 changes: 102 additions & 5 deletions protocol/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log"
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -61,8 +62,19 @@ func init() {

// Open opens a URL as file.
func (f *File) Open(u *url.URL) (*resource.Resource, error) {
// TODO: consider relative path.
name := u.Path
m, err := filepath.Glob(name)
if err != nil {
return nil, err
}
if len(m) == 1 {
return f.openOne(name)
}
return f.openMulti(m, name)
}

func (f *File) openOne(name string) (*resource.Resource, error) {
// TODO: consider relative path.
if !fc.isAccessible(name) {
return nil, fmt.Errorf("forbidden: %s", name)
}
Expand All @@ -71,16 +83,30 @@ func (f *File) Open(u *url.URL) (*resource.Resource, error) {
return nil, err
}
if fi.IsDir() {
return f.openDir(name)
return fileOpenDir(name)
}
rc, err := f.openFile(name)
rc, err := fileOpen(name)
if err != nil {
return nil, err
}
return resource.New(rc), nil
}

func (f *File) openDir(name string) (*resource.Resource, error) {
func (f *File) openMulti(names []string, pattern string) (*resource.Resource, error) {
readers := make([]io.Reader, 0, len(names))
for _, n := range names {
if !fc.isAccessible(n) {
continue
}
readers = append(readers, newDelayFile(n))
}
if len(readers) == 0 {
return nil, fmt.Errorf("no matches: %s", pattern)
}
return resource.New(newMultiRC(readers...)), nil
}

func fileOpenDir(name string) (*resource.Resource, error) {
list, err := ioutil.ReadDir(name)
if err != nil {
log.Printf("ReadDir failed: %s", name)
Expand Down Expand Up @@ -125,7 +151,7 @@ var (
rxLz4 = regexp.MustCompile(`\.lz4$`)
)

func (f *File) openFile(name string) (io.ReadCloser, error) {
func fileOpen(name string) (io.ReadCloser, error) {
r, err := os.Open(name)
if err != nil {
return nil, err
Expand All @@ -146,6 +172,15 @@ func (f *File) openFile(name string) (io.ReadCloser, error) {
return r, nil
}

func fileFailure(err error, readers []io.Reader) error {
for _, r := range readers {
if rc, ok := r.(io.ReadCloser); ok {
rc.Close()
}
}
return err
}

type wrapRC struct {
io.Reader
c io.Closer
Expand All @@ -158,3 +193,65 @@ func newWrapRC(r io.Reader, c io.Closer) io.ReadCloser {
func (rc *wrapRC) Close() error {
return rc.c.Close()
}

type delayFile struct {
rc io.ReadCloser
n string
err error
}

func newDelayFile(name string) *delayFile {
return &delayFile{n: name}
}

func (d *delayFile) Read(b []byte) (int, error) {
if d.err != nil {
return 0, d.err
}
if d.rc == nil {
d.rc, d.err = fileOpen(d.n)
if d.err != nil {
return 0, d.err
}
}
return d.rc.Read(b)
}

func (d *delayFile) Close() error {
if d.err != nil {
return d.err
}
if d.rc == nil {
d.err = io.EOF
return nil
}
d.err = d.rc.Close()
d.rc = nil
return d.err
}

type multiRC struct {
io.Reader
rcs []io.ReadCloser
}

func newMultiRC(readers ...io.Reader) *multiRC {
rcs := make([]io.ReadCloser, 0, len(readers))
for _, r := range readers {
if rc, ok := r.(io.ReadCloser); ok {
rcs = append(rcs, rc)
}
}
return &multiRC{
Reader: io.MultiReader(readers...),
rcs: rcs,
}
}

func (mrc *multiRC) Close() error {
for _, rc := range mrc.rcs {
rc.Close()
}
mrc.rcs = nil
return nil
}
33 changes: 27 additions & 6 deletions protocol/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
)

func TestBzip2(t *testing.T) {
f := &File{}
r, err := f.openFile("testdata/file_test.bz2")
r, err := fileOpen("testdata/file_test.bz2")
if err != nil {
t.Fatal(err)
}
defer r.Close()
b, err := ioutil.ReadAll(r)
if err != nil {
t.Fatal(err)
Expand All @@ -22,11 +22,11 @@ func TestBzip2(t *testing.T) {
}

func TestGzip(t *testing.T) {
f := &File{}
r, err := f.openFile("testdata/file_test.gz")
r, err := fileOpen("testdata/file_test.gz")
if err != nil {
t.Fatal(err)
}
defer r.Close()
b, err := ioutil.ReadAll(r)
if err != nil {
t.Fatal(err)
Expand All @@ -38,11 +38,11 @@ func TestGzip(t *testing.T) {
}

func TestLZ4(t *testing.T) {
f := &File{}
r, err := f.openFile("testdata/file_test.lz4")
r, err := fileOpen("testdata/file_test.lz4")
if err != nil {
t.Fatal(err)
}
defer r.Close()
b, err := ioutil.ReadAll(r)
if err != nil {
t.Fatal(err)
Expand All @@ -52,3 +52,24 @@ func TestLZ4(t *testing.T) {
t.Errorf("content of \"testdata/file_test.lz4\" is unexpected: %q", s)
}
}

func TestMultiRC(t *testing.T) {
f := &File{}
r, err := f.openMulti([]string{
"testdata/file_test.bz2",
"testdata/file_test.gz",
"testdata/file_test.lz4",
}, "testdata/;file_test.*")
if err != nil {
t.Fatal(err)
}
defer r.Close()
b, err := ioutil.ReadAll(r)
if err != nil {
t.Fatal(err)
}
s := string(b)
if s != `this is bz2 compressedthis is gzip compressedthis is lz4 compressed` {
t.Errorf("multi RC failed: %q", s)
}
}
Loading