forked from Radmind/radmind
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrmdirs.c
116 lines (100 loc) · 2.68 KB
/
rmdirs.c
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
/*
* Copyright (c) 2003 Regents of The University of Michigan.
* All Rights Reserved. See COPYRIGHT.
*/
#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <errno.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "rmdirs.h"
int
rmdirs( char *path )
{
int i, len, unlinkedfiles;
char temp[ MAXPATHLEN ];
DIR *dir;
struct dirent *dirent;
struct stat st;
if (( dir = opendir( path )) == NULL ) {
return( -1 );
}
/* readdir() on HFS+ is broken:
* http://docs.info.apple.com/article.html?artnum=107884
*
* "The unspecified behavior is what readdir() should return after the
* directory has been modified. Many file systems have been implemented
* such that subsequent readdir() calls will return the next directory
* entry. The implementation of the HFS file system cannot guarantee
* that all enclosed files or directories will be removed using the
* above method."
*/
do {
unlinkedfiles = 0;
while (( dirent = readdir( dir )) != NULL ) {
/* don't include . and .. */
if (( strcmp( dirent->d_name, "." ) == 0 ) ||
( strcmp( dirent->d_name, ".." ) == 0 )) {
continue;
}
len = strlen( path );
/* absolute pathname. add 2 for / and NULL termination. */
if (( len + strlen( dirent->d_name ) + 2 ) > MAXPATHLEN ) {
fprintf( stderr, "Absolute pathname too long\n" );
goto error;
}
if ( path[ len - 1 ] == '/' ) {
if ( snprintf( temp, MAXPATHLEN, "%s%s", path, dirent->d_name )
>= MAXPATHLEN ) {
fprintf( stderr, "%s%s: path too long\n", path,
dirent->d_name );
goto error;
}
} else {
if ( snprintf( temp, MAXPATHLEN, "%s/%s", path, dirent->d_name )
>= MAXPATHLEN ) {
fprintf( stderr, "%s/%s: path too long\n", path,
dirent->d_name );
goto error;
}
}
if ( lstat( temp, &st ) != 0 ) {
/* XXX - how to return path that gave error? */
fprintf( stderr, "%s: %s\n", temp, strerror( errno ));
goto error;
}
if ( S_ISDIR( st.st_mode )) {
if ( rmdirs( temp ) != 0 ) {
fprintf( stderr, "%s: %s\n", temp, strerror( errno ));
goto error;
}
} else {
if ( unlink( temp ) != 0 ) {
fprintf( stderr, "%s: %s\n", temp, strerror( errno ));
goto error;
}
unlinkedfiles = 1;
}
if ( unlinkedfiles ) {
rewinddir( dir );
}
}
} while ( unlinkedfiles );
if ( closedir( dir ) != 0 ) {
return( -1 );
}
if ( rmdir( path ) != 0 ) {
return( -1 );
}
return ( 0 );
error:
i = errno;
if ( closedir( dir ) != 0 ) {
errno = i;
}
return( -1 );
}