I am using below code to create hard link and identify whether the file is having hard link count of 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';
}
This is what i have done below for symlink:
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);
}
}
But, i want to know the file to which hard link is pointing to again preserve it while copying it to new directory by calling create_hardlink.