-
Notifications
You must be signed in to change notification settings - Fork 0
/
archi.c
73 lines (60 loc) · 1.72 KB
/
archi.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
#include "archi.h"
#include "common.h"
static void write_to_bin(const char *rootpath, const char *path, int outbin_fd)
{
const char *newpath;
size_t pathlen, datalen;
int in_fd;
char *buf;
newpath = safe_clear_path(rootpath, path);
pathlen = strlen(newpath);
safe_write(outbin_fd, &pathlen, sizeof(size_t));
safe_write(outbin_fd, newpath, pathlen);
in_fd = safe_open(path, O_RDONLY);
datalen = safe_get_file_size(in_fd);
safe_write(outbin_fd, &datalen, sizeof(size_t));
buf = safe_malloc(datalen);
safe_read(in_fd, buf, datalen);
safe_close(in_fd);
safe_write(outbin_fd, buf, datalen);
safe_free(buf);
}
void pack_dir(const char *indir, const char *outbin)
{
int outbin_fd = safe_creat(outbin, 0700);
size_t filecount = safe_find_files_in_dir(
indir,
indir,
outbin_fd,
&write_to_bin
);
safe_write(outbin_fd, &filecount, sizeof(size_t));
safe_close(outbin_fd);
}
void unpack_dir(const char *inbin, const char *outdir)
{
size_t pathlen, filecount, datalen;
int inbin_fd, outf_fd;
char *path, *new_path;
void *buf;
inbin_fd = safe_open(inbin, O_RDONLY);
safe_lseek(inbin_fd, -sizeof(size_t), SEEK_END);
safe_read(inbin_fd, &filecount, sizeof(size_t));
safe_lseek(inbin_fd, 0, SEEK_SET);
for (size_t i = 0; i < filecount; i++) {
safe_read(inbin_fd, &pathlen, sizeof(size_t));
path = safe_malloc(pathlen + 1);
safe_read(inbin_fd, path, pathlen);
safe_read(inbin_fd, &datalen, sizeof(size_t));
buf = safe_malloc(datalen);
safe_read(inbin_fd, buf, datalen);
new_path = safe_join_str(outdir, path);
outf_fd = safe_creat_from_filepath(new_path, 0700);
safe_write(outf_fd, buf, datalen);
safe_close(outf_fd);
safe_free(new_path);
safe_free(path);
safe_free(buf);
}
safe_close(inbin_fd);
}