Main Page | Modules | Data Structures | File List | Data Fields | Related Pages

mntd_dir.c

00001 /***************************************************************************
00002  * CVSID: $Id: mntd_dir.c,v 1.2 2004/05/26 19:12:40 stefanb Exp $
00003  *
00004  * mntd_dir.c : directory handler for MNT daemon
00005  *
00006  * Copyright (C) 2004 Stefan Bambach, <stefan@bambach.biz>
00007  *
00008  * Licensed under the GNU General Public License 2.0
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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     // check params
00116     if(pathname == NULL) {
00117         return -2;
00118     }
00119 
00120     // get length of pathname
00121     pathLength = strlen(pathname);
00122 
00123     // check about enough space for creating dirs
00124     if(pathLength > FILENAME_MAX - 2) {
00125         return -2;
00126     }
00127 
00128     // fill strings with \0
00129     memset(path, 0, FILENAME_MAX);
00130     memset(subpath, 0, FILENAME_MAX);
00131 
00132     // make working copy
00133     strncpy(path, pathname, FILENAME_MAX);
00134 
00135     // if DIRSEP not at the end of path, add it
00136     if(path[pathLength - 1] != DIRSEP) {
00137         path[pathLength] = DIRSEP;
00138     }
00139 
00140     // If dir exist thenwe have nothing to do
00141     if(mntd_dir_is_dir(path)) {
00142         return 0;
00143     }
00144 
00145     // If first char an DIRSEP then we need an other counter start
00146     if(path[0] != '/') {
00147         i=0;
00148     } else {
00149         subpath[0] = path[0];
00150         i=1;
00151     }
00152 
00153     // Go char for char thru the pathname an in case of a DIRSEP we check for DIR
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 

Generated on Thu May 27 23:27:28 2004 for Mntd by doxygen 1.3.5