first rev of some stupid code for reading/writing cpio archives
[rcpio.git] / creator.c
diff --git a/creator.c b/creator.c
new file mode 100644 (file)
index 0000000..542d697
--- /dev/null
+++ b/creator.c
@@ -0,0 +1,184 @@
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <sys/mman.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <assert.h>
+
+#include "cpio-fmt.h"
+
+static void write_cpio_record(const char *ptr, const char *data, struct stat *st)
+{
+    struct cpio_hdr *hdr;
+    char *cpio_rec;
+    int data_start;
+    int size;
+
+    data_start = size = pad(sizeof(*hdr) + strlen(ptr) + 1);
+    if (data)
+       size += pad(strlen(data)+1);
+
+    hdr = malloc(size);
+    cpio_rec = (char *)hdr; 
+    memset(hdr, 0, size);
+
+    sprintf(hdr->c_magic, "%6.6s", CPIO_MAGIC);
+    sprintf(hdr->c_ino, "%8.8x", 0);
+    sprintf(hdr->c_mode, "%8.8x", st->st_mode);
+    sprintf(hdr->c_uid, "%8.8x", 0);
+    sprintf(hdr->c_gid, "%8.8x", 0);
+    sprintf(hdr->c_nlink, "%8.8x", 0);
+    sprintf(hdr->c_mtime, "%8.8x", 0);
+    sprintf(hdr->c_filesize, "%8.8x", data ? strlen(data)+1 : 0);
+    sprintf(hdr->c_devmajor, "%8.8x", 0);
+    sprintf(hdr->c_devminor, "%8.8x", 0);
+    sprintf(hdr->c_rdevmajor, "%8.8x", major(st->st_rdev));
+    sprintf(hdr->c_rdevminor, "%8.8x", minor(st->st_rdev));
+    sprintf(hdr->c_namesize, "%8.8x", strlen(ptr)+1);
+    sprintf(hdr->c_checksum, "%8.8x", 0);
+    memcpy(hdr+1, ptr, strlen(ptr));
+    if (data)
+       memcpy(cpio_rec+data_start, data, strlen(data));
+
+    fprintf(stderr, "got size %d\n", size);
+    fflush(stdout);
+    write(STDOUT_FILENO, cpio_rec, size);
+}
+
+static int parse_rwx(char thing)
+{
+    switch (thing) {
+       case 'r': return 4;
+       case 'w': return 2;
+       case 'x': return 1;
+       case '-': return 0;
+    }
+    abort();
+}
+
+static int parse_rwx_group(char **data, int mult)
+{
+    char *ptr = *data;
+    int x = parse_rwx(*ptr++) + parse_rwx(*ptr++) + parse_rwx(*ptr++);
+    *data = ptr;
+    return x * mult;
+}
+
+static int parse_mode(char **data, unsigned int *mode)
+{
+    char *ptr = *data;
+
+    switch (*ptr) {
+       case 'c':
+           *mode |= S_IFCHR; break;
+       case 'l':
+           *mode |= S_IFLNK; break;
+       case 'd':
+           *mode |= S_IFDIR; break;
+       case 'b':
+           *mode |= S_IFBLK; break;
+       case '-':
+           *mode |= S_IFREG; break;
+       default:
+           fprintf(stderr, "unknown mode %c\n", *ptr);
+           exit(1);
+    }
+    *data = ++ptr;
+    *mode |= (parse_rwx_group(data, 8*8) + parse_rwx_group(data, 8) + parse_rwx_group(data, 1));
+    return 0;
+}
+
+void parse_major_minor(char **data, struct stat *st)
+{
+    char *ptr = *data, *end;
+    int maj, min;
+
+    if (S_ISDIR(st->st_mode)) {
+       goto out;
+    } else if (S_ISLNK(st->st_mode)) {
+       strtoul(ptr, &end, 10);
+       ptr = end;
+       goto out;
+    }
+
+    maj = strtoul(ptr, &end, 10);
+    assert(*end == ',');
+    assert(maj);
+
+    ptr = end+1;
+    min = strtoul(ptr, &end, 10);
+    assert(*end == ' ');
+
+    st->st_rdev = makedev(maj, min);
+    assert(*end == ' ');
+    ptr = end;
+
+out:
+    while (*ptr == ' ') 
+       ++ptr;
+    *data = ptr;
+}
+
+/** chomp whitespace from the end of the string */
+int chomp(char *str)
+{
+    int len=strlen(str);
+    while ((len > 0) && isspace(str[len-1]))
+       str[--len] = '\0';
+    return len;
+}
+
+
+static void parse_link_name(char *ptr, char **data)
+{
+    char *arrow = strstr(ptr, " -> ");
+
+    if (!arrow)
+       abort();
+
+    *arrow = '\0';
+    *data = arrow+4;
+    fprintf(stderr, "got name '%s' and link '%s'\n", ptr, *data);
+}
+
+
+int main(int argc, char **argv) {
+    char buf[BUFSIZ];
+    struct stat st;
+
+    if (argc != 1) {
+       fprintf(stderr, "%s\n", argv[0]);
+       exit(1);
+    }
+
+    while ((fgets(buf, sizeof(buf), stdin))) {
+       char *ptr = buf;
+       char *data = NULL;
+
+       chomp(ptr);
+       memset(&st, 0, sizeof(st));
+       fprintf(stderr, "\ngot line '%s'\n", buf);
+
+       parse_mode(&ptr, &st.st_mode);
+       fprintf(stderr, "got st_mode %o\n", st.st_mode);
+       assert(*ptr == ' ');
+
+       parse_major_minor(&ptr, &st);
+       fprintf(stderr, "got maj %d min %d\n", major(st.st_rdev), minor(st.st_rdev));
+       fprintf(stderr, "got name '%s'\n", ptr);
+
+       if (S_ISLNK(st.st_mode))
+           parse_link_name(ptr, &data);
+       write_cpio_record(ptr, data, &st);
+
+    }
+    memset(&st, 0, sizeof(st));
+    write_cpio_record("TRAILER!!!", NULL, &st);
+    return 0;
+}