如何递归执行chmod?

发布于 2025-01-09 17:51:42 字数 128 浏览 0 评论 0原文

如何在运行时以递归方式将文件夹及其所有子文件夹的权限更改为 0777?

代码是c++,mac。我包括其中有 chmod,但是没有关于如何递归执行此操作的文档。

How can I change permissions to 0777, at runtime, of a folder and all its subfolders, recursively?

The code is in c++, mac. I'm including <sys/stat.h> which has chmod, however there's no documentation on how to do it recursively.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长途伴 2025-01-16 17:51:42

最简单、最便携的方法是使用 std::filesystem C++17 中添加的库。在那里,您会找到一个recursive_directory_iterator和许多其他方便的类和函数来处理文件系统特定的事情。

示例:

#include <iostream>

#include <filesystem>            // see notes about these two lines at the bottom
namespace fs = std::filesystem;  // -"-

void chmodr(const fs::path& path, fs::perms perm) {
    fs::permissions(path, perm);      // set permissions on the top directory
    for(auto& de : fs::recursive_directory_iterator(path)) {
        fs::permissions(de, perm);    // set permissions
        std::cout << de << '\n';      // debug print
    }
}

int main() {
    chmodr("your_top_directory", fs::perms::all); // perms::all = 0777
}

但是,当涉及的目录太多时,recursive_directory_iterator 就会出现问题。它可能会用完文件描述符,因为它需要保持许多目录打开。因此,我更喜欢使用 directory_iterator 来代替 - 并收集子目录以供稍后检查。

示例:

#include <iostream>
#include <stack>
#include <utility>

#include <filesystem>            // see notes about these two lines at the bottom
namespace fs = std::filesystem;  // -"-

void chmodr(const fs::path& path, fs::perms perm) {
    std::stack<fs::path> dirs;
    dirs.push(path);

    fs::permissions(path, perm);

    do {
        auto pa = std::move(dirs.top()); // extract the top dir from the stack
        dirs.pop();                      // and remove it

        for(auto& de : fs::directory_iterator(pa)) {
            // save subdirectories for later:
            if(fs::is_directory(de)) dirs.push(de);
            else fs::permissions(de, perm);
        }
    } while(!dirs.empty());              // loop until there are no dirs left
}

int main() {
    chmodr("your_top_directory", fs::perms::all);
}

您可以在链接 I 中了解示例中使用的 std::filesystem:: (上面代码中的 fs::)函数、类和权限枚举提供在顶部。

在某些实现中,仅部分支持 C++17,您可能会在 experimental/filesystem 中找到 filesystem。如果是这种情况,您可以将上面的内容替换

#include <filesystem>
namespace fs = std::filesystem;

为我在这个答案<中提供的#ifdef丛林。 /a>.

The simplest and most portable way would be to use the std::filesystem library that was added in C++17. In there, you'll find a recursive_directory_iterator and many other handy classes and functions for dealing with filesystem specific things.

Example:

#include <iostream>

#include <filesystem>            // see notes about these two lines at the bottom
namespace fs = std::filesystem;  // -"-

void chmodr(const fs::path& path, fs::perms perm) {
    fs::permissions(path, perm);      // set permissions on the top directory
    for(auto& de : fs::recursive_directory_iterator(path)) {
        fs::permissions(de, perm);    // set permissions
        std::cout << de << '\n';      // debug print
    }
}

int main() {
    chmodr("your_top_directory", fs::perms::all); // perms::all = 0777
}

However, recursive_directory_iterator has an issue when there are too many directories involved. It may run out of file descriptors because it needs to keep many directories open. For that reason, I prefer to use a directory_iterator instead - and collect the subdirectories to examine for later.

Example:

#include <iostream>
#include <stack>
#include <utility>

#include <filesystem>            // see notes about these two lines at the bottom
namespace fs = std::filesystem;  // -"-

void chmodr(const fs::path& path, fs::perms perm) {
    std::stack<fs::path> dirs;
    dirs.push(path);

    fs::permissions(path, perm);

    do {
        auto pa = std::move(dirs.top()); // extract the top dir from the stack
        dirs.pop();                      // and remove it

        for(auto& de : fs::directory_iterator(pa)) {
            // save subdirectories for later:
            if(fs::is_directory(de)) dirs.push(de);
            else fs::permissions(de, perm);
        }
    } while(!dirs.empty());              // loop until there are no dirs left
}

int main() {
    chmodr("your_top_directory", fs::perms::all);
}

You can read about the std::filesystem:: (fs:: in the code above) functions, classes and permission enums used in the example in the link I provided at the top.

In some implementations, with only partial C++17 support, you may find filesystem in experimental/filesystem instead. If that's the case, you can replace the above

#include <filesystem>
namespace fs = std::filesystem;

with the #ifdef jungle I've provided in this answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文