view src/lib/mkdir-parents.c @ 7086:7ed926ed7aa4 HEAD

Updated copyright notices to include year 2008.
author Timo Sirainen <tss@iki.fi>
date Tue, 01 Jan 2008 22:05:21 +0200
parents 414c9d631a81
children e6693a0ec8e1
line wrap: on
line source

/* Copyright (c) 2003-2008 Dovecot authors, see the included COPYING file */

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

#include <sys/stat.h>

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

	/* EISDIR check is for BSD/OS which returns it if path contains '/'
	   at the end and it exists.

	   ENOSYS check is for NFS mount points.
	*/
	if (mkdir(path, mode) < 0 && errno != EEXIST &&
	    errno != EISDIR && errno != ENOSYS) {
		if (errno != ENOENT)
			return -1;

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

		T_FRAME(
			ret = mkdir_parents(t_strdup_until(path, p), mode);
		);
		if (ret < 0)
			return -1;

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

	return 0;
}