How to remove directories I don't have permission to access in Rust?










1














How to remove a directory recursively in Rust, deleting empty directories with Unix permissions of 000 as well? These permissions are read as "no reading, writing or executing is allowed by any user".



I've previously created the directory like this:



mkdir -p dir/trap
chmod 000 dir/trap


I've tried this method:



std::fs::remove_dir_all("dir").unwrap();


It fails with "Permission denied" however. Are there any other solutions that still remove such directory, like rm -rf would?










share|improve this question























  • "Are there any other solutions that ignore the read-only flag?" be root ? You are saying is there a solution to ignore permission in linux... I hope there is none ;)
    – Stargateur
    Nov 11 at 11:31






  • 2




    @Stargateur: Actually, to delete a file (or directory) in Unix you need write permissions to the parent directory, not to the file itself... except in sticky directories.
    – rodrigo
    Nov 11 at 11:44










  • @Stargateur I need something akin to rm -rf. Now, rm -rf surely does delete "000" directories and their children.
    – VasyaNovikov
    Nov 11 at 12:08






  • 2




    Actually, I've just tested it in my Linux box and rm -rf does not delete 000, non-empty directories. It fails with EPERM.
    – rodrigo
    Nov 11 at 12:12







  • 1




    @MatthieuM note that I'm not asking to remove a non-empty directory. I'm basically asking the same behavior of rm -rf. It removes empty "000" directories, as well as accessible non-empty ones.
    – VasyaNovikov
    Nov 11 at 17:44
















1














How to remove a directory recursively in Rust, deleting empty directories with Unix permissions of 000 as well? These permissions are read as "no reading, writing or executing is allowed by any user".



I've previously created the directory like this:



mkdir -p dir/trap
chmod 000 dir/trap


I've tried this method:



std::fs::remove_dir_all("dir").unwrap();


It fails with "Permission denied" however. Are there any other solutions that still remove such directory, like rm -rf would?










share|improve this question























  • "Are there any other solutions that ignore the read-only flag?" be root ? You are saying is there a solution to ignore permission in linux... I hope there is none ;)
    – Stargateur
    Nov 11 at 11:31






  • 2




    @Stargateur: Actually, to delete a file (or directory) in Unix you need write permissions to the parent directory, not to the file itself... except in sticky directories.
    – rodrigo
    Nov 11 at 11:44










  • @Stargateur I need something akin to rm -rf. Now, rm -rf surely does delete "000" directories and their children.
    – VasyaNovikov
    Nov 11 at 12:08






  • 2




    Actually, I've just tested it in my Linux box and rm -rf does not delete 000, non-empty directories. It fails with EPERM.
    – rodrigo
    Nov 11 at 12:12







  • 1




    @MatthieuM note that I'm not asking to remove a non-empty directory. I'm basically asking the same behavior of rm -rf. It removes empty "000" directories, as well as accessible non-empty ones.
    – VasyaNovikov
    Nov 11 at 17:44














1












1








1







How to remove a directory recursively in Rust, deleting empty directories with Unix permissions of 000 as well? These permissions are read as "no reading, writing or executing is allowed by any user".



I've previously created the directory like this:



mkdir -p dir/trap
chmod 000 dir/trap


I've tried this method:



std::fs::remove_dir_all("dir").unwrap();


It fails with "Permission denied" however. Are there any other solutions that still remove such directory, like rm -rf would?










share|improve this question















How to remove a directory recursively in Rust, deleting empty directories with Unix permissions of 000 as well? These permissions are read as "no reading, writing or executing is allowed by any user".



I've previously created the directory like this:



mkdir -p dir/trap
chmod 000 dir/trap


I've tried this method:



std::fs::remove_dir_all("dir").unwrap();


It fails with "Permission denied" however. Are there any other solutions that still remove such directory, like rm -rf would?







rust filesystems






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 14:09









Shepmaster

147k11282415




147k11282415










asked Nov 11 at 11:05









VasyaNovikov

5,03922336




