00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifdef HAVE_CONFIG_H
00027 # include <config.h>
00028 #endif
00029
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <sys/stat.h>
00034 #include <sys/types.h>
00035 #include <fcntl.h>
00036 #include <dirent.h>
00037 #include <unistd.h>
00038 #include <errno.h>
00039
00040 #include "mntd_dir.h"
00041 #include "mntd_file.h"
00042 #include "errmanager.h"
00043
00044
00045 extern int errno;
00046
00047
00062 int
00063 mntd_dir_is_dir(char *dirpath)
00064 {
00065 struct stat st;
00066
00067 if (stat(dirpath, &st) != 0) {
00068 return 0;
00069 }
00070
00071 if (!S_ISDIR(st.st_mode)) {
00072 return 0;
00073 }
00074
00075 return 1;
00076 }
00077
00078
00085 int
00086 mntd_dir_remove(char *dirpath)
00087 {
00088 if(mntd_dir_is_dir(dirpath)) {
00089 if(rmdir(dirpath)==0) {
00090 return 1;
00091 }
00092 }
00093
00094 return 0;
00095 }
00096
00097
00106 int
00107 mntd_dir_mkdirs(const char *pathname, mode_t mode, int *error)
00108 {
00109 char path[FILENAME_MAX];
00110 char subpath[FILENAME_MAX];
00111 int i;
00112 int pathLength;
00113 int subpathLength = 0;
00114
00115
00116 if(pathname == NULL) {
00117 return -2;
00118 }
00119
00120
00121 pathLength = strlen(pathname);
00122
00123
00124 if(pathLength > FILENAME_MAX - 2) {
00125 return -2;
00126 }
00127
00128
00129 memset(path, 0, FILENAME_MAX);
00130 memset(subpath, 0, FILENAME_MAX);
00131
00132
00133 strncpy(path, pathname, FILENAME_MAX);
00134
00135
00136 if(path[pathLength - 1] != DIRSEP) {
00137 path[pathLength] = DIRSEP;
00138 }
00139
00140
00141 if(mntd_dir_is_dir(path)) {
00142 return 0;
00143 }
00144
00145
00146 if(path[0] != '/') {
00147 i=0;
00148 } else {
00149 subpath[0] = path[0];
00150 i=1;
00151 }
00152
00153
00154 for(; path[i] != '\0'; i++) {
00155 subpath[i] = path[i];
00156 if(path[i] == DIRSEP) {
00157 subpathLength++;
00158 if(!mntd_dir_is_dir(subpath)) {
00159 if(mkdir(subpath, mode) == -1) {
00160 *error = errno;
00161 return -1;
00162 }
00163 }
00164 }
00165 }
00166
00167 return 0;
00168 }
00169
00170
00171
00180 int
00181 mntd_dir_rmdirs(char *dirname)
00182 {
00183 DIR *dir = NULL;
00184 struct dirent *dent = NULL;
00185 char *entry = NULL;
00186 char *rootdir = NULL;
00187
00188 rootdir = strdup(dirname);
00189 if (rootdir == NULL) {
00190 return 0;
00191 }
00192
00193 if (LAST_CHAR(rootdir)==DIRSEP) {
00194 LAST_CHAR(rootdir)='\0';
00195 }
00196
00197 dir = opendir(rootdir);
00198 if (dir == NULL) {
00199 return 0;
00200 }
00201
00202 while ((dent = readdir(dir)) != NULL) {
00203 if ((strcmp(dent->d_name, ".") != 0) && (strcmp(dent->d_name, "..") != 0)) {
00204 entry = (char *) malloc(sizeof(char)*(strlen(rootdir)+1+strlen(dent->d_name)+1));
00205 if (entry == NULL) {
00206 if (dir != NULL) {
00207 closedir(dir);
00208 dir = NULL;
00209 }
00210 return 0;
00211 }
00212
00213 sprintf(entry, "%s/%s", rootdir, dent->d_name);
00214
00215 if(mntd_file_is_file(entry)) {
00216 if(!mntd_file_remove(entry)) {
00217 ;
00218 }
00219 } else if(mntd_dir_is_dir(entry)) {
00220 mntd_dir_rmdirs(entry);
00221 if(!rmdir(entry)) {
00222 ;
00223 }
00224 } else {
00225 ;
00226 }
00227
00228 if (entry != NULL) {
00229 free(entry);
00230 entry = NULL;
00231 }
00232 }
00233 }
00234
00235 if (dir != NULL) {
00236 closedir(dir);
00237 dir = NULL;
00238 }
00239 if (rootdir != NULL) {
00240 free(rootdir);
00241 rootdir = NULL;
00242 }
00243
00244 return 1;
00245 }
00246
00247