Finish the first version of propfind.
This commit is contained in:
@ -17,9 +17,9 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// slashClean is equivalent to but slightly more efficient than
|
||||
// SlashClean is equivalent to but slightly more efficient than
|
||||
// path.Clean("/" + name).
|
||||
func slashClean(name string) string {
|
||||
func SlashClean(name string) string {
|
||||
if name == "" || name[0] != '/' {
|
||||
name = "/" + name
|
||||
}
|
||||
@ -74,7 +74,7 @@ func (d Dir) resolve(name string) string {
|
||||
if dir == "" {
|
||||
dir = "."
|
||||
}
|
||||
return filepath.Join(dir, filepath.FromSlash(slashClean(name)))
|
||||
return filepath.Join(dir, filepath.FromSlash(SlashClean(name)))
|
||||
}
|
||||
|
||||
func (d Dir) Mkdir(ctx context.Context, name string, perm os.FileMode) error {
|
||||
@ -166,7 +166,7 @@ type memFS struct {
|
||||
// ends at that root node.
|
||||
func (fs *memFS) walk(op, fullname string, f func(dir *memFSNode, frag string, final bool) error) error {
|
||||
original := fullname
|
||||
fullname = slashClean(fullname)
|
||||
fullname = SlashClean(fullname)
|
||||
|
||||
// Strip any leading "/"s to make fullname a relative path, as the walk
|
||||
// starts at fs.root.
|
||||
@ -335,8 +335,8 @@ func (fs *memFS) Rename(ctx context.Context, oldName, newName string) error {
|
||||
fs.mu.Lock()
|
||||
defer fs.mu.Unlock()
|
||||
|
||||
oldName = slashClean(oldName)
|
||||
newName = slashClean(newName)
|
||||
oldName = SlashClean(oldName)
|
||||
newName = SlashClean(newName)
|
||||
if oldName == newName {
|
||||
return nil
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ func TestSlashClean(t *testing.T) {
|
||||
"a/b/c",
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
got := slashClean(tc)
|
||||
got := SlashClean(tc)
|
||||
want := path.Clean("/" + tc)
|
||||
if got != want {
|
||||
t.Errorf("tc=%q: got %q, want %q", tc, got, want)
|
||||
|
@ -151,12 +151,12 @@ func (m *memLS) Confirm(now time.Time, name0, name1 string, conditions ...Condit
|
||||
|
||||
var n0, n1 *memLSNode
|
||||
if name0 != "" {
|
||||
if n0 = m.lookup(slashClean(name0), conditions...); n0 == nil {
|
||||
if n0 = m.lookup(SlashClean(name0), conditions...); n0 == nil {
|
||||
return nil, ErrConfirmationFailed
|
||||
}
|
||||
}
|
||||
if name1 != "" {
|
||||
if n1 = m.lookup(slashClean(name1), conditions...); n1 == nil {
|
||||
if n1 = m.lookup(SlashClean(name1), conditions...); n1 == nil {
|
||||
return nil, ErrConfirmationFailed
|
||||
}
|
||||
}
|
||||
@ -233,7 +233,7 @@ func (m *memLS) Create(now time.Time, details LockDetails) (string, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.collectExpiredNodes(now)
|
||||
details.Root = slashClean(details.Root)
|
||||
details.Root = SlashClean(details.Root)
|
||||
|
||||
if !m.canCreate(details.Root, details.ZeroDepth) {
|
||||
return "", ErrLocked
|
||||
|
@ -336,7 +336,7 @@ loop:
|
||||
return []Propstat{pstat}, nil
|
||||
}
|
||||
|
||||
func escapeXML(s string) string {
|
||||
func EscapeXML(s string) string {
|
||||
for i := 0; i < len(s); i++ {
|
||||
// As an optimization, if s contains only ASCII letters, digits or a
|
||||
// few special characters, the escaped value is s itself and we don't
|
||||
@ -364,11 +364,11 @@ func findResourceType(ctx context.Context, fs FileSystem, ls LockSystem, name st
|
||||
}
|
||||
|
||||
func findDisplayName(ctx context.Context, fs FileSystem, ls LockSystem, name string, fi os.FileInfo) (string, error) {
|
||||
if slashClean(name) == "/" {
|
||||
if SlashClean(name) == "/" {
|
||||
// Hide the real name of a possibly prefixed root directory.
|
||||
return "", nil
|
||||
}
|
||||
return escapeXML(fi.Name()), nil
|
||||
return EscapeXML(fi.Name()), nil
|
||||
}
|
||||
|
||||
func findContentLength(ctx context.Context, fs FileSystem, ls LockSystem, name string, fi os.FileInfo) (string, error) {
|
||||
|
@ -208,7 +208,7 @@ func TestEscapeXML(t *testing.T) {
|
||||
// These test cases aren't exhaustive, and there is more than one way to
|
||||
// escape e.g. a quot (as """ or """) or an apos. We presume that
|
||||
// the encoding/xml package tests xml.EscapeText more thoroughly. This test
|
||||
// here is just a sanity check for this package's escapeXML function, and
|
||||
// here is just a sanity check for this package's EscapeXML function, and
|
||||
// its attempt to provide a fast path (and avoid a bytes.Buffer allocation)
|
||||
// when escaping filenames is obviously a no-op.
|
||||
testCases := map[string]string{
|
||||
@ -236,7 +236,7 @@ func TestEscapeXML(t *testing.T) {
|
||||
}
|
||||
|
||||
for in, want := range testCases {
|
||||
if got := escapeXML(in); got != want {
|
||||
if got := EscapeXML(in); got != want {
|
||||
t.Errorf("in=%q: got %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user