/* This file is part of "mirrorer" (TODO: find better title) * Copyright (C) 2003 Bernhard R. Link * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include "error.h" #include "dirs.h" extern int verbose; //TODO: rewrite with retvalue return values... /* create directory dirname. returns 0 on success or if already existing, -1 otherwise */ static retvalue dirs_check(const char *dirname) { int ret; ret = mkdir(dirname,0775); if( ret == 0 ) { if( verbose > 0) fprintf(stderr,"Created directory \"%s\"\n",dirname); return RET_OK; } else if( ret < 0 && errno != EEXIST ) { fprintf(stderr,"Can not create directory \"%s\": %m\n",dirname); return RET_ERROR; } return RET_NOTHING; } /* create recursively all parent directories before the last '/' */ retvalue dirs_make_parent(const char *filename) { const char *p; char *h; int i; retvalue r; for( p = filename+1, i = 1 ; *p != '\0' ; p++,i++) { if( *p == '/' ) { h = strndup(filename,i); r = dirs_check(h); if( RET_WAS_ERROR(r) ) { free(h); return r; } free(h); } } return RET_OK; } /* create dirname and any '/'-seperated part of it */ retvalue dirs_make_recursive(const char *directory) { retvalue r,result; r = dirs_make_parent(directory); result = dirs_check(directory); RET_UPDATE(result,r); return result; }