2

I have some code that iterates through files in a directory and does useful things with the non-directory files, like so:

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());
    }
}

This works fine in my unit tests, but when I run the full program as a service, my test directories (seemingly erroneously) get past the !bfs::is_directory check and DoUsefulThings's ifstream.good() check fails, with an errno of 13.

I tried changing !bfs::is_directory to bfs::is_regular_file (thinking that maybe there was a system condition causing it to be something else), but I got the same results. The is_regular_file condition fails on the directory in my unit test but passes when run as a service.

I also added a try/catch around my if statement to see if it was throwing an exception and verified that it wasn't (probably could use one anyway, but didn't help with this).

I considered that the problem could be related to the service's permissions level, so I changed the properties of the service to log on as the same account that I use to log in to that system. Same result. I've dabbled with PerformanceMonitor some as well to try to get some clues there, but I haven't gleaned much from it yet.

Can someone suggest why this might be happening? Errno=13 == "permission denied", right? Is there an additional check I need to perform before calling is_directory?

I'm using Windows XP, Visual Studio 2008/C++, version 1.44 of the Boost library, and version 3 of filesystem.

ETA: I added the following to test the directory manually (the direction of the slash marks didn't make a difference), and is_regular_file behaves as expected:

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

I have log statements printing out both *iterDir and iterDir->path() and they both match the one I put in manually. Does this rule out permissions issues? Will continue testing, as this result doesn't really make sense to me yet.

4

2 に答える 2

2

@Ennael:

アクセスしようとしているフォルダのすべての親フォルダ/デバイスノードに対するトラバーサル権限が必要であることを忘れないでください。ローマの提案は、疑いを排除するための最初の列になると思います(もちろん、これは本当に不合理です:) Errno=13 == "permission denied"

そこから次のようなツールで始めることができます

  • cacls.exe

コマンドラインACLのリスト/編集を行うには

  • AccessEnum v1.32は、ファイルシステムツリー全体のアクセス許可の変更を検出します(アクセス許可がより制限されたり、より許可されたりした場合にのみ警告する便利なオプションがあります)
于 2011-10-11T20:55:35.127 に答える
0

ああ。それは私の「Condition2」のバグでした。助けてくれてありがとう。

于 2011-10-12T02:55:10.617 に答える