summaryrefslogtreecommitdiffstats
path: root/src/metainfo.c
blob: 680505a5093fea9140220ba700611b7f8d236f3d (plain) (blame)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <assert.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sha1.h>
#include <sha2.h>
#include <stddef.h>
#include <stdbool.h>
#include <metainfo.h>
#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <error.h>
#include <bencoding.c>

static void inode_free (struct inode * inode) {
	if (!inode)
		return;
	inode_free(inode->child);
	inode_free(inode->next);
	free(inode);
}

/**
 * returns next file, be it file, not dir. not sure why is this useful actually. I thought I would sum filesizes with that, but I already store cached sums of dir size in dir inodes.
 * maybe for searching files only, not dirs ?!?!?!
 */

const struct inode * next_file (const struct inode * inode) {
	if (inode->child)
		return next_file(inode->child);
	if (inode->next) {
		if (inode->next->child)
			return next_file(inode->next);
		return inode->next;
	}
	const struct inode * predecesor = inode;
	while (!predecesor->next) {
		predecesor = predecesor->parent;
		if (!predecesor)
			return NULL;
	}
	if (predecesor->next->child)
		return next_file(predecesor->next);
	return predecesor->next;
}

/**
 * useful for dirlisting.
 *
 * @param inode	[in]	the inode of which next should be returned. start at metainfo->files inode to get the full tree
 * @param paths [out]	when user builds a path, this amount of paths needs to be popped from path stack before appending resulting inode's name name to it. may be NULL. writes -1 to variable if we descended into a directory.
 * @return next inode, be it dir or file, or NULL if this is the end of inodes
 */

const struct inode * next_inode (const struct inode * inode, int * paths) {
	if (paths)
		*paths = 0;
	if (inode->child) {
		if (paths)
			*paths = -1;
		return inode->child;
	}
	if (inode->next)
		return inode->next;
	const struct inode * predecesor = inode;
	while (!predecesor->next) {
		if (paths)
			(*paths)++; // OB1 possible
		predecesor = predecesor->parent;
		if (!predecesor)
			return NULL;
	}
	return predecesor->next;
}

void metainfo_free (struct metainfo * metainfo) {
	inode_free(metainfo->files);
	free(metainfo->name);
	free(metainfo->client);
	free(metainfo->source);
	free(metainfo->publisher);
	free(metainfo->publisher_url);
	free(metainfo->comment);
	free(metainfo);
}

/**
 * note that strings of path get removed from path blist and their values are changed to NULL, to prevent the need for malloc. path list itself is not freed, only string ownership is transfered and strings are NULLed. some strings may be left in path list elements. existing files are overwritten and/or changed to dirs.
 *
 * @param root	[io]	pointer to root inode of torrent -- &(metainfo->files)
 * @param path	[io]	first element of bencoding list describing the path
 * @param size	[in]	size of file to be inserted
 * @param prnt	[in]	parent/caller object -- used internally, user should call with NULL
 * @return 1 on success, 0 on failure if path element had a NULL value, 2 if a padding file was skipped
 */

static int insert_file (struct inode ** root, struct bencoding * path, inode_length_t size, struct inode * prnt) {
tail_recursion:
	if (!path)
		return 1;
	if (!path->value)
		return 0;
	if (!strcmp(path->value, ".pad") || !strncmp(path->value, "_____padding_file_", strlen("_____padding_file_")))
		return 2;
	if (!*root) {
		*root = calloc(1, sizeof(struct inode));
		(*root)->parent = prnt;
		(*root)->name = path->value;
		path->value = NULL;
		(*root)->length += size;
		return insert_file(&(*root)->child, path->next, size, *root);
	}
	if (!path->next) {
		struct inode * file = calloc(1, sizeof(struct inode));
		file->parent = prnt;
		file->name = path->value;
		path->value = NULL;
		file->next = (*root)->next;
		(*root)->next = file;
		return 1;
	}
	if (strcmp((*root)->name, path->value)) {
		root = &(*root)->next;
		goto tail_recursion;
	} else {
		(*root)->length += size;
		return insert_file(&(*root)->child, path->next, size, *root);
	}
}

/**
 * parses a metainfo file tree into inode file tree. same as with insert_file, values are removed -- original is destroyed, but must stil be freed -- it will be together with metainfo decoded object
 *
 * @param root	[io]	pointer to root inode of torrent -- user sets this to &metainfo->files
 * @param dict	[io]	first element of file tree dict -- user sets this to bpath(file, "info/file tree")->child
 * @param cunt	[out]	increment this variable on every added file. may be NULL.
 * @param prnt	[in]	parent/caller object -- used internally, user should call with NULL
 * @return sum length of the filetree
 */

static int filetree (struct inode ** root, struct bencoding * dict, unsigned * cunt, struct inode * prnt) {
	if (!dict)
		return 0;
	assert(!*root);
	if (!dict->key->valuelen) {
		struct bencoding * length = bpath(dict, "length");
		if (cunt)
			(*cunt)++;
		if (length)
			return (prnt->length = length->intvalue); // XXX crash if root element is ""
		return 0;
	}
	*root = calloc(1, sizeof(struct inode));
	(*root)->parent = prnt;
	(*root)->name = dict->key->value; // XXX crash
	dict->key->value = NULL;
	return ((*root)->length = filetree(&(*root)->child, dict->child, cunt, *root) + filetree(&(*root)->next, dict->next, cunt, prnt));
}

