1
0
mirror of https://github.com/lammertb/libhttp.git synced 2026-01-27 08:02:47 +03:00

Moved scan_directory to own file

This commit is contained in:
Lammert Bies
2016-12-11 01:50:17 +01:00
parent 730d44a6fa
commit f559d336f2
3 changed files with 73 additions and 45 deletions

View File

@@ -104,6 +104,7 @@ LIB_SOURCES = src/libhttp.c \
src/httplib_remove_bad_file.c \
src/httplib_remove_directory.c \
src/httplib_reset_per_request_attributes.c \
src/httplib_scan_directory.c \
src/httplib_send_file.c \
src/httplib_send_file_data.c \
src/httplib_send_options.c \

View File

@@ -0,0 +1,72 @@
/*
* Copyright (c) 2016 Lammert Bies
* Copyright (c) 2013-2016 the Civetweb developers
* Copyright (c) 2004-2013 Sergey Lyubka
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "libhttp-private.h"
int XX_httplib_scan_directory( struct mg_connection *conn, const char *dir, void *data, void (*cb)(struct de *, void *) ) {
char path[PATH_MAX];
struct dirent *dp;
DIR *dirp;
struct de de;
int truncated;
if ((dirp = mg_opendir(conn, dir)) == NULL) {
return 0;
} else {
de.conn = conn;
while ((dp = mg_readdir(dirp)) != NULL) {
/* Do not show current dir and hidden files */
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..") || XX_httplib_must_hide_file(conn, dp->d_name)) continue;
XX_httplib_snprintf( conn, &truncated, path, sizeof(path), "%s/%s", dir, dp->d_name);
/* If we don't memset stat structure to zero, mtime will have
* garbage and strftime() will segfault later on in
* XX_httplib_print_dir_entry(). memset is required only if XX_httplib_stat()
* fails. For more details, see
* http://code.google.com/p/mongoose/issues/detail?id=79 */
memset(&de.file, 0, sizeof(de.file));
if (truncated) {
/* If the path is not complete, skip processing. */
continue;
}
if (!XX_httplib_stat(conn, path, &de.file)) {
mg_cry(conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, strerror(ERRNO));
}
de.file_name = dp->d_name;
cb(&de, data);
}
(void)mg_closedir(dirp);
}
return 1;
} /* XX_httplib_scan_directory */

View File

@@ -4564,48 +4564,3 @@ int XX_httplib_must_hide_file( struct mg_connection *conn, const char *path ) {
return 0;
} /* XX_httplib_must_hide_file */
int XX_httplib_scan_directory( struct mg_connection *conn, const char *dir, void *data, void (*cb)(struct de *, void *) ) {
char path[PATH_MAX];
struct dirent *dp;
DIR *dirp;
struct de de;
int truncated;
if ((dirp = mg_opendir(conn, dir)) == NULL) {
return 0;
} else {
de.conn = conn;
while ((dp = mg_readdir(dirp)) != NULL) {
/* Do not show current dir and hidden files */
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..") || XX_httplib_must_hide_file(conn, dp->d_name)) continue;
XX_httplib_snprintf( conn, &truncated, path, sizeof(path), "%s/%s", dir, dp->d_name);
/* If we don't memset stat structure to zero, mtime will have
* garbage and strftime() will segfault later on in
* XX_httplib_print_dir_entry(). memset is required only if XX_httplib_stat()
* fails. For more details, see
* http://code.google.com/p/mongoose/issues/detail?id=79 */
memset(&de.file, 0, sizeof(de.file));
if (truncated) {
/* If the path is not complete, skip processing. */
continue;
}
if (!XX_httplib_stat(conn, path, &de.file)) {
mg_cry(conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, strerror(ERRNO));
}
de.file_name = dp->d_name;
cb(&de, data);
}
(void)mg_closedir(dirp);
}
return 1;
} /* XX_httplib_scan_directory */