--- /var/folders/kF/kFsRoAvkGg822Pmk7AchLk+++TI/-Tmp-/.XcodeSCMTemp/temp96D0C9F7.HEAD.cache-file.c	2010-02-02 18:46:29.000000000 +0100
+++ /Users/varenet/ESIEE/mod_musicindex/src/cache-file.c	2010-02-02 18:46:18.000000000 +0100
@@ -220,7 +220,7 @@ static void file_cache_remove_dir(reques
 	struct stat	origdirstat;
 	const char 	*origdir = NULL;
 
-	fchdir(dirfd(cachedir));			/* on se place dans le repertoire de cache. XXX Pas de test ici, tout est verifie avant (en principe). */
+	fchdir(dirfd(cachedir));			/* on se place dans le repertoire de cache. XXX Pas de test, au pire fail sur readdir() */
 	while ((cachedirent = readdir(cachedir))) {	/* on parcourt le repertoire */
 		if (ISDOT(cachedirent->d_name))		/* We'd rather avoid trying to remove the whole filesystem... */
 			continue;
@@ -256,28 +256,6 @@ static void file_cache_remove_dir(reques
 }
 
 /**
- * Initializes flat file cache subsystem.
- *
- * Basically we do nothing more here than creating the root cache folder.
- *
- * @param s Apache server_rec struct to handle log writings.
- * @param cache_setup configuration string in which the path to the cache root can be found.
- *
- * @return CA_OK on succes, CA_FATAL otherwise.
- */
-static short file_cache_init(server_rec *s, const char *const cache_setup)
-{
-	if (chdir("/") || file_cache_make_dir(NULL, cache_setup + 1))	/* since we've chdir'd, we send the path without the leading '/' */
-		goto error_out;
-
-	return CA_OK;
-
-error_out:
-	mi_serror("Cannot setup file cache!");
-	return CA_FATAL;
-}
-
-/**
  * Checks if a directory already exists in the cache.
  *
  * This function takes advantage of the standard behaviour of the cache
@@ -304,7 +282,7 @@ static void* cache_file_opendir(request_
 	DIR		*cachedir = NULL;
 	struct stat	cachedirstat, dirstat;
 
-	if (!path)
+	if (!path || !conf->cache_setup)
 		return NULL;
 
 	/* Bear in mind we're chdir'd from now on. */
@@ -374,7 +352,8 @@ static mu_ent *file_make_cache_entry(req
 	struct mi_data_buffer	*data_buffer = NULL;
 
 	/* Bear in mind we're chdir'd from now on. */
-	chdir((char *)(conf->cache_setup));
+	if (unlikely(chdir((char *)(conf->cache_setup))))
+		return p;
 
 	/* Actually check for the file in the cache, open it if possible.
 	 * "+ 1" offset to suppress leading '/'.
@@ -476,7 +455,8 @@ static void cache_file_write(request_rec
 	if (p->filetype < 0)
 		return;
 	
-	chdir((char *)conf->cache_setup);	/* Par securite on se re-chdir(). XXX Considere sans echec */
+	if (chdir((char *)conf->cache_setup))
+		return;
 	
 	/* Dev note: O_EXLOCK is BSD specific. */
 	fdesc = open(names->filename + 1, O_WRONLY|O_NONBLOCK|O_CREAT, S_IRUSR|S_IWUSR);
@@ -529,18 +509,29 @@ int cache_file_setup(cmd_parms *cmd, con
 {
 	server_rec *s = cmd->server;
 	static const char biniou[] = "file://";
+	int ret = 1;
 
 	if (strncmp(biniou, setup_string, 7) == 0) {
-		conf->cache_setup = apr_pstrdup(cmd->pool, setup_string+6);
-		if (!conf->cache_setup)
+		ret = -1;
+		const char *csetup = apr_pstrdup(cmd->pool, setup_string+6);
+		if (!csetup)
 			goto exit;
-		if (chdir((char *)(conf->cache_setup))) {
+		if (csetup[0] != '/') {
+			/* for now we only work with absolute paths */
+			mi_serror("Non absolute cache directory path");
+			goto exit;
+		}
+		if ( (access(csetup, X_OK|W_OK)) || (chdir((char *)(csetup))) ) {
 			mi_serror("%s", strerror(errno));
 			goto exit;
 		}
+		conf->cache_setup = csetup;
 		conf->cache = &cache_backend_file;
-		return file_cache_init(s, conf->cache_setup);
+		ret = 0;
 	}
+	
 exit:
-	return 1;
+	if (-1 == ret)
+		mi_serror("Error setting up file cache!");
+	return ret;
 }
