lib/model: Handle filename conflicts on Windows.

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3810
LGTM: calmh
This commit is contained in:
Unrud
2016-12-22 23:04:53 +00:00
committed by Audrius Butkevicius
parent bab7c8ebbf
commit 01e50eb3fa
6 changed files with 222 additions and 1 deletions

View File

@@ -0,0 +1,17 @@
// Copyright (C) 2016 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
// +build !windows
package osutil
// CheckNameConflict returns true if every path component of name up to and
// including filepath.Join(base, name) doesn't conflict with any existing
// files or folders with different names. Base and name must both be clean and
// name must be relative to base.
func CheckNameConflict(base, name string) bool {
return true
}

View File

@@ -0,0 +1,48 @@
// Copyright (C) 2016 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
// +build windows
package osutil
import (
"os"
"path/filepath"
"strings"
"syscall"
)
// CheckNameConflict returns true if every path component of name up to and
// including filepath.Join(base, name) doesn't conflict with any existing
// files or folders with different names. Base and name must both be clean and
// name must be relative to base.
func CheckNameConflict(base, name string) bool {
// Conflicts can be caused by different casing (e.g. foo and FOO) or
// by the use of short names (e.g. foo.barbaz and FOO~1.BAR).
path := base
parts := strings.Split(name, string(os.PathSeparator))
for _, part := range parts {
path = filepath.Join(path, part)
pathp, err := syscall.UTF16PtrFromString(path)
if err != nil {
return false
}
var data syscall.Win32finddata
handle, err := syscall.FindFirstFile(pathp, &data)
if err == syscall.ERROR_FILE_NOT_FOUND {
return true
}
if err != nil {
return false
}
syscall.FindClose(handle)
fileName := syscall.UTF16ToString(data.FileName[:])
if part != fileName {
return false
}
}
return true
}

View File

@@ -0,0 +1,108 @@
// Copyright (C) 2016 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
// +build windows
package osutil_test
import (
"os"
"path"
"path/filepath"
"syscall"
"testing"
"unsafe"
"github.com/syncthing/syncthing/lib/osutil"
)
func TestCheckNameConflictCasing(t *testing.T) {
os.RemoveAll("testdata")
defer os.RemoveAll("testdata")
os.MkdirAll("testdata/Foo/BAR/baz", 0755)
// check if the file system is case-sensitive
if _, err := os.Lstat("testdata/foo"); err != nil {
t.Skip("pointless test")
return
}
cases := []struct {
name string
conflictFree bool
}{
// Exists
{"Foo", true},
{"Foo/BAR", true},
{"Foo/BAR/baz", true},
// Doesn't exist
{"bar", true},
{"Foo/baz", true},
// Conflicts
{"foo", false},
{"foo/BAR", false},
{"Foo/bar", false},
{"Foo/BAR/BAZ", false},
}
for _, tc := range cases {
nativeName := filepath.FromSlash(tc.name)
if res := osutil.CheckNameConflict("testdata", nativeName); res != tc.conflictFree {
t.Errorf("CheckNameConflict(%q) = %v, should be %v", tc.name, res, tc.conflictFree)
}
}
}
func TestCheckNameConflictShortName(t *testing.T) {
os.RemoveAll("testdata")
defer os.RemoveAll("testdata")
os.MkdirAll("testdata/foobarbaz/qux", 0755)
ppath, err := syscall.UTF16PtrFromString("testdata/foobarbaz")
if err != nil {
t.Fatal("unexpected error", err)
}
// check if the file system supports short names
bufferSize, err := syscall.GetShortPathName(ppath, nil, 0)
if err != nil {
t.Skip("pointless test")
return
}
// get the short name
buffer := make([]uint16, bufferSize)
length, err := syscall.GetShortPathName(ppath,
(*uint16)(unsafe.Pointer(&buffer[0])), bufferSize)
if err != nil {
t.Fatal("unexpected error", err)
}
// on success length doesn't contain the terminating null character
if bufferSize != length+1 {
t.Fatal("length of short name changed")
}
shortName := filepath.Base(syscall.UTF16ToString(buffer))
cases := []struct {
name string
conflictFree bool
}{
// Exists
{"foobarbaz", true},
{"foobarbaz/qux", true},
// Doesn't exist
{"foo", true},
{"foobarbaz/quux", true},
// Conflicts
{shortName, false},
{path.Join(shortName, "qux"), false},
{path.Join(shortName, "quux"), false},
}
for _, tc := range cases {
nativeName := filepath.FromSlash(tc.name)
if res := osutil.CheckNameConflict("testdata", nativeName); res != tc.conflictFree {
t.Errorf("CheckNameConflict(%q) = %v, should be %v", tc.name, res, tc.conflictFree)
}
}
}