At least on unix, every directory entry (besides a symlink) is technically "a" hard link. If the link count is greater than 1 then it may have multiple links.
Directories are >1 multiplicity hard links (link count > 1) because they all have a "." hard link pointing to themselves. Dir link counts can also can be > 2 if they have subdirectories whose ".." hard links to them.
Anyway, if you are just looking for a portable way to test multiplicity of linkage, then os.getFileInfo(handle|File|Path).linkCount > 1 is a way. Some Windows expert could speak to the semantics.
To actually identify "what all the names are", however, is much more involved, though cligen/examples/dups.nim has some of the logic. On Unix you can only have "same device" hard links, i.e. not crossing filesystem boundaries.
forum bug, I hadn't seen your post while editing my answer :-)
in any case, let's followup in https://github.com/nim-lang/Nim/pull/17238, hardlinkCount seems more useful.
Though sameFile seems the answer for @RainbowAsteroids, since someone might search the forum and wind up here and since my dups example has way more going on than necessary for just this and is not standalone, here is how one might do a "print sets of referrers" style query:
import os, tables, strutils
type Id = tuple[device: DeviceId, file: FileId]
proc id2sets(paths: seq[string]): Table[Id,seq[string]] =
result = initTable[Id, seq[string]](paths.len)
for path in paths:
try:
let fi = getFileInfo(path, followSymlink=false)
if fi.kind == pcFile: # skip dirs & symlinks
result.mgetOrPut(fi.id, @[]).add(path)
except OSError as e:
stderr.write "getFileInfo failed on: ",path,": ",
e.msg, "\n"
if (let paths = commandLineParams(); paths.len > 0):
for id, set in paths.id2sets:
if set.len > 1: echo set.join("\t") # sort in-row?
else:
echo "Print hardlink sets for all paths on cmd-line"
Exercise for would be user is to extend to os.walkDirRec or elsewise.since my dups example has way more going on than necessary for just this and is not standalone, here is how one might do a "print sets of referrers" style query
that indeed clarifies your initial link cligen/examples/dups.nim
Exercise for would be user is to extend to os.walkDirRec or elsewise.
this would be easy via using filewalks refs https://github.com/nim-lang/fusion/pull/32 (which IMO I should re-open in nim repo instead of fusion, since it's broadly useful, and given recent discussions about fusion)