librazor/util.c
changeset 404 bd740859c4a0
parent 377 5549419824b4
child 406 5ab137def3d1
     1.1 --- a/librazor/util.c	Wed Jul 08 22:14:16 2009 +0100
     1.2 +++ b/librazor/util.c	Thu Nov 10 10:35:21 2011 +0000
     1.3 @@ -1,7 +1,7 @@
     1.4  /*
     1.5   * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     1.6   * Copyright (C) 2008  Red Hat, Inc
     1.7 - * Copyright (C) 2009  J. Ali Harlow <ali@juiblex.co.uk>
     1.8 + * Copyright (C) 2009, 2011  J. Ali Harlow <ali@juiblex.co.uk>
     1.9   *
    1.10   * This program is free software; you can redistribute it and/or modify
    1.11   * it under the terms of the GNU General Public License as published by
    1.12 @@ -33,6 +33,8 @@
    1.13  #ifdef MSWIN_API
    1.14  #include <windows.h>
    1.15  #include <direct.h>
    1.16 +#else
    1.17 +#include <sys/utsname.h>
    1.18  #endif
    1.19  #if HAVE_SYS_MMAN_H
    1.20  #include <sys/mman.h>
    1.21 @@ -46,137 +48,9 @@
    1.22  #define O_BINARY	0
    1.23  #endif
    1.24  
    1.25 -#define RAZOR_ASCII_ISALPHA(c)	\
    1.26 -			((c) >= 'A' && (c) <= 'Z' || (c) >= 'a' && (c) <= 'z')
    1.27 -
    1.28  /* Required by gnulib on non-libc platforms */
    1.29  char *program_name = "librazor";
    1.30  
    1.31 -static int allow_all_root_names = 0;
    1.32 -
    1.33 -/*
    1.34 - * Primarily intended for testing named roots under UNIX platforms.
    1.35 - */
    1.36 -RAZOR_EXPORT void razor_disable_root_name_checks(int disable)
    1.37 -{
    1.38 -	allow_all_root_names = disable;
    1.39 -}
    1.40 -
    1.41 -static int razor_valid_root_name(const char *name)
    1.42 -{
    1.43 -	if (allow_all_root_names)
    1.44 -		return !strchr(name,'/');
    1.45 -
    1.46 -#ifdef MSWIN_API
    1.47 -	return RAZOR_ASCII_ISALPHA(name[0]) && name[1] == ':' &&
    1.48 -	       name[2] == '\0';
    1.49 -#else
    1.50 -	return name[0] == '\0';
    1.51 -#endif
    1.52 -}
    1.53 -
    1.54 -int
    1.55 -razor_create_dir(const char *root, const char *path)
    1.56 -{
    1.57 -	char buffer[PATH_MAX], *p;
    1.58 -	const char *slash, *next;
    1.59 -	struct stat buf;
    1.60 -
    1.61 -	/* Create all sub-directories in dir. We know root exists and
    1.62 -	 * is a dir, root does not end in a '/', and path either has a
    1.63 -	 * leading '/' or (on MS-Windows only) root is the empty string
    1.64 -	 * and path starts with drive (eg., "c:/windows"). */
    1.65 -
    1.66 -	strcpy(buffer, root);
    1.67 -	p = buffer + strlen(buffer);
    1.68 -	slash = path;
    1.69 -	for (slash = path; *slash != '\0'; slash = next) {
    1.70 -		next = strchr(slash + 1, '/');
    1.71 -		if (next == NULL)
    1.72 -			break;
    1.73 -
    1.74 -		memcpy(p, slash, next - slash);
    1.75 -		p += next - slash;
    1.76 -		*p = '\0';
    1.77 -
    1.78 -		if (razor_valid_root_name(buffer))
    1.79 -			continue;
    1.80 -
    1.81 -		if (stat(buffer, &buf) == 0) {
    1.82 -			if (!S_ISDIR(buf.st_mode)) {
    1.83 -				fprintf(stderr,
    1.84 -					"%s exists but is not a directory\n",
    1.85 -					buffer);
    1.86 -				return -1;
    1.87 -			}
    1.88 -		} else if (mkdir(buffer, 0777) < 0) {
    1.89 -			fprintf(stderr, "failed to make directory %s: %s\n",
    1.90 -				buffer, strerror(errno));
    1.91 -			return -1;
    1.92 -		}
    1.93 -
    1.94 -		/* FIXME: What to do about permissions for dirs we
    1.95 -		 * have to create but are not in the cpio archive? */
    1.96 -	}
    1.97 -
    1.98 -	return 0;
    1.99 -}
   1.100 -
   1.101 -int
   1.102 -razor_remove(const char *path)
   1.103 -{
   1.104 -#ifdef MSWIN_API
   1.105 -	DWORD err;
   1.106 -
   1.107 -	if (DeleteFile(path))
   1.108 -		return 0;
   1.109 -
   1.110 -	err = GetLastError();
   1.111 -	if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND)
   1.112 -		return 0;
   1.113 -
   1.114 -	if (SetFileAttributes(path, FILE_ATTRIBUTE_NORMAL) && DeleteFile(path))
   1.115 -		return 0;
   1.116 -
   1.117 -	if (RemoveDirectory(path) || GetLastError() == ERROR_DIR_NOT_EMPTY)
   1.118 -		return 0;
   1.119 -
   1.120 -	/*
   1.121 -	 * It would be tempting to use:
   1.122 -	 * 	MoveFileEx(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT)
   1.123 -	 * but unless we can guarantee that the system will be rebooted
   1.124 -	 * before we (or some other application) write another file with the
   1.125 -	 * same path, this is likely to cause more problems than it solves.
   1.126 -	 */
   1.127 -
   1.128 -	/* Use remove() as a fallback so that errno is set appropriately */
   1.129 -#endif
   1.130 -
   1.131 -	return remove(path);
   1.132 -}
   1.133 -
   1.134 -int
   1.135 -razor_write(int fd, const void *data, size_t size)
   1.136 -{
   1.137 -	size_t rest;
   1.138 -	ssize_t written;
   1.139 -	const unsigned char *p;
   1.140 -
   1.141 -	rest = size;
   1.142 -	p = data;
   1.143 -	while (rest > 0) {
   1.144 -		written = write(fd, p, rest);
   1.145 -		if (written < 0) {
   1.146 -			perror("write error");
   1.147 -			return -1;
   1.148 -		}
   1.149 -		rest -= written;
   1.150 -		p += written;
   1.151 -	}
   1.152 -
   1.153 -	return 0;
   1.154 -}
   1.155 -
   1.156  void *
   1.157  razor_file_get_contents(const char *filename, size_t *length)
   1.158  {
   1.159 @@ -225,7 +99,6 @@
   1.160  	return addr;
   1.161  }
   1.162  
   1.163 -int
   1.164  razor_file_free_contents(void *addr, size_t length)
   1.165  {
   1.166  #if HAVE_SYS_MMAN_H
   1.167 @@ -396,3 +269,66 @@
   1.168  	array_release(&env->string_pool);
   1.169  	array_release(&env->vars);
   1.170  }
   1.171 +
   1.172 +RAZOR_EXPORT char *razor_concat(const char *s, ...)
   1.173 +{
   1.174 +	va_list args;
   1.175 +	const char *string;
   1.176 +	char *concat;
   1.177 +	size_t n, len;
   1.178 +
   1.179 +	va_start(args, s);
   1.180 +
   1.181 +	len = strlen(s);
   1.182 +	while((string = va_arg(args, const char *)))
   1.183 +		len += strlen(string);
   1.184 +
   1.185 +	va_end(args);
   1.186 +
   1.187 +	concat = malloc(len + 1);
   1.188 +
   1.189 +	if (!concat)
   1.190 +		return NULL;
   1.191 +
   1.192 +	va_start(args, s);
   1.193 +
   1.194 +	len = strlen(s);
   1.195 +	memcpy(concat, s, len);
   1.196 +	n = len;
   1.197 +	while((string = va_arg(args, const char *))) {
   1.198 +		len = strlen(string);
   1.199 +		memcpy(concat + n, string, len);
   1.200 +		n += len;
   1.201 +	}
   1.202 +
   1.203 +	va_end(args);
   1.204 +
   1.205 +	concat[n] = '\0';
   1.206 +
   1.207 +	return concat;
   1.208 +}
   1.209 +
   1.210 +RAZOR_EXPORT const char *razor_system_arch(void)
   1.211 +{
   1.212 +#ifdef MSWIN_API
   1.213 +	SYSTEM_INFO si;
   1.214 +
   1.215 +	GetNativeSystemInfo(&si);
   1.216 +	switch(si.wProcessorArchitecture)
   1.217 +	{
   1.218 +		case PROCESSOR_ARCHITECTURE_INTEL:
   1.219 +			return "i686";
   1.220 +		case PROCESSOR_ARCHITECTURE_AMD64:
   1.221 +			return "x86_64";
   1.222 +		default:
   1.223 +			return NULL;
   1.224 +	}
   1.225 +#else
   1.226 +	static struct utsname un;
   1.227 +
   1.228 +	if (uname(&un))
   1.229 +		return NULL;
   1.230 +	else
   1.231 +		return un.machine;
   1.232 +#endif
   1.233 +}