File: archive.c

package info (click to toggle)
vips 8.16.1-2
  • links: PTS
  • area: main
  • in suites: sid
  • size: 51,940 kB
  • sloc: ansic: 169,179; cpp: 11,890; python: 4,620; xml: 4,353; sh: 732; perl: 40; makefile: 19
file content (330 lines) | stat: -rw-r--r-- 7,231 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* wrapper around libarchive
 *
 * 8/9/23
 *	- extracted from dzsave
 */

/*

	This file is part of VIPS.

	VIPS is free software; you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
	02110-1301  USA

 */

/*

	These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk

 */

/*
#define DEBUG_VERBOSE
#define VIPS_DEBUG
#define DEBUG
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <glib/gi18n-lib.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <vips/vips.h>
#include <vips/internal.h>

#include "pforeign.h"

#ifdef HAVE_LIBARCHIVE
#include <archive.h>
#include <archive_entry.h>

static GMutex *vips_libarchive_mutex = NULL;

struct _VipsArchive {
	// prepend filenames with this for filesystem output
	char *base_dirname;

	// write a zip to a target
	struct archive *archive;
	VipsTarget *target;
};

void
vips__archive_free(VipsArchive *archive)
{
	// flush any pending writes to zip output
	if (archive->archive)
		archive_write_close(archive->archive);

	VIPS_FREE(archive->base_dirname);
	VIPS_FREEF(archive_write_free, archive->archive);
	VIPS_FREE(archive);
}

static ssize_t
zip_write_target_cb(struct archive *a, void *client_data,
	const void *data, size_t length)
{
	VipsArchive *archive = (VipsArchive *) client_data;

	if (vips_target_write(archive->target, data, length))
		return -1;

	return length;
}

static int
zip_close_target_cb(struct archive *a, void *client_data)
{
	VipsArchive *archive = (VipsArchive *) client_data;

	if (vips_target_end(archive->target))
		return ARCHIVE_FATAL;

	return ARCHIVE_OK;
}

static void *
vips__archive_once_init(void *client)
{
	vips_libarchive_mutex = vips_g_mutex_new();

	return NULL;
}

static void
vips__archive_init(void)
{
	static GOnce once = G_ONCE_INIT;

	VIPS_ONCE(&once, vips__archive_once_init, NULL);
}

// write to a filesystem directory
VipsArchive *
vips__archive_new_to_dir(const char *base_dirname)
{
	VipsArchive *archive;

	vips__archive_init();

	if (!(archive = VIPS_NEW(NULL, VipsArchive)))
		return NULL;

	archive->base_dirname = g_strdup(base_dirname);

	return archive;
}

// write a zip to a target
VipsArchive *
vips__archive_new_to_target(VipsTarget *target,
	const char *base_dirname, int compression)
{
	VipsArchive *archive;

#ifdef DEBUG
	printf("vips__archive_new_to_target: base_dirname = %s, compression = %d\n",
		base_dirname, compression);
#endif /*DEBUG*/

	vips__archive_init();

	if (!(archive = VIPS_NEW(NULL, VipsArchive)))
		return NULL;

	archive->target = target;
	archive->base_dirname = g_strdup(base_dirname);

	if (!(archive->archive = archive_write_new())) {
		vips_error("archive", "%s", _("unable to create archive"));
		vips__archive_free(archive);
		return NULL;
	}

	/* Set format to zip.
	 */
	if (archive_write_set_format(archive->archive, ARCHIVE_FORMAT_ZIP)) {
		vips_error("archive", "%s", _("unable to set zip format"));
		vips__archive_free(archive);
		return NULL;
	}

	/* Remap compression=-1 to compression=6.
	 */
	if (compression == -1)
		compression = 6; /* Z_DEFAULT_COMPRESSION */

