为什么opendir()会随机导致enent

Why is opendir() randomly resulting in ENOENT?

本文关键字:enent 随机 opendir 为什么      更新时间:2023-10-16

为了在Linux Ubuntu上(递归地)检索目录的内容,我使用以下RAII结构体:

struct L_DirectoryReader
{
    //Fields
    L_PathData pathData;
    DIR *dirHandle;
    struct dirent *currentDirEntry;
    //Method declaration happens before constructor,
    //because it is used in the constructor.
    void readDir()
    {
        errno = 0;
        this->currentDirEntry = readdir(this->dirHandle);
        //Error checking
        if(errno != 0)
        {
            int errorcode = errno;
            switch(errorcode)
            {
                default:
                {
                    throw os3util::fileh::exc::FileIOException(
                        std::string("Failed to retrieve contents of dir:")
                        + kNEWLINE_STR + this->pathData.relativePath.getFullPath()
                        + kNEWLINE_STR + "with readdir() errorcode "
                        + os3util::strfunc::intToString(errorcode)
                        + std::string(".")
                    );
                }
            }
        }
    }
    //Constructor
    L_DirectoryReader(const L_PathData& pathData) :
        pathData(pathData),
        dirHandle(),
        currentDirEntry()
    {
        //Obtain file handle
        const char* openDirPathCString = (kSLASH_STR + this->pathData.absolutePath.getFullPath()).c_str();
        errno = 0;
        this->dirHandle = opendir(openDirPathCString);
        //Check file handle validity
        if(this->dirHandle == nullptr)
        {
            int errorCode = errno;
            switch(errorCode)
            {
                case EACCES:
                {
                    throw os3util::fileh::exc::FileIOException(std::string("Could not find directory:") + kNEWLINE_STR + this->pathData.relativePath.getFullPath() + kNEWLINE_STR + std::string("permission denied."));
                } break;
                case ENOENT:
                {
                    throw os3util::fileh::exc::FileIOException(std::string("Could not find directory:") + kNEWLINE_STR + this->pathData.relativePath.getFullPath() + kNEWLINE_STR + std::string("does not exist."));
                } break;
                case ENOTDIR:
                {
                    throw os3util::fileh::exc::FileIOException(std::string("Could not find directory:") + kNEWLINE_STR + this->pathData.relativePath.getFullPath() + kNEWLINE_STR + std::string("is not a directory."));
                } break;
                default:
                {
                    throw os3util::fileh::exc::FileIOException(std::string("Could not find directory:") + kNEWLINE_STR + this->pathData.relativePath.getFullPath() + kNEWLINE_STR + std::string("error code ") + os3util::strfunc::intToString(errorCode) + std::string(" received."));
                }
            }
        }
        else
        {
            try
            {
                this->readDir();
            }
            catch(...)
            {
                closedir(this->dirHandle);
                throw;
            }
        }
    }
    //Destructor
    ~L_DirectoryReader()
    {
        try
        {
            errno = 0;
            closedir(this->dirHandle);
            if(errno != 0)
            {
                int errorcode = errno;
                std::cout << "failed to close dirhandle for directory "" << this->pathData.relativePath.getFullPath() << "" with error code " << errorcode << std::endl;
            }
        }
        catch(...)
        {
            try
            {
                std::cout << "exception occured while closing dir handle" << std::endl;
            }
            catch(...)
            {
                /*Swallow, destructors should never throw*/
            }
        }
    }
};

问题是,我一直得到ENOENT的异常。但并非总是如此。对于同一个目录,我(手动)一次又一次地调用它,有时它按预期打印所有内容(和子目录内容),有时它抛出异常。有时第一次呼叫成功,有时第一次呼叫失败。我根本没有对目录做任何操作,它们只是留在同一个地方,不动。

我发现有类似问题的其他主题没有将errno设置为0,但正如上面的代码所示,我正在这样做。我甚至在将errno设置为0之前构建const char *参数,以防万一。

结构体在函数内部构造。如果抛出任何异常,析构函数激活并关闭DIR句柄。调试证实了这一点。析构函数中的错误消息永远不会发生。唯一可能出错的地方是dirHandle为空,在这种情况下构造函数失败。

当我禁用递归时,它总是对根目录有效,但对根目录的任何子目录都经常失败。根目录为home/myname/os3/serverfiles。我的项目在一个完全不同的目录,所以我总是通过绝对路径访问它。

每次访问这个结构体都是通过完全相同的函数调用。在我的程序中没有任何随机因素

我不知道是什么原因引起的。我特别困惑的是,当文件明显存在时,错误是ENOENT,并且它经常发现它们。

const char* openDirPathCString = (kSLASH_STR + this->pathData.absolutePath.getFullPath()).c_str();

你在一个临时字符串上调用string::c_str(),该字符串在语句结束时被销毁,导致一个悬空指针。