physfs: PhysicsFS

Main Page | Data Structures | File List | Data Fields | Globals

# PhysicsFS

### 1.0.1

The latest version of PhysicsFS can be found at: http://icculus.org/physfs/

PhysicsFS; a portable, flexible file i/o abstraction.

This API gives you access to a system file system in ways superior to the stdio or system i/o calls. The brief benefits:

- It's portable.
- It's safe. No file access is permitted outside the specified dirs.
- It's flexible. Archives (.ZIP files) can be used transparently as directory structures.

This system is largely inspired by Quake 3's PK3 files and the related fs_* cvars. If you've ever tinkered with these, then this API will be familiar to you.

With PhysicsFS, you have a single writing directory and multiple directories (the "search path") for reading. You can think of this as a filesystem within a filesystem. If (on Windows) you were to set the writing directory to "C:\MyGame\MyWritingDirectory", then no PHYSFS calls could touch anything above this directory, including the "C:\MyGame" and "C:\" directories. This prevents an application's internal scripting language from piddling over c:\config.sys, for example. If you'd rather give PHYSFS full access to the system's REAL file system, set the writing dir to "C:\", but that's generally A Bad Thing for several reasons.

Drive letters are hidden in PhysicsFS once you set up your initial paths. The search path creates a single, hierarchical directory structure. Not only does this lend itself well to general abstraction with archives, it also gives better support to operating systems like MacOS and Unix. Generally speaking, you shouldn't ever hardcode a drive letter; not only does this hurt portability to non-Microsoft OSes, but it limits your win32 users to a single drive, too. Use the PhysicsFS abstraction functions and allow user-defined configuration options, too. When opening a file, you specify it like it was on a Unix filesystem: if you want to write to "C:\MyGame\MyConfigFiles\game.cfg", then you might set the write dir to "C:\MyGame" and then open "MyConfigFiles/game.cfg". This gives an abstraction across all platforms. Specifying a file in this way is termed "platform-independent notation" in this documentation. Specifying a a filename in a form such as "C:\mydir\myfile" or "MacOS hard drive:My Directory:My File" is termed "platform-dependent notation". The only time you use platform-dependent notation is when setting up your write directory and search path; after that, all file access into those directories are done with platform-independent notation.

All files opened for writing are opened in relation to the write directory, which is the root of the writable filesystem. When opening a file for reading, PhysicsFS goes through the search path. This is NOT the same thing as the PATH environment variable. An application using PhysicsFS specifies directories to be searched which may be actual directories, or archive files that contain files and subdirectories of their own. See the end of these docs for currently supported archive formats.

Once the search path is defined, you may open files for reading. If you've got the following search path defined (to use a win32 example again):

- C:\mygame
- C:\mygame\myuserfiles
- D:\mygamescdromdatafiles
- C:\mygame\installeddatafiles.zip

Then a call to PHYSFS_openRead("textfiles/myfile.txt") (note the directory separator, lack of drive letter, and lack of dir separator at the start of the string; this is platform-independent notation) will check for C:\mygame\textfiles\myfile.txt, then C:\mygame\myuserfiles\textfiles\myfile.txt, then D:\mygamescdromdatafiles\textfiles\myfile.txt, then, finally, for textfiles\myfile.txt inside of C:\mygame\installeddatafiles.zip. Remember that most archive types and platform filesystems store their filenames in a case-sensitive manner, so you should be careful to specify it correctly.

Files opened through PhysicsFS may NOT contain "." or ".." or ":" as dir elements. Not only are these meaningless on MacOS and/or Unix, they are a security hole. Also, symbolic links (which can be found in some archive types and directly in the filesystem on Unix platforms) are NOT followed until you call PHYSFS_permitSymbolicLinks(). That's left to your own discretion, as following a symlink can allow for access outside the write dir and search paths. There is no mechanism for creating new symlinks in PhysicsFS.

The write dir is not included in the search path unless you specifically add it. While you CAN change the write dir as many times as you like, you should probably set it once and stick to it. Remember that your program will not have permission to write in every directory on Unix and NT systems.

All files are opened in binary mode; there is no endline conversion for textfiles. Other than that, PhysicsFS has some convenience functions for platform-independence. There is a function to tell you the current platform's dir separator ("\\" on windows, "/" on Unix, ":" on MacOS), which is needed only to set up your search/write paths. There is a function to tell you what CD-ROM drives contain accessible discs, and a function to recommend a good search path, etc.

A recommended order for the search path is the write dir, then the base dir, then the cdrom dir, then any archives discovered. Quake 3 does something like this, but moves the archives to the start of the search path. Build Engine games, like Duke Nukem 3D and Blood, place the archives last, and use the base dir for both searching and writing. There is a helper function (PHYSFS_setSaneConfig()) that puts together a basic configuration for you, based on a few parameters. Also see the comments on PHYSFS_getBaseDir(), and PHYSFS_getUserDir() for info on what those are and how they can help you determine an optimal search path.

PhysicsFS is mostly thread safe. The error messages returned by PHYSFS_getLastError are unique by thread, and library-state-setting functions are mutex'd. For efficiency, individual file accesses are not locked, so you can not safely read/write/seek/close/etc the same file from two threads at the same time. Other race conditions are bugs that should be reported/patched.

While you CAN use stdio/syscall file access in a program that has PHYSFS_* calls, doing so is not recommended, and you can not use system filehandles with PhysicsFS and vice versa.

Note that archives need not be named as such: if you have a ZIP file and rename it with a .PKG extension, the file will still be recognized as a ZIP archive by PhysicsFS; the file's contents are used to determine its type.

Currently supported archive types:

- .ZIP (pkZip/WinZip/Info-ZIP compatible)
- .GRP (Build Engine groupfile archives)
- .PAK (Quake I/II archive format)
- .HOG (Descent I/II HOG file archives)
- .MVL (Descent II movielib archives)
- .WAD (DOOM engine archives)

Please see the file LICENSE in the source's root directory for licensing and redistribution rights.

Please see the file CREDITS in the source's root directory for a complete list of who's responsible for this.

Author:
  Ryan C. Gordon.