如何在目录上迭代并识别或省略NTFS连接(符号链接ish)
How can I iterate over a directory and identify or omit NTFS junctions (symlink-ish)
我有一些代码来列出目录中的文件。对于Windows系统,我希望最终能得到一个文件和文件夹列表,与您在Windows资源管理器中看到的内容相匹配。例如,当我在Server 2016上列出C:\时,我希望拥有Users
文件夹,而不是Documents and Settings
连接。目前我两者都有,没有明显的区分方法。
我当前的代码如下:
boost::filesystem::directory_iterator itr(dir);
boost::filesystem::directory_iterator end;
Poco::SharedPtr<Poco::JSON::Array> fileList(new Poco::JSON::Array);
for (; itr != end; ++itr) {
boost::filesystem::path entryPath = itr->path();
Poco::File file(entryPath.string());
// ...
我尝试了PocoisLink()
方法,但对于连接,它返回false。
我还尝试了Poco::DirectoryIterator
,它给出了与Boost相同的行为,以及Poco::SortedDirectoryIterator
,它在读取C:
时总是抛出File access error: sharing violation: pagefile.sys
。
理想情况下,此代码应包括Linux和MacOS系统上的符号链接,而忽略Windows上的连接。
以下是我最终想到的。这不是一个完美的解决方案——它更像是一个启发式而不是一个合适的标识符——但它似乎对我的用例来说足够好:
#ifdef _WIN32
#include <windows.h>
#endif
bool FileController::isNtfsJunction(const std::string& dirPath) const {
#ifdef _WIN32
DWORD attrs = GetFileAttributesA(dirPath.c_str());
if (INVALID_FILE_ATTRIBUTES == attrs) {
DWORD err = GetLastError();
logger.error("Could not determine if path is NTFS Junction: %s. Error: %s", dirPath, err);
return false;
}
return attrs & FILE_ATTRIBUTE_DIRECTORY &&
attrs & FILE_ATTRIBUTE_REPARSE_POINT &&
attrs & FILE_ATTRIBUTE_HIDDEN &&
attrs & FILE_ATTRIBUTE_SYSTEM;
#else
return false;
#endif
}
相关文章:
- 当套接字连接断开时检测C/C++Unix
- 无法在windows上使用mingw将sqlite3与c连接
- 到连接组件算法的问题(递归)
- QTcpSocket在不阻塞GUI的情况下重新连接到服务器
- 无法在C++中建立与MySQL数据库的连接
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 连接 dockerized 模型和 dockerized 数据库时出现"无法 SQLConnect"错误
- 使用 bfs 解决连接组件问题时得到错误的答案
- 在C++的两个字符串中连接以逗号分隔的唯一值
- 如何修复valgrind启动时的致命错误(与libc6-dbg和libc6-dbg:i386连接)
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- OSX蓝牙打开RFCOMMChannelAsync声称已连接,但未建立连接,并且从未调用过委托
- 在 libcurl 连接池中预创建连接
- 套接字连接"Operation not permitted"错误,甚至使用升压/平发器根.cpp
- 以非特权用户身份查询 NTFS 特殊文件的元数据?
- libcurl 和 DNS ttl 中的内部连接管理
- 如何应用 libcurl 的持久连接选项
- 提升 ASIO - io_service 不要等待连接到线程
- 如何在目录上迭代并识别或省略NTFS连接(符号链接ish)