-
Notifications
You must be signed in to change notification settings - Fork 0
/
td_ar_unpack.c
175 lines (160 loc) · 3.78 KB
/
td_ar_unpack.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
/*
** Copyright 2018 K.J. Hermans (kees.jan.hermans@gmail.com)
** This code is part of simpledbm, an API to a dbm on a finite resource.
** License: BSD
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/file.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <dirent.h>
#include <endian.h>
#include <inttypes.h>
#include <time.h>
#include "td_private.h"
#include "td_ar.h"
/*
static
char md[ 32 ];
static
char* td_ar_modestring
(uint16_t type, uint16_t flags)
{
memset(md, '-', 10);
switch (type) {
case TDAR_TYP_FILE: md[ 0 ] = '-'; break;
case TDAR_TYP_DIR: md[ 0 ] = 'd'; break;
case TDAR_TYP_SYMLINK: md[ 0 ] = 'l'; break;
}
if ((flags & TDAR_IRUSR) == TDAR_IRUSR) {
md[ 1 ] = 'r';
}
if ((flags & TDAR_IWUSR) == TDAR_IWUSR) {
md[ 2 ] = 'w';
}
if ((flags & TDAR_IXUSR) == TDAR_IXUSR) {
md[ 3 ] = 'x';
}
if ((flags & TDAR_IRGRP) == TDAR_IRGRP) {
md[ 4 ] = 'r';
}
if ((flags & TDAR_IWGRP) == TDAR_IWGRP) {
md[ 5 ] = 'w';
}
if ((flags & TDAR_IXGRP) == TDAR_IXGRP) {
md[ 6 ] = 'x';
}
if ((flags & TDAR_IROTH) == TDAR_IROTH) {
md[ 7 ] = 'r';
}
if ((flags & TDAR_IWOTH) == TDAR_IWOTH) {
md[ 8 ] = 'w';
}
if ((flags & TDAR_IXOTH) == TDAR_IXOTH) {
md[ 9 ] = 'x';
}
md[ 10 ] = 0;
return md;
}
static
char ds[ 64 ];
static
char* mon[] = {
"jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov", "dec"
};
static
char* td_ar_datestring
(uint64_t date)
{
time_t now = time(0);
time_t file = (time_t)date;
struct tm* tm = localtime(&file);
if (file < now - (365 * 86400)) {
snprintf(ds, sizeof(ds),
"%s %2u %5d", mon[ tm->tm_mon ], tm->tm_mday, tm->tm_year + 1900
);
} else {
snprintf(ds, sizeof(ds),
"%s %2u %.2u:%.2u"
, mon[ tm->tm_mon ], tm->tm_mday, tm->tm_hour, tm->tm_min
);
}
return ds;
}
*/
int td_ar_unpack
(td_t* td)
{
unsigned char keydata[ TD_AR_KEY_MAX ];
tdt_t key = { keydata, sizeof(keydata) };
tdc_t cursor;
tdc_init(td, &cursor);
while (tdc_get(&cursor, &key, 0, 0) == 0) {
char path[ 2048 ];
td_ar_stat_t s;
char* filename;
unsigned filenamesize;
if (key.size < sizeof(s)) {
fprintf(stderr, "Key size too small.\n");
return ~0;
}
memcpy(&s, keydata, sizeof(s));
if (s.magic != htonl(0x74647200)) {
fprintf(stderr, "File is not a td archive.\n");
return ~0;
}
s.bits = htons(s.bits);
s.type = htons(s.type);
s.uid = htonl(s.uid);
s.gid = htonl(s.gid);
s.size = htobe64(s.size);
s.ctime = htobe64(s.ctime);
s.mtime = htobe64(s.mtime);
s.atime = htobe64(s.atime);
filename = (char*)(&(keydata[ sizeof(s) ]));
filenamesize = key.size - sizeof(s);
snprintf(path, sizeof(path), "%-.*s", filenamesize, filename);
fprintf(stdout, "%s\n", path);
if (s.type == TDAR_TYP_FILE) {
int fd;
if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, s.bits)) < 0) {
fprintf(stderr, "Couldn't open file %s\n", path);
return ~0;
}
if (tdc_get_stream(&cursor, 0, fd, 0)) {
close(fd);
fprintf(stderr, "Error writing file %s\n", path);
return ~0;
}
close(fd);
} else if (s.type == TDAR_TYP_SYMLINK) {
char link[ 1024 ];
tdt_t value = { link, sizeof(link)-1 };
if (tdc_get(&cursor, 0, &value, 0) == 0) {
link[ value.size ] = 0;
unlink(path);
if (symlink(link, path)) {
fprintf(stderr, "Error creating symlink %s -> %s\n", path, link);
return ~0;
}
}
} else {
fprintf(stderr, "WTH %u\n", s.type);
}
if (tdc_nxt(&cursor, 0, 0, 0)) {
break;
}
key.size = sizeof(keydata);
}
return 0;
}
#ifdef __cplusplus
}
#endif