/**
 * parses a torrent bencoding
 *
 * @param data	[in]	metainfo dict as bytes
 * @param size	[in]	length of data
 * @return allocated metainfo object on heap, memory ownership is transfered to the caller
 */

struct metainfo * parse (const char * data, off_t size) {
	struct bencoding * metainfo = bdecode(data, size, 0);
	struct metainfo * r = calloc(1, sizeof(struct metainfo));
#define EXTRACT(attribute, localvar, from) \
	struct bencoding * localvar = bpath(metainfo, from); \
	if (localvar && localvar->valuelen) { \
		free(r->attribute); \
		r->attribute = localvar->value; \
		localvar->value = NULL; /* this is nonstandard, but it's a monorepo */ \
	}
	EXTRACT(name, name, "info/name");
	EXTRACT(name, nameutf8, "info/name.utf-8");
	EXTRACT(client, client, "source/v");
	EXTRACT(source, source, "info/source");
	EXTRACT(publisher, publisher, "info/publisher");
	EXTRACT(publisher, publisherutf8, "info/publisher.utf-8");
	EXTRACT(publisher_url, publisher_url, "info/publisher-url");
	EXTRACT(publisher_url, publisher_urlutf8, "info/publisher-url.utf-8");
	EXTRACT(comment, comment, "comment");
	struct bencoding * retrieved = bpath(metainfo, "creation date");
	if (retrieved && retrieved->valuelen)
		r->retrieved = atoi(retrieved->value);
	struct bencoding * created = bpath(metainfo, "info/creation date");
	if (created && created->intvalue)
		r->created = created->intvalue;
	char * ip = bpath(metainfo, "source/ip")->value; // XXX crash if non-travnik torrents
	char * slash = strchr(ip, '/');
	*slash = '\0'; // XXX crash if non-travnik torrents
	r->ip.sin6_family = AF_INET6;
	r->ip.sin6_port = htons(atoi(++slash));
	inet_pton(AF_INET6, ip, r->ip.sin6_addr.s6_addr);
	struct bencoding * files = bpath(metainfo, "info/files");
	if (files) {
		r->type = V1;
		bforeach (files, file) {
			struct bencoding * attr = bpath(file, "attr");
			if (attr && attr->valuelen && strchr(attr->value, 'p'))
			  	continue;
			inode_length_t len = bpath(file, "length")->intvalue; // XXX crash
			r->length += len;
			r->filecount++;
			insert_file(&r->files, bpath(file, "path")->child, len, NULL); // XXX crash
		}
	}
	struct bencoding * file_tree = bpath(metainfo, "info/file tree");
	if (file_tree && file_tree->child) {
		r->type |= V2;
		if (!r->files)
			r->length = filetree(&r->files, file_tree->child, &r->filecount, NULL);
	}
	if (!r->files) {
		r->filecount = 1;
		r->type = V1;
		r->files = calloc(1, sizeof(struct inode));
		r->files->name = strdup(r->name); // XXX crash
		r->length = r->files->length = bpath(metainfo, "info/length")->intvalue; // XXX crash
	}
	if (!r->files->next && !r->files->child && !r->files->length)
		r->files->length = bpath(metainfo, "info/length")->intvalue; // XXX crash
	char * start = strstr(data, "4:info")+6; // XXX crash if non-travnik torrents
	ptrdiff_t len = bpath(metainfo, "info")->after-start; // XXX crash if non-travnik torrents
	SHA1_CTX sha1; // we abuse ->after private variable. this is nonstandard, but it's a monorepo
	SHA1Init(&sha1);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpointer-sign"
	SHA1Update(&sha1, start, len);
#pragma GCC diagnostic pop
	SHA1Final(r->sha1, &sha1);
	SHA2_CTX sha2;
	SHA256Init(&sha2);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpointer-sign"
	SHA256Update(&sha2, start, len);
#pragma GCC diagnostic pop
	SHA256Final(r->sha256, &sha2);
	free_bencoding(metainfo);
	return r;
}

/**
 * parse all .torrent files in directory
 *
 * @param path	[in]	path to directory
 * @param out	[out]	pointer to variable into which a pointer to the array containing pointers to metainfo. you must metainfo_free() all elements of array and array itself.
 * @return number of metainfo structures created or -errno on failure
 */

