{"id":10103,"date":"2011-07-20T07:00:00","date_gmt":"2011-07-20T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2011\/07\/20\/how-do-i-find-the-original-name-of-a-hard-link\/"},"modified":"2011-07-20T07:00:00","modified_gmt":"2011-07-20T07:00:00","slug":"how-do-i-find-the-original-name-of-a-hard-link","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20110720-00\/?p=10103","title":{"rendered":"How do I find the original name of a hard link?"},"content":{"rendered":"<p>\nA customer asked,\n&#8220;Given a hardlink name,\nis it possible to get the original file name used\nto create it in the first place?&#8221;\n<\/p>\n<p>\nRecall that hard links create an alternate name for a file.\nOnce that alternate name is created, there is no way to tell\nwhich is the original name and which is the new name.\nThe new file does not have a &#8220;link back to the original&#8221;;\nthey are both links to the underlying file content.\nThis is\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2009\/09\/28\/9900082.aspx\">\nan old topic<\/a>,\nso I won&#8217;t go into further detail.\nThough this question does illustrate that many people\ncontinue to misunderstand what hard links are.\n<\/p>\n<p>\nAnyway, once you figure out what the customer is actually asking,\nyou can give a meaningful answer:\n&#8220;Given the path to a file,\nhow can I get all the names\nby which the file can be accessed?&#8221;\nThe answer is\n<code>Find&shy;First&shy;File&shy;NameW<\/code>.\n<\/p>\n<p>\nNote that the names returned by the\n<code>Find&shy;First&shy;File&shy;NameW<\/code>\nfamily\nof functions are relative to the volume mount point.\nTo convert it to a full path, you need to append it to the mount point.\nSomething like this:\n<\/p>\n<pre>\ntypedef void (*ENUMERATEDNAMEPROC)(__in PCWSTR);\nvoid ProcessOneName(\n    __in PCWSTR pszVolumeRoot,\n    __in PCWSTR pszLink,\n    __in ENUMERATEDNAMEPROC pfnCallback)\n{\n  wchar_t szFile[MAX_PATH];\n  if (SUCCEEDED(StringCchCopy(szFile, ARRAYSIZE(szFile), pszVolumeRoot)) &amp;&amp;\n      PathAppend(szFile, pszLink)) {\n   pfnCallback(szFile);\n  }\n}\nvoid EnumerateAllNames(\n    __in PCWSTR pszFileName,\n    __in ENUMERATEDNAMEPROC pfnCallback)\n{\n \/\/ Supporting paths longer than MAX_PATH left as an exercise\n wchar_t szVolumeRoot[MAX_PATH];\n if (GetVolumePathName(pszFileName, szVolumeRoot, ARRAYSIZE(szVolumeRoot))) {\n  wchar_t szLink[MAX_PATH];\n  DWORD cchLink = ARRAYSIZE(szLink);\n  HANDLE hFind = FindFirstFileNameW(pszFileName, 0, &amp;cchLink, szLink);\n  if (hFind != INVALID_HANDLE_VALUE) {\n   ProcessOneName(szVolumeRoot, szLink, pfnCallback);\n   while (cchLink = ARRAYSIZE(szLink),\n          FindNextFileNameW(hFind, &amp;cchLink, szLink)) {\n    ProcessOneName(szVolumeRoot, szLink, pfnCallback);\n   }\n   FindClose(hFind);\n  }\n }\n}\n\/\/ for demonstration purposes, we just print the name\nvoid PrintEachFoundName(__in PCWSTR pszFile)\n{\n _putws(pszFile);\n}\nint __cdecl wmain(int argc, wchar_t **argv)\n{\n for (int i = 1; i &lt; argc; i++) {\n  EnumerateAllNames(argv[i], PrintEachFoundName);\n }\n return 0;\n}\n<\/pre>\n<p>\n<b>Update<\/b>: Minor errors corrected, as noted by\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/07\/20\/10188033.aspx#10188208\">\nacq<\/a> and\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/07\/20\/10188033.aspx#10188232\">\nAdrian<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer asked, &#8220;Given a hardlink name, is it possible to get the original file name used to create it in the first place?&#8221; Recall that hard links create an alternate name for a file. Once that alternate name is created, there is no way to tell which is the original name and which is [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-10103","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer asked, &#8220;Given a hardlink name, is it possible to get the original file name used to create it in the first place?&#8221; Recall that hard links create an alternate name for a file. Once that alternate name is created, there is no way to tell which is the original name and which is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/10103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=10103"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/10103\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=10103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=10103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=10103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}