5,03922336











  • "Are there any other solutions that ignore the read-only flag?" be root ? You are saying is there a solution to ignore permission in linux... I hope there is none ;)
    – Stargateur
    Nov 11 at 11:31






  • 2




    @Stargateur: Actually, to delete a file (or directory) in Unix you need write permissions to the parent directory, not to the file itself... except in sticky directories.
    – rodrigo
    Nov 11 at 11:44










  • @Stargateur I need something akin to rm -rf. Now, rm -rf surely does delete "000" directories and their children.
    – VasyaNovikov
    Nov 11 at 12:08






  • 2




    Actually, I've just tested it in my Linux box and rm -rf does not delete 000, non-empty directories. It fails with EPERM.
    – rodrigo
    Nov 11 at 12:12







  • 1




    @MatthieuM note that I'm not asking to remove a non-empty directory. I'm basically asking the same behavior of rm -rf. It removes empty "000" directories, as well as accessible non-empty ones.
    – VasyaNovikov
    Nov 11 at 17:44

















  • "Are there any other solutions that ignore the read-only flag?" be root ? You are saying is there a solution to ignore permission in linux... I hope there is none ;)
    – Stargateur
    Nov 11 at 11:31






  • 2




    @Stargateur: Actually, to delete a file (or directory) in Unix you need write permissions to the parent directory, not to the file itself... except in sticky directories.
    – rodrigo
    Nov 11 at 11:44










  • @Stargateur I need something akin to rm -rf. Now, rm -rf surely does delete "000" directories and their children.
    – VasyaNovikov
    Nov 11 at 12:08






  • 2




    Actually, I've just tested it in my Linux box and rm -rf does not delete 000, non-empty directories. It fails with EPERM.
    – rodrigo
    Nov 11 at 12:12







  • 1




    @MatthieuM note that I'm not asking to remove a non-empty directory. I'm basically asking the same behavior of rm -rf. It removes empty "000" directories, as well as accessible non-empty ones.
    – VasyaNovikov
    Nov 11 at 17:44
















"Are there any other solutions that ignore the read-only flag?" be root ? You are saying is there a solution to ignore permission in linux... I hope there is none ;)
– Stargateur
Nov 11 at 11:31




"Are there any other solutions that ignore the read-only flag?" be root ? You are saying is there a solution to ignore permission in linux... I hope there is none ;)
– Stargateur
Nov 11 at 11:31




2




2




@Stargateur: Actually, to delete a file (or directory) in Unix you need write permissions to the parent directory, not to the file itself... except in sticky directories.
– rodrigo
Nov 11 at 11:44




@Stargateur: Actually, to delete a file (or directory) in Unix you need write permissions to the parent directory, not to the file itself... except in sticky directories.
– rodrigo
Nov 11 at 11:44












@Stargateur I need something akin to rm -rf. Now, rm -rf surely does delete "000" directories and their children.
– VasyaNovikov
Nov 11 at 12:08




@Stargateur I need something akin to rm -rf. Now, rm -rf surely does delete "000" directories and their children.
– VasyaNovikov
Nov 11 at 12:08




2




2




Actually, I've just tested it in my Linux box and rm -rf does not delete 000, non-empty directories. It fails with EPERM.
– rodrigo
Nov 11 at 12:12





Actually, I've just tested it in my Linux box and rm -rf does not delete 000, non-empty directories. It fails with EPERM.
– rodrigo
Nov 11 at 12:12





1




1




@MatthieuM note that I'm not asking to remove a non-empty directory. I'm basically asking the same behavior of rm -rf. It removes empty "000" directories, as well as accessible non-empty ones.
– VasyaNovikov
Nov 11 at 17:44





@MatthieuM note that I'm not asking to remove a non-empty directory. I'm basically asking the same behavior of rm -rf. It removes empty "000" directories, as well as accessible non-empty ones.
– VasyaNovikov
Nov 11 at 17:44













2 Answers
2






active

oldest

votes


















2














The problem is that remove_dir_all() first walks the contents of the directory to delete any file or subdirectory that may be there. And you do not have permissions to read the directory, thus the error.



You can call instead:



std::fs::remove_dir("dir").unwrap();


It does not try to walk the directory, instead assumes that it is already empty, and fails if it is not.



If you want to delete a non-empty, 000 moded directory you will have to change the permissions first back to a sensible value and then you can call remove_dir_all() safely.






share|improve this answer




















  • set_permissions is a possible solution but I believe that it's uncommon for a program to change permission on the file, it's better to let the user fix the problem by using chmod or equivalent.
    – Stargateur
    Nov 11 at 11:35










  • The problem is that I want to do it recursively. Like some inner folder can have "000" permissions. I'll update the question. UPD: done, updated the question.
    – VasyaNovikov
    Nov 11 at 12:08



















0














No easy answer was found yet, so I've written my own implementation: https://crates.io/crates/rm_rf



Usage:



rm_rf::force_remove_all("target", true).expect("Failed to remove file/directory");


