replace net/url parser with simple filepath parsing

This commit is contained in:
Anand Babu (AB) Periasamy
2015-04-21 16:05:16 -07:00
parent 8813ecd24d
commit 5a7b8da98d
7 changed files with 44 additions and 168 deletions
+2 -1
View File
@@ -18,6 +18,7 @@ package main
import (
"net"
"path/filepath"
"time"
"io"
@@ -133,7 +134,7 @@ func (manager mcClientManager) getNewClient(urlStr string, auth *hostConfig, deb
clnt = s3.New(s3Config)
return clnt, nil
case client.Filesystem:
absURLStr, err := client.GetFilesystemAbsURL(urlStr)
absURLStr, err := filepath.Abs(filepath.Clean(urlStr))
if err != nil {
return nil, iodine.New(err, nil)
}
+1 -3
View File
@@ -23,7 +23,6 @@ import (
"github.com/cheggaaa/pb"
"github.com/minio-io/cli"
"github.com/minio-io/mc/pkg/client"
"github.com/minio-io/mc/pkg/console"
"github.com/minio-io/minio/pkg/iodine"
"github.com/minio-io/minio/pkg/utils/log"
@@ -46,8 +45,7 @@ func runCopyCmd(ctx *cli.Context) {
switch e := iodine.ToError(err).(type) {
case errUnsupportedScheme:
log.Debug.Println(iodine.New(err, nil))
console.Fatalf("mc: reading URL [%s] failed with following reason: [%s], possible suggestions [%s]\n",
e.url, e, client.GuessPossibleURL(e.url))
console.Fatalf("mc: reading URL [%s] failed with following reason: [%s]\n", e.url, e)
default:
log.Debug.Println(iodine.New(err, nil))
console.Fatalf("mc: reading URLs failed with following reason: [%s]\n", e)
+5 -6
View File
@@ -72,7 +72,6 @@ func doList(clnt client.Client, targetURL string) (string, error) {
msg := fmt.Sprintf("\nmc: listing objects for URL [%s] failed with following reason: [%s]\n", targetURL, iodine.ToError(err))
return msg, err
}
console.Infoln()
printItems(items)
return "", nil
}
@@ -85,7 +84,7 @@ func runListCmd(ctx *cli.Context) {
config, err := getMcConfig()
if err != nil {
log.Debug.Println(iodine.New(err, nil))
console.Fatalf("mc: reading config file failed with following reason: [%s]\n", iodine.ToError(err))
console.Fatalf("mc: Error reading config file. Reason: %s\n", iodine.ToError(err))
}
targetURLConfigMap := make(map[string]*hostConfig)
for _, arg := range ctx.Args() {
@@ -94,16 +93,16 @@ func runListCmd(ctx *cli.Context) {
switch iodine.ToError(err).(type) {
case errUnsupportedScheme:
log.Debug.Println(iodine.New(err, nil))
console.Fatalf("mc: reading URL [%s] failed with invalid scheme, %s\n", arg, client.GuessPossibleURL(arg))
console.Fatalf("mc: Unknown type of URL [%s].\n", arg)
default:
log.Debug.Println(iodine.New(err, nil))
console.Fatalf("mc: reading URL [%s] failed with following reason: [%s]\n", arg, iodine.ToError(err))
console.Fatalf("mc: Unknown type of URL [%s]. Reason: %s\n", arg, iodine.ToError(err))
}
}
targetConfig, err := getHostConfig(targetURL)
if err != nil {
log.Debug.Println(iodine.New(err, nil))
console.Fatalf("mc: reading config URL [%s] failed with following reason: [%s]\n", targetURL, iodine.ToError(err))
console.Fatalf("mc: Error reading config for URL [%s]. Reason: %s\n", targetURL, iodine.ToError(err))
}
targetURLConfigMap[targetURL] = targetConfig
}
@@ -111,7 +110,7 @@ func runListCmd(ctx *cli.Context) {
err = iodine.New(err, nil)
if err != nil {
if errorMsg == "" {
errorMsg = "No error message present, please rerun with --debug and report a bug."
errorMsg = "mc: List command failed. Please re-run with --debug and report this bug."
}
log.Debug.Println(err)
console.Fatalf("%s", errorMsg)
+1 -1
View File
@@ -45,7 +45,7 @@ func runMakeBucketCmd(ctx *cli.Context) {
switch iodine.ToError(err).(type) {
case errUnsupportedScheme:
log.Debug.Println(iodine.New(err, nil))
console.Fatalf("mc: reading URL [%s] failed, %s\n", arg, client.GuessPossibleURL(arg))
console.Fatalf("mc: Unknown type of URL [%s].\n", arg)
default:
log.Debug.Println(iodine.New(err, nil))
console.Fatalf("mc: reading URL [%s] failed with following reason: [%s]\n", arg, iodine.ToError(err))
+29 -64
View File
@@ -21,47 +21,36 @@ package fs
import (
"io"
"os"
"path/filepath"
"sort"
"strings"
"io/ioutil"
"net/url"
"path/filepath"
"github.com/minio-io/mc/pkg/client"
"github.com/minio-io/minio/pkg/iodine"
)
type fsClient struct {
*url.URL
Path string
}
// New - instantiate a new fs client
func New(path string) client.Client {
u, err := url.Parse(path)
if err != nil {
return nil
}
return &fsClient{u}
return &fsClient{Path: path}
}
/// Object operations
// url2Object converts URL to bucketName and objectName
func (f *fsClient) url2Object() (bucketName, objectName string) {
return filepath.Split(f.Path)
}
// getObjectMetadata - wrapper function to get file stat
func (f *fsClient) getObjectMetadata() (os.FileInfo, error) {
bucket, object := f.url2Object()
st, err := os.Stat(f.Path)
/* FIXME: after rewritng client.errors to handle generic path and URLs, fix the error messages appropriately.
if os.IsNotExist(err) {
return nil, iodine.New(client.ObjectNotFound{Bucket: bucket, Object: object}, nil)
}
if st.IsDir() {
return nil, iodine.New(client.InvalidObjectName{Bucket: bucket, Object: object}, nil)
}
}*/
if err != nil {
return nil, iodine.New(err, nil)
}
@@ -119,58 +108,34 @@ func (f *fsClient) GetObjectMetadata() (item *client.Item, reterr error) {
return item, nil
}
/// Bucket operations
// listBuckets - get list of buckets
func (f *fsClient) listBuckets() ([]*client.Item, error) {
buckets, err := ioutil.ReadDir(f.Path)
// List - get a list of items
func (f *fsClient) List() (items []*client.Item, err error) {
visitFS := func(fp string, fi os.FileInfo, err error) error {
if err != nil {
if os.IsPermission(err) { // skip inaccessible files
return nil
}
return err // fatal
}
if fi.IsDir() {
return nil // not a fs skip
}
// trim f.Path
item := &client.Item{
Name: strings.TrimPrefix(fp, f.Path+string(filepath.Separator)),
Time: fi.ModTime(),
Size: fi.Size(),
}
items = append(items, item)
return nil
}
err = filepath.Walk(f.Path, visitFS)
if err != nil {
return nil, iodine.New(err, nil)
}
var results []*client.Item
for _, bucket := range buckets {
result := new(client.Item)
result.Name = bucket.Name()
result.Time = bucket.ModTime()
results = append(results, result)
}
return results, nil
}
// List - get a list of items
func (f *fsClient) List() (items []*client.Item, err error) {
item, err := f.GetObjectMetadata()
switch err {
case nil:
items = append(items, item)
return items, nil
default:
visitFS := func(fp string, fi os.FileInfo, err error) error {
if err != nil {
if os.IsPermission(err) { // skip inaccessible files
return nil
}
return err // fatal
}
if fi.IsDir() {
return nil // not a fs skip
}
// trim f.Path
item := &client.Item{
Name: strings.TrimPrefix(fp, f.Path+string(filepath.Separator)),
Time: fi.ModTime(),
Size: fi.Size(),
}
items = append(items, item)
return nil
}
err = filepath.Walk(f.Path, visitFS)
if err != nil {
return nil, iodine.New(err, nil)
}
sort.Sort(client.BySize(items))
return items, nil
}
sort.Sort(client.BySize(items))
return items, nil
}
// PutBucket - create a new bucket
+5 -23
View File
@@ -23,7 +23,6 @@ import (
"strings"
"io/ioutil"
"net/url"
"path/filepath"
"github.com/minio-io/mc/pkg/client"
@@ -31,47 +30,30 @@ import (
)
type fsClient struct {
*url.URL
Path string
}
// New - instantiate a new fs client
func New(path string) client.Client {
u, err := url.Parse(path)
if err != nil {
return nil
}
return &fsClient{u}
}
/// Object operations
// url2Object converts URL to bucketName and objectName
func (f *fsClient) url2Object() (bucketName, objectName string) {
unescapedURL, _ := url.QueryUnescape(f.String())
return filepath.Split(unescapedURL)
return &fsClient{Path: path}
}
// getObjectMetadata - wrapper function to get file stat
func (f *fsClient) getObjectMetadata() (os.FileInfo, error) {
bucket, object := f.url2Object()
st, err := os.Stat(f.normalizedPath())
st, err := os.Stat(filepath.Clean(f.Path))
/* FIXME: after rewritng client.errors to handle generic path and URLs, fix the error messages appropriately.
if os.IsNotExist(err) {
return nil, iodine.New(client.ObjectNotFound{Bucket: bucket, Object: object}, nil)
}
if st.IsDir() {
return nil, iodine.New(client.InvalidObjectName{Bucket: bucket, Object: object}, nil)
}
}*/
if err != nil {
return nil, iodine.New(err, nil)
}
return st, nil
}
func (f *fsClient) normalizedPath() string {
unescapedURL, _ := url.QueryUnescape(f.String())
return filepath.Clean(unescapedURL)
}
// Get - download an object from bucket
func (f *fsClient) Get() (body io.ReadCloser, size int64, md5 string, err error) {
st, err := f.getObjectMetadata()
+1 -70
View File
@@ -1,14 +1,6 @@
package client
import (
"fmt"
"net/url"
"path/filepath"
"runtime"
"strings"
"github.com/minio-io/minio/pkg/iodine"
)
import "net/url"
// Type - enum of different url types
type Type int
@@ -20,53 +12,6 @@ const (
Filesystem // POSIX compatible file systems
)
// GuessPossibleURL - provide guesses for possible mistakes in user input url
func GuessPossibleURL(urlStr string) string {
u, err := url.Parse(urlStr)
if err != nil {
return ""
}
if u.Scheme == "file" || !strings.Contains(urlStr, ":///") {
possible := u.Scheme + ":///" + u.Host + u.Path
guess := fmt.Sprintf("Did you mean? %s", possible)
return guess
}
// TODO(y4m4) - add more guesses if possible
return ""
}
// GetFilesystemAbsURL - construct an absolute path for all POSIX paths
func GetFilesystemAbsURL(urlStr string) (string, error) {
var absStrURL string
var err error
u, err := url.Parse(urlStr)
if err != nil {
return "", iodine.New(err, nil)
}
switch true {
case u.Scheme == "file" && u.IsAbs():
absStrURL, err = filepath.Abs(filepath.Clean(u.Path))
if err != nil {
return "", iodine.New(err, nil)
}
default:
absStrURL, err = filepath.Abs(filepath.Clean(u.String()))
if err != nil {
return "", iodine.New(err, nil)
}
// url parse converts "\" on windows as "%5c" unescape it
unescapedAbsURL, err := url.QueryUnescape(absStrURL)
if err != nil {
return "", iodine.New(err, nil)
}
absStrURL = unescapedAbsURL
}
return absStrURL, nil
}
// GetType returns the type of .
func GetType(urlStr string) Type {
u, err := url.Parse(urlStr)
@@ -78,20 +23,6 @@ func GetType(urlStr string) Type {
return Object
}
// while Scheme file, host should be empty
// if windows skip this check, not going to support file:/// style on windows
// we should just check for VolumeName on windows
if runtime.GOOS != "windows" {
if u.Scheme == "file" && u.Host == "" && strings.Contains(urlStr, ":///") {
return Filesystem
}
} else {
if filepath.VolumeName(urlStr) != "" {
return Filesystem
}
}
// local path, without the file:/// or C:\
if u.Scheme == "" {
return Filesystem
}