mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-09 23:00:58 +00:00
Do not remove the file when renaming on a case-insensitive platform (fixes #1743)
This commit is contained in:
parent
58b15f9452
commit
67f0c9bef0
@ -32,7 +32,7 @@ func TryRename(from, to string) error {
|
|||||||
renameLock.Lock()
|
renameLock.Lock()
|
||||||
defer renameLock.Unlock()
|
defer renameLock.Unlock()
|
||||||
|
|
||||||
return withPreparedTarget(to, func() error {
|
return withPreparedTarget(from, to, func() error {
|
||||||
return os.Rename(from, to)
|
return os.Rename(from, to)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -44,7 +44,9 @@ func TryRename(from, to string) error {
|
|||||||
// permissions and removing the destination file when necessary.
|
// permissions and removing the destination file when necessary.
|
||||||
func Rename(from, to string) error {
|
func Rename(from, to string) error {
|
||||||
// Don't leave a dangling temp file in case of rename error
|
// Don't leave a dangling temp file in case of rename error
|
||||||
|
if !(runtime.GOOS == "windows" && strings.EqualFold(from, to)) {
|
||||||
defer os.Remove(from)
|
defer os.Remove(from)
|
||||||
|
}
|
||||||
return TryRename(from, to)
|
return TryRename(from, to)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,7 +54,7 @@ func Rename(from, to string) error {
|
|||||||
// Tries hard to succeed on various systems by temporarily tweaking directory
|
// Tries hard to succeed on various systems by temporarily tweaking directory
|
||||||
// permissions and removing the destination file when necessary.
|
// permissions and removing the destination file when necessary.
|
||||||
func Copy(from, to string) (err error) {
|
func Copy(from, to string) (err error) {
|
||||||
return withPreparedTarget(to, func() error {
|
return withPreparedTarget(from, to, func() error {
|
||||||
return copyFileContents(from, to)
|
return copyFileContents(from, to)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -143,7 +145,7 @@ func getHomeDir() (string, error) {
|
|||||||
|
|
||||||
// Tries hard to succeed on various systems by temporarily tweaking directory
|
// Tries hard to succeed on various systems by temporarily tweaking directory
|
||||||
// permissions and removing the destination file when necessary.
|
// permissions and removing the destination file when necessary.
|
||||||
func withPreparedTarget(to string, f func() error) error {
|
func withPreparedTarget(from, to string, f func() error) error {
|
||||||
// Make sure the destination directory is writeable
|
// Make sure the destination directory is writeable
|
||||||
toDir := filepath.Dir(to)
|
toDir := filepath.Dir(to)
|
||||||
if info, err := os.Stat(toDir); err == nil && info.IsDir() && info.Mode()&0200 == 0 {
|
if info, err := os.Stat(toDir); err == nil && info.IsDir() && info.Mode()&0200 == 0 {
|
||||||
@ -154,11 +156,13 @@ func withPreparedTarget(to string, f func() error) error {
|
|||||||
// On Windows, make sure the destination file is writeable (or we can't delete it)
|
// On Windows, make sure the destination file is writeable (or we can't delete it)
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
os.Chmod(to, 0666)
|
os.Chmod(to, 0666)
|
||||||
|
if !strings.EqualFold(from, to) {
|
||||||
err := os.Remove(to)
|
err := os.Remove(to)
|
||||||
if err != nil && !os.IsNotExist(err) {
|
if err != nil && !os.IsNotExist(err) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return f()
|
return f()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,7 +172,7 @@ func alterFiles(dir string) error {
|
|||||||
base[i] = unicode.ToUpper(r)
|
base[i] = unicode.ToUpper(r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = os.Rename(path, strings.Replace(path, filepath.Base(path), string(base), 1))
|
err = osutil.TryRename(path, strings.Replace(path, filepath.Base(path), string(base), 1))
|
||||||
return err
|
return err
|
||||||
|
|
||||||
// Switch between files and directories
|
// Switch between files and directories
|
||||||
@ -188,7 +188,7 @@ func alterFiles(dir string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err := os.Remove(path)
|
err := osutil.Remove(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -207,7 +207,7 @@ func alterFiles(dir string) error {
|
|||||||
rpath = filepath.Join(rpath, "..")
|
rpath = filepath.Join(rpath, "..")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = os.Rename(path, filepath.Join(rpath, randomName()))
|
err = osutil.TryRename(path, filepath.Join(rpath, randomName()))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user