LibreOffice Module onlineupdate (master) 1
mar_extract.c
Go to the documentation of this file.
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>
13#include <onlineupdate/mar.h>
14
15#ifdef _WIN32
16#include <io.h>
17#include <direct.h>
18#endif
19
20/* Ensure that the directory containing this file exists */
21static int mar_ensure_parent_dir(const char *path)
22{
23 char *slash = strrchr(path, '/');
24 if (slash)
25 {
26 *slash = '\0';
28#ifdef _WIN32
29 _mkdir(path);
30#else
31 mkdir(path, 0755);
32#endif
33 *slash = '/';
34 }
35 return 0;
36}
37
38static int mar_test_callback(MarFile *mar, const MarItem *item, void *unused) {
39 FILE *fp;
40 char buf[BLOCKSIZE];
41 int fd, len, offset = 0;
42
43 (void) unused; // avoid warnings
44
45 if (mar_ensure_parent_dir(item->name))
46 return -1;
47
48#ifdef _WIN32
49 fd = _open(item->name, _O_BINARY|_O_CREAT|_O_TRUNC|_O_WRONLY, item->flags);
50#else
51 fd = creat(item->name, item->flags);
52#endif
53 if (fd == -1) {
54 fprintf(stderr, "ERROR: could not create file in mar_test_callback()\n");
55 perror(item->name);
56 return -1;
57 }
58
59 fp = fdopen(fd, "wb");
60 if (!fp)
61 return -1;
62
63 while ((len = mar_read(mar, item, offset, buf, sizeof(buf))) > 0) {
64 if (fwrite(buf, len, 1, fp) != 1)
65 break;
66 offset += len;
67 }
68
69 fclose(fp);
70 return len == 0 ? 0 : -1;
71}
72
73int mar_extract(const char *path) {
74 MarFile *mar;
75 int rv;
76
77 mar = mar_open(path);
78 if (!mar)
79 return -1;
80
82
83 mar_close(mar);
84 return rv;
85}
#define _O_BINARY
Definition: bsdiff.cxx:32
return NULL
static int mar_test_callback(MarFile *mar, const MarItem *item, void *unused)
Definition: mar_extract.c:38
static int mar_ensure_parent_dir(const char *path)
Definition: mar_extract.c:21
int mar_extract(const char *path)
Extract a MAR file to the current working directory.
Definition: mar_extract.c:73
#define BLOCKSIZE
Definition: mar_private.h:14
int mar_read(MarFile *mar, const MarItem *item, int offset, char *buf, int bufsize)
Read from MAR item at given offset up to bufsize bytes.
Definition: mar_read.c:514
void mar_close(MarFile *mar)
Close a MAR file that was opened using mar_open.
Definition: mar_read.c:195
int mar_enum_items(MarFile *mar, MarItemCallback callback, void *closure)
Enumerate all MAR items via callback function.
Definition: mar_read.c:497
MarFile * mar_open(const char *path)
Open a MAR file for reading.
Definition: mar_read.c:167
Definition: mar.h:48
The MAR item data structure.
Definition: mar.h:38
uint32_t flags
Definition: mar.h:42
char name[1]
Definition: mar.h:43