Browse Source

refactoring

pull/1287/head
Chris Lu 5 years ago
parent
commit
91da7057b1
  1. 3
      weed/filer2/filer.go
  2. 3
      weed/replication/replicator.go
  3. 11
      weed/server/filer_grpc_server.go
  4. 2
      weed/server/filer_grpc_server_listen.go
  5. 5
      weed/server/filer_ui/breadcrumb.go
  6. 2
      weed/server/volume_grpc_copy.go
  7. 3
      weed/shell/command_fs_mv.go
  8. 3
      weed/shell/commands.go
  9. 8
      weed/util/fullpath.go

3
weed/filer2/filer.go

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"os" "os"
"path/filepath"
"strings" "strings"
"time" "time"
@ -94,7 +93,7 @@ func (f *Filer) CreateEntry(ctx context.Context, entry *Entry, o_excl bool) erro
var lastDirectoryEntry *Entry var lastDirectoryEntry *Entry
for i := 1; i < len(dirParts); i++ { for i := 1; i < len(dirParts); i++ {
dirPath := "/" + filepath.ToSlash(filepath.Join(dirParts[:i]...))
dirPath := "/" + util.Join(dirParts[:i]...)
// fmt.Printf("%d directory: %+v\n", i, dirPath) // fmt.Printf("%d directory: %+v\n", i, dirPath)
// first check local cache // first check local cache

3
weed/replication/replicator.go

@ -3,7 +3,6 @@ package replication
import ( import (
"context" "context"
"fmt" "fmt"
"path/filepath"
"strings" "strings"
"github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/glog"
@ -36,7 +35,7 @@ func (r *Replicator) Replicate(ctx context.Context, key string, message *filer_p
glog.V(4).Infof("skipping %v outside of %v", key, r.source.Dir) glog.V(4).Infof("skipping %v outside of %v", key, r.source.Dir)
return nil return nil
} }
newKey := filepath.ToSlash(filepath.Join(r.sink.GetSinkToDirectory(), key[len(r.source.Dir):]))
newKey := util.Join(r.sink.GetSinkToDirectory(), key[len(r.source.Dir):])
glog.V(3).Infof("replicate %s => %s", key, newKey) glog.V(3).Infof("replicate %s => %s", key, newKey)
key = newKey key = newKey
if message.OldEntry != nil && message.NewEntry == nil { if message.OldEntry != nil && message.NewEntry == nil {

11
weed/server/filer_grpc_server.go

@ -19,7 +19,7 @@ import (
func (fs *FilerServer) LookupDirectoryEntry(ctx context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) { func (fs *FilerServer) LookupDirectoryEntry(ctx context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) {
entry, err := fs.filer.FindEntry(ctx, util.FullPath(filepath.ToSlash(filepath.Join(req.Directory, req.Name))))
entry, err := fs.filer.FindEntry(ctx, util.JoinPath(req.Directory, req.Name))
if err == filer_pb.ErrNotFound { if err == filer_pb.ErrNotFound {
return &filer_pb.LookupDirectoryEntryResponse{}, nil return &filer_pb.LookupDirectoryEntryResponse{}, nil
} }
@ -137,7 +137,6 @@ func (fs *FilerServer) CreateEntry(ctx context.Context, req *filer_pb.CreateEntr
resp = &filer_pb.CreateEntryResponse{} resp = &filer_pb.CreateEntryResponse{}
fullpath := util.FullPath(filepath.ToSlash(filepath.Join(req.Directory, req.Entry.Name)))
chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks) chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks)
if req.Entry.Attributes == nil { if req.Entry.Attributes == nil {
@ -147,7 +146,7 @@ func (fs *FilerServer) CreateEntry(ctx context.Context, req *filer_pb.CreateEntr
} }
createErr := fs.filer.CreateEntry(ctx, &filer2.Entry{ createErr := fs.filer.CreateEntry(ctx, &filer2.Entry{
FullPath: fullpath,
FullPath: util.JoinPath(req.Directory, req.Entry.Name),
Attr: filer2.PbToEntryAttribute(req.Entry.Attributes), Attr: filer2.PbToEntryAttribute(req.Entry.Attributes),
Chunks: chunks, Chunks: chunks,
}, req.OExcl) }, req.OExcl)
@ -164,7 +163,7 @@ func (fs *FilerServer) CreateEntry(ctx context.Context, req *filer_pb.CreateEntr
func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) { func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
fullpath := filepath.ToSlash(filepath.Join(req.Directory, req.Entry.Name))
fullpath := util.Join(req.Directory, req.Entry.Name)
entry, err := fs.filer.FindEntry(ctx, util.FullPath(fullpath)) entry, err := fs.filer.FindEntry(ctx, util.FullPath(fullpath))
if err != nil { if err != nil {
return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("not found %s: %v", fullpath, err) return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("not found %s: %v", fullpath, err)
@ -176,7 +175,7 @@ func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntr
chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks) chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks)
newEntry := &filer2.Entry{ newEntry := &filer2.Entry{
FullPath: util.FullPath(filepath.ToSlash(filepath.Join(req.Directory, req.Entry.Name))),
FullPath: util.JoinPath(req.Directory, req.Entry.Name),
Attr: entry.Attr, Attr: entry.Attr,
Extended: req.Entry.Extended, Extended: req.Entry.Extended,
Chunks: chunks, Chunks: chunks,
@ -219,7 +218,7 @@ func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntr
} }
func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) { func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) {
err = fs.filer.DeleteEntryMetaAndData(ctx, util.FullPath(filepath.ToSlash(filepath.Join(req.Directory, req.Name))), req.IsRecursive, req.IgnoreRecursiveError, req.IsDeleteData)
err = fs.filer.DeleteEntryMetaAndData(ctx, util.JoinPath(req.Directory, req.Name), req.IsRecursive, req.IgnoreRecursiveError, req.IsDeleteData)
resp = &filer_pb.DeleteEntryResponse{} resp = &filer_pb.DeleteEntryResponse{}
if err != nil { if err != nil {
resp.Error = err.Error() resp.Error = err.Error()

2
weed/server/filer_grpc_server_listen.go

@ -34,7 +34,7 @@ func (fs *FilerServer) ListenForEvents(req *filer_pb.ListenForEventsRequest, str
entryName = eventNotification.NewEntry.Name entryName = eventNotification.NewEntry.Name
} }
fullpath := util.PathJoin(dirPath, entryName)
fullpath := util.Join(dirPath, entryName)
// skip on filer internal meta logs // skip on filer internal meta logs
if strings.HasPrefix(fullpath, "/.meta") { if strings.HasPrefix(fullpath, "/.meta") {

5
weed/server/filer_ui/breadcrumb.go

@ -1,8 +1,9 @@
package master_ui package master_ui
import ( import (
"path/filepath"
"strings" "strings"
"github.com/chrislusf/seaweedfs/weed/util"
) )
type Breadcrumb struct { type Breadcrumb struct {
@ -16,7 +17,7 @@ func ToBreadcrumb(fullpath string) (crumbs []Breadcrumb) {
for i := 0; i < len(parts); i++ { for i := 0; i < len(parts); i++ {
crumb := Breadcrumb{ crumb := Breadcrumb{
Name: parts[i] + " /", Name: parts[i] + " /",
Link: "/" + filepath.ToSlash(filepath.Join(parts[0:i+1]...)),
Link: "/" + util.Join(parts[0:i+1]...),
} }
if !strings.HasSuffix(crumb.Link, "/") { if !strings.HasSuffix(crumb.Link, "/") {
crumb.Link += "/" crumb.Link += "/"

2
weed/server/volume_grpc_copy.go

@ -225,7 +225,7 @@ func (vs *VolumeServer) CopyFile(req *volume_server_pb.CopyFileRequest, stream v
} else { } else {
baseFileName := erasure_coding.EcShardBaseFileName(req.Collection, int(req.VolumeId)) + req.Ext baseFileName := erasure_coding.EcShardBaseFileName(req.Collection, int(req.VolumeId)) + req.Ext
for _, location := range vs.store.Locations { for _, location := range vs.store.Locations {
tName := util.PathJoin(location.Directory, baseFileName)
tName := util.Join(location.Directory, baseFileName)
if util.FileExists(tName) { if util.FileExists(tName) {
fileName = tName fileName = tName
} }

3
weed/shell/command_fs_mv.go

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"path/filepath"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb" "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"github.com/chrislusf/seaweedfs/weed/util" "github.com/chrislusf/seaweedfs/weed/util"
@ -65,7 +64,7 @@ func (c *commandFsMv) Do(args []string, commandEnv *CommandEnv, writer io.Writer
// moving a file or folder // moving a file or folder
if err == nil && respDestinationLookupEntry.Entry.IsDirectory { if err == nil && respDestinationLookupEntry.Entry.IsDirectory {
// to a directory // to a directory
targetDir = filepath.ToSlash(filepath.Join(destinationDir, destinationName))
targetDir = util.Join(destinationDir, destinationName)
targetName = sourceName targetName = sourceName
} else { } else {
// to a file or folder // to a file or folder

3
weed/shell/commands.go

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"io" "io"
"net/url" "net/url"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -55,7 +54,7 @@ func (ce *CommandEnv) parseUrl(input string) (path string, err error) {
return return
} }
if !strings.HasPrefix(input, "/") { if !strings.HasPrefix(input, "/") {
input = filepath.ToSlash(filepath.Join(ce.option.Directory, input))
input = util.Join(ce.option.Directory, input)
} }
return input, err return input, err
} }

8
weed/util/fullpath.go

@ -47,6 +47,10 @@ func (fp FullPath) Split() []string {
return strings.Split(string(fp)[1:], "/") return strings.Split(string(fp)[1:], "/")
} }
func PathJoin(dir, name string) string {
return filepath.ToSlash(filepath.Join(dir, name))
func Join(names ...string) string {
return filepath.ToSlash(filepath.Join(names...))
}
func JoinPath(names ...string) FullPath {
return FullPath(Join(names...))
} }
Loading…
Cancel
Save