<h2>ZIPFS</h2>

[AndroWish] comes with a special <a virtual file system which uses mmap(2) to read-only map a ZIP file (in this case AndroWish's APK, i.e. its own installation package) into the process address space to speed up startup time and subsequent read accesses. While this file system was designed primarily for AndroWish it can be used on other platforms, too. Namely, [undroidwish] uses it on Windows and Linux to mount an archive of Tcl and native extensions which is appended to the executable portion of its binary. It is implemented in the files <tt><a and <tt><a in AndroWish's folder and enabled in the Tcl core by the presence of the C preprocessor macro <tt>ZIPFS_IN_TCL</tt>.

<h3>Low-level C interface</h3>

<tt>Tclzipfs_Init(Tcl_Interp *interp)</tt>

    Performs one-time initialization of the file system and registers it process wide. Additionally, a package named <i>zipfs</i> is provided and supplemental Tcl commands are created in the given interpreter.

<tt>Tclzipfs_Mount(Tcl_Interp *<i>interp</i>, const char *<i>zipname</i>, const char *<i>mntpt</i>, const char

    Mounts the ZIP archive file on the mount point using the optional ZIP password Errors during that process are reported in the interpreter If is a NULL pointer, information on all currently mounted ZIP file systems is written into result as a sequence of mount points and ZIP file names.

<tt>Tclzipfs_MountBuffer(Tcl_Interp *<i>interp</i>, const char *<i>mntpt</i>, unsigned char *<i>data</i>, int <i>length</i>, int

    Mounts the ZIP archive contained in the memory buffer described by and on the mount point Depending on a private copy of this memory buffer is made and used for the mount operation. Errors during that process are reported in the interpreter If the mount operation succeeds, a string of the form "<tt>memory_&lt;size&gt;_&lt;id&gt;</tt>" is left in result identifying the archive from the memory buffer. This information is useful as parameter in a later unmount operation. If is a NULL pointer, information on all currently mounted ZIP file systems is written into result as a sequence of mount points and ZIP file names.

<tt>Tclzipfs_Unmount(Tcl_Interp *<i>interp</i>, const char

    Undoes the effect of <tt>Tclzipfs_Mount()</tt>, i.e. unmounts the mounted ZIP archive file Errors are reported in the interpreter

<h3>Tcl commands</h3>

The <tt>zipfs</tt> package provides Tcl with the ability to mount the contents of a ZIP file as a virtual file system.

<tt>zipfs::exists

    Return 1 if the given exists in the mounted zipfs and 0 if it does not.

<tt>zipfs::find

    Recursively lists files including and below the directory The result list consists of relative path names starting from the given directory. This command is also used by the <tt>zipfs::mkzip</tt> and <tt>zipfs::mkimg</tt> commands.

<tt>zipfs::info

    Return information about the given in the mounted zipfs. The information consists of (1) the name of the ZIP archive file that contains the file, (2) the size of the file after decompression, (3) the compressed size of the file, and (4) the offset of the compressed data in the ZIP archive file.

    Note: querying the mount point gives the start of ZIP data offset in (4), which can be used to truncate the ZIP info off an executable.

    Note: the file of a mounted ZIP archive appears as directory but can  be opened and read like a regular file if the mount process detected a non archive area in front of the  ZIP archive,  e.g. when the ZIP archive was appended to an executable file. In this case that area can be read using the Tcl <tt>open</tt> and <tt>read</tt> commands but <tt>file copy</tt> treats the mounted archive as a directory.

<tt>zipfs::list

    Lists files of any or all of the mounted ZIP archives. If is omitted all files are listed. Otherwise is interpreted as a glob or regexp pattern and used to list only files matching this pattern.

<tt>zipfs::lmkimg <i>outfile inlist</i> ?<i>password</i>?

    Like <tt>zipfs::mkimg</tt> but instead of an input directory must be a list where the odd elements are the original input file names as copied into the archive and the even elements their respective names within the archive.

<tt>zipfs::lmkzip <i>outfile inlist</i>

    Like <tt>zipfs::mkzip</tt> but instead of an input directory must be a list where the odd elements are the original input file names as copied into the archive and the even elements their respective names within the archive.

<tt>zipfs::mkimg <i>outfile indir</i> ?<i>strip</i>? ?<i>password</i>?

    Create an image (potentially a new executable file) similar to <tt>zipfs::mkzip</tt>. If the parameter is specified, this file is prepended in front of the ZIP archive, otherwise the file returned by <tt>Tcl_NameOfExecutable(3)</tt> (i.e. the executable file of the running process) is used. If the parameter is not empty, an obfuscated version of that password is placed between the image and ZIP chunks of the output file and the contents of the ZIP chunk are protected with that password.

    Caution: highly  experimental, not usable on Android, only partially tested on Linux and Windows.

<tt>zipfs::mkkey

    For the clear text argument an obfuscated string  version is returned with the same format used in the <tt>zipfs::mkimg</tt> command.

<tt>zipfs::mkzip <i>outfile indir</i> ?<i>strip</i>?

    Creates a ZIP archive file named from the contents of the input directory (contained regular files only) with optional ZIP password While processing the files below the optional prefix given in is stripped off the beginning of the respective file name.

    Caution: the choice of the parameter (less the optional prefix) determines the later root name of the archive's content.

<tt>zipfs::mount ?<i>zipfile</i> ?<i>mountpoint</i>?

<tt>zipfs::mount -file <i>zipfile mountpoint</i>

<tt>zipfs::mount -- <i>zipfile mountpoint</i>

    This command mounts a ZIP archive file as a VFS. After this command  executes, files contained in will appear to Tcl to be regular files at the mount point.

    In the first command form, with no returns the mount point for With no return all zipfile/mount point pairs. If is specified as an empty string, the mount point will be the current directory. If is specified, files from are decrypted using this password when read.

<tt>zipfs::mount -data <i>bytearray

    The data in must represent a ZIP archive which gets mounted on If the mount operation succeeds, the result is a string of the form "<tt>memory_&lt;size&gt;_&lt;id&gt;</tt>" which can later be used as parameter in an unmount operation.

<tt>zipfs::mount -chan <i>channelId

    A ZIP archive is read from channel and mounted on If the mount operation succeeds, the result is a string of the form "<tt>memory_&lt;size&gt;_&lt;id&gt;</tt>" which can later be used as parameter in an unmount operation.

<tt>zipfs::unmount <i>zipfile<i></tt>

    Unmounts the mounted ZIP archive file

<tt>zipfs::unwrap

    If is the root of a mounted ZIP archive its content is unpacked to a local directory named <tt>filename.vfs</tt>. This directory must not exists prior to the call. Otherwise, is temporarily mounted before the unpack operation takes place and unmounted afterwards. If is omitted the result of <tt>info nameofexecutable</tt> is used instead, i.e. the main ZIP archive of the running process is unpacked.

The commands described above are available as subcommands in the <tt>zipfs</tt>
ensemble, i.e. <tt>zipfs list</tt> is equivalent to <tt>zipfs::list</tt>.

<h3>zipfs as Tcl (and Tk) bootstrap file system</h3>

On the Android platform zipfs is used to boot Tcl/Tk from the APK by early mounting the APK file on the file system root as seen by Tcl. Since nearly all relevant files within the APK are below the <tt>assets</tt> folder, this lets Tcl see the directory with its library directories, e.g. the directory with Tcl's library modules, encoding tables etc. That relationship to is hard coded into the Tcl shared library and based on it all other packaged library directories can be found during Tcl initialization.

For standalone apps a similar approach is chosen by hard coding the file as the file to be sourced (if present) right after Tcl's initialization. This allows for packaging Tcl based apps as an APK, see the description in [AndroWish SDK] for instructions.

On other platforms (currently tested Linux and Windows) the initial mount of an embedded ZIP file system is done on the executable itself, e.g. if is the Tcl/Tk binary with an included ZIP file system, the Tcl library directory of the file system when mounted becomes Similarly, built in application code will be started from the file if present. Additionally, the contents of the optional file are appended to the command line before Tk is initialized and control is transferred to the <tt>main.tcl</tt> script. This is useful to setup certain aspects of SDL, e.g. to start in full screen mode with or without changed display resolution (see description of SDL startup options in [Beyond AndroWish]). Another hook is which (if present) should be a Windows BMP 24 bit RGB bitmap file used as the icon for the SDL root window.

On Windows platforms the drive letter of the base executable is prepended to the respective path names. For the example above this means: is the binary, becomes the Tcl library directory, is the optional application script, and so on.

<h3>Some delicate implementation details</h3>

For loading binary Tcl extensions (shared libraries) on certain platforms (Linux and FreeBSD) special handling is tried to be carried out:

  *  on Linux, the <b>memfd_create</b> system call is used (if available) to make a memory backed file with the payload in the namespace which finally is <b>dlopen</b>'ed to provide the shared library.

  *  on FreeBSD, there's <b>fdlopen</b> which allows a file descriptor to be treated as a shared library. Very similar to the Linux approach, the file descriptor refers to a memory backed file which is primed with the contents for the shared library from the ZIP archive.

For improving <b>glob</b> operations the ZIP virtual file system uses two hash based data structures: <b>ZipEntry</b> for regular files and <b>ZipDirEntry</b> for directories which additionally contains a hash table to accelerate lookups in this directory. For typical searches, this usually outperforms the native OS functions.