readlink/realpath -m in busybox

Rob Landley rob at landley.net
Mon Mar 23 08:07:17 UTC 2020


On 3/21/20 8:26 PM, Eli Schwartz wrote:
> On 3/21/20 5:12 AM, Johannes Schauer wrote:
>> Hi,
>>
>> is there existing code that works with busybox and replaces "readlink -m" or
>> "realpath -m" from coreutils?
>>
>> Resources I found online either show solution for "readlink -f" only, when
>> neither readlink nor realpath exists, when Perl is available or when bash is
>> available.
>>
>> Specifically I want to replace the "readlink -m" invocation here [1] by
>> something that works with busybox.
>>
>> Thanks!
>>
>> cheers, josch
>>
>> [1] https://sources.debian.org/src/glibc/2.30-2/debian/debhelper.in/libc.preinst/#L226
> 
> I can't think of one, but you could implement it in pure shell by
> checking the return code of readlink without options (it will fail if
> the file is not a symlink), and walk the path to find the first
> directory that exists in order to support -m. This would be pretty
> compatible with any readlink implementation, including ones without -f.

You could just grab the toybox xabspath() from lib/xwrap.c. It's zero clause BSD
which is convertible to literally any license (public domain equivalent). See
toybox toys/*/readlink.c to see how it's used to implement -m there. Wouldn't be
the first large chunk of toybox code copied into busybox. :)

(As the commit adding that said, ubuntu's -m will happily show
/bin/ls/blah/../potato and the code I wrote considers descending through a file
to be an error even if you back out again, because you can't mkpath -p that so
how is it _useful_?)

Rob

P.S. Or if you just want something fast, the instructions in
https://busybox.net/FAQ.html#backporting also apply to having a mix of busybox
and toybox on the system. And toybox can build any command standalone via "make
defconfig; make readlink". If you need a static target version that would be
export LDFLAGS=--static CROSS_COMPILE=/path/to/prefix- ; make defconfig;
make readlink


More information about the busybox mailing list