Skip to content

Instantly share code, notes, and snippets.

@zsrinivas
Created November 15, 2015 04:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zsrinivas/553cfd249bffef44369c to your computer and use it in GitHub Desktop.
Save zsrinivas/553cfd249bffef44369c to your computer and use it in GitHub Desktop.
git diff src/mod_dirlisting.c
diff --git a/src/mod_dirlisting.c b/src/mod_dirlisting.c
index 48ac9b4..2921325 100644
--- a/src/mod_dirlisting.c
+++ b/src/mod_dirlisting.c
@@ -12,9 +12,11 @@
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
+#include <sys/types.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
+#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
@@ -84,6 +86,8 @@ static excludes_buffer *excludes_buffer_init(void) {
return exb;
}
+char __item_path[1000], __request_dir_path[1000];
+
static int excludes_buffer_append(excludes_buffer *exb, buffer *string) {
#ifdef HAVE_PCRE_H
@@ -433,6 +437,25 @@ static void http_dirls_sort(dirls_entry_t **ent, int num) {
} while (gap > 1 || swapped);
}
+void setItemModes(const char* itemname, char* modes) {
+ struct stat info;
+ stat(itemname, &info);
+ mode_t mode = info.st_mode;
+ strcpy(modes, "----------");
+ if ( S_ISDIR(mode) ) modes[0] = 'd'; /* directory? */
+ if ( S_ISCHR(mode) ) modes[0] = 'c'; /* char devices */
+ if ( S_ISBLK(mode) ) modes[0] = 'b'; /* block device */
+ if ( mode & S_IRUSR ) modes[1] = 'r'; /* 3 bits for user */
+ if ( mode & S_IWUSR ) modes[2] = 'w';
+ if ( mode & S_IXUSR ) modes[3] = 'x';
+ if ( mode & S_IRGRP ) modes[4] = 'r'; /* 3 bits for group */
+ if ( mode & S_IWGRP ) modes[5] = 'w';
+ if ( mode & S_IXGRP ) modes[6] = 'x';
+ if ( mode & S_IROTH ) modes[7] = 'r'; /* 3 bits for other */
+ if ( mode & S_IWOTH ) modes[8] = 'w';
+ if ( mode & S_IXOTH ) modes[9] = 'x';
+}
+
/* buffer must be able to hold "999.9K"
* conversion is simple but not perfect
*/
@@ -551,26 +574,28 @@ static void http_list_directory_header(server *srv, connection *con, plugin_data
stream_close(&s);
}
- buffer_append_string_len(out, CONST_STR_LEN("<h2>Index of "));
+ buffer_append_string_len(out, CONST_STR_LEN("<h2 class=\"lty_h2\">Index of "));
buffer_append_string_encoded(out, CONST_BUF_LEN(con->uri.path), ENCODING_MINIMAL_XML);
buffer_append_string_len(out, CONST_STR_LEN(
"</h2>\n"
- "<div class=\"list\">\n"
- "<table summary=\"Directory Listing\" cellpadding=\"0\" cellspacing=\"0\">\n"
- "<thead>"
+ "<div class=\"lty_list\">\n"
+ "<table summary=\"Directory Listing\" class=\"lty_table\">\n"
+ "<thead class=\"lty_thead\">"
"<tr>"
- "<th class=\"n\">Name</th>"
- "<th class=\"m\">Last Modified</th>"
- "<th class=\"s\">Size</th>"
- "<th class=\"t\">Type</th>"
+ "<th class=\"lty_modes\">Modes</th>"
+ "<th class=\"lty_name\">Name</th>"
+ "<th class=\"lty_modified\">Last Modified</th>"
+ "<th class=\"lty_size\">Size</th>"
+ "<th class=\"lty_type\">Type</th>"
"</tr>"
"</thead>\n"
- "<tbody>\n"
+ "<tbody class=\"lty_tbody\">\n"
"<tr>"
- "<td class=\"n\"><a href=\"../\">Parent Directory</a>/</td>"
- "<td class=\"m\">&nbsp;</td>"
- "<td class=\"s\">- &nbsp;</td>"
- "<td class=\"t\">Directory</td>"
+ "<th class=\"lty_modes\">----------</th>"
+ "<td class=\"lty_name\"><a href=\"../\">..</a>/</td>"
+ "<td class=\"lty_modified\">&nbsp;</td>"
+ "<td class=\"lty_size\">- &nbsp;</td>"
+ "<td class=\"lty_type\">Directory</td>"
"</tr>\n"
));
}
@@ -606,7 +631,7 @@ static void http_list_directory_footer(server *srv, connection *con, plugin_data
if(p->conf.auto_layout) {
buffer_append_string_len(out, CONST_STR_LEN(
- "<div class=\"foot\">"
+ "<div class=\"lty_foot\">"
));
if (!buffer_string_is_empty(p->conf.set_footer)) {
@@ -632,6 +657,7 @@ static int http_list_directory(server *srv, connection *con, plugin_data *p, buf
struct stat st;
char *path, *path_file;
size_t i;
+ char ent_modes[10];
int hide_dotfiles = p->conf.hide_dot_files;
dirls_list_t dirs, files, *list;
dirls_entry_t *tmp;
@@ -672,6 +698,7 @@ static int http_list_directory(server *srv, connection *con, plugin_data *p, buf
strcpy(path, dir->ptr);
path_file = path + i;
+ strcpy(__request_dir_path, path);
if (NULL == (dp = opendir(path))) {
log_error_write(srv, __FILE__, __LINE__, "sbs",
"opendir failed:", dir, strerror(errno));
@@ -799,14 +826,25 @@ static int http_list_directory(server *srv, connection *con, plugin_data *p, buf
#else
strftime(datebuf, sizeof(datebuf), "%Y-%b-%d %H:%M:%S", localtime(&(tmp->mtime)));
#endif
+ __item_path[0] = '\0';
+ strcat(__item_path, __request_dir_path);
+ strncat(__item_path, DIRLIST_ENT_NAME(tmp), tmp->namelen);
+
+ setItemModes(__item_path, ent_modes);
+
+ buffer_append_string_len(out, CONST_STR_LEN("<tr><td class=\"lty_modes\">")); /********/
+ buffer_append_string_encoded(out, ent_modes, sizeof(ent_modes), ENCODING_MINIMAL_XML);
+ buffer_append_string_len(out, CONST_STR_LEN("</td>"));
- buffer_append_string_len(out, CONST_STR_LEN("<tr><td class=\"n\"><a href=\""));
+ buffer_append_string_len(out, CONST_STR_LEN("<td class=\"lty_name\"><a href=\""));
buffer_append_string_encoded(out, DIRLIST_ENT_NAME(tmp), tmp->namelen, ENCODING_REL_URI_PART);
buffer_append_string_len(out, CONST_STR_LEN("/\">"));
buffer_append_string_encoded(out, DIRLIST_ENT_NAME(tmp), tmp->namelen, ENCODING_MINIMAL_XML);
- buffer_append_string_len(out, CONST_STR_LEN("</a>/</td><td class=\"m\">"));
+
+ buffer_append_string_len(out, CONST_STR_LEN("</a>/</td><td class=\"lty_modified\">"));
buffer_append_string_len(out, datebuf, sizeof(datebuf) - 1);
- buffer_append_string_len(out, CONST_STR_LEN("</td><td class=\"s\">- &nbsp;</td><td class=\"t\">Directory</td></tr>\n"));
+
+ buffer_append_string_len(out, CONST_STR_LEN("</td><td class=\"lty_size\">- &nbsp;</td><td class=\"lty_type\">Directory</td></tr>\n"));
free(tmp);
}
@@ -863,15 +901,25 @@ static int http_list_directory(server *srv, connection *con, plugin_data *p, buf
#endif
http_list_directory_sizefmt(sizebuf, tmp->size);
- buffer_append_string_len(out, CONST_STR_LEN("<tr><td class=\"n\"><a href=\""));
+ __item_path[0] = '\0';
+ strcat(__item_path, __request_dir_path);
+ strncat(__item_path, DIRLIST_ENT_NAME(tmp), tmp->namelen);
+ setItemModes(__item_path, ent_modes);
+
+ buffer_append_string_len(out, CONST_STR_LEN("<tr><td class=\"lty_modes\">")); /********/
+ buffer_append_string_encoded(out, ent_modes, sizeof(ent_modes), ENCODING_MINIMAL_XML);
+ buffer_append_string_len(out, CONST_STR_LEN("</td>"));
+
+
+ buffer_append_string_len(out, CONST_STR_LEN("<td class=\"lty_name\"><a href=\""));
buffer_append_string_encoded(out, DIRLIST_ENT_NAME(tmp), tmp->namelen, ENCODING_REL_URI_PART);
buffer_append_string_len(out, CONST_STR_LEN("\">"));
buffer_append_string_encoded(out, DIRLIST_ENT_NAME(tmp), tmp->namelen, ENCODING_MINIMAL_XML);
- buffer_append_string_len(out, CONST_STR_LEN("</a></td><td class=\"m\">"));
+ buffer_append_string_len(out, CONST_STR_LEN("</a></td><td class=\"lty_modified\">"));
buffer_append_string_len(out, datebuf, sizeof(datebuf) - 1);
- buffer_append_string_len(out, CONST_STR_LEN("</td><td class=\"s\">"));
+ buffer_append_string_len(out, CONST_STR_LEN("</td><td class=\"lty_size\">"));
buffer_append_string(out, sizebuf);
- buffer_append_string_len(out, CONST_STR_LEN("</td><td class=\"t\">"));
+ buffer_append_string_len(out, CONST_STR_LEN("</td><td class=\"lty_type\">"));
buffer_append_string(out, content_type);
buffer_append_string_len(out, CONST_STR_LEN("</td></tr>\n"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment