util.c
author Dan Winship <danw@gnome.org>
Wed Mar 12 13:27:26 2008 -0400 (2008-03-12)
changeset 170 44d7bec477d5
parent 136 eef2b734f2cc
child 186 7f45d0401e37
permissions -rw-r--r--
"requires x > n" matches "provides x", but "obsoletes x < n" doesn't
     1 #include <limits.h>
     2 #include <string.h>
     3 #include <sys/stat.h>
     4 #include <stdlib.h>
     5 #include <stdio.h>
     6 #include <unistd.h>
     7 
     8 #include "razor-internal.h"
     9 
    10 int
    11 razor_create_dir(const char *root, const char *path)
    12 {
    13 	char buffer[PATH_MAX], *p;
    14 	const char *slash, *next;
    15 	struct stat buf;
    16 
    17 	/* Create all sub-directories in dir and then create name. We
    18 	 * know root exists and is a dir, root does not end in a '/',
    19 	 * and path has a leading '/'. */
    20 
    21 	strcpy(buffer, root);
    22 	p = buffer + strlen(buffer);
    23 	slash = path;
    24 	for (slash = path; *slash != '\0'; slash = next) {
    25 		next = strchr(slash + 1, '/');
    26 		if (next == NULL)
    27 			next = slash + strlen(slash);
    28 
    29 		memcpy(p, slash, next - slash);
    30 		p += next - slash;
    31 		*p = '\0';
    32 
    33 		if (stat(buffer, &buf) == 0) {
    34 			if (!S_ISDIR(buf.st_mode)) {
    35 				fprintf(stderr,
    36 					"%s exists but is not a directory\n",
    37 					buffer);
    38 				return -1;
    39 			}
    40 		} else if (mkdir(buffer, 0777) < 0) {
    41 			fprintf(stderr, "failed to make directory %s: %m\n",
    42 				buffer);
    43 			return -1;
    44 		}
    45 
    46 		/* FIXME: What to do about permissions for dirs we
    47 		 * have to create but are not in the cpio archive? */
    48 	}
    49 
    50 	return 0;
    51 }
    52 
    53 int
    54 razor_write(int fd, const void *data, size_t size)
    55 {
    56 	size_t rest;
    57 	ssize_t written;
    58 	const unsigned char *p;
    59 
    60 	rest = size;
    61 	p = data;
    62 	while (rest > 0) {
    63 		written = write(fd, p, rest);
    64 		if (written < 0) {
    65 			fprintf(stderr, "write error: %m\n");
    66 			return -1;
    67 		}
    68 		rest -= written;
    69 		p += written;
    70 	}
    71 
    72 	return 0;
    73 }