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

Moved remove_directory to own file

This commit is contained in:
Lammert Bies
2016-12-11 01:48:04 +01:00
parent 580c07328f
commit 730d44a6fa
3 changed files with 90 additions and 62 deletions

View File

@@ -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 \

View File

@@ -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

View File

@@ -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