int directory (const char * path, struct metainfo *** out) {
	size_t size = 65536;
	int dirfd = open(path, O_DIRECTORY | O_CLOEXEC | O_RDONLY);
	if (dirfd == -1)
		return -errno;
	int fd2 = dup(dirfd);
	if (fd2 == -1) {
		int err = errno;
		close(dirfd);
		return -err;
	}
	DIR * dir = fdopendir(fd2);
	if (!dir) {
		int err = errno;
		close(fd2);
		close(dirfd);
		return -err;
	}
	*out = malloc(65535*sizeof(*out));
	struct dirent * dirent;
	unsigned i = 0;
	while ((dirent = readdir(dir))) {
		if (!(i % 1000)) {
			fprintf(stderr, "%u\n", i);
		}
		if (dirent->d_type != DT_REG)
			continue;
		if (!strstr(dirent->d_name, ".torrent"))
			continue;
		if (i+1 >= size) {
			size *= 2;
			*out = reallocarray(*out, size, sizeof(*out));
		}
		int fd = openat(dirfd, dirent->d_name, O_CLOEXEC | O_RDONLY);
		if (fd == -1) {
			error_at_line(0, errno, __FILE__, __LINE__, "openat(dirfd, %s, O_CLOEXEC | O_RDONLY)", dirent->d_name);
			continue;
		}
		struct stat statbuf;
		if (fstat(fd, &statbuf) == -1) {
			error_at_line(0, errno, __FILE__, __LINE__, "stat(%s)", dirent->d_name);
			close(fd);
			continue;
		}
		char * content = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
		if (content == MAP_FAILED) {
			error_at_line(0, errno, __FILE__, __LINE__, "mmap(NULL, %lu, PROT_READ, MAP_SHARED, %s, 0)", statbuf.st_size, dirent->d_name);
			close(fd);
			continue;
		}
		(*out)[i++] = parse(content, statbuf.st_size);
		munmap(content, statbuf.st_size);
		close(fd);
	}
	closedir(dir);
	close(dirfd);
	return i;
}

#if __INCLUDE_LEVEL__ == 0
#include <lib.c>
#include <time.h>
#include <locale.h>
#define S0(x) ((x) ? (x) : "")
int main (int argc, char ** argv) {
	setlocale(LC_ALL, "");
	int r = 0;
	char * content = NULL;
	if (argc < 1+1)
		error_at_line(1, 0, __FILE__, __LINE__, "%s metainfo.torrent", S0(argv[0]));
	int fd = open(argv[1], O_CLOEXEC | O_RDONLY);
	if (fd == -1)
		error_at_line(2, errno, __FILE__, __LINE__, "open(%s, O_CLOEXEC | O_RDONLY)", argv[1]);
	struct stat statbuf;
	if (fstat(fd, &statbuf) == -1) {
		error_at_line(0, errno, __FILE__, __LINE__, "stat(%s)", argv[1]);
		r = 3;
		goto r;
	}
	if (!(content = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0))) {
		error_at_line(0, errno, __FILE__, __LINE__, "mmap(NULL, %lu, PROT_READ, MAP_SHARED, %s, 0)", statbuf.st_size, argv[1]);
		r = 4;
		goto r;
	}
	struct metainfo * metainfo = parse(content, statbuf.st_size);
	char bufip[INET6_ADDRSTRLEN];
	char created[256];
	char retrieved[256];
	strftime(created, 256, "%c", localtime(&metainfo->created));
	strftime(retrieved, 256, "%c", localtime(&metainfo->retrieved));
	char sha1[41];
	char sha256[65];
	bin2hex(sha1, metainfo->sha1, 20);
	bin2hex(sha256, metainfo->sha256, 32);
	sha1[40] = '\0';
	sha256[64] = '\0';
	printf("name: %s\nsha1: %s\nsha256: %s\ntype: %s\nlength: %f GiB\nfilecount: %u\nclient: %s\nsource: %s\npublisher: %s\npublisher_url: %s\ncomment: %s\nip: %s/%u\ncreated: %s\nretrieved: %s\nfiles:\n", metainfo->name, sha1, sha256, type_str[metainfo->type], (float) metainfo->length/(1024*1024*1024), metainfo->filecount, S0(metainfo->client), S0(metainfo->source), S0(metainfo->publisher), S0(metainfo->publisher_url), S0(metainfo->comment), inet_ntop(AF_INET6, metainfo->ip.sin6_addr.s6_addr, bufip, INET6_ADDRSTRLEN), ntohs(metainfo->ip.sin6_port), metainfo->created ? created : "", retrieved); // XXX crash if non-travnik torrent
	int leap = 0;
	int depth = 0;
	for (const struct inode * inode = metainfo->files; inode; inode = next_inode(inode, &leap)) {
		depth -= leap;
		putchar('\t');
		for (int i = 0; i < depth; i++)
			fwrite("|\t", 1, 2, stdout);
		printf("%s %s (%f MiB)\n", inode->child ? "\\" : "*", inode->name, (float) inode->length/(1024*1024));
	}
	metainfo_free(metainfo);
	metainfo = NULL;
	struct metainfo ** metainfos;
	directory(".", &metainfos);
	r:
	if (content && munmap(content, statbuf.st_size))
		error_at_line(0, errno, __FILE__, __LINE__, "munmap(%s, %lu)", argv[1], statbuf.st_size);
	if (close(fd) == -1)
		error_at_line(0, errno, __FILE__, __LINE__, "close(%s)", argv[1]);
	return r;
}
#endif