wok-6.x annotate gen-init-cpio/stuff/gen_init_cpio.c @ rev 16986

Add engrampa
author Yuri Pourre <yuripourre@gmail.com>
date Fri Aug 08 00:41:35 2014 -0300 (2014-08-08)
parents
children
rev   line source
slaxemulator@5345 1 #include <stdio.h>
slaxemulator@5345 2 #include <stdlib.h>
slaxemulator@5345 3 #include <sys/types.h>
slaxemulator@5345 4 #include <sys/stat.h>
slaxemulator@5345 5 #include <string.h>
slaxemulator@5345 6 #include <unistd.h>
slaxemulator@5345 7 #include <time.h>
slaxemulator@5345 8 #include <fcntl.h>
slaxemulator@5345 9 #include <errno.h>
slaxemulator@5345 10 #include <ctype.h>
slaxemulator@5345 11 #include <limits.h>
slaxemulator@5345 12
slaxemulator@5345 13 /*
slaxemulator@5345 14 * Original work by Jeff Garzik
slaxemulator@5345 15 *
slaxemulator@5345 16 * External file lists, symlink, pipe and fifo support by Thayne Harbaugh
slaxemulator@5345 17 * Hard link support by Luciano Rocha
slaxemulator@5345 18 */
slaxemulator@5345 19
slaxemulator@5345 20 #define xstr(s) #s
slaxemulator@5345 21 #define str(s) xstr(s)
slaxemulator@5345 22
slaxemulator@5345 23 static unsigned int offset;
slaxemulator@5345 24 static unsigned int ino = 721;
slaxemulator@5345 25
slaxemulator@5345 26 struct file_handler {
slaxemulator@5345 27 const char *type;
slaxemulator@5345 28 int (*handler)(const char *line);
slaxemulator@5345 29 };
slaxemulator@5345 30
slaxemulator@5345 31 static void push_string(const char *name)
slaxemulator@5345 32 {
slaxemulator@5345 33 unsigned int name_len = strlen(name) + 1;
slaxemulator@5345 34
slaxemulator@5345 35 fputs(name, stdout);
slaxemulator@5345 36 putchar(0);
slaxemulator@5345 37 offset += name_len;
slaxemulator@5345 38 }
slaxemulator@5345 39
slaxemulator@5345 40 static void push_pad (void)
slaxemulator@5345 41 {
slaxemulator@5345 42 while (offset & 3) {
slaxemulator@5345 43 putchar(0);
slaxemulator@5345 44 offset++;
slaxemulator@5345 45 }
slaxemulator@5345 46 }
slaxemulator@5345 47
slaxemulator@5345 48 static void push_rest(const char *name)
slaxemulator@5345 49 {
slaxemulator@5345 50 unsigned int name_len = strlen(name) + 1;
slaxemulator@5345 51 unsigned int tmp_ofs;
slaxemulator@5345 52
slaxemulator@5345 53 fputs(name, stdout);
slaxemulator@5345 54 putchar(0);
slaxemulator@5345 55 offset += name_len;
slaxemulator@5345 56
slaxemulator@5345 57 tmp_ofs = name_len + 110;
slaxemulator@5345 58 while (tmp_ofs & 3) {
slaxemulator@5345 59 putchar(0);
slaxemulator@5345 60 offset++;
slaxemulator@5345 61 tmp_ofs++;
slaxemulator@5345 62 }
slaxemulator@5345 63 }
slaxemulator@5345 64
slaxemulator@5345 65 static void push_hdr(const char *s)
slaxemulator@5345 66 {
slaxemulator@5345 67 fputs(s, stdout);
slaxemulator@5345 68 offset += 110;
slaxemulator@5345 69 }
slaxemulator@5345 70
slaxemulator@5345 71 static void cpio_trailer(void)
slaxemulator@5345 72 {
slaxemulator@5345 73 char s[256];
slaxemulator@5345 74 const char name[] = "TRAILER!!!";
slaxemulator@5345 75
slaxemulator@5345 76 sprintf(s, "%s%08X%08X%08lX%08lX%08X%08lX"
slaxemulator@5345 77 "%08X%08X%08X%08X%08X%08X%08X",
slaxemulator@5345 78 "070701", /* magic */
slaxemulator@5345 79 0, /* ino */
slaxemulator@5345 80 0, /* mode */
slaxemulator@5345 81 (long) 0, /* uid */
slaxemulator@5345 82 (long) 0, /* gid */
slaxemulator@5345 83 1, /* nlink */
slaxemulator@5345 84 (long) 0, /* mtime */
slaxemulator@5345 85 0, /* filesize */
slaxemulator@5345 86 0, /* major */
slaxemulator@5345 87 0, /* minor */
slaxemulator@5345 88 0, /* rmajor */
slaxemulator@5345 89 0, /* rminor */
slaxemulator@5345 90 (unsigned)strlen(name)+1, /* namesize */
slaxemulator@5345 91 0); /* chksum */
slaxemulator@5345 92 push_hdr(s);
slaxemulator@5345 93 push_rest(name);
slaxemulator@5345 94
slaxemulator@5345 95 while (offset % 512) {
slaxemulator@5345 96 putchar(0);
slaxemulator@5345 97 offset++;
slaxemulator@5345 98 }
slaxemulator@5345 99 }
slaxemulator@5345 100
slaxemulator@5345 101 static int cpio_mkslink(const char *name, const char *target,
slaxemulator@5345 102 unsigned int mode, uid_t uid, gid_t gid)
slaxemulator@5345 103 {
slaxemulator@5345 104 char s[256];
slaxemulator@5345 105 time_t mtime = time(NULL);
slaxemulator@5345 106
slaxemulator@5345 107 sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
slaxemulator@5345 108 "%08X%08X%08X%08X%08X%08X%08X",
slaxemulator@5345 109 "070701", /* magic */
slaxemulator@5345 110 ino++, /* ino */
slaxemulator@5345 111 S_IFLNK | mode, /* mode */
slaxemulator@5345 112 (long) uid, /* uid */
slaxemulator@5345 113 (long) gid, /* gid */
slaxemulator@5345 114 1, /* nlink */
slaxemulator@5345 115 (long) mtime, /* mtime */
slaxemulator@5345 116 (unsigned)strlen(target)+1, /* filesize */
slaxemulator@5345 117 3, /* major */
slaxemulator@5345 118 1, /* minor */
slaxemulator@5345 119 0, /* rmajor */
slaxemulator@5345 120 0, /* rminor */
slaxemulator@5345 121 (unsigned)strlen(name) + 1,/* namesize */
slaxemulator@5345 122 0); /* chksum */
slaxemulator@5345 123 push_hdr(s);
slaxemulator@5345 124 push_string(name);
slaxemulator@5345 125 push_pad();
slaxemulator@5345 126 push_string(target);
slaxemulator@5345 127 push_pad();
slaxemulator@5345 128 return 0;
slaxemulator@5345 129 }
slaxemulator@5345 130
slaxemulator@5345 131 static int cpio_mkslink_line(const char *line)
slaxemulator@5345 132 {
slaxemulator@5345 133 char name[PATH_MAX + 1];
slaxemulator@5345 134 char target[PATH_MAX + 1];
slaxemulator@5345 135 unsigned int mode;
slaxemulator@5345 136 int uid;
slaxemulator@5345 137 int gid;
slaxemulator@5345 138 int rc = -1;
slaxemulator@5345 139
slaxemulator@5345 140 if (5 != sscanf(line, "%" str(PATH_MAX) "s %" str(PATH_MAX) "s %o %d %d", name, target, &mode, &uid, &gid)) {
slaxemulator@5345 141 fprintf(stderr, "Unrecognized dir format '%s'", line);
slaxemulator@5345 142 goto fail;
slaxemulator@5345 143 }
slaxemulator@5345 144 rc = cpio_mkslink(name, target, mode, uid, gid);
slaxemulator@5345 145 fail:
slaxemulator@5345 146 return rc;
slaxemulator@5345 147 }
slaxemulator@5345 148
slaxemulator@5345 149 static int cpio_mkgeneric(const char *name, unsigned int mode,
slaxemulator@5345 150 uid_t uid, gid_t gid)
slaxemulator@5345 151 {
slaxemulator@5345 152 char s[256];
slaxemulator@5345 153 time_t mtime = time(NULL);
slaxemulator@5345 154
slaxemulator@5345 155 sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
slaxemulator@5345 156 "%08X%08X%08X%08X%08X%08X%08X",
slaxemulator@5345 157 "070701", /* magic */
slaxemulator@5345 158 ino++, /* ino */
slaxemulator@5345 159 mode, /* mode */
slaxemulator@5345 160 (long) uid, /* uid */
slaxemulator@5345 161 (long) gid, /* gid */
slaxemulator@5345 162 2, /* nlink */
slaxemulator@5345 163 (long) mtime, /* mtime */
slaxemulator@5345 164 0, /* filesize */
slaxemulator@5345 165 3, /* major */
slaxemulator@5345 166 1, /* minor */
slaxemulator@5345 167 0, /* rmajor */
slaxemulator@5345 168 0, /* rminor */
slaxemulator@5345 169 (unsigned)strlen(name) + 1,/* namesize */
slaxemulator@5345 170 0); /* chksum */
slaxemulator@5345 171 push_hdr(s);
slaxemulator@5345 172 push_rest(name);
slaxemulator@5345 173 return 0;
slaxemulator@5345 174 }
slaxemulator@5345 175
slaxemulator@5345 176 enum generic_types {
slaxemulator@5345 177 GT_DIR,
slaxemulator@5345 178 GT_PIPE,
slaxemulator@5345 179 GT_SOCK
slaxemulator@5345 180 };
slaxemulator@5345 181
slaxemulator@5345 182 struct generic_type {
slaxemulator@5345 183 const char *type;
slaxemulator@5345 184 mode_t mode;
slaxemulator@5345 185 };
slaxemulator@5345 186
slaxemulator@5345 187 static struct generic_type generic_type_table[] = {
slaxemulator@5345 188 [GT_DIR] = {
slaxemulator@5345 189 .type = "dir",
slaxemulator@5345 190 .mode = S_IFDIR
slaxemulator@5345 191 },
slaxemulator@5345 192 [GT_PIPE] = {
slaxemulator@5345 193 .type = "pipe",
slaxemulator@5345 194 .mode = S_IFIFO
slaxemulator@5345 195 },
slaxemulator@5345 196 [GT_SOCK] = {
slaxemulator@5345 197 .type = "sock",
slaxemulator@5345 198 .mode = S_IFSOCK
slaxemulator@5345 199 }
slaxemulator@5345 200 };
slaxemulator@5345 201
slaxemulator@5345 202 static int cpio_mkgeneric_line(const char *line, enum generic_types gt)
slaxemulator@5345 203 {
slaxemulator@5345 204 char name[PATH_MAX + 1];
slaxemulator@5345 205 unsigned int mode;
slaxemulator@5345 206 int uid;
slaxemulator@5345 207 int gid;
slaxemulator@5345 208 int rc = -1;
slaxemulator@5345 209
slaxemulator@5345 210 if (4 != sscanf(line, "%" str(PATH_MAX) "s %o %d %d", name, &mode, &uid, &gid)) {
slaxemulator@5345 211 fprintf(stderr, "Unrecognized %s format '%s'",
slaxemulator@5345 212 line, generic_type_table[gt].type);
slaxemulator@5345 213 goto fail;
slaxemulator@5345 214 }
slaxemulator@5345 215 mode |= generic_type_table[gt].mode;
slaxemulator@5345 216 rc = cpio_mkgeneric(name, mode, uid, gid);
slaxemulator@5345 217 fail:
slaxemulator@5345 218 return rc;
slaxemulator@5345 219 }
slaxemulator@5345 220
slaxemulator@5345 221 static int cpio_mkdir_line(const char *line)
slaxemulator@5345 222 {
slaxemulator@5345 223 return cpio_mkgeneric_line(line, GT_DIR);
slaxemulator@5345 224 }
slaxemulator@5345 225
slaxemulator@5345 226 static int cpio_mkpipe_line(const char *line)
slaxemulator@5345 227 {
slaxemulator@5345 228 return cpio_mkgeneric_line(line, GT_PIPE);
slaxemulator@5345 229 }
slaxemulator@5345 230
slaxemulator@5345 231 static int cpio_mksock_line(const char *line)
slaxemulator@5345 232 {
slaxemulator@5345 233 return cpio_mkgeneric_line(line, GT_SOCK);
slaxemulator@5345 234 }
slaxemulator@5345 235
slaxemulator@5345 236 static int cpio_mknod(const char *name, unsigned int mode,
slaxemulator@5345 237 uid_t uid, gid_t gid, char dev_type,
slaxemulator@5345 238 unsigned int maj, unsigned int min)
slaxemulator@5345 239 {
slaxemulator@5345 240 char s[256];
slaxemulator@5345 241 time_t mtime = time(NULL);
slaxemulator@5345 242
slaxemulator@5345 243 if (dev_type == 'b')
slaxemulator@5345 244 mode |= S_IFBLK;
slaxemulator@5345 245 else
slaxemulator@5345 246 mode |= S_IFCHR;
slaxemulator@5345 247
slaxemulator@5345 248 sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
slaxemulator@5345 249 "%08X%08X%08X%08X%08X%08X%08X",
slaxemulator@5345 250 "070701", /* magic */
slaxemulator@5345 251 ino++, /* ino */
slaxemulator@5345 252 mode, /* mode */
slaxemulator@5345 253 (long) uid, /* uid */
slaxemulator@5345 254 (long) gid, /* gid */
slaxemulator@5345 255 1, /* nlink */
slaxemulator@5345 256 (long) mtime, /* mtime */
slaxemulator@5345 257 0, /* filesize */
slaxemulator@5345 258 3, /* major */
slaxemulator@5345 259 1, /* minor */
slaxemulator@5345 260 maj, /* rmajor */
slaxemulator@5345 261 min, /* rminor */
slaxemulator@5345 262 (unsigned)strlen(name) + 1,/* namesize */
slaxemulator@5345 263 0); /* chksum */
slaxemulator@5345 264 push_hdr(s);
slaxemulator@5345 265 push_rest(name);
slaxemulator@5345 266 return 0;
slaxemulator@5345 267 }
slaxemulator@5345 268
slaxemulator@5345 269 static int cpio_mknod_line(const char *line)
slaxemulator@5345 270 {
slaxemulator@5345 271 char name[PATH_MAX + 1];
slaxemulator@5345 272 unsigned int mode;
slaxemulator@5345 273 int uid;
slaxemulator@5345 274 int gid;
slaxemulator@5345 275 char dev_type;
slaxemulator@5345 276 unsigned int maj;
slaxemulator@5345 277 unsigned int min;
slaxemulator@5345 278 int rc = -1;
slaxemulator@5345 279
slaxemulator@5345 280 if (7 != sscanf(line, "%" str(PATH_MAX) "s %o %d %d %c %u %u",
slaxemulator@5345 281 name, &mode, &uid, &gid, &dev_type, &maj, &min)) {
slaxemulator@5345 282 fprintf(stderr, "Unrecognized nod format '%s'", line);
slaxemulator@5345 283 goto fail;
slaxemulator@5345 284 }
slaxemulator@5345 285 rc = cpio_mknod(name, mode, uid, gid, dev_type, maj, min);
slaxemulator@5345 286 fail:
slaxemulator@5345 287 return rc;
slaxemulator@5345 288 }
slaxemulator@5345 289
slaxemulator@5345 290 static int cpio_mkfile(const char *name, const char *location,
slaxemulator@5345 291 unsigned int mode, uid_t uid, gid_t gid,
slaxemulator@5345 292 unsigned int nlinks)
slaxemulator@5345 293 {
slaxemulator@5345 294 char s[256];
slaxemulator@5345 295 char *filebuf = NULL;
slaxemulator@5345 296 struct stat buf;
slaxemulator@5345 297 long size;
slaxemulator@5345 298 int file = -1;
slaxemulator@5345 299 int retval;
slaxemulator@5345 300 int rc = -1;
slaxemulator@5345 301 int namesize;
slaxemulator@5345 302 int i;
slaxemulator@5345 303
slaxemulator@5345 304 mode |= S_IFREG;
slaxemulator@5345 305
slaxemulator@5345 306 retval = stat (location, &buf);
slaxemulator@5345 307 if (retval) {
slaxemulator@5345 308 fprintf (stderr, "File %s could not be located\n", location);
slaxemulator@5345 309 goto error;
slaxemulator@5345 310 }
slaxemulator@5345 311
slaxemulator@5345 312 file = open (location, O_RDONLY);
slaxemulator@5345 313 if (file < 0) {
slaxemulator@5345 314 fprintf (stderr, "File %s could not be opened for reading\n", location);
slaxemulator@5345 315 goto error;
slaxemulator@5345 316 }
slaxemulator@5345 317
slaxemulator@5345 318 filebuf = malloc(buf.st_size);
slaxemulator@5345 319 if (!filebuf) {
slaxemulator@5345 320 fprintf (stderr, "out of memory\n");
slaxemulator@5345 321 goto error;
slaxemulator@5345 322 }
slaxemulator@5345 323
slaxemulator@5345 324 retval = read (file, filebuf, buf.st_size);
slaxemulator@5345 325 if (retval < 0) {
slaxemulator@5345 326 fprintf (stderr, "Can not read %s file\n", location);
slaxemulator@5345 327 goto error;
slaxemulator@5345 328 }
slaxemulator@5345 329
slaxemulator@5345 330 size = 0;
slaxemulator@5345 331 for (i = 1; i <= nlinks; i++) {
slaxemulator@5345 332 /* data goes on last link */
slaxemulator@5345 333 if (i == nlinks) size = buf.st_size;
slaxemulator@5345 334
slaxemulator@5345 335 namesize = strlen(name) + 1;
slaxemulator@5345 336 sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
slaxemulator@5345 337 "%08lX%08X%08X%08X%08X%08X%08X",
slaxemulator@5345 338 "070701", /* magic */
slaxemulator@5345 339 ino, /* ino */
slaxemulator@5345 340 mode, /* mode */
slaxemulator@5345 341 (long) uid, /* uid */
slaxemulator@5345 342 (long) gid, /* gid */
slaxemulator@5345 343 nlinks, /* nlink */
slaxemulator@5345 344 (long) buf.st_mtime, /* mtime */
slaxemulator@5345 345 size, /* filesize */
slaxemulator@5345 346 3, /* major */
slaxemulator@5345 347 1, /* minor */
slaxemulator@5345 348 0, /* rmajor */
slaxemulator@5345 349 0, /* rminor */
slaxemulator@5345 350 namesize, /* namesize */
slaxemulator@5345 351 0); /* chksum */
slaxemulator@5345 352 push_hdr(s);
slaxemulator@5345 353 push_string(name);
slaxemulator@5345 354 push_pad();
slaxemulator@5345 355
slaxemulator@5345 356 if (size) {
slaxemulator@5345 357 fwrite(filebuf, size, 1, stdout);
slaxemulator@5345 358 offset += size;
slaxemulator@5345 359 push_pad();
slaxemulator@5345 360 }
slaxemulator@5345 361
slaxemulator@5345 362 name += namesize;
slaxemulator@5345 363 }
slaxemulator@5345 364 ino++;
slaxemulator@5345 365 rc = 0;
slaxemulator@5345 366
slaxemulator@5345 367 error:
slaxemulator@5345 368 if (filebuf) free(filebuf);
slaxemulator@5345 369 if (file >= 0) close(file);
slaxemulator@5345 370 return rc;
slaxemulator@5345 371 }
slaxemulator@5345 372
slaxemulator@5345 373 static char *cpio_replace_env(char *new_location)
slaxemulator@5345 374 {
slaxemulator@5345 375 char expanded[PATH_MAX + 1];
slaxemulator@5345 376 char env_var[PATH_MAX + 1];
slaxemulator@5345 377 char *start;
slaxemulator@5345 378 char *end;
slaxemulator@5345 379
slaxemulator@5345 380 for (start = NULL; (start = strstr(new_location, "${")); ) {
slaxemulator@5345 381 end = strchr(start, '}');
slaxemulator@5345 382 if (start < end) {
slaxemulator@5345 383 *env_var = *expanded = '\0';
slaxemulator@5345 384 strncat(env_var, start + 2, end - start - 2);
slaxemulator@5345 385 strncat(expanded, new_location, start - new_location);
slaxemulator@5345 386 strncat(expanded, getenv(env_var), PATH_MAX);
slaxemulator@5345 387 strncat(expanded, end + 1, PATH_MAX);
slaxemulator@5345 388 strncpy(new_location, expanded, PATH_MAX);
slaxemulator@5345 389 } else
slaxemulator@5345 390 break;
slaxemulator@5345 391 }
slaxemulator@5345 392
slaxemulator@5345 393 return new_location;
slaxemulator@5345 394 }
slaxemulator@5345 395
slaxemulator@5345 396
slaxemulator@5345 397 static int cpio_mkfile_line(const char *line)
slaxemulator@5345 398 {
slaxemulator@5345 399 char name[PATH_MAX + 1];
slaxemulator@5345 400 char *dname = NULL; /* malloc'ed buffer for hard links */
slaxemulator@5345 401 char location[PATH_MAX + 1];
slaxemulator@5345 402 unsigned int mode;
slaxemulator@5345 403 int uid;
slaxemulator@5345 404 int gid;
slaxemulator@5345 405 int nlinks = 1;
slaxemulator@5345 406 int end = 0, dname_len = 0;
slaxemulator@5345 407 int rc = -1;
slaxemulator@5345 408
slaxemulator@5345 409 if (5 > sscanf(line, "%" str(PATH_MAX) "s %" str(PATH_MAX)
slaxemulator@5345 410 "s %o %d %d %n",
slaxemulator@5345 411 name, location, &mode, &uid, &gid, &end)) {
slaxemulator@5345 412 fprintf(stderr, "Unrecognized file format '%s'", line);
slaxemulator@5345 413 goto fail;
slaxemulator@5345 414 }
slaxemulator@5345 415 if (end && isgraph(line[end])) {
slaxemulator@5345 416 int len;
slaxemulator@5345 417 int nend;
slaxemulator@5345 418
slaxemulator@5345 419 dname = malloc(strlen(line));
slaxemulator@5345 420 if (!dname) {
slaxemulator@5345 421 fprintf (stderr, "out of memory (%d)\n", dname_len);
slaxemulator@5345 422 goto fail;
slaxemulator@5345 423 }
slaxemulator@5345 424
slaxemulator@5345 425 dname_len = strlen(name) + 1;
slaxemulator@5345 426 memcpy(dname, name, dname_len);
slaxemulator@5345 427
slaxemulator@5345 428 do {
slaxemulator@5345 429 nend = 0;
slaxemulator@5345 430 if (sscanf(line + end, "%" str(PATH_MAX) "s %n",
slaxemulator@5345 431 name, &nend) < 1)
slaxemulator@5345 432 break;
slaxemulator@5345 433 len = strlen(name) + 1;
slaxemulator@5345 434 memcpy(dname + dname_len, name, len);
slaxemulator@5345 435 dname_len += len;
slaxemulator@5345 436 nlinks++;
slaxemulator@5345 437 end += nend;
slaxemulator@5345 438 } while (isgraph(line[end]));
slaxemulator@5345 439 } else {
slaxemulator@5345 440 dname = name;
slaxemulator@5345 441 }
slaxemulator@5345 442 rc = cpio_mkfile(dname, cpio_replace_env(location),
slaxemulator@5345 443 mode, uid, gid, nlinks);
slaxemulator@5345 444 fail:
slaxemulator@5345 445 if (dname_len) free(dname);
slaxemulator@5345 446 return rc;
slaxemulator@5345 447 }
slaxemulator@5345 448
slaxemulator@5345 449 static void usage(const char *prog)
slaxemulator@5345 450 {
slaxemulator@5345 451 fprintf(stderr, "Usage:\n"
slaxemulator@5345 452 "\t%s <cpio_list>\n"
slaxemulator@5345 453 "\n"
slaxemulator@5345 454 "<cpio_list> is a file containing newline separated entries that\n"
slaxemulator@5345 455 "describe the files to be included in the initramfs archive:\n"
slaxemulator@5345 456 "\n"
slaxemulator@5345 457 "# a comment\n"
slaxemulator@5345 458 "file <name> <location> <mode> <uid> <gid> [<hard links>]\n"
slaxemulator@5345 459 "dir <name> <mode> <uid> <gid>\n"
slaxemulator@5345 460 "nod <name> <mode> <uid> <gid> <dev_type> <maj> <min>\n"
slaxemulator@5345 461 "slink <name> <target> <mode> <uid> <gid>\n"
slaxemulator@5345 462 "pipe <name> <mode> <uid> <gid>\n"
slaxemulator@5345 463 "sock <name> <mode> <uid> <gid>\n"
slaxemulator@5345 464 "\n"
slaxemulator@5345 465 "<name> name of the file/dir/nod/etc in the archive\n"
slaxemulator@5345 466 "<location> location of the file in the current filesystem\n"
slaxemulator@5345 467 " expands shell variables quoted with ${}\n"
slaxemulator@5345 468 "<target> link target\n"
slaxemulator@5345 469 "<mode> mode/permissions of the file\n"
slaxemulator@5345 470 "<uid> user id (0=root)\n"
slaxemulator@5345 471 "<gid> group id (0=root)\n"
slaxemulator@5345 472 "<dev_type> device type (b=block, c=character)\n"
slaxemulator@5345 473 "<maj> major number of nod\n"
slaxemulator@5345 474 "<min> minor number of nod\n"
slaxemulator@5345 475 "<hard links> space separated list of other links to file\n"
slaxemulator@5345 476 "\n"
slaxemulator@5345 477 "example:\n"
slaxemulator@5345 478 "# A simple initramfs\n"
slaxemulator@5345 479 "dir /dev 0755 0 0\n"
slaxemulator@5345 480 "nod /dev/console 0600 0 0 c 5 1\n"
slaxemulator@5345 481 "dir /root 0700 0 0\n"
slaxemulator@5345 482 "dir /sbin 0755 0 0\n"
slaxemulator@5345 483 "file /sbin/kinit /usr/src/klibc/kinit/kinit 0755 0 0\n",
slaxemulator@5345 484 prog);
slaxemulator@5345 485 }
slaxemulator@5345 486
slaxemulator@5345 487 struct file_handler file_handler_table[] = {
slaxemulator@5345 488 {
slaxemulator@5345 489 .type = "file",
slaxemulator@5345 490 .handler = cpio_mkfile_line,
slaxemulator@5345 491 }, {
slaxemulator@5345 492 .type = "nod",
slaxemulator@5345 493 .handler = cpio_mknod_line,
slaxemulator@5345 494 }, {
slaxemulator@5345 495 .type = "dir",
slaxemulator@5345 496 .handler = cpio_mkdir_line,
slaxemulator@5345 497 }, {
slaxemulator@5345 498 .type = "slink",
slaxemulator@5345 499 .handler = cpio_mkslink_line,
slaxemulator@5345 500 }, {
slaxemulator@5345 501 .type = "pipe",
slaxemulator@5345 502 .handler = cpio_mkpipe_line,
slaxemulator@5345 503 }, {
slaxemulator@5345 504 .type = "sock",
slaxemulator@5345 505 .handler = cpio_mksock_line,
slaxemulator@5345 506 }, {
slaxemulator@5345 507 .type = NULL,
slaxemulator@5345 508 .handler = NULL,
slaxemulator@5345 509 }
slaxemulator@5345 510 };
slaxemulator@5345 511
slaxemulator@5345 512 #define LINE_SIZE (2 * PATH_MAX + 50)
slaxemulator@5345 513
slaxemulator@5345 514 int main (int argc, char *argv[])
slaxemulator@5345 515 {
slaxemulator@5345 516 FILE *cpio_list;
slaxemulator@5345 517 char line[LINE_SIZE];
slaxemulator@5345 518 char *args, *type;
slaxemulator@5345 519 int ec = 0;
slaxemulator@5345 520 int line_nr = 0;
slaxemulator@5345 521
slaxemulator@5345 522 if (2 != argc) {
slaxemulator@5345 523 usage(argv[0]);
slaxemulator@5345 524 exit(1);
slaxemulator@5345 525 }
slaxemulator@5345 526
slaxemulator@5345 527 if (!strcmp(argv[1], "-"))
slaxemulator@5345 528 cpio_list = stdin;
slaxemulator@5345 529 else if (! (cpio_list = fopen(argv[1], "r"))) {
slaxemulator@5345 530 fprintf(stderr, "ERROR: unable to open '%s': %s\n\n",
slaxemulator@5345 531 argv[1], strerror(errno));
slaxemulator@5345 532 usage(argv[0]);
slaxemulator@5345 533 exit(1);
slaxemulator@5345 534 }
slaxemulator@5345 535
slaxemulator@5345 536 while (fgets(line, LINE_SIZE, cpio_list)) {
slaxemulator@5345 537 int type_idx;
slaxemulator@5345 538 size_t slen = strlen(line);
slaxemulator@5345 539
slaxemulator@5345 540 line_nr++;
slaxemulator@5345 541
slaxemulator@5345 542 if ('#' == *line) {
slaxemulator@5345 543 /* comment - skip to next line */
slaxemulator@5345 544 continue;
slaxemulator@5345 545 }
slaxemulator@5345 546
slaxemulator@5345 547 if (! (type = strtok(line, " \t"))) {
slaxemulator@5345 548 fprintf(stderr,
slaxemulator@5345 549 "ERROR: incorrect format, could not locate file type line %d: '%s'\n",
slaxemulator@5345 550 line_nr, line);
slaxemulator@5345 551 ec = -1;
slaxemulator@5345 552 break;
slaxemulator@5345 553 }
slaxemulator@5345 554
slaxemulator@5345 555 if ('\n' == *type) {
slaxemulator@5345 556 /* a blank line */
slaxemulator@5345 557 continue;
slaxemulator@5345 558 }
slaxemulator@5345 559
slaxemulator@5345 560 if (slen == strlen(type)) {
slaxemulator@5345 561 /* must be an empty line */
slaxemulator@5345 562 continue;
slaxemulator@5345 563 }
slaxemulator@5345 564
slaxemulator@5345 565 if (! (args = strtok(NULL, "\n"))) {
slaxemulator@5345 566 fprintf(stderr,
slaxemulator@5345 567 "ERROR: incorrect format, newline required line %d: '%s'\n",
slaxemulator@5345 568 line_nr, line);
slaxemulator@5345 569 ec = -1;
slaxemulator@5345 570 }
slaxemulator@5345 571
slaxemulator@5345 572 for (type_idx = 0; file_handler_table[type_idx].type; type_idx++) {
slaxemulator@5345 573 int rc;
slaxemulator@5345 574 if (! strcmp(line, file_handler_table[type_idx].type)) {
slaxemulator@5345 575 if ((rc = file_handler_table[type_idx].handler(args))) {
slaxemulator@5345 576 ec = rc;
slaxemulator@5345 577 fprintf(stderr, " line %d\n", line_nr);
slaxemulator@5345 578 }
slaxemulator@5345 579 break;
slaxemulator@5345 580 }
slaxemulator@5345 581 }
slaxemulator@5345 582
slaxemulator@5345 583 if (NULL == file_handler_table[type_idx].type) {
slaxemulator@5345 584 fprintf(stderr, "unknown file type line %d: '%s'\n",
slaxemulator@5345 585 line_nr, line);
slaxemulator@5345 586 }
slaxemulator@5345 587 }
slaxemulator@5345 588 if (ec == 0)
slaxemulator@5345 589 cpio_trailer();
slaxemulator@5345 590
slaxemulator@5345 591 exit(ec);
slaxemulator@5345 592 }