view src/lib/mkdir-parents.c @ 1483:836c952e17ec HEAD

More robust error handling for mbox.
author Timo Sirainen <tss@iki.fi>
date Sun, 18 May 2003 19:02:46 +0300
parents
children 9ce3f3f950c5
line wrap: on
line source

/* Copyright (c) 2003 Timo Sirainen */

#include "lib.h"
#include "mkdir-parents.h"

#include <sys/stat.h>

int mkdir_parents(const char *path, mode_t mode)
{
	const char *p;

	if (mkdir(path, mode) < 0 && errno != EEXIST) {
		if (errno != ENOENT)
			return -1;

		p = strrchr(path, '/');
		if (p == NULL || p == path)
			return -1; /* shouldn't happen */

		t_push();
		if (mkdir_parents(t_strdup_until(path, p), mode) < 0) {
			t_pop();
			return -1;
		}
		t_pop();

		/* should work now */
		if (mkdir(path, mode) < 0 && errno != EEXIST)
			return -1;
	}

	return 0;
}