11.4. File and Directory Management

Apache provides routines for opening files, reading and writing to them, and closing them. Some of these routines are wrappers around the standard operating system calls in order to provide a compatibility layer for the Unix, Win32, and other ports. Other functions are an improvement over their operating system equivalents because they take advantage of Apache's memory management system.

11.4.1. Pathname Manipulation

These routines are handy for parsing strings that contain filenames and paths. See the next section, Section 11.4.2," for functions that operate on files and directories themselves.

These functions are all declared in httpd.h:

void ap_chdir_ file (const char *file)

Given a pathname in file, this peculiar little function identifies the directory part and invokes chdir() to make it the current working directory. Here's an example of calling the function on the request record's filename field in order to chdir() to the directory that contains the requested file:


int ap_os_is_path_absolute (const char *filename)

This function provides a portable test as to whether a filename is absolute or not. On Unix systems, an absolute filename begins with a /, whereas under Win32 systems, an absolute filename may begin with a drive letter, a colon delimiter, and a / or \.

if(!ap_os_is_path_absolute(filename)) {
   ... must resolve the relative filename somehow ...

char *ap_make_ full_path (pool *p, const char *directory, ...

Get Writing Apache Modules with Perl and C now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.