*** empty log message ***
[arla.git] / tools / release-tools / tar-rootify.c
blob5cf8d29dc9a2a465d9ba19ca7deb2c89e5c521bd
1 /*
2 * Copyright (c) 2002, Stockholms Universitet
3 * (Stockholm University, Stockholm Sweden)
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the university nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
35 #include <atypes.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <limits.h>
40 #include <err.h>
41 #include <unistd.h>
42 #include <string.h>
44 #include "common.h"
46 /* from gnu tar manual */
48 struct posix_header
49 { /* byte offset */
50 char name[100]; /* 0 */
51 char mode[8]; /* 100 */
52 char uid[8]; /* 108 */
53 char gid[8]; /* 116 */
54 char size[12]; /* 124 */
55 char mtime[12]; /* 136 */
56 char chksum[8]; /* 148 */
57 char typeflag; /* 156 */
58 char linkname[100]; /* 157 */
59 char magic[6]; /* 257 */
60 char version[2]; /* 263 */
61 char uname[32]; /* 265 */
62 char gname[32]; /* 297 */
63 char devmajor[8]; /* 329 */
64 char devminor[8]; /* 337 */
65 char prefix[155]; /* 345 */
66 /* 500 */
69 #define TMAGIC "ustar" /* ustar and a null */
70 #define OLDGNU_MAGIC "ustar " /* 7 chars and a null */
71 #define TMAGLEN 6
72 #define TVERSION "00" /* 00 and no null */
73 #define TVERSLEN 2
75 int
76 main(int argc, char **argv)
78 char *buf;
79 int bufsz = 512;
80 int i, skip = 0, rec = 0;
81 struct posix_header *p;
82 uint64_t size;
83 unsigned long cksum, hcksum;
84 int verbose = 0;
86 buf = malloc(bufsz);
87 if (buf == NULL)
88 err(1, "malloc");
90 while (read(0, buf, bufsz) == bufsz) {
91 rec++;
92 p = (void *)buf;
94 if (skip || p->name[0] == '\0') {
95 if (write(1, buf, bufsz) != bufsz)
96 err(1, "write");
97 skip--;
98 continue;
101 if (strcmp(p->magic, TMAGIC) != 0
102 && strcmp(p->magic, OLDGNU_MAGIC) != 0)
103 errx(1, "bad magic in #%d '%.*s'\n", rec,
104 (int)sizeof(p->magic), p->magic);
106 cksum = 256;
107 for (i = 0; i < sizeof(*p); i++)
108 if (i < 148 || 155 < i)
109 cksum += (u_long)(buf[i] & 0xff);
111 size = estrntoll(p->size, 12, 8);
112 if (verbose) {
113 fprintf(stderr, "rec #%d\n", rec);
114 fprintf(stderr, "name = %.*s\n", (int)sizeof(p->name), p->name);
115 fprintf(stderr, "uid = %.*s\n", (int)sizeof(p->uid), p->uid);
116 fprintf(stderr, "gid = %.*s\n", (int)sizeof(p->gid), p->gid);
117 fprintf(stderr, "uname = %.*s\n", (int)sizeof(p->uname), p->uname);
118 fprintf(stderr, "gname = %.*s\n", (int)sizeof(p->gname), p->gname);
119 fprintf(stderr, "type = %c\n", p->typeflag);
120 fprintf(stderr, "size = %.*s\n", (int)sizeof(p->size), p->size);
121 fprintf(stderr, "size = %llo\n", size);
124 hcksum = estrntoll(p->chksum, 8, 8);
125 if (hcksum != cksum)
126 errx(1, "invalid cksum %d != %d", (int)hcksum, (int)cksum);
127 snprintf(p->uid, (int)sizeof(p->uid), "%6o ", 0);
128 snprintf(p->gid, (int)sizeof(p->gid), "%6o ", 0);
129 snprintf(p->uname, (int)sizeof(p->uname), "root");
130 snprintf(p->gname, (int)sizeof(p->gname), "wheel");
132 cksum = 256;
133 for (i = 0; i < sizeof(*p); i++)
134 if (i < 148 || 155 < i)
135 cksum += (u_long)(buf[i] & 0xff);
137 snprintf(p->chksum, (int)sizeof(p->chksum), " %6o ", (unsigned)cksum);
139 if (write(1, buf, bufsz) != bufsz)
140 errx(1, "write");
142 if (size)
143 skip = (size + bufsz - 1) / bufsz;
145 return 0;