提问人:Witty Apps 提问时间:10/2/2023 最后编辑:Witty Apps 更新时间:10/2/2023 访问量:63
如何在 c++ 中检查文件路径是否为硬链接并在复制到其他目录时保留它
How to check whether the file path is a hard link and preserve it while copying to other directory in c++
问:
我正在使用下面的代码来创建硬链接并确定文件的硬链接计数是否为 2。
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main()
{
// On a POSIX-style filesystem, each directory has at least 2 hard links:
// itself and the special member pathname "."
fs::path p = fs::current_path();
std::cout << "Number of hard links for current path is "
<< fs::hard_link_count(p) << '\n';
// Each ".." is a hard link to the parent directory, so the total number
// of hard links for any directory is 2 plus number of direct subdirectories
p = fs::current_path() / ".."; // Each dot-dot is a hard link to parent
std::cout << "Number of hard links for .. is "
<< fs::hard_link_count(p) << '\n';
}
这是我在下面为符号链接所做的:
else if (fs::is_symlink(srcFilePath)){
std::cout<<srcFilePath;
std::filesystem::path p(srcFilePath);
cpSoftLink(srcFilePath, dstFilePath);
}
void cpSoftLink(const fs::path & srcPath, const fs::path & dstpath){
// if symbolic link already exists then don't create it again
if ( !fs::exists(dstpath) ) {
fs::create_symlink(fs::read_symlink(srcPath),dstpath);
}
}
但是,我想知道硬链接指向的文件再次保留它,同时通过调用将其复制到新目录。create_hardlink
答: 暂无答案
评论
NTQueryInformationFile()
FileHardLinkInformation