我有一些代码遍历目录中的文件并对非目录文件执行有用的操作,如下所示:
namespace bfs = boost::filesystem;
for (bfs::directory_iterator iterDir(m_inPath);
bContinue && iterDir!=bfs::directory_iterator(); iterDir++)
{
std::string filename = iterDir->path().filename().string();
boost::to_lower(filename);
if (!bfs::is_directory(*iterDir) && Condition2(filename)) {
std::ifstream ifFile(iterDir->path().string().c_str());
DoUsefulThings(iterDir());
}
}
这在我的单元测试中运行良好,但是当我将完整的程序作为服务运行时,我的测试目录(看似错误地)通过了!bfs::is_directory
检查并且DoUsefulThings
'ifstream.good()
检查失败,errno 为 13。
我尝试更改!bfs::is_directory
为bfs::is_regular_file
(认为可能存在系统条件导致它成为其他东西),但我得到了相同的结果。该is_regular_file
条件在我的单元测试中的目录上失败,但在作为服务运行时通过。
我还在 if 语句周围添加了一个 try/catch 以查看它是否抛出异常并验证它不是(可能无论如何都可以使用一个,但对此没有帮助)。
我认为问题可能与服务的权限级别有关,因此我更改了服务的属性以使用我用于登录该系统的相同帐户登录。结果相同。我也尝试过一些 PerformanceMonitor 以尝试在那里找到一些线索,但我还没有从中收集到太多信息。
有人可以建议为什么会发生这种情况吗?Errno=13 == “权限被拒绝”,对吧?在调用 is_directory 之前我需要执行额外的检查吗?
我使用的是 Windows XP、Visual Studio 2008/C++、Boost 库 1.44 版和文件系统 3 版。
ETA:我添加了以下内容来手动测试目录(斜线标记的方向没有影响),并且 is_regular_file 的行为符合预期:
std::string strDir = "D:/Dir1/Dir2/Dir3/Dir4/Dir5\\Dir6";
if (bfs::is_regular_file(strDir))
LOG("It's a regular file"); //This does not get executed
else
LOG("Not a regular file"); //This does
我有打印出 *iterDir 和 iterDir->path() 的日志语句,它们都与我手动输入的相匹配。这是否排除了权限问题?将继续测试,因为这个结果对我来说还没有意义。