简体   繁体   中英

Do I have to delete recursively all files and folders before deleting any directory with smbd or ssh protocol?

I have tried to delete directory in a remote Linux machine using SMBD client:

  DiskShare.rmdir(folderName, true); 

Do I have to delete recursively all files and folders before deleting any directory with smbd or with another protocol - ssh protocol?

if yes - why?

In order to delete a directory (at least on Unix), it must be empty. If it is not, the system call will fail with ENOTEMPTY or EEXIST . That means that, yes, you will need to recursively delete all files and directories within it before deleting the directory itself. It may be that in your case, there is a convenient way to do that (such as a recursive deletion function), or you may need to implement it yourself.

The reason is that system calls, like rmdir , are generally supposed to be reasonably efficient and easy to reason about. The files and contained directories must be removed to keep the file system consistent; after all, if there is no way to access them because the parent directory will no longer exist, you won't want them to consume disk space.

If rmdir removed them implicitly, that would be potentially expensive, since there might be a lot of files and directories to remove, and it would also be hard to reason about, because if removing one of them failed, you'd have no clue which one it was, only a single error code to represent the entire failure.

At least the SFTP protocol doesn't provide a way to recursively delete data, but your library may. In your particular example, the true argument appears to be the recursive deletion flag, so the function call you've specified will delete the directory and all of its contents; you don't need to do that separately. If you're using the equivalent SFTP client, it looks like it doesn't implement that itself, so you'll need to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM