From 730d44a6fa8cc8ea76a0d03d5a8506847b3f4506 Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Sun, 11 Dec 2016 01:48:04 +0100 Subject: [PATCH] Moved remove_directory to own file --- Makefile | 1 + src/httplib_remove_directory.c | 89 ++++++++++++++++++++++++++++++++++ src/libhttp.c | 62 ----------------------- 3 files changed, 90 insertions(+), 62 deletions(-) create mode 100644 src/httplib_remove_directory.c diff --git a/Makefile b/Makefile index 60dc2254..d3edee5f 100644 --- a/Makefile +++ b/Makefile @@ -102,6 +102,7 @@ LIB_SOURCES = src/libhttp.c \ src/httplib_redirect_to_https_port.c \ src/httplib_refresh_trust.c \ src/httplib_remove_bad_file.c \ + src/httplib_remove_directory.c \ src/httplib_reset_per_request_attributes.c \ src/httplib_send_file.c \ src/httplib_send_file_data.c \ diff --git a/src/httplib_remove_directory.c b/src/httplib_remove_directory.c new file mode 100644 index 00000000..e7128107 --- /dev/null +++ b/src/httplib_remove_directory.c @@ -0,0 +1,89 @@ +/* + * 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" + + +#if !defined(NO_FILES) +int XX_httplib_remove_directory( struct mg_connection *conn, const char *dir ) { + + char path[PATH_MAX]; + struct dirent *dp; + DIR *dirp; + struct de de; + int truncated; + int ok = 1; + + if ((dirp = mg_opendir(conn, dir)) == NULL) { + return 0; + } else { + de.conn = conn; + + while ((dp = mg_readdir(dirp)) != NULL) { + /* Do not show current dir (but show hidden files as they will + * also be removed) */ + if (!strcmp(dp->d_name, ".") || !strcmp(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) { + /* Do not delete anything shorter */ + ok = 0; + continue; + } + + if (!XX_httplib_stat(conn, path, &de.file)) { + mg_cry(conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, strerror(ERRNO)); + ok = 0; + } + if (de.file.membuf == NULL) { + /* file is not in memory */ + if (de.file.is_directory) { + if (XX_httplib_remove_directory(conn, path) == 0) ok = 0; + } else { + if (mg_remove(conn, path) == 0) ok = 0; + } + } else { + /* file is in memory. It can not be deleted. */ + ok = 0; + } + } + mg_closedir(dirp); + + IGNORE_UNUSED_RESULT(rmdir(dir)); + } + + return ok; + +} /* XX_httplib_remove_directory */ +#endif diff --git a/src/libhttp.c b/src/libhttp.c index 38cddab4..8d46c5db 100644 --- a/src/libhttp.c +++ b/src/libhttp.c @@ -4609,65 +4609,3 @@ int XX_httplib_scan_directory( struct mg_connection *conn, const char *dir, void return 1; } /* XX_httplib_scan_directory */ - - -#if !defined(NO_FILES) -int XX_httplib_remove_directory( struct mg_connection *conn, const char *dir ) { - - char path[PATH_MAX]; - struct dirent *dp; - DIR *dirp; - struct de de; - int truncated; - int ok = 1; - - if ((dirp = mg_opendir(conn, dir)) == NULL) { - return 0; - } else { - de.conn = conn; - - while ((dp = mg_readdir(dirp)) != NULL) { - /* Do not show current dir (but show hidden files as they will - * also be removed) */ - if (!strcmp(dp->d_name, ".") || !strcmp(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) { - /* Do not delete anything shorter */ - ok = 0; - continue; - } - - if (!XX_httplib_stat(conn, path, &de.file)) { - mg_cry(conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, strerror(ERRNO)); - ok = 0; - } - if (de.file.membuf == NULL) { - /* file is not in memory */ - if (de.file.is_directory) { - if (XX_httplib_remove_directory(conn, path) == 0) ok = 0; - } else { - if (mg_remove(conn, path) == 0) ok = 0; - } - } else { - /* file is in memory. It can not be deleted. */ - ok = 0; - } - } - mg_closedir(dirp); - - IGNORE_UNUSED_RESULT(rmdir(dir)); - } - - return ok; - -} /* XX_httplib_remove_directory */ -#endif