#if ARCHIVE_VERSION_NUMBER >= 3002000
	/* Deflate compression requires libarchive >= v3.2.0.
	 * https://github.com/libarchive/libarchive/pull/84
	 */
	char compression_string[2] = { '0' + compression, 0 };
	if (archive_write_set_format_option(archive->archive, "zip",
			"compression-level", compression_string)) {
		vips_error("archive", "%s", _("unable to set compression"));
		vips__archive_free(archive);
		return NULL;
	}
#else
	if (compression > 0)
		g_warning("libarchive >= v3.2.0 required for Deflate compression");
#endif

	/* Do not pad last block.
	 */
	if (archive_write_set_bytes_in_last_block(archive->archive, 1)) {
		vips_error("archive", "%s", _("unable to set padding"));
		vips__archive_free(archive);
		return NULL;
	}

	/* Register target callback functions.
	 */
	if (archive_write_open(archive->archive, archive, NULL,
			zip_write_target_cb, zip_close_target_cb)) {
		vips_error("archive", "%s", _("unable to open for write"));
		vips__archive_free(archive);
		return NULL;
	}

	return archive;
}

static int
vips__archive_mkdir_file(VipsArchive *archive, const char *dirname)
{
	char *path;

	path = g_build_filename(archive->base_dirname, dirname, NULL);

	if (g_mkdir_with_parents(path, 0777) &&
		errno != EEXIST) {
		int save_errno = errno;
		char *utf8name;

		utf8name = g_filename_display_name(path);
		vips_error("archive", _("unable to create directory \"%s\", %s"),
			utf8name, g_strerror(save_errno));

		g_free(utf8name);
		g_free(path);

		return -1;
	}

	g_free(path);

	return 0;
}

int
vips__archive_mkdir(VipsArchive *archive, const char *dirname)
{
	/* The ZIP format maintains a hierarchical structure, avoiding
	 * the need to create individual entries for each (sub-)directory.
	 */
	if (archive->archive)
		return 0;

	return vips__archive_mkdir_file(archive, dirname);
}

static int
vips__archive_mkfile_zip(VipsArchive *archive,
	const char *filename, void *buf, size_t len)
{
	struct archive_entry *entry;

	vips__worker_lock(vips_libarchive_mutex);

	if (!(entry = archive_entry_new())) {
		vips_error("archive", "%s", _("unable to create entry"));
		g_mutex_unlock(vips_libarchive_mutex);
		return -1;
	}

	char *path;

	path = g_build_filename(archive->base_dirname, filename, NULL);

	archive_entry_set_pathname(entry, path);
	archive_entry_set_mode(entry, S_IFREG | 0664);
	archive_entry_set_size(entry, len);

	g_free(path);

	if (archive_write_header(archive->archive, entry)) {
		vips_error("archive", "%s", _("unable to write header"));
		archive_entry_free(entry);
		g_mutex_unlock(vips_libarchive_mutex);
		return -1;
	}

	archive_entry_free(entry);

	if (archive_write_data(archive->archive, buf, len) != len) {
		vips_error("archive", "%s", _("unable to write data"));
		g_mutex_unlock(vips_libarchive_mutex);
		return -1;
	}

	g_mutex_unlock(vips_libarchive_mutex);

	return 0;
}

static int
vips__archive_mkfile_file(VipsArchive *archive,
	const char *filename, void *buf, size_t len)
{
	char *path;
	FILE *f;

	path = g_build_filename(archive->base_dirname, filename, NULL);

	if (!(f = vips__file_open_write(path, FALSE))) {
		g_free(path);
		return -1;
	}

	if (vips__file_write(buf, sizeof(char), len, f)) {
		g_free(path);
		fclose(f);
		return -1;
	}

	fclose(f);
	g_free(path);

	return 0;
}

int
vips__archive_mkfile(VipsArchive *archive,
	const char *filename, void *buf, size_t len)
{
	return ((archive->archive)
			? vips__archive_mkfile_zip
			: vips__archive_mkfile_file)(archive, filename, buf, len);
}

#endif /*HAVE_LIBARCHIVE*/