如何递归执行chmod?
如何在运行时以递归方式将文件夹及其所有子文件夹的权限更改为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单、最便携的方法是使用
std::filesystem
C++17 中添加的库。在那里,您会找到一个recursive_directory_iterator
和许多其他方便的类和函数来处理文件系统特定的事情。示例:
但是,当涉及的目录太多时,
recursive_directory_iterator
就会出现问题。它可能会用完文件描述符,因为它需要保持许多目录打开。因此,我更喜欢使用directory_iterator
来代替 - 并收集子目录以供稍后检查。示例:
您可以在链接 I 中了解示例中使用的
std::filesystem::
(上面代码中的fs::
)函数、类和权限枚举提供在顶部。在某些实现中,仅部分支持 C++17,您可能会在
experimental/filesystem
中找到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 arecursive_directory_iterator
and many other handy classes and functions for dealing with filesystem specific things.Example:
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 adirectory_iterator
instead - and collect the subdirectories to examine for later.Example:
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
inexperimental/filesystem
instead. If that's the case, you can replace the abovewith the
#ifdef
jungle I've provided in this answer.