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: */
4 * This file is part of Evergreen.
6 * Evergreen is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation, either version 2 of the License,
9 * or (at your option) any later version.
11 * Evergreen is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Evergreen. If not, see <http://www.gnu.org/licenses/>.
19 * This Source Code Form is derived from code that was originally
20 * subject to the terms of the Mozilla Public License, v. 2.0 and
21 * included in Evergreen. You may, therefore, use this Source Code
22 * Form under the terms of the Mozilla Public License 2.0. This
23 * licensing option does not affect the larger Evergreen project, only
24 * the Source Code Forms bearing this exception are affected. If a
25 * copy of the MPL was not distributed with this file, You can obtain
26 * one at http://mozilla.org/MPL/2.0/.
29 #include <sys/types.h>
34 #include "mar_private.h"
42 /* Ensure that the directory containing this file exists */
43 static int mar_ensure_parent_dir(const char *path)
45 char *slash = strrchr(path, '/');
49 mar_ensure_parent_dir(path);
60 static int mar_test_callback(MarFile *mar, const MarItem *item, void *unused) {
63 int fd, len, offset = 0;
65 if (mar_ensure_parent_dir(item->name))
69 fd = _open(item->name, _O_BINARY|_O_CREAT|_O_TRUNC|_O_WRONLY, item->flags);
71 fd = creat(item->name, item->flags);
76 fp = fdopen(fd, "wb");
80 while ((len = mar_read(mar, item, offset, buf, sizeof(buf))) > 0) {
81 if (fwrite(buf, len, 1, fp) != 1)
87 return len == 0 ? 0 : -1;
90 int mar_extract(const char *path) {
98 rv = mar_enum_items(mar, mar_test_callback, NULL);