【发布时间】:2014-06-25 22:57:50
【问题描述】:
为了在 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。
【问题讨论】: