Golang Chmod文件和目录递归

发布于 2025-02-14 01:07:30 字数 1490 浏览 5 评论 0原文

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

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

发布评论

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

评论(1

眼眸里的快感 2025-02-21 01:07:31

您的代码在chmodrec中不检查err参数。这是官方文档的摘录:

Walkdir在两种情况下用非nil Err参数调用该函数。

首先,如果根目录上的初始fs.Stat失败,则Walkdir
将路径设置为root,d设置为nil的函数,然后将ERR设置为
fs.Stat。

的错误

第二,如果目录的readdir方法失败,Walkdir呼叫
将路径设置为目录路径的功能,d设置为
fs.direntry描述目录,而err设置为从
readdir。在第二种情况下,该功能与
目录的路径:第一个呼叫是在目录读取之前
尝试并已设置为零,使功能有机会
返回skipdir,完全避免使用readdir。第二个电话是
失败后,ReadDir并报告了ReadDir的错误。 (如果readdir
成功,没有第二个电话。)

将此代码添加到函数的开头。它可以给您一个提示:

func ChmodRec(path string, di fs.DirEntry, err error) error {
        if err != nil {
            log.Fatal(err)
        }

Your code does not check err argument in ChmodRec. This is an extract from official documentation:

WalkDir calls the function with a non-nil err argument in two cases.

First, if the initial fs.Stat on the root directory fails, WalkDir
calls the function with path set to root, d set to nil, and err set to
the error from fs.Stat.

Second, if a directory's ReadDir method fails, WalkDir calls the
function with path set to the directory's path, d set to an
fs.DirEntry describing the directory, and err set to the error from
ReadDir. In this second case, the function is called twice with the
path of the directory: the first call is before the directory read is
attempted and has err set to nil, giving the function a chance to
return SkipDir and avoid the ReadDir entirely. The second call is
after a failed ReadDir and reports the error from ReadDir. (If ReadDir
succeeds, there is no second call.)

Add this code to the beginning of the function. It can give you a hint:

func ChmodRec(path string, di fs.DirEntry, err error) error {
        if err != nil {
            log.Fatal(err)
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文