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/. */
12 #include <onlineupdate/mar_private.h>
13 #include <onlineupdate/mar.h>
20 /* Ensure that the directory containing this file exists */
21 static int mar_ensure_parent_dir(const char *path
)
23 char *slash
= strrchr(path
, '/');
27 mar_ensure_parent_dir(path
);
38 static int mar_test_callback(MarFile
*mar
, const MarItem
*item
, void *unused
) {
41 int fd
, len
, offset
= 0;
43 (void) unused
; // avoid warnings
45 if (mar_ensure_parent_dir(item
->name
))
49 fd
= _open(item
->name
, _O_BINARY
|_O_CREAT
|_O_TRUNC
|_O_WRONLY
, item
->flags
);
51 fd
= creat(item
->name
, item
->flags
);
54 fprintf(stderr
, "ERROR: could not create file in mar_test_callback()\n");
59 fp
= fdopen(fd
, "wb");
63 while ((len
= mar_read(mar
, item
, offset
, buf
, sizeof(buf
))) > 0) {
64 if (fwrite(buf
, len
, 1, fp
) != 1)
70 return len
== 0 ? 0 : -1;
73 int mar_extract(const char *path
) {
81 rv
= mar_enum_items(mar
, mar_test_callback
, NULL
);