dirs.c 2.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*  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
 */
Bernhard Link's avatar
Bernhard Link committed
17 18
#include <config.h>

Bernhard Link's avatar
Bernhard Link committed
19 20 21 22 23 24
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
25
#include "error.h"
Bernhard Link's avatar
Bernhard Link committed
26 27 28 29
#include "dirs.h"

extern int verbose;

30 31
//TODO: rewrite with retvalue return values...

Bernhard Link's avatar
Bernhard Link committed
32
/* create directory dirname. returns 0 on success or if already existing, -1 otherwise */
33
static retvalue dirs_check(const char *dirname) {
Bernhard Link's avatar
Bernhard Link committed
34 35
	int ret;

36
	ret = mkdir(dirname,0775);
Bernhard Link's avatar
Bernhard Link committed
37
	if( ret == 0 ) {
38
		if( verbose > 0)
39 40
			fprintf(stderr,"Created directory \"%s\"\n",dirname);
		return RET_OK;
Bernhard Link's avatar
Bernhard Link committed
41 42
	} else if( ret < 0 && errno != EEXIST ) {
		fprintf(stderr,"Can not create directory \"%s\": %m\n",dirname);
43
		return RET_ERROR;
Bernhard Link's avatar
Bernhard Link committed
44
	}
45
	return RET_NOTHING;
Bernhard Link's avatar
Bernhard Link committed
46 47 48
}

/* create recursively all parent directories before the last '/' */
49
retvalue dirs_make_parent(const char *filename) {
Bernhard Link's avatar
Bernhard Link committed
50 51 52
	const char *p;
	char *h;
	int i;
53
	retvalue r;
Bernhard Link's avatar
Bernhard Link committed
54

55
	for( p = filename+1, i = 1 ; *p != '\0' ; p++,i++) {
Bernhard Link's avatar
Bernhard Link committed
56 57
		if( *p == '/' ) {
			h = strndup(filename,i);
58 59
			r = dirs_check(h);
			if( RET_WAS_ERROR(r) ) {
Bernhard Link's avatar
Bernhard Link committed
60
				free(h);
61
				return r;
Bernhard Link's avatar
Bernhard Link committed
62 63 64 65
			}
			free(h);
		}
	}
66
	return RET_OK;
Bernhard Link's avatar
Bernhard Link committed
67 68 69
}

/* create dirname and any '/'-seperated part of it */
70 71 72 73 74 75 76
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;
Bernhard Link's avatar
Bernhard Link committed
77
}