michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include "mar_private.h" michael@0: #include "mar.h" michael@0: michael@0: #ifdef XP_WIN michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: /* Ensure that the directory containing this file exists */ michael@0: static int mar_ensure_parent_dir(const char *path) michael@0: { michael@0: char *slash = strrchr(path, '/'); michael@0: if (slash) michael@0: { michael@0: *slash = '\0'; michael@0: mar_ensure_parent_dir(path); michael@0: #ifdef XP_WIN michael@0: _mkdir(path); michael@0: #else michael@0: mkdir(path, 0755); michael@0: #endif michael@0: *slash = '/'; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: static int mar_test_callback(MarFile *mar, const MarItem *item, void *unused) { michael@0: FILE *fp; michael@0: char buf[BLOCKSIZE]; michael@0: int fd, len, offset = 0; michael@0: michael@0: if (mar_ensure_parent_dir(item->name)) michael@0: return -1; michael@0: michael@0: #ifdef XP_WIN michael@0: fd = _open(item->name, _O_BINARY|_O_CREAT|_O_TRUNC|_O_WRONLY, item->flags); michael@0: #else michael@0: fd = creat(item->name, item->flags); michael@0: #endif michael@0: if (fd == -1) { michael@0: fprintf(stderr, "ERROR: could not create file in mar_test_callback()\n"); michael@0: perror(item->name); michael@0: return -1; michael@0: } michael@0: michael@0: fp = fdopen(fd, "wb"); michael@0: if (!fp) michael@0: return -1; michael@0: michael@0: while ((len = mar_read(mar, item, offset, buf, sizeof(buf))) > 0) { michael@0: if (fwrite(buf, len, 1, fp) != 1) michael@0: break; michael@0: offset += len; michael@0: } michael@0: michael@0: fclose(fp); michael@0: return len == 0 ? 0 : -1; michael@0: } michael@0: michael@0: int mar_extract(const char *path) { michael@0: MarFile *mar; michael@0: int rv; michael@0: michael@0: mar = mar_open(path); michael@0: if (!mar) michael@0: return -1; michael@0: michael@0: rv = mar_enum_items(mar, mar_test_callback, NULL); michael@0: michael@0: mar_close(mar); michael@0: return rv; michael@0: }