FileSysOrg / jfileserver

Java file server with SMB, FTP/FTPS and NFS support, virtual filesystems, database filesystems
GNU Lesser General Public License v3.0
44 stars 18 forks source link

Non-empty folders cannot be deleted using the smb protocol #16

Open zglqaq opened 10 months ago

zglqaq commented 10 months ago

in class org.filesys.smb.server.disk.original.JavaFileDiskDriver.deleteDirectory line 267

            String[] fileList = delDir.list();
            if (fileList != null && fileList.length > 0) {
                throw new AccessDeniedException("Directory not empty");
            } else {
                //  Delete the directory
                delDir.delete();
            }

In order to be able to delete non-empty folders directly,I changed the above code to

            String[] fileList = delDir.list();
            if (fileList != null && fileList.length > 0) {
                //throw new AccessDeniedException("Directory not empty");          
                deleteFolder(delDir); //Recursive deletion
            } else {
                //  Delete the directory
                delDir.delete();
            }
    private static void deleteFolder(File folder) {
        File[] files = folder.listFiles();

        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    deleteFolder(file);
                } else {
                    file.delete();
                }
            }
        }

        folder.delete();
    }

After the modification, the non-empty folder can be successfully deleted on the client.

But when I cut the non-empty folder on the client, the client only cut the empty folder, there is no content in it, and the server folder was completely deleted.

I used the packet capture tool to analyze the data message and found that the client did not send the ReadAndX(0x2e) command image

FileSysOrg commented 10 months ago

This looks like SMB1 traffic, according to the spec the DeleteDirectory request should return a STATUS_DIRECTORY_NOT_EMPTY if there are files/folders within the folder being deleted. The Java File delete() method will throw an exception if there are files/folders in the folder you are trying to delete.

It's usually up to the client to deal with the non-empty folder.

I've not done any work with the SMB1 code for quite a while as Windows clients have switched SMB1 off, using the more efficient and secure SMB2/SMB3 protocols.