代码之家  ›  专栏  ›  技术社区  ›  Megidd

操作系统。Stat:permission denied=>error(syscall.Errno)EACCES(13)

go
  •  -1
  • Megidd  · 技术社区  · 3 年前

    我正在一个文件夹中创建一个目录

    // The directory which is going to be created next to unit test executable.
    const DirName string = "test-files"
    

    正在创建目录:

        // Create the directory if doesn't already exist.
        err := os.MkdirAll(DirName, os.ModeDir)
        if err != nil {
            return err
        }
    
        // Compose a sample file path to double-check its existence.
        pth := DirName + string(os.PathSeparator) + "samplefile.txt"
    
    
        exists, err := doesExist(pth)
        if err != nil {
            return err
        }
    

    以及检查文件是否存在:

    
    // Does file exist?
    func doesExist(pth string) (bool, error) {
    
        // Check file existence
        // https://stackoverflow.com/a/10510718/3405291
        if _, err := os.Stat(pth); err != nil {
            if os.IsNotExist(err) {
                return false, nil
            } else {
                return true, err
            }
        }
    
        // Above conditions are skipped,
        // therefore file exists.
        return true, nil
    }
    

    上述代码返回此错误:

    错误(syscall.Errno)EACCES(13)

    我可以再次检查目录是否已实际创建。但是权限是有限的 d---------

    > ls -lh
    d--------- 2 m3 users    6 Dec 23 20:30 test-files
    

    如何创建具有相应权限的目录?

    1 回复  |  直到 3 年前
        1
  •  1
  •   kostix    3 年前

    你在滥用法律 os.ModeDir 常数它被发明用作检查文件是否正确的位掩码 mode+permissions bits os.(*File).Stat

    创建目录的默认权限模式位为 0777 但是,它们受到影响 umask .