home | help
UNHARE(1)		     General Commands Manual		       UNHARE(1)

NAME
     unhare -- extract the filesystem bundled into a Bun standalone binary

SYNOPSIS
     unhare [-flv] [-o pattern] [-s section] [-x pattern] file [pattern ...]
     unhare -t [-flv] [-o pattern] [-s section] [-x pattern] [pattern ...]
     unhare -h

DESCRIPTION
     The  unhare  utility  unpacks  the  modules that bun(1) embeds into an exe-
     cutable produced by bun build --compile, writing each of them out as a reg-
     ular file.

     The bundle is located by asking elf(3) for the ".bun" section of file, so a
     payload that is not the last thing in the file (one followed by  a  section
     header table, a symbol table or debugging information) is found just as re-
     liably as one appended to the end.  Section names are not unique in ELF, so
     every  section  called  ".bun"  is gathered, and each bundle among them un-
     packed in turn.  Module names are a bundle's own business and nothing keeps
     them apart across bundles, so each is given a directory of its  own,  named
     by  the  -o  pattern.   Should  no such section hold one, as happens with a
     stripped executable, with the older bun(1) releases  that	merely	concate-
     nated  the  payload,  or with a Mach-O or PE binary built for another plat-
     form, unhare scans the whole file for  the  bundle  trailer  instead.   The
     trailer  search  knows nothing of ELF, so a binary of any format unpacks so
     long as the bundle is in it somewhere.

     Should elf_begin(3) itself fail, which takes  an  exhausted  system  rather
     than  an  unusual file, unhare says so and stops.	It does not scan in that
     case: the bytes it would scan are the ones libelf could not get hold of.

     Module names are taken from Bun's virtual filesystem,  whose  /$bunfs/  and
     B:\~BUN\  prefixes  are  stripped before the name is appended to the output
     directory.  A name that would place a file outside that  directory  is  re-
     fused and the module is skipped with a warning.

     An existing file is never replaced unless -f is given.

     Neither  is  a  symbolic  link  followed, with -f or without it, whether it
     stands for the file itself or for a directory along the way.  A  link  left
     in  the  output directory would otherwise carry the write outside it, which
     is the very thing refusing .. in a name is meant to prevent.  The directory
     named by -o is the user's own and is exempt; only what lies beneath  it  is
     held to this.

     Names  are printed with their control characters escaped, as `\033' and the
     like.  A name out of the bundle is not to be trusted with	a  terminal:  an
     escape  sequence  among  them  can rewrite what is shown, and a newline can
     forge an entry in a listing meant to be read a line at a time.  Bytes at or
     above 0x80 are left alone, so UTF-8 names stay legible.

     Each module records the encoding of its contents.	Text that does	not  fit
     in  Latin-1 is bundled as UTF-16LE, and unhare converts it back to UTF-8 on
     the way out, so that the extracted file reads the way the	original  source
     did.   Contents  recorded	as  Latin-1 or as binary are written through un-
     changed.

     When one or more pattern operands follow file, only files matching at least
     one of them are extracted.  With no patterns every file is  selected.   The
     patterns  are  shell globs with the same bundle-relative names and matching
     rules described for -x below.  An exclusion takes precedence  when  a  file
     matches  both.   With  -t,  which supplies its input binary implicitly, the
     patterns follow the options.

     The options are as follows:

     -f      Overwrite files that are already there.  Without it, unhare  writes
	     nothing  at  all  when  any of the files it would create exists al-
	     ready, so that a run which would clobber something leaves	the  di-
	     rectory  as  it  found it rather than stopping partway through with
	     the job half done.

     -h      Print the usage to standard output and exit successfully.

     -l      List the files that would be written, one to a line, and write none
	     of them.  Nothing is created, not even the output directory, and an
	     existing file in the way is neither an error  nor	worth  remarking
	     on,  there  being	nothing  to overwrite.	The names are the ones a
	     real run would produce, output directory and all, so that	what  is
	     listed is what would appear.  A single -v annotates them, the list-
	     ing being the point already.

     -o pattern
	     Write the extracted files below the directory pattern names, creat-
	     ing  it  and  any missing parents if necessary.  The default is the
	     current directory.

	     A single printf(3) conversion in pattern is replaced by  the  index
	     of  the  section  a  bundle came from, so that -o out%u unpacks the
	     bundle in section 29 into out29.  Only the integer conversions  are
	     allowed,  with  their  flags  and	widths; anything else is refused
	     rather than handed on to printf(3).  A bundle  found  by  scanning,
	     rather than in a section, answers to zero.  When more than one bun-
	     dle  is  to be unpacked a pattern without a conversion is an error,
	     naming as it does a single place for all of them.

     -s section
	     Unpack only the bundle in the given section,  which  may  be  given
	     more than once to name several.  The default is to unpack every one
	     of them.

     -t      Unpack  the  bundle built into unhare itself, rather than one named
	     on the command line, and  take  no  file  operand.   Any  remaining
	     operands  are  selection  patterns.  The program to read is the one
	     named by argv[0], or, when that names no readable file, the one the
	     kernel reports for this process.  The bundle holds the tests direc-
	     tory of the distribution, which  make  test  unpacks  and	compares
	     against the original.

     -v      Be  verbose.   Given once, write the name of each extracted file to
	     standard output, one to a line, and report on standard error  where
	     the  bundle was found and how much was written.  Given twice, anno-
	     tate each name with the number of bytes written, the loader, module
	     format, encoding and side, and report the bundle header as well.

	     The listing goes to standard output and everything else to standard
	     error, so that a single -v leaves a list of names fit to  pipe  on-
	     ward.

     -x pattern
	     Skip  the	files whose names match pattern, which may be given more
	     than once to name several.  A file matching any of them is  neither
	     written nor listed, as though the bundle did not hold it.

	     pattern is a shell glob, matched by fnmatch(3) against the name the
	     bundle records, with the virtual filesystem prefix stripped and be-
	     fore  the -o directory is prepended -- the name -l lists, less that
	     directory.  The rules are those of the --exclude option of  tar(1),
	     so  a  pattern  written for that one means the same here: `*' spans
	     the separators, so that `*.node' reaches an entry however deeply it
	     is buried, and each pattern  is  tried  again  at	every  component
	     boundary, so that `node_modules/*' catches a nested tree as readily
	     as a top-level one.

	     A	file  already in the way of an entry that -x skips is in the way
	     of nothing, and no longer a reason to refuse the whole extraction.

EXIT STATUS
     unhare exits 0 on success.   A  failure  exits  with  whichever  code  from
     <sysexits.h> fits it:
	   EX_USAGE	 An unknown option, or the wrong number of operands.
	   EX_NOINPUT	 The named file cannot be read at all.
	   EX_DATAERR	 It can, but holds no bundle that can be made sense of.
	   EX_CANTCREAT  The output cannot be laid down, an existing file in the
			 way among the reasons.
	   EX_IOERR	 Writing the output failed partway through.
	   EX_OSERR	 The  system denied us memory, or an answer it was asked
			 for.
	   EX_SOFTWARE	 The ELF library is too old to talk to.

EXAMPLES
     Unpack an application into ./app-files, listing what is written:

	   unhare -v -o app-files ./app

     See what would be written, and where, without writing it:

	   unhare -l -o app-files ./app

     Unpack a binary carrying more than one bundle, each below the number of the
     section it came from:

	   unhare -o 'app%u' ./app

     Unpack only the bundle in section 29:

	   unhare -s 29 -o app-files ./app

     Unpack an application without its native addons, and without anything  bun-
     dled from a node_modules tree:

	   unhare -o app-files -x '*.node' -x 'node_modules/*' ./app

     Unpack only JavaScript modules and configuration files:

	   unhare -o app-files ./app '*.js' '*.json'

     Unpack the bundle this program carries:

	   unhare -t -o /tmp/unhare-tests

DIAGNOSTICS
     unhare carries a bundle of its own, in its own ".bun" section, so that make
     test  can	check  a  freshly built program against a known answer.  Running
     unhare on its own executable, with or without -t,	therefore  unpacks  that
     bundle rather than reporting that nothing was found.

     Each  directory  of the bundled tree is stored under the Bun encoding it is
     named for, so that one tree  exercises  all  three:  US-ASCII  as	Latin-1,
     UTF-16  as  UTF-16LE,  and  anything else as binary, which is what a legacy
     eight-bit encoding such as KOI8-U needs if its bytes  are	to  survive  the
     round trip.

     The  files  under UTF-16 are themselves UTF-8 on disk.  The conversion hap-
     pens on the way into the bundle and is undone on the way out, so the  round
     trip returns them unchanged; a file already in UTF-16 could not round-trip,
     this program's business being to hand back decoded UTF-8.

COMPATIBILITY
     The  encoding  recorded  as  value  2 in a module entry holds UTF-16LE code
     units, not UTF-8; unhare reports it as "utf16".  Tools that follow an older
     description of this format name it "utf8" and write its contents out verba-
     tim, which yields text files no other program can read.

SEE ALSO
     tar(1),   elf(3),	 elf_getshdrstrndx(3),	 fnmatch(3),	gelf_getshdr(3),
     printf(3)

AUTHORS
     Mikhail Teterin

CAVEATS
     Bytecode, module metadata and source maps stored alongside a module are re-
     ported  under  -v but are not written out; only the module contents are ex-
     tracted.

FreeBSD ports 15.quarterly	 August 31, 2026		       UNHARE(1)

home | help