Skip to content

Commit

Permalink
fix httpfs redirect of local relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
barnex committed Oct 15, 2014
1 parent 012f3c4 commit af25be5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions engine/od.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package engine
import (
"github.com/mumax/3/httpfs"
"github.com/mumax/3/util"
"strings"
)

var (
Expand All @@ -28,6 +29,9 @@ func InitIO(inputfile string, force bool) {

InputFile = inputfile
outputdir = util.NoExt(InputFile) + ".out/"
if strings.HasPrefix(outputdir, "http://") {
httpfs.SetWD(outputdir + "/../")
}
LogOut("output directory:", outputdir)

od := OD()
Expand Down
34 changes: 33 additions & 1 deletion httpfs/httpfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,33 @@ import (
"strings"
)

var Logging = false
var (
Logging = false // enables logging
wd = ""
)

const (
DirPerm = 0777 // permissions for new directory
FilePerm = 0666 // permissions for new files
)

// SetWD sets a "working directory", prefixed to all relative local paths.
// dir may start with "http://", turning local relative paths into remote paths.
// E.g.:
// http://path -> http://path
// path/file -> wd/path/file
// /path/file -> /path/file
func SetWD(dir string) {
if dir != "" && !strings.HasSuffix(dir, "/") {
dir = dir + "/"
}
wd = dir
}

// Creates the directory at specified URL (or local file),
// creating all needed parent directories as well.
func Mkdir(URL string) error {
URL = cleanup(URL)
if isRemote(URL) {
return httpMkdir(URL)
} else {
Expand All @@ -33,6 +50,7 @@ func Mkdir(URL string) error {
}

func ReadDir(URL string) ([]string, error) {
URL = cleanup(URL)
if isRemote(URL) {
return httpLs(URL)
} else {
Expand All @@ -41,6 +59,7 @@ func ReadDir(URL string) ([]string, error) {
}

func Remove(URL string) error {
URL = cleanup(URL)
if isRemote(URL) {
return httpRemove(URL)
} else {
Expand All @@ -49,6 +68,7 @@ func Remove(URL string) error {
}

func Read(URL string) ([]byte, error) {
URL = cleanup(URL)
if isRemote(URL) {
return httpRead(URL)
} else {
Expand All @@ -57,6 +77,7 @@ func Read(URL string) ([]byte, error) {
}

func Append(URL string, p []byte) error {
URL = cleanup(URL)
if isRemote(URL) {
return httpAppend(URL, p)
} else {
Expand All @@ -65,6 +86,7 @@ func Append(URL string, p []byte) error {
}

func Put(URL string, p []byte) error {
URL = cleanup(URL)
if isRemote(URL) {
return httpPut(URL, p)
} else {
Expand All @@ -76,6 +98,16 @@ func isRemote(URL string) bool {
return strings.HasPrefix(URL, "http://")
}

func cleanup(URL string) string {
if isRemote(URL) {
return URL
}
if !path.IsAbs(URL) {
return wd + URL
}
return URL
}

// file action gets its own type to avoid mixing up with other strings
type action string

Expand Down

0 comments on commit af25be5

Please sign in to comment.