1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/util.c Mon Feb 04 10:12:04 2008 -0500
1.3 @@ -0,0 +1,68 @@
1.4 +#include <limits.h>
1.5 +#include <string.h>
1.6 +#include <sys/stat.h>
1.7 +#include <stdlib.h>
1.8 +#include <stdio.h>
1.9 +#include <unistd.h>
1.10 +
1.11 +int
1.12 +razor_create_dir(const char *root, const char *path)
1.13 +{
1.14 + char buffer[PATH_MAX], *p;
1.15 + const char *slash, *next;
1.16 + struct stat buf;
1.17 +
1.18 + /* Create all sub-directories in dir and then create name. We
1.19 + * know root exists and is a dir, root does not end in a '/',
1.20 + * and path has a leading '/'. */
1.21 +
1.22 + strcpy(buffer, root);
1.23 + p = buffer + strlen(buffer);
1.24 + slash = path;
1.25 + for (slash = path; slash[1] != '\0'; slash = next) {
1.26 + next = strchr(slash + 1, '/');
1.27 + memcpy(p, slash, next - slash);
1.28 + p += next - slash;
1.29 + *p = '\0';
1.30 +
1.31 + if (stat(buffer, &buf) == 0) {
1.32 + if (!S_ISDIR(buf.st_mode)) {
1.33 + fprintf(stderr,
1.34 + "%s exists but is not a directory\n",
1.35 + buffer);
1.36 + return -1;
1.37 + }
1.38 + } else if (mkdir(buffer, 0777) < 0) {
1.39 + fprintf(stderr, "failed to make directory %s: %m\n",
1.40 + buffer);
1.41 + return -1;
1.42 + }
1.43 +
1.44 + /* FIXME: What to do about permissions for dirs we
1.45 + * have to create but are not in the cpio archive? */
1.46 + }
1.47 +
1.48 + return 0;
1.49 +}
1.50 +
1.51 +int
1.52 +razor_write(int fd, const void *data, size_t size)
1.53 +{
1.54 + size_t rest;
1.55 + ssize_t written;
1.56 + const unsigned char *p;
1.57 +
1.58 + rest = size;
1.59 + p = data;
1.60 + while (rest > 0) {
1.61 + written = write(fd, p, rest);
1.62 + if (written < 0) {
1.63 + fprintf(stderr, "write error: %m\n");
1.64 + return -1;
1.65 + }
1.66 + rest -= written;
1.67 + p += written;
1.68 + }
1.69 +
1.70 + return 0;
1.71 +}