#Hardlinking during the install process with std.Build/build.zig

1 messages · Page 1 of 1 (latest)

lethal island
#

I'd like to install multiple copies of my binary, hard-linked with different names. This is common for compression tools, e.g. xz/xzcat/unxz..., etc. are all the same binary (and point to the same inode) but look at their program name before deciding what to do (same with zstd/unzstd/zstdcat/..., bzip2/bzcat/..., gzip/gzcat/... and so on). Is this possible with std.Build? I see how to install multiple copies of the same binary (I think I do anyway), but ideally I'd install one copy, and hard-link the others to it. Thanks.

random patrol
#

you can use b.addSystemCommand to execute the ln command.

#

Also afaik the examples you picked are symlinked and not hardlinked

lethal island
#

they're hardlinked on my machine

#

try stat $(realpath $(which xz)) and stat $(realpath $(which unxz))

#

the second number is the inode, and you'll see it probably matches

blissful seal
#

realpath follows the symlink

lethal island
#

yeah, i know.

#

that's because usually it's symlinked from where it's installed to somewhere on your path

#

but the actual things that are installed are hardlinked together

#

symlinking would be tricky on windows for various reasons. as would using the ln command. i guess zigs stdlib doesnt have hardlinking in it though 😦

blissful seal
#

no, std.fs also doesn't support stuff like O_NOFOLLOW, you need to use posix API

lethal island
#

hardlink is pretty portable (more than symlinking) so it's a surprising absense

blissful seal
#
$ stat -c "%i" $(realpath $(which xz)) $(realpath $(which xzcat))
12027
12027
$ stat -c "%i" $(which xz) $(which xzcat)
12027
1949
$ ls -ld $(which xz) $(which xzcat)
-rwxr-xr-x. 1 root root 91200 Mar 27  2024 /usr/bin/xz
lrwxrwxrwx. 1 root root     2 Mar 27  2024 /usr/bin/xzcat -> xz

^ symlinks. realpath makes it look otherwise

lethal island
#

that matches what i said

#

that's because usually it's symlinked from where it's installed to somewhere on your path
but the actual things that are installed are hardlinked together

blissful seal
#

the actual things that are installed is xz, a file, and xzcat, which is a symlink to xz.

lethal island
#

ah, sorry i see.

#

on my machine they're actually hardlinks

blissful seal
#

that's fair. stat $(which xz) would also suggest that by showing the number of Links