librazor/util.c
changeset 370 a3e288343fe7
parent 339 159067260aad
child 372 6e93e5485947
     1.1 --- a/librazor/util.c	Fri Jan 09 12:32:57 2009 +0000
     1.2 +++ b/librazor/util.c	Thu Jul 02 11:31:03 2009 +0100
     1.3 @@ -30,19 +30,49 @@
     1.4  #include <errno.h>
     1.5  #include <unistd.h>
     1.6  #include <fcntl.h>
     1.7 +#ifdef MSWIN_API
     1.8 +#include <direct.h>
     1.9 +#endif
    1.10  #if HAVE_SYS_MMAN_H
    1.11  #include <sys/mman.h>
    1.12  #endif
    1.13  
    1.14 +#include "razor.h"
    1.15  #include "razor-internal.h"
    1.16  
    1.17  #ifndef O_BINARY
    1.18  #define O_BINARY	0
    1.19  #endif
    1.20  
    1.21 +#define RAZOR_ASCII_ISALPHA(c)	\
    1.22 +			((c) >= 'A' && (c) <= 'Z' || (c) >= 'a' && (c) <= 'z')
    1.23 +
    1.24  /* Required by gnulib on non-libc platforms */
    1.25  char *program_name = "librazor";
    1.26  
    1.27 +static int allow_all_root_names = 0;
    1.28 +
    1.29 +/*
    1.30 + * Primarily intended for testing named roots under UNIX platforms.
    1.31 + */
    1.32 +RAZOR_EXPORT void razor_disable_root_name_checks(int disable)
    1.33 +{
    1.34 +	allow_all_root_names = disable;
    1.35 +}
    1.36 +
    1.37 +static int razor_valid_root_name(const char *name)
    1.38 +{
    1.39 +	if (allow_all_root_names)
    1.40 +		return !strchr(name,'/');
    1.41 +
    1.42 +#ifdef MSWIN_API
    1.43 +	return RAZOR_ASCII_ISALPHA(name[0]) && name[1] == ':' &&
    1.44 +	       name[2] == '\0';
    1.45 +#else
    1.46 +	return name[0] == '\0';
    1.47 +#endif
    1.48 +}
    1.49 +
    1.50  int
    1.51  razor_create_dir(const char *root, const char *path)
    1.52  {
    1.53 @@ -51,8 +81,9 @@
    1.54  	struct stat buf;
    1.55  
    1.56  	/* Create all sub-directories in dir. We know root exists and
    1.57 -	 * is a dir, root does not end in a '/', and path has a
    1.58 -	 * leading '/'. */
    1.59 +	 * is a dir, root does not end in a '/', and path either has a
    1.60 +	 * leading '/' or (on MS-Windows only) root is the empty string
    1.61 +	 * and path starts with drive (eg., "c:/windows"). */
    1.62  
    1.63  	strcpy(buffer, root);
    1.64  	p = buffer + strlen(buffer);
    1.65 @@ -66,6 +97,9 @@
    1.66  		p += next - slash;
    1.67  		*p = '\0';
    1.68  
    1.69 +		if (razor_valid_root_name(buffer))
    1.70 +			continue;
    1.71 +
    1.72  		if (stat(buffer, &buf) == 0) {
    1.73  			if (!S_ISDIR(buf.st_mode)) {
    1.74  				fprintf(stderr,