Drop drive letter from path to razor root when RAZOR_ROOT set.
authorJ. Ali Harlow <ali@juiblex.co.uk>
Sat Aug 23 16:07:09 2014 +0100 (2014-08-23)
changeset 441cf499fd51df7
parent 440 48204dea0b9f
child 442 c4bcba8023a9
Drop drive letter from path to razor root when RAZOR_ROOT set.

If the RAZOR_ROOT environment variable was set to eg., /root then on
Microsoft Windows we were trying to use paths such as /rootC:/Programs
which is obviously wrong. Instead we should drop the drive letter
giving paths of the form /root/Programs. Note that the drive letter is
_not_ migrated to C:/root/Programs: If a root of C:/root was desired
then RAZOR_ROOT would have been set to C:/root.
librazor/atomic-ktm.c
librazor/atomic-none.c
librazor/razor-internal.h
librazor/razor.h
librazor/root.c
librazor/util.c
     1.1 --- a/librazor/atomic-ktm.c	Sat Aug 23 11:13:48 2014 +0100
     1.2 +++ b/librazor/atomic-ktm.c	Sat Aug 23 16:07:09 2014 +0100
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (C) 2011-2012  J. Ali Harlow <ali@juiblex.co.uk>
     1.6 + * Copyright (C) 2011, 2012, 2014  J. Ali Harlow <ali@juiblex.co.uk>
     1.7   *
     1.8   * This program is free software; you can redistribute it and/or modify
     1.9   * it under the terms of the GNU General Public License as published by
    1.10 @@ -36,9 +36,6 @@
    1.11  #include "razor.h"
    1.12  #include "razor-internal.h"
    1.13  
    1.14 -#define RAZOR_ASCII_ISALPHA(c)	\
    1.15 -			((c) >= 'A' && (c) <= 'Z' || (c) >= 'a' && (c) <= 'z')
    1.16 -
    1.17  static int
    1.18  razor_valid_root_name2(const wchar_t *name)
    1.19  {
    1.20 @@ -187,7 +184,7 @@
    1.21  		return -1;
    1.22  
    1.23  	buffer = razor_wstr_create(root, -1);
    1.24 -	slash = path;
    1.25 +	slash = buffer->len ? SKIP_DRIVE_LETTER(path) : path;
    1.26  
    1.27  	for (; *slash != '\0'; slash = next) {
    1.28  		next = strpbrk(slash + 1, "/\\");
     2.1 --- a/librazor/atomic-none.c	Sat Aug 23 11:13:48 2014 +0100
     2.2 +++ b/librazor/atomic-none.c	Sat Aug 23 16:07:09 2014 +0100
     2.3 @@ -1,5 +1,5 @@
     2.4  /*
     2.5 - * Copyright (C) 2011-2012  J. Ali Harlow <ali@juiblex.co.uk>
     2.6 + * Copyright (C) 2011, 2012, 2014  J. Ali Harlow <ali@juiblex.co.uk>
     2.7   *
     2.8   * This program is free software; you can redistribute it and/or modify
     2.9   * it under the terms of the GNU General Public License as published by
    2.10 @@ -78,7 +78,8 @@
    2.11  
    2.12  	strcpy(buffer, root);
    2.13  	p = buffer + strlen(buffer);
    2.14 -	slash = path;
    2.15 +	slash = (p > buffer) ? SKIP_DRIVE_PATH(path) : path;
    2.16 +
    2.17  	for (slash = path; *slash != '\0'; slash = next) {
    2.18  #ifdef MSWIN_API
    2.19  		next = strpbrk(slash + 1, "/\\");
     3.1 --- a/librazor/razor-internal.h	Sat Aug 23 11:13:48 2014 +0100
     3.2 +++ b/librazor/razor-internal.h	Sat Aug 23 16:07:09 2014 +0100
     3.3 @@ -346,4 +346,15 @@
     3.4  int razor_allow_all_root_names(void);
     3.5  int razor_valid_root_name(const char *name);
     3.6  
     3.7 +#define RAZOR_ASCII_ISALPHA(c) \
     3.8 +	((c) >= 'A' && (c) <= 'Z' || (c) >= 'a' && (c) <= 'z')
     3.9 +
    3.10 +#ifdef MSWIN_API
    3.11 +#define SKIP_DRIVE_LETTER(path) \
    3.12 +	((RAZOR_ASCII_ISALPHA(path[0]) && (path)[1] == ':') ? \
    3.13 +	   (path) + 2 : (path))
    3.14 +#else
    3.15 +#define SKIP_DRIVE_LETTER(path) (path)
    3.16 +#endif /* MSWIN_API */
    3.17 +
    3.18  #endif /* _RAZOR_INTERNAL_H_ */
     4.1 --- a/librazor/razor.h	Sat Aug 23 11:13:48 2014 +0100
     4.2 +++ b/librazor/razor.h	Sat Aug 23 16:07:09 2014 +0100
     4.3 @@ -590,6 +590,8 @@
     4.4  
     4.5  char *razor_concat(const char *s, ...) RAZOR_MALLOC RAZOR_NULL_TERMINATED;
     4.6  
     4.7 +char *razor_path_add_root(const char *path, const char *root) RAZOR_MALLOC;
     4.8 +
     4.9  const char *razor_system_arch(void);
    4.10  
    4.11  #endif /* _RAZOR_H_ */
     5.1 --- a/librazor/root.c	Sat Aug 23 11:13:48 2014 +0100
     5.2 +++ b/librazor/root.c	Sat Aug 23 16:07:09 2014 +0100
     5.3 @@ -102,7 +102,7 @@
     5.4  	}
     5.5  
     5.6  	file = razor_concat(razor_root_path, "/", system_repo_filename, NULL);
     5.7 -	path = razor_concat(root, file, NULL);
     5.8 +	path = razor_path_add_root(file, root);
     5.9  	retval = !stat(path, &buf);
    5.10  	if (retval) {
    5.11  		razor_set_error(error, NULL,
    5.12 @@ -133,7 +133,7 @@
    5.13  razor_root_open(const char *root, struct razor_error **error)
    5.14  {
    5.15  	struct razor_root *image;
    5.16 -	char *lock_path;
    5.17 +	char *s, *lock_path;
    5.18  	int r;
    5.19  
    5.20  	assert (root != NULL);
    5.21 @@ -152,8 +152,9 @@
    5.22  		return NULL;
    5.23  	}
    5.24  
    5.25 -	lock_path = razor_concat(root, razor_root_path, "/",
    5.26 -				 system_lock_filename, NULL);
    5.27 +	s = razor_concat(razor_root_path, "/", system_lock_filename, NULL);
    5.28 +	lock_path = razor_path_add_root(s, root);
    5.29 +	free(s);
    5.30  
    5.31  	r = razor_set_aquire_lock(image->system, lock_path, 1);
    5.32  
    5.33 @@ -167,8 +168,9 @@
    5.34  		return NULL;
    5.35  	}
    5.36  
    5.37 -	image->path = razor_concat(root, razor_root_path, "/",
    5.38 -				   system_repo_filename, NULL);
    5.39 +	s = razor_concat(razor_root_path, "/", system_repo_filename, NULL);
    5.40 +	image->path = razor_path_add_root(s, root);
    5.41 +	free(s);
    5.42  
    5.43  	if (razor_set_bind_sections(image->system, image->path,
    5.44  				    RAZOR_SET_PRIVATE, error)) {
    5.45 @@ -184,7 +186,7 @@
    5.46  RAZOR_EXPORT struct razor_set *
    5.47  razor_root_open_read_only(const char *root, struct razor_error **error)
    5.48  {
    5.49 -	char *path;
    5.50 +	char *s, *path;
    5.51  	struct razor_set *set;
    5.52  
    5.53  	assert (root != NULL);
    5.54 @@ -196,8 +198,10 @@
    5.55  		return NULL;
    5.56  	}
    5.57  
    5.58 -	path = razor_concat(root, razor_root_path, "/", system_lock_filename,
    5.59 -			    NULL);
    5.60 +	s = razor_concat(razor_root_path, "/", system_lock_filename, NULL);
    5.61 +	path = razor_path_add_root(s, root);
    5.62 +	free(s);
    5.63 +
    5.64  	if (razor_set_aquire_lock(set, path, 0) < 0) {
    5.65  		razor_set_error(error, NULL,
    5.66  				"Failed to aquire non-exclusive system lock");
    5.67 @@ -207,8 +211,10 @@
    5.68  	}
    5.69  
    5.70  	free(path);
    5.71 -	path = razor_concat(root, razor_root_path, "/", system_repo_filename,
    5.72 -			    NULL);
    5.73 +
    5.74 +	s = razor_concat(razor_root_path, "/", system_repo_filename, NULL);
    5.75 +	path = razor_path_add_root(s, root);
    5.76 +	free(s);
    5.77  
    5.78  	if (razor_set_bind_sections(set, path, 0, error)) {
    5.79  		razor_set_unref(set);
     6.1 --- a/librazor/util.c	Sat Aug 23 11:13:48 2014 +0100
     6.2 +++ b/librazor/util.c	Sat Aug 23 16:07:09 2014 +0100
     6.3 @@ -1,7 +1,7 @@
     6.4  /*
     6.5   * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     6.6   * Copyright (C) 2008  Red Hat, Inc
     6.7 - * Copyright (C) 2009, 2011, 2012  J. Ali Harlow <ali@juiblex.co.uk>
     6.8 + * Copyright (C) 2009, 2011, 2012, 2014  J. Ali Harlow <ali@juiblex.co.uk>
     6.9   *
    6.10   * This program is free software; you can redistribute it and/or modify
    6.11   * it under the terms of the GNU General Public License as published by
    6.12 @@ -341,6 +341,24 @@
    6.13  	return concat;
    6.14  }
    6.15  
    6.16 +/**
    6.17 + * razor_path_add_root:
    6.18 + *
    6.19 + * Adds a root to a path. path must be an absolute pathname. In POSIX
    6.20 + * environments this is equivalent to the concationation of root and path.
    6.21 + * In Microsoft Windows an adjustment may need to be made for a drive letter
    6.22 + * in path (which will be dropped).
    6.23 + *
    6.24 + * Returns: The new pathname.
    6.25 + **/
    6.26 +RAZOR_EXPORT char *razor_path_add_root(const char *path, const char *root)
    6.27 +{
    6.28 +	if (root && *root)
    6.29 +		return razor_concat(root, SKIP_DRIVE_LETTER(path), NULL);
    6.30 +	else
    6.31 +		return strdup(path);
    6.32 +}
    6.33 +
    6.34  RAZOR_EXPORT const char *razor_system_arch(void)
    6.35  {
    6.36  #ifdef MSWIN_API