[英]How can I detect if two file entries refer to the same physical file system with Qt?
I have a link to the file and an actual file. 我有一个指向文件和实际文件的链接。 How can I detect if both of them refer to the same physical location? 如何检测两个人是否都指向同一物理位置?
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
bool isSameFileSystem(QString path1, QString path2)
{
struct stat stat1, stat2;
QFileInfo fi1(path1);
QFileInfo fi2(path2);
stat(fi1.absoluteDir().absolutePath().toUtf8().constData(), &stat1);
stat(fi2.absoluteDir().absolutePath().toUtf8().constData(), &stat2);
return stat1.st_dev == stat2.st_dev;
}
There is a way to detect whether or not the given file spec is a symbolic link spec or a real file system object: 有一种方法可以检测给定的文件规范是符号链接规范还是真实的文件系统对象:
bool point2Same(QString path1, QString path2)
{
QFileInfo fi1(path1);
QFileInfo fi2(path2);
QString p1, p2;
if (fi1.isSymLink())
p1 = fi1.symLinkTarget();
else
p1 = fi1.absolutePath();
if (fi2.isSymLink())
p2 = fi2.symLinkTarget();
else
p2 = fi2.absolutePath();
return p1 == p2;
}
You can either scan a QFileSystemModel, or perhaps subclass it? 您可以扫描QFileSystemModel,也可以对其进行子类化? It's a quick way to pull in a file system from a specified root and have access to the tree & its properties. 这是从指定的根目录引入文件系统并可以访问树及其属性的快速方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.