From d08453d40240338ac9ba87337713ea91a5b15174 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Wed, 9 Jan 2019 19:27:15 +0000 Subject: [PATCH] local: fix renaming/deleting open files on Windows #2730 This uses the lib/file package to open files in such a way open files can be renamed or deleted even under Windows. --- backend/local/local.go | 7 ++++--- backend/local/local_internal_test.go | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/backend/local/local.go b/backend/local/local.go index 5ffee86ad..d86951720 100644 --- a/backend/local/local.go +++ b/backend/local/local.go @@ -21,6 +21,7 @@ import ( "github.com/ncw/rclone/fs/config/configstruct" "github.com/ncw/rclone/fs/fserrors" "github.com/ncw/rclone/fs/hash" + "github.com/ncw/rclone/lib/file" "github.com/ncw/rclone/lib/readers" "github.com/pkg/errors" ) @@ -651,7 +652,7 @@ func (o *Object) Hash(r hash.Type) (string, error) { o.fs.objectHashesMu.Unlock() if !o.modTime.Equal(oldtime) || oldsize != o.size || hashes == nil { - in, err := os.Open(o.path) + in, err := file.Open(o.path) if err != nil { return "", errors.Wrap(err, "hash: failed to open") } @@ -780,7 +781,7 @@ func (o *Object) Open(options ...fs.OpenOption) (in io.ReadCloser, err error) { } } - fd, err := os.Open(o.path) + fd, err := file.Open(o.path) if err != nil { return } @@ -826,7 +827,7 @@ func (o *Object) Update(in io.Reader, src fs.ObjectInfo, options ...fs.OpenOptio return err } - out, err := os.OpenFile(o.path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) + out, err := file.OpenFile(o.path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) if err != nil { return err } diff --git a/backend/local/local_internal_test.go b/backend/local/local_internal_test.go index d69e7cf22..fc86010af 100644 --- a/backend/local/local_internal_test.go +++ b/backend/local/local_internal_test.go @@ -1,13 +1,13 @@ package local import ( - "os" "path" "testing" "time" "github.com/ncw/rclone/fs/hash" "github.com/ncw/rclone/fstest" + "github.com/ncw/rclone/lib/file" "github.com/ncw/rclone/lib/readers" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -38,7 +38,7 @@ func TestUpdatingCheck(t *testing.T) { filePath := "sub dir/local test" r.WriteFile(filePath, "content", time.Now()) - fd, err := os.Open(path.Join(r.LocalName, filePath)) + fd, err := file.Open(path.Join(r.LocalName, filePath)) if err != nil { t.Fatalf("failed opening file %q: %v", filePath, err) }