-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtarout.c
191 lines (172 loc) · 4.37 KB
/
tarout.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "tar.h"
#include "util.h"
#include <stdio.h>
#include <string.h>
int tarout_init(struct tar_stream *t, int fd)
{
t->cksum = 0;
t->needpad = 0;
t->tptr = 0;
t->hold = 0;
t->fd = fd;
return 1;
}
int tarout_addch(struct tar_stream *t, int ch)
{
int r;
t->block[t->tptr++] = ch;
if (!t->hold && t->tptr == 512) {
r = bwrite(t->fd, t->block, 512);
t->tptr = 0;
return r;
}
return 1;
}
int tarout_nullpad(struct tar_stream *t, const char *in, int inlen, int pad)
{
while (inlen > 0 && pad > 0) {
if (!tarout_addch(t, *in)) return 0;
in++;
inlen--;
pad--;
}
while (pad > 0) {
if (!tarout_addch(t, 0)) return 0;
pad--;
}
return 1;
}
int tarout_octal(struct tar_stream *t, unsigned long long o, int digits, int size)
{
char mask[32], data[64];
sprintf(mask, "%%%dllo ", digits);
return tarout_nullpad(t, data,
sprintf(data, mask, (unsigned long long)o), size);
}
int tarout_heading(struct tar_stream *t, int type,
const char *filename, int mode, int uid, int gid,
unsigned long long size, time_t mtime,
const char *linkname, const char *username)
{
int i;
unsigned long sum;
const char *lfn;
const char *pfn;
unsigned int lfn_len;
unsigned int pfn_len;
memset(t->block, 0, 512);
t->tptr = 0;
if (!filename) filename = "";
if (!linkname) linkname = "";
if (!username) username = "";
pfn = "";
pfn_len = 0;
if (strlen(filename) > 100) {
/* ooh, not good. */
for (i = strlen(filename) - 100; filename[i]; i++) {
if (filename[i] == '/') break;
}
if (filename[i]) {
/* okay, THIS will be lfn */
lfn = filename+i+1;
lfn_len = strlen(lfn);
} else {
/* okay, simply not enough room. take first 100
* chars of last path segment
*/
lfn_len = 100;
for (lfn = 0; i >= 0; i--) {
if (filename[i] == '/') {
lfn = filename+i+1;
break;
}
}
if (!lfn) {
/* forget it */
lfn = filename;
}
}
if (lfn > filename) {
i = lfn - filename;
if (i > 155) {
/* oh geez, this is huge.
* take last 155 characters of path
* then chomp up to slash
*/
pfn_len = i;
for (i = 0; i - pfn_len > 155;) {
for (; i < pfn_len; i++) {
if (filename[i] == '/') {
pfn_len -= (i+1);
}
}
}
if (pfn_len > 0) {
pfn = filename+i+1;
}
} else {
pfn = filename;
pfn_len = i;
}
}
} else {
lfn = filename;
lfn_len = strlen(filename);
}
t->hold = 1;
if (!tarout_nullpad(t, lfn, lfn_len, 100)) return 0;
if (!tarout_octal(t, (unsigned long long)mode, 6, 8)) return 0;
if (!tarout_octal(t, (unsigned long long)uid, 6, 8)) return 0;
if (!tarout_octal(t, (unsigned long long)gid, 6, 8)) return 0;
if (!tarout_octal(t, (unsigned long long)size, 11, 12)) return 0;
if (!tarout_octal(t, (unsigned long long)mtime, 11, 12)) return 0;
if (!tarout_nullpad(t, " ", 8, 8)) return 0; /* checksum */
if (!tarout_addch(t, type)) return 0;
if (!tarout_nullpad(t, linkname, strlen(linkname), 100)) return 0;
if (!tarout_nullpad(t, "ustar", 5, 6)) return 0; /* ustar\0 */
if (!tarout_nullpad(t, "00", 2, 2)) return 0;
if (!tarout_nullpad(t, username, strlen(username), 32)) return 0;
if (!tarout_nullpad(t, "", 0, 32)) return 0;
if (!tarout_nullpad(t, "0", 1, 8)) return 0;
if (!tarout_nullpad(t, "0", 1, 8)) return 0;
if (!tarout_nullpad(t, pfn, pfn_len, 155)) return 0;
/* calculate checksum (skipping 8byte chunk) */
for (i = 0, sum = 0; i < 512; i++) {
sum += t->block[i];
}
t->tptr = 148; /* checksum location */
if (!tarout_octal(t, (unsigned long long)sum, 6, 8)) return 0;
/* write out block */
if (!bwrite(t->fd, t->block, 512)) return 0;
/* adjust pointer in case someone else wants to use it */
t->tptr = 0;
t->hold = 0;
/* set up padding requirements for tail */
t->needpad = 512 - (size % 512);
if (t->needpad == 512) t->needpad = 0;
return 1;
}
int tarout_dir(struct tar_stream *t, const char *filename,
int mode, int uid, int gid, time_t mtime)
{
return tarout_heading(t, '5', filename, mode, uid, gid,
0, mtime, 0, 0);
}
int tarout_file(struct tar_stream *t, const char *filename,
int mode, int uid, int gid, time_t mtime,
unsigned long long size)
{
return tarout_heading(t, '0', filename, mode, uid, gid,
size, mtime, 0, 0);
}
int tarout_tail(struct tar_stream *t)
{
int r;
if (t->needpad) {
memset(t->block, 0, t->needpad);
r = bwrite(t->fd, t->block, t->needpad);
t->needpad = 0;
return r;
}
return 1;
}