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