Skip to content
Browse files

added ls

  • Loading branch information...
1 parent 73bd087 commit 5efd67dcfd3ea12427e186db6b2b9cfd25e06d99 @nutrun nutrun committed May 4, 2012
Showing with 57 additions and 14 deletions.
  1. +1 −1 datastore.go
  2. +21 −2 http.go
  3. +14 −0 main.go
  4. +14 −9 ring.go
  5. +2 −2 ring_test.go
  6. +5 −0 test/test.sh
View
2 datastore.go
@@ -8,7 +8,7 @@ type Datastore interface {
Get(remote string) (io.ReadCloser, error)
Put(local, remote string) (io.ReadCloser, error)
Delete(remote string) (io.ReadCloser, error)
- Ls(remote string) []string
+ Ls(path string) ([]string, error)
Capacity() uint64
Host() string
}
View
23 http.go
@@ -5,8 +5,10 @@ import (
"errors"
"fmt"
"io"
+ "io/ioutil"
"net/http"
"os"
+ "regexp"
)
type HttpDatastore struct {
@@ -82,6 +84,23 @@ func (this *HttpDatastore) Delete(remote string) (io.ReadCloser, error) {
return resp.Body, nil
}
-func (this *HttpDatastore) Ls(s string) []string {
- return make([]string, 0)
+func (this *HttpDatastore) Ls(path string) ([]string, error) {
+ resp, e := http.Get(this.url(path))
+ if e != nil {
+ return nil, e
+ }
+ body, e := ioutil.ReadAll(resp.Body)
+ if e != nil {
+ return nil, e
+ }
+ re, e := regexp.Compile(`\<a href\=\"(.*)\"\>`)
+ if e != nil {
+ panic(e)
+ }
+ results := re.FindAllSubmatch(body, -1)
+ links := make([]string, len(results))
+ for i, result := range results {
+ links[i] = string(result[1])
+ }
+ return links, nil
}
View
14 main.go
@@ -3,6 +3,7 @@ package main
import (
"bufio"
"flag"
+ "fmt"
"io"
"log"
"os"
@@ -11,6 +12,7 @@ import (
var get *bool = flag.Bool("get", true, "get <remote>")
var put *bool = flag.Bool("put", false, "put <local> <remote>")
var del *bool = flag.Bool("del", false, "del <remote>")
+var ls *bool = flag.Bool("ls", false, "ls <path>")
func main() {
// Remove all info from log output
@@ -39,6 +41,18 @@ func main() {
log.Fatal(err)
WriteBody(body, os.Stderr)
}
+ } else if *ls {
+ if len(args) != 1 {
+ flag.Usage()
+ os.Exit(1)
+ }
+ results, err := ring.Ls(args[0])
+ if err != nil {
+ log.Fatal(err)
+ }
+ for _, result := range results {
+ fmt.Println(result)
+ }
} else if *get {
if len(args) != 1 {
flag.Usage()
View
23 ring.go
@@ -119,18 +119,23 @@ func (this *Ring) Put(local, remote string) (io.ReadCloser, error) {
return MultiReadCloser(closers), nil
}
-func (this *Ring) Ls(remote string) []string {
+func (this *Ring) Ls(path string) ([]string, error) {
set := make(map[string]bool)
- for _, server := range this.config {
- for _, path := range server.Ls(remote) {
- set[path] = true
+ links := make([]string, 0)
+ for _, host := range this.config {
+ results, e := host.Ls(path)
+ if e != nil {
+ return nil, e
+ }
+ for _, result := range results {
+ _, exists := set[result]
+ if !exists {
+ links = append(links, result)
+ set[result] = true
+ }
}
}
- paths := make([]string, 0)
- for path, _ := range set {
- paths = append(paths, path)
- }
- return paths
+ return links, nil
}
func (this *Ring) redudantServers(remote string) []Datastore {
View
4 ring_test.go
@@ -20,8 +20,8 @@ func (this *DudStore) Get(remote string) (io.ReadCloser, error) {
func (this *DudStore) Put(local, remote string) (io.ReadCloser, error) {
return nil, nil
}
-func (this *DudStore) Ls(remote string) []string {
- return make([]string, 0)
+func (this *DudStore) Ls(path string) ([]string, error) {
+ return nil, nil
}
func (this *DudStore) Capacity() uint64 {
View
5 test/test.sh
@@ -1,7 +1,10 @@
#!/bin/sh
function fail {
+ echo
echo $1
+ echo
+ echo FAIL
exit 1
}
@@ -18,6 +21,8 @@ curl -I --silent $server/B/hi.txt | grep -q 200 || fail "Should have found /B/hi
curl -I --silent $server/C/hi.txt | grep -q 200 || fail "Should have found /C/hi.txt"
echo "testing get"
NGXFS_CONF=./ngxfs.conf ngxfs /hi.txt | grep -q helo || fail "Didn't find /hi.txt"
+echo "testing ls"
+NGXFS_CONF=./ngxfs.conf ngxfs -ls / | grep -q "hi.txt" || fail "Incorrect ls list"
echo "testing del"
NGXFS_CONF=./ngxfs.conf ngxfs -del /hi.txt
curl -I --silent $server/A/hi.txt | grep -q 404 || fail "Shouldn't have found /A/hi.txt"

0 comments on commit 5efd67d

Please sign in to comment.
Something went wrong with that request. Please try again.