From 512daf71d1ecee1c67801627f7e40ff018e5e1a6 Mon Sep 17 00:00:00 2001 From: Robert van Gent Date: Wed, 7 Nov 2018 10:56:46 -0800 Subject: [PATCH 1/4] blob/fileblob: ignore URL's Host and drop leading / on Windows --- .travis.yml | 1 + blob/example_test.go | 11 ++++++++++- blob/fileblob/fileblob.go | 13 ++++++++++--- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3f583ebf7e..acd93200d9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ os: - linux - osx + - windows language: go go_import_path: github.com/google/go-cloud diff --git a/blob/example_test.go b/blob/example_test.go index 3fca924336..42f853167f 100644 --- a/blob/example_test.go +++ b/blob/example_test.go @@ -22,6 +22,7 @@ import ( "log" "os" "path/filepath" + "strings" "github.com/google/go-cloud/blob" "github.com/google/go-cloud/blob/fileblob" @@ -335,7 +336,15 @@ func ExampleOpen() { } fmt.Println("Got expected error opening a nonexistent path") - if _, err := blob.Open(ctx, "file://"+dir); err != nil { + // Ensure the path has a leading slash; fileblob ignores the URL's + // Host field, so URLs should always start with "file:///". On + // Windows, the leading "/" will be stripped, so "file:///c:/foo" + // will refer to c:/foo. + urlpath := filepath.ToSlash(dir) + if !strings.HasPrefix(urlpath, "/") { + urlpath = "/" + urlpath + } + if _, err := blob.Open(ctx, "file://"+urlpath); err != nil { log.Fatal(err) } fmt.Println("Got a bucket for valid path") diff --git a/blob/fileblob/fileblob.go b/blob/fileblob/fileblob.go index 8cc19d192c..6450d28b23 100644 --- a/blob/fileblob/fileblob.go +++ b/blob/fileblob/fileblob.go @@ -32,9 +32,12 @@ // aren't visible using fileblob. // // For blob.Open URLs, fileblob registers for the "file" scheme. -// The URL's Host and Path are concatenated and used as the root directory. -// For example, blob.Open("file:///a/directory") is equivalent to -// fileblob.OpenBucket("/a/directory"). No query options are supported. +// The URL's Path is used as the root directory; the URL's Host is ignored. +// If os.PathSeparator != "/", any leading "/" from the Path is dropped. +// No query options are supported. Examples: +// -- file:///a/directory passes "/a/directory" to OpenBucket. +// -- file://localhost/a/directory also passes "/a/directory". +// -- file:///c:/foo/bar passes "c:/foo/bar". // // fileblob does not support any types for As. package fileblob @@ -60,6 +63,10 @@ const defaultPageSize = 1000 func init() { blob.Register("file", func(_ context.Context, u *url.URL) (driver.Bucket, error) { + path := u.Path + if os.PathSeparator != '/' && strings.HasPrefix(path, "/") { + path = path[1:] + } return openBucket(u.Host+u.Path, nil) }) } From 51a33b07ee1dd29063919d783f6ab33bf4bb5340 Mon Sep 17 00:00:00 2001 From: Robert van Gent Date: Wed, 7 Nov 2018 11:10:26 -0800 Subject: [PATCH 2/4] actually use path variable --- blob/fileblob/fileblob.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blob/fileblob/fileblob.go b/blob/fileblob/fileblob.go index 6450d28b23..9f49dc0688 100644 --- a/blob/fileblob/fileblob.go +++ b/blob/fileblob/fileblob.go @@ -67,7 +67,7 @@ func init() { if os.PathSeparator != '/' && strings.HasPrefix(path, "/") { path = path[1:] } - return openBucket(u.Host+u.Path, nil) + return openBucket(path, nil) }) } From 22450f91165faac54399fb76e62d9815334901cb Mon Sep 17 00:00:00 2001 From: Robert van Gent Date: Wed, 7 Nov 2018 11:20:35 -0800 Subject: [PATCH 3/4] drop travis change of adding windows --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index acd93200d9..3f583ebf7e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ os: - linux - osx - - windows language: go go_import_path: github.com/google/go-cloud From 3b33b2614411147010044d364ad1e9a37051f2b8 Mon Sep 17 00:00:00 2001 From: Robert van Gent Date: Wed, 7 Nov 2018 11:25:08 -0800 Subject: [PATCH 4/4] update example to use url.PathEscape --- blob/example_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/blob/example_test.go b/blob/example_test.go index 42f853167f..0da0709d8e 100644 --- a/blob/example_test.go +++ b/blob/example_test.go @@ -20,6 +20,7 @@ import ( "io" "io/ioutil" "log" + "net/url" "os" "path/filepath" "strings" @@ -340,7 +341,7 @@ func ExampleOpen() { // Host field, so URLs should always start with "file:///". On // Windows, the leading "/" will be stripped, so "file:///c:/foo" // will refer to c:/foo. - urlpath := filepath.ToSlash(dir) + urlpath := url.PathEscape(filepath.ToSlash(dir)) if !strings.HasPrefix(urlpath, "/") { urlpath = "/" + urlpath }