为什么opendir()会随机导致enent
Why is opendir() randomly resulting in ENOENT?
为了在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()
,该字符串在语句结束时被销毁,导致一个悬空指针。
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 为什么 Serial.println(<char[]>);返回随机字符?
- 字符串-C++后显示的随机字符
- 循环中的随机函数
- 在c++构造函数中使用随机字符串生成器
- 使用std::mt19937从字符串中返回一个随机单词
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- 如何在C++中高效地构造随机骰子
- 在类中使用随机生成器时出现性能问题
- 在将数字随机生成为数组期间从内存输出随机数的数组
- 将字符随机转换为大写的函数
- 为什么 vector 的随机访问迭代器给出与指针不同的内存地址?
- 如何生成一个随机的 n 位数,其中 n 是任意的
- 将随机生成的数字添加到数组 + 对这些数组求平均值
- 如何使用要传递给 mt19937 的可选随机种子参数设计函数
- 在C++中随机生成 20 个非重复数字
- GCC:随机构建导致执行期间分段错误
- 如何使用 SML 随机生成八进制元组
- 当我尝试使用它时,Scanf 会抛出一个随机异常(scanf_s 也是如此)
- 为什么opendir()会随机导致enent