1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
/*----------------------------------------------------------------------------*/
/* Xymon monitor library. */
/* */
/* This is a library module, part of libxymon. */
/* It contains routines for file- and directory manipulation. */
/* */
/* Copyright (C) 2002-2011 Henrik Storner <henrik@storner.dk> */
/* */
/* This program is released under the GNU General Public License (GPL), */
/* version 2. See the file "COPYING" for details. */
/* */
/*----------------------------------------------------------------------------*/
static char rcsid[] = "$Id: files.c 8069 2019-07-23 15:29:06Z jccleaver $";
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include "libxymon.h"
void dropdirectory(char *dirfn, int background)
{
DIR *dirfd;
struct dirent *de;
char fn[PATH_MAX];
struct stat st;
pid_t childpid = 0;
if (background) {
/* Caller wants us to run as a background task. */
childpid = fork();
}
MEMDEFINE(fn);
if (childpid == 0) {
dbgprintf("Starting to remove directory %s\n", dirfn);
dirfd = opendir(dirfn);
if (dirfd) {
while ( (de = readdir(dirfd)) != NULL ) {
snprintf(fn, sizeof(fn), "%s/%s", dirfn, de->d_name);
if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..") && (stat(fn, &st) == 0)) {
if (S_ISREG(st.st_mode)) {
dbgprintf("Removing file %s\n", fn);
unlink(fn);
}
else if (S_ISDIR(st.st_mode)) {
dbgprintf("Recurse into %s\n", fn);
dropdirectory(fn, 0); /* Don't background the recursive calls! */
}
}
}
closedir(dirfd);
}
dbgprintf("Removing directory %s\n", dirfn);
rmdir(dirfn);
if (background) {
/* Background task just exits */
exit(0);
}
}
else if (childpid < 0) {
errprintf("Could not fork child to remove directory %s\n", dirfn);
}
MEMUNDEFINE(fn);
}
|