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

dir.c

00001 /***************************************************************************
00002  * CVSID: $Id: dir.c,v 1.7 2004/09/01 22:13:53 stefanb Exp $
00003  *
00004  * dir.c : Functions for handling directories
00005  *
00006  * Copyright (C) 2004 Stefan Bambach, <sbambach@gmx.net>
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 "dir.h"
00041 #include "file.h"
00042 #include "dbug_mem.h"
00043 
00044 
00045 
00046 extern int errno;
00047 
00048 
00049 
00050 int
00051 sb_dir_is_dir(char *dirpath)
00052 {
00053     struct stat st;
00054     
00055     if (stat(dirpath, &st) != 0) {
00056         return 0;
00057     }
00058 
00059     if (!S_ISDIR(st.st_mode)) {
00060         return 0;
00061     }
00062     
00063     return 1;
00064 }
00065 
00066 
00067 
00068 int
00069 sb_dir_remove(char *dirpath)
00070 {
00071     if(sb_dir_is_dir(dirpath)) {
00072         if(rmdir(dirpath)==0) {
00073             return 0;
00074         }
00075     }
00076     
00077     return -1;
00078 }
00079 
00080 
00081 
00082 int
00083 sb_dir_mkdirs(const char *pathname, mode_t mode, int *error)
00084 {
00085     char path[FILENAME_MAX];
00086     char subpath[FILENAME_MAX];
00087     int i;
00088     int pathLength;
00089     int subpathLength = 0; 
00090 
00091     // check params
00092     if(pathname == NULL) {
00093         return -2;
00094     }
00095 
00096     // get length of pathname
00097     pathLength = strlen(pathname);
00098 
00099     // check about enough space for creating dirs
00100     if(pathLength > FILENAME_MAX - 2) {
00101         return -2;
00102     }
00103 
00104     // fill strings with \0
00105     memset(path, 0, FILENAME_MAX);
00106     memset(subpath, 0, FILENAME_MAX);
00107 
00108     // make working copy
00109     strncpy(path, pathname, FILENAME_MAX);
00110 
00111     // if DIRSEP not at the end of path, add it
00112     if(path[pathLength - 1] != DIRSEP) {
00113         path[pathLength] = DIRSEP;
00114     }
00115 
00116     // If dir exist then we have nothing to do
00117     if(sb_dir_is_dir(path)) {
00118         return 0;
00119     }
00120 
00121     // If first char an DIRSEP then we need an other counter start
00122     if(path[0] != '/') {
00123         i=0;
00124     } else {
00125         subpath[0] = path[0];
00126         i=1;
00127     }
00128 
00129     // Go char for char thru the pathname an in case of a DIRSEP we check for DIR
00130     for(; path[i] != '\0'; i++) {
00131         subpath[i] = path[i];
00132         if(path[i] == DIRSEP) {
00133             subpathLength++;
00134             if(!sb_dir_is_dir(subpath)) {
00135                 if(mkdir(subpath, mode) == -1) {
00136                     *error = errno;
00137                     return -1;
00138                 }
00139             }
00140         }
00141     }
00142     
00143     return 0;
00144 }
00145 
00146 
00147 
00148 int
00149 sb_dir_rmdirs(char *dirname)
00150 {
00151     DIR *dir = NULL;
00152     struct dirent *dent = NULL;
00153     char *entry = NULL;
00154     char *rootdir = NULL;
00155     int res = 0;
00156     
00157     rootdir = strdup(dirname);
00158     if (rootdir == NULL) {
00159         goto error;
00160     }
00161         
00162     if (LAST_CHAR(rootdir)==DIRSEP) {
00163         LAST_CHAR(rootdir)='\0';
00164     }
00165         
00166     dir = opendir(rootdir);
00167     if (dir == NULL) {
00168         goto error;
00169     }
00170     
00171     while ((dent = readdir(dir)) != NULL) {
00172         if ((strcmp(dent->d_name, ".") != 0) && (strcmp(dent->d_name, "..") != 0)) {
00173             entry = (char *) malloc(sizeof(char)*(strlen(rootdir)+1+strlen(dent->d_name)+1));
00174             if (entry == NULL) {
00175                 goto error;
00176             }
00177             
00178             sprintf(entry, "%s/%s", rootdir, dent->d_name);
00179             
00180             if(sb_file_is_file(entry)) {
00181                 if(!sb_file_remove(entry)) {
00182                     ;
00183                 }
00184             } else if(sb_dir_is_dir(entry)) {
00185                 sb_dir_rmdirs(entry);
00186                 if(!rmdir(entry)) {
00187                     ;
00188                 }
00189             } else {
00190                 ;
00191             }
00192             
00193             if (entry != NULL) {
00194                 free(entry);
00195                 entry = NULL;
00196             }
00197         }
00198     }
00199     
00200     goto cleanup;
00201     
00202 error:
00203     res = -1;
00204     
00205 cleanup:
00206     if (entry != NULL) {
00207         free(entry);
00208         entry = NULL;
00209     }
00210     if (dir != NULL) {
00211         closedir(dir);
00212         dir = NULL;
00213     }
00214     if (rootdir != NULL) {
00215         free(rootdir);
00216         rootdir = NULL;
00217     }
00218 
00219     return res;
00220 }

Generated on Wed Mar 30 13:43:26 2005 for Mntd by  doxygen 1.3.9.1