File: rm_r.c

package info (click to toggle)
mimedefang 2.84-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,928 kB
  • sloc: ansic: 9,315; perl: 6,709; sh: 2,392; tcl: 693; makefile: 74; php: 19
file content (177 lines) | stat: -rw-r--r-- 4,379 bytes parent folder | download | duplicates (4)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/***********************************************************************
*
* rm_r.c
*
* Implementation in C of recursive deletion of directory (rm -r dir)
*
* Copyright (C) 2002-2005 Roaring Penguin Software Inc.
* http://www.roaringpenguin.com
*
* This program may be distributed under the terms of the GNU General
* Public License, Version 2, or (at your option) any later version.
*
***********************************************************************/

#include "config.h"
#include "mimedefang.h"
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <syslog.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#ifdef EXEC_RM_FOR_CLEANUP
#include <sys/wait.h>
#endif


#ifdef ENABLE_DEBUGGING
extern void *malloc_debug(void *, size_t, char const *fname, int);
extern char *strdup_debug(void *, char const *, char const *, int);
extern void free_debug(void *, void *, char const *, int);
#undef malloc
#undef strdup
#undef free
#define malloc(x) malloc_debug(ctx, x, __FILE__, __LINE__)
#define strdup(x) strdup_debug(ctx, x, __FILE__, __LINE__)
#define free(x) free_debug(ctx, x, __FILE__, __LINE__)
#define malloc_with_log(x) malloc_debug(ctx, x, __FILE__, __LINE__)
#define strdup_with_log(x) strdup_debug(ctx, x, __FILE__, __LINE__)
#endif

#ifndef EXEC_RM_FOR_CLEANUP
/**********************************************************************
* %FUNCTION: rm_r
* %ARGUMENTS:
*  dir -- directory or file name
* %RETURNS:
*  -1 on error, 0 otherwise.
* %DESCRIPTION:
*  Deletes dir and recursively deletes contents
***********************************************************************/
int
rm_r(char const *qid, char const *dir)
{
    char buf[SMALLBUF];
    struct stat sbuf;
    DIR *d;
    struct dirent *entry;
    struct dirent *result = NULL;
    int n;
    int retcode = 0;

    if (!qid || !*qid) {
	qid = "NOQUEUE";
    }

    if (lstat(dir, &sbuf) < 0) {
	syslog(LOG_WARNING, "%s: lstat(%s) failed: %m", qid, dir);
	return -1;
    }

    if (!S_ISDIR(sbuf.st_mode)) {
	/* Not a directory - just unlink */
	if (unlink(dir) < 0) {
	    syslog(LOG_WARNING, "%s: unlink(%s) failed: %m", qid, dir);
	    return -1;
	}
	return 0;
    }

    /* Allocate room for entry */
#ifdef HAVE_PATHCONF
    entry = (struct dirent *) malloc(sizeof(struct dirent) + pathconf(dir, _PC_NAME_MAX) + 1);
#else
    /* Can't use _POSIX_NAME_MAX because it's often defined as 14...
       useless... */
    entry = (struct dirent *) malloc(sizeof(struct dirent) + 257);
#endif
    if (!entry) {
	syslog(LOG_WARNING, "%s: Unable to allocate space for dirent entry: %m", qid);
	return -1;
    }

    d = opendir(dir);
    if (!d) {
	int errno_save = errno;
	syslog(LOG_WARNING, "%s: opendir(%s) failed: %m", qid, dir);
	free(entry);
	errno = errno_save;
	return -1;
    }

    for (;;) {
	n = readdir_r(d, entry, &result);
	if (n != 0) {
	    errno = n;
	    syslog(LOG_WARNING, "%s: readdir_r failed: %m", qid);
	    closedir(d);
	    free(entry);
	    errno = n;
	    return -1;
	}
	if (!result) break;
	if (!strcmp(entry->d_name, ".") ||
	    !strcmp(entry->d_name, "..")) {
	    continue;
	}
	snprintf(buf, sizeof(buf), "%s/%s", dir, entry->d_name);
	if (rm_r(qid, buf) < 0) {
	    retcode = -1;
	}
    }
    free(entry);
    closedir(d);
    if (rmdir(dir) < 0) {
	syslog(LOG_WARNING, "%s: rmdir(%s) failed: %m", qid, dir);
	return -1;
    }
    return retcode;
}

#else /* EXEC_RM_FOR_CLEANUP */
/**********************************************************************
* %FUNCTION: rm_r
* %ARGUMENTS:
*  dir -- directory or file name
* %RETURNS:
*  -1 on error, 0 otherwise.
* %DESCRIPTION:
*  Deletes dir and recursively deletes contents.  Does so by forking and
*  exec'ing "rm".
***********************************************************************/
int
rm_r(char const *qid, char const *dir)
{
    pid_t pid;

    if (!qid || !*qid) {
	qid = "NOQUEUE";
    }


    pid = fork();
    if (pid == (pid_t) -1) {
	syslog(LOG_WARNING, "%s: fork() failed -- cannot clean up: %m", qid);
	return -1;
    }

    if (pid == 0) {
	/* In the child */
	execl(RM, "rm", "-r", "-f", dir, NULL);
	_exit(EXIT_FAILURE);
    }

    /* In the parent */
    if (waitpid(pid, NULL, 0) < 0) {
	syslog(LOG_ERR, "%s: waitpid() failed -- cannot clean up: %m", qid);
	return -1;
    }

    return 0;
}
#endif  /* EXEC_RM_FOR_CLEANUP */