This will remove read-only files on Windows and empty directories lacking read access on Linux.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248086%2fhow-to-remove-directories-i-dont-have-permission-to-access-in-rust%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    The problem is that remove_dir_all() first walks the contents of the directory to delete any file or subdirectory that may be there. And you do not have permissions to read the directory, thus the error.



    You can call instead:



    std::fs::remove_dir("dir").unwrap();


    It does not try to walk the directory, instead assumes that it is already empty, and fails if it is not.



    If you want to delete a non-empty, 000 moded directory you will have to change the permissions first back to a sensible value and then you can call remove_dir_all() safely.






    share|improve this answer




















    • set_permissions is a possible solution but I believe that it's uncommon for a program to change permission on the file, it's better to let the user fix the problem by using chmod or equivalent.
      – Stargateur
      Nov 11 at 11:35










    • The problem is that I want to do it recursively. Like some inner folder can have "000" permissions. I'll update the question. UPD: done, updated the question.
      – VasyaNovikov
      Nov 11 at 12:08
















    2














    The problem is that remove_dir_all() first walks the contents of the directory to delete any file or subdirectory that may be there. And you do not have permissions to read the directory, thus the error.



    You can call instead:



    std::fs::remove_dir("dir").unwrap();


    It does not try to walk the directory, instead assumes that it is already empty, and fails if it is not.



    If you want to delete a non-empty, 000 moded directory you will have to change the permissions first back to a sensible value and then you can call remove_dir_all() safely.






    share|improve this answer




















    • set_permissions is a possible solution but I believe that it's uncommon for a program to change permission on the file, it's better to let the user fix the problem by using chmod or equivalent.
      – Stargateur
      Nov 11 at 11:35










    • The problem is that I want to do it recursively. Like some inner folder can have "000" permissions. I'll update the question. UPD: done, updated the question.
      – VasyaNovikov
      Nov 11 at 12:08














    2












    2








    2






    The problem is that remove_dir_all() first walks the contents of the directory to delete any file or subdirectory that may be there. And you do not have permissions to read the directory, thus the error.



    You can call instead:



    std::fs::remove_dir("dir").unwrap();


    It does not try to walk the directory, instead assumes that it is already empty, and fails if it is not.



    If you want to delete a non-empty, 000 moded directory you will have to change the permissions first back to a sensible value and then you can call remove_dir_all() safely.






    share|improve this answer












    The problem is that remove_dir_all() first walks the contents of the directory to delete any file or subdirectory that may be there. And you do not have permissions to read the directory, thus the error.



    You can call instead:



    std::fs::remove_dir("dir").unwrap();


    It does not try to walk the directory, instead assumes that it is already empty, and fails if it is not.



    If you want to delete a non-empty, 000 moded directory you will have to change the permissions first back to a sensible value and then you can call remove_dir_all() safely.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 11 at 11:23









    rodrigo

    62.9k491128




    62.9k491128











    • set_permissions is a possible solution but I believe that it's uncommon for a program to change permission on the file, it's better to let the user fix the problem by using chmod or equivalent.
      – Stargateur
      Nov 11 at 11:35










    • The problem is that I want to do it recursively. Like some inner folder can have "000" permissions. I'll update the question. UPD: done, updated the question.
      – VasyaNovikov
      Nov 11 at 12:08

















    • set_permissions is a possible solution but I believe that it's uncommon for a program to change permission on the file, it's better to let the user fix the problem by using chmod or equivalent.
      – Stargateur
      Nov 11 at 11:35










    • The problem is that I want to do it recursively. Like some inner folder can have "000" permissions. I'll update the question. UPD: done, updated the question.
      – VasyaNovikov
      Nov 11 at 12:08
















    set_permissions is a possible solution but I believe that it's uncommon for a program to change permission on the file, it's better to let the user fix the problem by using chmod or equivalent.
    – Stargateur
    Nov 11 at 11:35




    set_permissions is a possible solution but I believe that it's uncommon for a program to change permission on the file, it's better to let the user fix the problem by using chmod or equivalent.
    – Stargateur
    Nov 11 at 11:35












    The problem is that I want to do it recursively. Like some inner folder can have "000" permissions. I'll update the question. UPD: done, updated the question.
    – VasyaNovikov
    Nov 11 at 12:08





    The problem is that I want to do it recursively. Like some inner folder can have "000" permissions. I'll update the question. UPD: done, updated the question.
    – VasyaNovikov
    Nov 11 at 12:08














    0














    No easy answer was found yet, so I've written my own implementation: https://crates.io/crates/rm_rf



    Usage:



    rm_rf::force_remove_all("target", true).expect("Failed to remove file/directory");


    This will remove read-only files on Windows and empty directories lacking read access on Linux.






    share|improve this answer

























      0














      No easy answer was found yet, so I've written my own implementation: https://crates.io/crates/rm_rf



      Usage:



      rm_rf::force_remove_all("target", true).expect("Failed to remove file/directory");


      This will remove read-only files on Windows and empty directories lacking read access on Linux.






      share|improve this answer























        0












        0








        0






        No easy answer was found yet, so I've written my own implementation: https://crates.io/crates/rm_rf



        Usage:



        rm_rf::force_remove_all("target", true).expect("Failed to remove file/directory");


        This will remove read-only files on Windows and empty directories lacking read access on Linux.






        share|improve this answer












        No easy answer was found yet, so I've written my own implementation: https://crates.io/crates/rm_rf



        Usage:



        rm_rf::force_remove_all("target", true).expect("Failed to remove file/directory");


        This will remove read-only files on Windows and empty directories lacking read access on Linux.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 19:51









        VasyaNovikov

        5,03922336




        5,03922336



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248086%2fhow-to-remove-directories-i-dont-have-permission-to-access-in-rust%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20