-
Notifications
You must be signed in to change notification settings - Fork 1
/
zfs.c
183 lines (160 loc) · 3.93 KB
/
zfs.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
/* zfs - build a disk image for Zollern
*
* Version 0 is more than trivial and hasn't any features to cope with
* interruptions in the middle of writing, or with media failures.
*
* Version 0 layout
*
* | toc | data_1 | ... | data_N |
*
* Both toc and data blocks have fixed sizes of 16 KiB.
*
* The toc block stores names for the files and pointers to their data
* blocks:
*
* | header | meta_1 | ... | meta_M |
*
* A meta slot is 256 bytes and looks like this:
*
* | name | size | time | ptr_1 | ... | ptr_L |
*
* The name is 64 bytes, size and time are 4 bytes and each ptr is 2
* bytes. Thus,
*
* M = 16 * 1024 / 256 - 1 = 63,
* L = (256 - 64 - 4 - 4) / 2 = 92,
* N = L * M = 5796.
*
* The header is also 256 bytes and looks like this:
*
* | magic | version | unused |
*
*/
#include <stdint.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#define block_size (16*1024)
#define slot_size 256
#define name_size 64
#define n_slots (block_size / slot_size - 1)
#define n_ptrs ((slot_size - name_size - sizeof(uint32_t) - sizeof(uint32_t)) / sizeof(uint16_t))
#define n_data (n_slots * n_ptrs)
struct header {
uint8_t magic[4];
uint32_t version;
uint8_t padding[slot_size - 2*sizeof(uint32_t)];
};
struct meta {
uint8_t name[name_size];
uint32_t size;
uint32_t time;
uint16_t ptr[n_ptrs];
};
struct toc {
struct header header;
struct meta meta[n_slots];
};
struct data {
uint8_t bytes[block_size];
};
struct disk {
struct toc toc;
struct data data[n_data];
};
void
create (char *dir, char *file)
{
char fn[256];
int fd;
struct disk *disk = malloc(sizeof(struct disk));
memset(disk, 0, sizeof(struct disk));
strncpy (disk->toc.header.magic, "ZOLL", 4);
disk->toc.header.version = 0;
int next_slot = 0;
int next_data = 0;
DIR *dd = opendir (dir);
struct dirent *de;
while (de = readdir (dd))
{
if (strcmp (de->d_name, ".") == 0
|| strcmp (de->d_name, "..") == 0)
continue;
int s = next_slot++;
strncpy (disk->toc.meta[s].name, de->d_name, name_size);
int sz = 0;
strcpy (fn, dir);
strcat (fn, "/");
strcat (fn, de->d_name);
fd = open (fn, O_RDONLY);
for (int i = 0; i < n_ptrs; i++)
{
int n = read (fd, disk->data[next_data].bytes, block_size);
if (n == 0)
break;
sz += n;
disk->toc.meta[s].ptr[i] = ++next_data;
next_data += 2; // force discontinuity to make it more interesting
if (n < block_size)
break;
}
close (fd);
disk->toc.meta[s].size = sz;
}
closedir (dd);
fd = open (file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
write (fd, disk, sizeof(struct disk));
close (fd);
}
void
extract (char *file, char *dir)
{
char fn[256];
int fd;
struct disk *disk = malloc(sizeof(struct disk));
fd = open (file, O_RDONLY);
read (fd, disk, sizeof(struct disk));
close (fd);
for (int s = 0; s < n_slots; s++)
{
if (*disk->toc.meta[s].name)
{
strcpy (fn, dir);
strcat (fn, "/");
strcat (fn, disk->toc.meta[s].name);
fd = open(fn, O_WRONLY | O_CREAT, 0666);
int sz = disk->toc.meta[s].size;
int i = 0;
while (sz > 0)
{
int n = block_size;
if (sz < n)
n = sz;
write (fd, disk->data[disk->toc.meta[s].ptr[i]-1].bytes, n);
sz -= n;
i += 1;
}
close(fd);
}
}
}
void usage()
{
fprintf (stderr, "usage: zfs create DIR DISK\n");
fprintf (stderr, " zfs extract DISK\n");
exit (1);
}
void
main (int argc, char **argv)
{
if (strcmp (argv[1], "create") == 0)
create (argv[2], argv[3]);
else if (strcmp (argv[1], "extract") == 0)
extract (argv[2], argv[3]);
else
usage ();
}