hierynomus / smbj

Server Message Block (SMB2, SMB3) implementation in Java
Other
713 stars 180 forks source link

Deletes all content from DiskSahre on empty folder name #756

Closed Redwid closed 1 year ago

Redwid commented 1 year ago

Seen that behaviour twice already. I've code to delete folder from DiskShare. But if I'm open DiskEntry with empty file path and then will call rmDir(), library deletes all files from that DiskShare.

Kotlin Code:

val share: DiskShare = session?.connectShare(FOLDER_MEDIA) as DiskShare
var diskEntry: DiskEntry? = null
try {
    assertIsSafeToDelete(path)
    diskEntry = share.open(
        "",
        EnumSet.of(AccessMask.FILE_DELETE_CHILD),
        EnumSet.of(FileAttributes.FILE_ATTRIBUTE_DIRECTORY),
        SMB2ShareAccess.ALL,
        SMB2CreateDisposition.FILE_OPEN,
        Collections.singleton(SMB2CreateOptions.FILE_DIRECTORY_FILE)
    )
    diskEntry.diskShare.rmdir(path, true)
    Timber.d("deleteFile(), diskEntry: %s", diskEntry)
}  finally {
    closeSafely(diskEntry)
}

I'm expecting exception here on share.open() as object not exists ... or something similar. Or fail of rmDir() call. But definitely not delete all.

Library version: 0.11.5 Samba version: 4.15.13-Ubuntu Server version: Ubuntu 22.04.1 User has read-write permission

hierynomus commented 1 year ago

Thanks for the report, indeed, using the empty path, it would select all files and directories for deletion. I've added a check that the path is non-null and non-empty.