modules/libmar/src/mar_extract.c

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include <sys/types.h>
michael@0 8 #include <sys/stat.h>
michael@0 9 #include <fcntl.h>
michael@0 10 #include <string.h>
michael@0 11 #include <stdlib.h>
michael@0 12 #include "mar_private.h"
michael@0 13 #include "mar.h"
michael@0 14
michael@0 15 #ifdef XP_WIN
michael@0 16 #include <io.h>
michael@0 17 #include <direct.h>
michael@0 18 #endif
michael@0 19
michael@0 20 /* Ensure that the directory containing this file exists */
michael@0 21 static int mar_ensure_parent_dir(const char *path)
michael@0 22 {
michael@0 23 char *slash = strrchr(path, '/');
michael@0 24 if (slash)
michael@0 25 {
michael@0 26 *slash = '\0';
michael@0 27 mar_ensure_parent_dir(path);
michael@0 28 #ifdef XP_WIN
michael@0 29 _mkdir(path);
michael@0 30 #else
michael@0 31 mkdir(path, 0755);
michael@0 32 #endif
michael@0 33 *slash = '/';
michael@0 34 }
michael@0 35 return 0;
michael@0 36 }
michael@0 37
michael@0 38 static int mar_test_callback(MarFile *mar, const MarItem *item, void *unused) {
michael@0 39 FILE *fp;
michael@0 40 char buf[BLOCKSIZE];
michael@0 41 int fd, len, offset = 0;
michael@0 42
michael@0 43 if (mar_ensure_parent_dir(item->name))
michael@0 44 return -1;
michael@0 45
michael@0 46 #ifdef XP_WIN
michael@0 47 fd = _open(item->name, _O_BINARY|_O_CREAT|_O_TRUNC|_O_WRONLY, item->flags);
michael@0 48 #else
michael@0 49 fd = creat(item->name, item->flags);
michael@0 50 #endif
michael@0 51 if (fd == -1) {
michael@0 52 fprintf(stderr, "ERROR: could not create file in mar_test_callback()\n");
michael@0 53 perror(item->name);
michael@0 54 return -1;
michael@0 55 }
michael@0 56
michael@0 57 fp = fdopen(fd, "wb");
michael@0 58 if (!fp)
michael@0 59 return -1;
michael@0 60
michael@0 61 while ((len = mar_read(mar, item, offset, buf, sizeof(buf))) > 0) {
michael@0 62 if (fwrite(buf, len, 1, fp) != 1)
michael@0 63 break;
michael@0 64 offset += len;
michael@0 65 }
michael@0 66
michael@0 67 fclose(fp);
michael@0 68 return len == 0 ? 0 : -1;
michael@0 69 }
michael@0 70
michael@0 71 int mar_extract(const char *path) {
michael@0 72 MarFile *mar;
michael@0 73 int rv;
michael@0 74
michael@0 75 mar = mar_open(path);
michael@0 76 if (!mar)
michael@0 77 return -1;
michael@0 78
michael@0 79 rv = mar_enum_items(mar, mar_test_callback, NULL);
michael@0 80
michael@0 81 mar_close(mar);
michael@0 82 return rv;
michael@0 83 }

mercurial