mirror of
https://github.com/rclone/rclone.git
synced 2025-11-23 21:44:49 +02:00
local: fix rmdir "Access is denied" on windows - fixes #8363
Before this change, Rmdir (and other commands that rely on Rmdir) would fail with "Access is denied" on Windows, if the directory had FILE_ATTRIBUTE_READONLY. This could happen if, for example, an empty folder had a custom icon added via Windows Explorer's interface (Properties => Customize => Change Icon...). However, Microsoft docs indicate that "This attribute is not honored on directories." https://learn.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants#file_attribute_readonly Accordingly, this created an odd situation where such directories were removable (by their owner) via File Explorer and the rd command, but not via rclone. An upstream issue has been open since 2018, but has not yet resulted in a fix. https://github.com/golang/go/issues/26295 This change gets around the issue by doing os.Chmod on the dir and then retrying os.Remove. If the dir is not empty, this will still fail with "The directory is not empty." A bisync user confirmed that it fixed their issue in https://forum.rclone.org/t/bisync-leaving-empty-directories-on-unc-path-1-or-local-filesystem-path-2-on-directory-renames/52456/4?u=nielash It is likely also a fix for #8019, although @ncw is correct that Purge would be a more efficient solution in that particular scenario.
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
iofs "io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
@@ -841,7 +842,13 @@ func (f *Fs) Rmdir(ctx context.Context, dir string) error {
|
||||
} else if !fi.IsDir() {
|
||||
return fs.ErrorIsFile
|
||||
}
|
||||
return os.Remove(localPath)
|
||||
err := os.Remove(localPath)
|
||||
if runtime.GOOS == "windows" && errors.Is(err, iofs.ErrPermission) { // https://github.com/golang/go/issues/26295
|
||||
if os.Chmod(localPath, 0o600) == nil {
|
||||
err = os.Remove(localPath)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Precision of the file system
|
||||
|
||||
Reference in New Issue
Block a user