Skip to content

Commit

Permalink
httpfs: Put
Browse files Browse the repository at this point in the history
  • Loading branch information
barnex committed Sep 24, 2014
1 parent 81e6d8b commit 6d16993
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
30 changes: 30 additions & 0 deletions httpfs/stateless.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ func Append(URL string, p []byte) error {
}
}

func Put(URL string, p []byte) error {
if isRemote(URL) {
return httpPut(URL, p)
} else {
return localPut(URL, p)
}
}

func isRemote(URL string) bool {
return strings.HasPrefix(URL, "http://")
}
Expand All @@ -75,6 +83,7 @@ const (
APPEND action = "append"
LS action = "ls"
MKDIR action = "mkdir"
PUT action = "put"
READ action = "read"
RM action = "rm"
)
Expand All @@ -86,6 +95,7 @@ func Handle() {
APPEND: handleAppend,
LS: handleLs,
MKDIR: handleMkdir,
PUT: handlePut,
READ: handleRead,
RM: handleRemove,
}
Expand Down Expand Up @@ -127,6 +137,10 @@ func handleAppend(fname string, data []byte, w io.Writer) error {
return localAppend(fname, data)
}

func handlePut(fname string, data []byte, w io.Writer) error {
return localPut(fname, data)
}

func handleLs(fname string, data []byte, w io.Writer) error {
ls, err := localLs(fname)
if err != nil {
Expand Down Expand Up @@ -209,6 +223,11 @@ func httpAppend(URL string, data []byte) error {
return err
}

func httpPut(URL string, data []byte) error {
_, err := do(PUT, URL, data)
return err
}

func httpRead(URL string) ([]byte, error) {
return do(READ, URL, nil)
}
Expand Down Expand Up @@ -247,6 +266,17 @@ func localAppend(fname string, data []byte) error {
return err2
}

func localPut(fname string, data []byte) error {
_ = os.MkdirAll(path.Dir(fname), DirPerm)
f, err := os.OpenFile(fname, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, FilePerm)
if err != nil {
return err
}
defer f.Close()
_, err2 := f.Write(data)
return err2
}

func localRead(fname string) ([]byte, error) {
return ioutil.ReadFile(fname)
}
Expand Down
16 changes: 11 additions & 5 deletions httpfs/stateless_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func init() {
}()
}

func TestStatelessMkdir(t *testing.T) {
func TestMkdir(t *testing.T) {
if err := Mkdir("testdata/delete/local/this/is/a/dir"); err != nil {
t.Error(err)
}
Expand All @@ -33,19 +33,25 @@ func TestStatelessMkdir(t *testing.T) {
}
}

func TestStatelessReadDir(t *testing.T) {
func TestReadDir(t *testing.T) {

}

func TestStatelessRemove(t *testing.T) {
func TestRemove(t *testing.T) {

}

func TestStatelessRead(t *testing.T) {
func TestRead(t *testing.T) {

}

func TestStatelessAppend(t *testing.T) {
func TestPut(t *testing.T) {
if err := Put(testURL+"testdata/a/file.txt", []byte("hello")); err != nil {
t.Error(err)
}
}

func TestAppend(t *testing.T) {
if err := Mkdir(testURL + "testdata/delete/bla/bla/"); err != nil {
t.Error(err)
}
Expand Down

0 comments on commit 6d16993

Please sign in to comment.