util.c
author Dan Winship <danw@gnome.org>
Fri Feb 29 12:45:08 2008 -0500 (2008-02-29)
changeset 138 49deac048d07
parent 91 6884cefd1b8c
child 149 43cac7931189
permissions -rw-r--r--
implement file dependencies for installs

removes are trickier because there are no backlinks from the files array
the properties array, so there's currently no way to efficiently determine
what packages are affected by the removal of a particular file
     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[1] != '\0'; slash = next) {
    25 		next = strchr(slash + 1, '/');
    26 		memcpy(p, slash, next - slash);
    27 		p += next - slash;
    28 		*p = '\0';
    29 
    30 		if (stat(buffer, &buf) == 0) {
    31 			if (!S_ISDIR(buf.st_mode)) {
    32 				fprintf(stderr,
    33 					"%s exists but is not a directory\n",
    34 					buffer);
    35 				return -1;
    36 			}
    37 		} else if (mkdir(buffer, 0777) < 0) {
    38 			fprintf(stderr, "failed to make directory %s: %m\n",
    39 				buffer);
    40 			return -1;
    41 		}
    42 
    43 		/* FIXME: What to do about permissions for dirs we
    44 		 * have to create but are not in the cpio archive? */
    45 	}
    46 
    47 	return 0;
    48 }
    49 
    50 int
    51 razor_write(int fd, const void *data, size_t size)
    52 {
    53 	size_t rest;
    54 	ssize_t written;
    55 	const unsigned char *p;
    56 
    57 	rest = size;
    58 	p = data;
    59 	while (rest > 0) {
    60 		written = write(fd, p, rest);
    61 		if (written < 0) {
    62 			fprintf(stderr, "write error: %m\n");
    63 			return -1;
    64 		}
    65 		rest -= written;
    66 		p += written;
    67 	}
    68 
    69 	return 0;
    70 }