@@ -2655,18 +2655,6 @@ func (fs *fileSystem) ensureNoLocalFilesInDirectory(dir inode.BucketOwnedDirInod
26552655 return nil
26562656}
26572657
2658- func (fs * fileSystem ) checkDirNotEmpty (dir inode.BucketOwnedDirInode , name string ) error {
2659- unexpected , err := dir .ReadDescendants (context .Background (), 1 )
2660- if err != nil {
2661- return fmt .Errorf ("read descendants of the new directory %q: %w" , name , err )
2662- }
2663-
2664- if len (unexpected ) > 0 {
2665- return fuse .ENOTEMPTY
2666- }
2667- return nil
2668- }
2669-
26702658// Rename an old folder to a new folder in a hierarchical bucket. If the new folder already
26712659// exists and is non-empty, return ENOTEMPTY. If old folder have open files then return
26722660// ENOTSUP.
@@ -2697,14 +2685,10 @@ func (fs *fileSystem) renameHierarchicalDir(ctx context.Context, oldParent inode
26972685 // If the call for getBucketDirInode fails it means directory does not exist.
26982686 newDirInode , err := fs .getBucketDirInode (ctx , newParent , newName )
26992687 if err == nil {
2700- // If the directory exists, then check if it is empty or not.
2701- if err = fs .checkDirNotEmpty (newDirInode , newName ); err != nil {
2702- return err
2703- }
2704-
2705- // This refers to an empty destination directory.
2706- // The RenameFolder API does not allow renaming to an existing empty directory.
2707- // To make this work, we delete the empty directory first from gcsfuse and then perform rename.
2688+ // The RenameFolder API does not allow renaming to an existing directory.
2689+ // To make this work, we attempt to delete the destination directory first.
2690+ // If it is non-empty, this deletion will fail (and we ignore the error here),
2691+ // and the subsequent RenameFolder call will fail, returning ENOTEMPTY.
27082692 newParent .Lock ()
27092693 _ = newParent .DeleteChildDir (ctx , newName , false , newDirInode )
27102694 newParent .Unlock ()
@@ -2723,6 +2707,10 @@ func (fs *fileSystem) renameHierarchicalDir(ctx context.Context, oldParent inode
27232707 // Rename old directory to the new directory, keeping both parent directories locked.
27242708 _ , err = oldParent .RenameFolder (ctx , oldDirName .GcsObjectName (), newDirName .GcsObjectName (), oldDirInode )
27252709 if err != nil {
2710+ var precondErr * gcs.PreconditionError
2711+ if errors .As (err , & precondErr ) {
2712+ return fuse .ENOTEMPTY
2713+ }
27262714 return fmt .Errorf ("failed to rename folder: %w" , err )
27272715 }
27282716
0 commit comments