-
Notifications
You must be signed in to change notification settings - Fork 6
/
sltar.c
219 lines (200 loc) · 5.03 KB
/
sltar.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* sltar - a simple tar
* © 2014 Enno Boland <g s01 de>
*
* See LICENSE for further informations
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <ftw.h>
#include <grp.h>
#include <pwd.h>
#ifndef major
#define major(x) gnu_dev_major(x)
#define minor(x) gnu_dev_minor(x)
#define makedev(x,y) gnu_dev_makedev(x,y)
unsigned int
gnu_dev_major (unsigned long long int dev)
{
return ((unsigned int)( ((dev >> 31 >> 1) & 0xfffff000)
| ((dev >> 8) & 0x00000fff) ));
}
unsigned int
gnu_dev_minor (unsigned long long int dev)
{
return ((unsigned int)( ((dev >> 12) & 0xffffff00)
| (dev & 0x000000ff) ));
}
unsigned long long int
gnu_dev_makedev (unsigned int major, unsigned int minor)
{
return (((major & 0xfffff000ULL) << 32)
| ((major & 0x00000fffULL) << 8)
| ((minor & 0xffffff00ULL) << 12)
| (minor & 0x000000ffULL));
}
#endif
enum Header {
NAME = 0, MODE = 100, UID = 108, GID = 116, SIZE = 124, MTIME = 136,
CHK = 148, TYPE = 156, LINK = 157, MAGIC = 257, VERS = 263, UNAME = 265,
GNAME = 297, MAJ = 329, MIN = 337, END = 512
};
enum Type {
REG = '0', HARDLINK = '1', SYMLINK = '2', CHARDEV = '3', BLOCKDEV = '4',
DIRECTORY = '5', FIFO = '6'
};
void chksum(const char b[END], char *chk) {
unsigned sum = 0, i;
for(i = 0; i<END; i++)
sum += (i >= CHK && i < CHK+8) ? ' ' : b[i];
snprintf(chk, 8, "%.7o", sum);
}
int c_file(const char* path, const struct stat* st, int type) {
int l;
char b[END] = { 0 };
FILE *f = NULL;
struct passwd *pw = getpwuid(st->st_uid);
struct group *gr = getgrgid(st->st_gid);
memset(b+SIZE, '0', 12);
strcpy(b+MAGIC, "ustar");
strcpy(b+VERS, "00");
snprintf(b+NAME, 100, "%s", path);
snprintf(b+MODE, 8, "%.7o", (unsigned)st->st_mode&0777);
snprintf(b+UID, 8, "%.7o", (unsigned)st->st_uid);
snprintf(b+GID, 8, "%.7o", (unsigned)st->st_gid);
snprintf(b+MTIME,12, "%.11o", (unsigned)st->st_mtime);
snprintf(b+UNAME, 32, "%s", pw->pw_name);
snprintf(b+GNAME, 32, "%s", gr->gr_name);
mode_t mode = st->st_mode;
if(S_ISREG(mode)) {
b[TYPE] = REG;
snprintf(b+SIZE, 12, "%.11o", (unsigned)st->st_size);
f = fopen(path, "r");
} else if(S_ISDIR(mode)) {
b[TYPE] = DIRECTORY;
} else if(S_ISLNK(mode)) {
b[TYPE] = SYMLINK;
readlink(path, b+LINK, 99);
} else if(S_ISCHR(mode)) {
b[TYPE] = CHARDEV;
snprintf(b+MAJ, 8, "%.7o", (unsigned)major(st->st_dev));
snprintf(b+MIN, 8, "%.7o", (unsigned)minor(st->st_dev));
} else if(S_ISBLK(mode)) {
b[TYPE] = BLOCKDEV;
snprintf(b+MAJ, 8, "%.7o", (unsigned)major(st->st_dev));
snprintf(b+MIN, 8, "%.7o", (unsigned)minor(st->st_dev));
} else if(S_ISFIFO(mode)) {
b[TYPE] = FIFO;
}
chksum(b, b+CHK);
fwrite(b, END, 1, stdout);
if(!f) return 0;
while((l = fread(b, 1, END, f)) > 0) {
if(l<END)
memset(b + l, 0, END - l);
fwrite(b, END, 1, stdout);
}
fclose(f);
return 0;
}
int x(char *fname, int l, char b[END]){
static char lname[101] = {0}, chk[8] = {0};
int r = 0;
FILE *f = NULL;
memcpy(lname, b+LINK, 100);
unlink(fname);
switch(b[TYPE]) {
case REG:
default:
r = !(f = fopen(fname, "w")) ||
chmod(fname,strtoul(b + MODE, 0, 8));
break;
case HARDLINK:
r = link(lname,fname);
break;
case SYMLINK:
r = symlink(lname,fname);
break;
case DIRECTORY:
r = mkdir(fname,(mode_t) strtoull(b + MODE,0,8));
break;
case CHARDEV:
case BLOCKDEV:
r = mknod(fname, (b[TYPE] == CHARDEV ?
S_IFCHR : S_IFBLK) | strtoul(b + MODE,0,8),
makedev(strtoul(b + MAJ,0,8),
strtoul(b + MIN,0,8)));
break;
case FIFO:
r = mknod(fname, S_IFIFO | strtoul(b + MODE, 0, 8), 0);
break;
// fprintf(stderr,"%s: unsupported filetype %c\n", fname, b[TYPE]);
}
if(r || (getuid() == 0 && chown(fname, strtoul(b + UID, 0, 8),
strtoul(b + GID, 0, 8))))
perror(fname);
chksum(b, chk);
if(strncmp(b+CHK, chk, 8))
fprintf(stderr, "%s: chksum failed\n", fname);
for(; l > 0; l -= END){
fread(b, END, 1, stdin);
if(f) fwrite(b, MIN(l, 512), 1, f);
}
if(f) fclose(f);
return 0;
}
int t(char *fname, int l, char b[END]){
puts(fname);
for(; l > 0; l -= END)
fread(b, END, 1, stdin);
return 0;
}
int tar(int (*fn)(char*, int, char[END])) {
int l;
char b[END], fname[101] = { 0 };
while(fread(b, END, 1, stdin)){
if(*b == '\0')
break;
memcpy(fname, b, 100);
l = strtol(b+SIZE, 0, 8);
fn(fname, l, b);
}
return EXIT_SUCCESS;
}
int c(char *p) {
static struct stat st;
if(lstat(p, &st))
perror(p);
else if(S_ISDIR(st.st_mode))
return ftw(p, c_file, 1024);
else
return c_file(p, &st, 0);
return 1;
}
void usage() {
fputs("sltar-" VERSION " - simple tar\n"
"Usage: sltar [tx]\n"
" sltar c PATH ...\n", stderr);
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[]) {
if(argc < 2 || strlen(argv[1]) != 1)
usage();
switch(argv[1][0]) {
case 'c':
if(argc < 3) usage();
while(argc-- >= 3)
if(c(argv[argc])) return EXIT_FAILURE;
return EXIT_SUCCESS;
case 'x':
if(argc == 2) return tar(x);
case 't':
if(argc == 2) return tar(t);
default:
usage();
}
return EXIT_FAILURE;
}