This patch adds "IfEmptyDir" option to Apache's mod_autoindex (Tested on Apache 1.3.20, should work on older releases too) This option is used to specify file that will be shown instead of empty directory, when Options Indexes is set. If the directory has files in it normal directory listing will be shown. IfEmptyDir can be used in global server config, inside Directory and Location Usage: IfEmptyDir /home/htdocs/somefile.html Example: IfEmptyDir /template/empty_dir.html To apply the patch goto apache_1.3.xx directory and run patch -p0 < IfEmptyDir.diff - Georgi Chorbadzhiyski georgi@top.bg http://georgi.top.bg/ --- src/modules/standard/mod_autoindex.c Tue Sep 25 17:12:14 2001 +++ src/modules/standard/mod_autoindex-hacked.c Tue Sep 25 17:13:43 2001 @@ -155,6 +155,7 @@ int icon_width; int icon_height; char *default_order; + char *empty_fname; array_header *icon_list; array_header *alt_list; @@ -565,7 +566,18 @@ return NULL; } +static const char *set_empty(cmd_parms *cmd, void *m, char *name) +{ + autoindex_config_rec *d_cfg = (autoindex_config_rec *) m; + if (d_cfg->empty_fname == NULL) { + d_cfg->empty_fname = ap_palloc(cmd->pool, sizeof(name)); + d_cfg->empty_fname = name; + } + return NULL; +} + #define DIR_CMD_PERMS OR_INDEXES +#define DIR_IFEMPTY_PERMS (RSRC_CONF|ACCESS_CONF) static const command_rec autoindex_cmds[] = { @@ -591,6 +603,7 @@ "Descriptive text followed by one or more filenames"}, {"HeaderName", add_header, NULL, DIR_CMD_PERMS, TAKE1, "a filename"}, {"ReadmeName", add_readme, NULL, DIR_CMD_PERMS, TAKE1, "a filename"}, + {"IfEmptyDir", set_empty, NULL, DIR_IFEMPTY_PERMS, TAKE1, "a filename"}, {"FancyIndexing", fancy_indexing, NULL, DIR_CMD_PERMS, FLAG, "Limited to 'on' or 'off' (superseded by IndexOptions FancyIndexing)"}, {"DefaultIcon", ap_set_string_slot, @@ -620,6 +633,7 @@ new->incremented_opts = 0; new->decremented_opts = 0; new->default_order = NULL; + new->empty_fname = NULL; return (void *) new; } @@ -642,6 +656,7 @@ new->desc_list = ap_append_arrays(p, add->desc_list, base->desc_list); new->icon_list = ap_append_arrays(p, add->icon_list, base->icon_list); new->rdme_list = ap_append_arrays(p, add->rdme_list, base->rdme_list); + new->empty_fname = add->empty_fname; if (add->opts & NO_OPTIONS) { /* * If the current directory says 'no options' then we also @@ -1602,6 +1617,8 @@ char *title_name = ap_escape_html(r->pool, r->uri); char *title_endp; char *name = r->filename; + char *emptyname = autoindex_conf->empty_fname; + FILE *f; DIR *d; struct DIR_TYPE *dstruct; @@ -1636,6 +1653,39 @@ ap_pclosedir(r->pool, d); return 0; } + + /* + * Since we don't know how many dir. entries there are, put them into a + * linked list and then arrayificate them so qsort can use them. + */ + head = NULL; + while ((dstruct = readdir(d))) { + p = make_autoindex_entry(dstruct->d_name, autoindex_opts, + autoindex_conf, r, keyid, direction); + if (p != NULL) { + p->next = head; + head = p; + num_ent++; + } + } + + /* + * Send file if IfEmptyDir is defined and directory is empty + */ + if ((emptyname != NULL) && (num_ent <= 1)) + { + if ( (f = ap_pfopen(r->pool, (const char *)emptyname, "r")) != 0) + { + ap_hard_timeout("send", r); + ap_send_fd (f, r); + ap_kill_timeout(r); + ap_pfclose (r->pool, f); + /* Cleanup */ + ap_pclosedir(r->pool, d); + return 0; + } + } + ap_hard_timeout("send directory", r); /* Spew HTML preamble */ @@ -1678,21 +1728,6 @@ } else { direction = D_ASCENDING; - } - } - - /* - * Since we don't know how many dir. entries there are, put them into a - * linked list and then arrayificate them so qsort can use them. - */ - head = NULL; - while ((dstruct = readdir(d))) { - p = make_autoindex_entry(dstruct->d_name, autoindex_opts, - autoindex_conf, r, keyid, direction); - if (p != NULL) { - p->next = head; - head = p; - num_ent++; } } if (num_ent > 0) {