[ Team LiB ] Previous Section Next Section

Virtual File Systems

The key concept in Tclkit and Starkits is the virtual file system (VFS). You may be familiar with the file system interface inside a Unix operating system that makes everything look the same (files, tape drives, network sockets, pipes). The nice thing about Unix is that a system programmer can use the same APIs to access all of these things. The goal of the Tcl VFS interface is similar in spirit: use the regular Tcl file system interface to make things like embedded databases, FTP servers, and zip files available to the Tcl programmer. The VFS layer in Tcl 8.4 is implemented below the Tcl C APIs for file system access (e.g., Tcl_CreateChannel, Tcl_FSDeleteFile). The result is that scripting commands (e.g., open, file, glob) and any C extensions that use these APIs automatically access any Virtual File Systems that are part of the Starkit.

The virtual file system is mounted on a regular file; by default it is mounted on the Starkit. For example, if the Starkit is named foo.kit, and its virtual file system contains a file named main.tcl, then it is visible to the Tcl application as foo.kit/main.tcl. The VFS can contain a whole directory structure (e.g., foo.kit/lib/httpd.tcl or foo.kit/htdocs/help/index.html.)

The next section explores some simple Starkits and their file system structure. The main idea is that the Starkit file itself is the root of the virtual file system hierarchy, and everything in the virtual file system is visible to Tcl via the regular scripting commands. If the VFS supports it, you can create and write files as well as read them.

Tclkit includes the TclVFS extension that exposes the ability to implement new file systems in Tcl. Ordinarily you do not need to use the vfs API directly when using a Starkit. However, the TclVFS project has created a number of VFS implementations that let you access web sites, FTP sites, zip files, tar files, and more through the filesystem interface. Tclkit does not include all of these, but you can get them as part of the TclVFS extension. Its home page is

http://sourceforge.net/projects/tclvfs

Accessing a Zip File Through a VFS

Tclkit includes a zipvfs package that lets you mount a compressed ZIP file archive and read its contents. This is currently limited to read-only access. Example 22-1 uses the vfs::zip::Mount command to set up the VFS access. If you use other VFS types supplied by the TclVFS extension, you will find that each supplies its own vfs::vfs_type::Mount API:

Example 22-1 Accessing a Zip file through a VFS
package require vfs::zip
=> 1.0
# Mount the zip file on "xyz"
vfs::zip::Mount c:/downloads/tclhttpd343.zip xyz
=> filecb15a8
# Examine the contents
glob xyz/*
=> xyz/tclhttpd3.4.3
# Open and read file inside the zip archive
set in [open xyz/tclhttpd3.4.3/README]
=> rechan16
gets $in
This HTTPD is written in Tcl and Tk.
    [ Team LiB ] Previous Section Next Section