summaryrefslogtreecommitdiff
path: root/libappstream-glib/as-store-cab.c
blob: 0684e974f71e12bd6dd26c1d0fadbef069f131a1 (plain)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2015 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU Lesser General Public License Version 2.1
 *
 * This library 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.1 of the License, or (at your option) any later version.
 *
 * This library 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include "config.h"

#include <libgcab.h>
#include <glib/gstdio.h>
#include <gio/gunixinputstream.h>

#include "as-store-cab.h"

#ifndef GCabCabinet_autoptr
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GCabCabinet, g_object_unref)
#endif

/**
 * as_store_cab_cb:
 **/
static gboolean
as_store_cab_cb (GCabFile *file, gpointer user_data)
{
	GPtrArray *filelist = (GPtrArray *) user_data;
	g_ptr_array_add (filelist, g_strdup (gcab_file_get_name (file)));
	return TRUE;
}

/**
 * as_store_cab_set_release_blobs:
 **/
static gboolean
as_store_cab_set_release_blobs (AsRelease *release, const gchar *tmp_path, GError **error)
{
	g_autofree gchar *asc_basename = NULL;
	g_autofree gchar *asc_fn = NULL;
	g_autofree gchar *rel_basename = NULL;
	g_autofree gchar *rel_fn = NULL;
	g_autoptr(GError) error_local = NULL;

	/* get the firmware filename */
	rel_basename = g_path_get_basename (as_release_get_filename (release));
	rel_fn = g_build_filename (tmp_path, rel_basename, NULL);

	/* add this information to the release objects */
	if (g_file_test (rel_fn, G_FILE_TEST_EXISTS)) {
		gchar *data = NULL;
		gsize data_len = 0;
		g_autoptr(GBytes) blob = NULL;

		if (!g_file_get_contents (rel_fn, &data, &data_len, &error_local)) {
			g_set_error (error,
				     AS_STORE_ERROR,
				     AS_STORE_ERROR_FAILED,
				     "failed to open %s: %s",
				     rel_fn, error_local->message);
			return FALSE;
		}

		/* this is the size of the firmware */
		if (as_release_get_size (release, AS_SIZE_KIND_INSTALLED) == 0) {
			as_release_set_size (release,
					     AS_SIZE_KIND_INSTALLED,
					     data_len);
		}

		/* set the data on the release object */
		blob = g_bytes_new_take (data, data_len);
		as_release_set_blob (release, rel_basename, blob);
	}

	/* if the signing file exists, set that too */
	asc_basename = g_strdup_printf ("%s.asc", rel_basename);
	asc_fn = g_build_filename (tmp_path, asc_basename, NULL);
	if (g_file_test (asc_fn, G_FILE_TEST_EXISTS)) {
		gchar *data = NULL;
		gsize data_len = 0;
		g_autoptr(GBytes) blob = NULL;

		if (!g_file_get_contents (asc_fn, &data, &data_len, &error_local)) {
			g_set_error (error,
				     AS_STORE_ERROR,
				     AS_STORE_ERROR_FAILED,
				     "failed to open %s: %s",
				     asc_fn, error_local->message);
			return FALSE;
		}

		/* set the data on the release object */
		blob = g_bytes_new_take (data, data_len);
		as_release_set_blob (release, asc_basename, blob);
	}
	return TRUE;
}

/**
 * as_store_cab_from_stream:
 **/
static gboolean
as_store_cab_from_stream (AsStore *store,
			  GInputStream *input_stream,
			  guint64 size,
			  GCancellable *cancellable,
			  GError **error)
{
	g_autoptr(GCabCabinet) gcab = NULL;
	g_autoptr(GError) error_local = NULL;
	g_autofree gchar *tmp_path = NULL;
	g_autoptr(GFile) tmp_file = NULL;
	g_autoptr(GPtrArray) filelist = NULL;
	g_autoptr(GPtrArray) apps = NULL;
	guint i;

	/* open the file */
	gcab = gcab_cabinet_new ();
	if (!gcab_cabinet_load (gcab, input_stream, NULL, &error_local)) {
		g_set_error (error,
			     AS_STORE_ERROR,
			     AS_STORE_ERROR_FAILED,
			     "cannot load .cab file: %s",
			     error_local->message);
		return FALSE;
	}

	/* decompress to /tmp */
	tmp_path = g_dir_make_tmp ("appstream-glib-XXXXXX", &error_local);
	if (tmp_path == NULL) {
		g_set_error (error,
			     AS_STORE_ERROR,
			     AS_STORE_ERROR_FAILED,
			     "failed to create temp dir: %s",
			     error_local->message);
		return FALSE;
	}

	/* extract the entire cab file */
	filelist = g_ptr_array_new_with_free_func (g_free);
	tmp_file = g_file_new_for_path (tmp_path);
	if (!gcab_cabinet_extract_simple (gcab, tmp_file,
					  as_store_cab_cb,
					  filelist, NULL, &error_local)) {
		g_set_error (error,
			     AS_STORE_ERROR,
			     AS_STORE_ERROR_FAILED,
			     "failed to extract .cab file: %s",
			     error_local->message);
		return FALSE;
	}

	/* loop through each file looking for components */
	apps = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
	for (i = 0; i < filelist->len; i++) {
		const gchar *fn;
		g_autofree gchar *tmp_fn = NULL;
		g_autoptr(AsApp) app = NULL;
		AsRelease *rel;

		/* debug */
		fn = g_ptr_array_index (filelist, i);
		g_debug ("found file %i\t%s", i, fn);

		/* if inf or metainfo, add */
		switch (as_app_guess_source_kind (fn)) {
		case AS_APP_SOURCE_KIND_METAINFO:
			tmp_fn = g_build_filename (tmp_path, fn, NULL);
			app = as_app_new ();
			if (!as_app_parse_file (app, tmp_fn,
						AS_APP_PARSE_FLAG_NONE, &error_local)) {
				g_set_error (error,
					     AS_STORE_ERROR,
					     AS_STORE_ERROR_FAILED,
					     "%s could not be loaded: %s",
					     tmp_fn,
					     error_local->message);
				return FALSE;
			}

			/* check release was valid */
			rel = as_app_get_release_default (app);
			if (rel == NULL) {
				g_set_error_literal (error,
						     AS_STORE_ERROR,
						     AS_STORE_ERROR_FAILED,
						     "no releases in metainfo file");
				return FALSE;
			}

			/* fix up legacy files */
			if (as_release_get_filename (rel) == NULL)
				as_release_set_filename (rel, "firmware.bin");

			/* this is the size of the cab file itself */
			if (size > 0 && as_release_get_size (rel, AS_SIZE_KIND_DOWNLOAD) == 0)
				as_release_set_size (rel, AS_SIZE_KIND_DOWNLOAD, size);

			g_ptr_array_add (apps, g_object_ref (app));
			break;
		case AS_APP_SOURCE_KIND_INF:
			/* FIXME: can we handle this? */
			break;
		default:
			break;
		}
	}

	/* add firmware blobs referenced by the metainfo or inf files */
	for (i = 0; i < apps->len; i++) {
		AsApp *app_tmp = AS_APP (g_ptr_array_index (apps, i));
		GPtrArray *releases = as_app_get_releases (app_tmp);
		guint j;
		for (j = 0; j < releases->len; j++) {
			AsRelease *rel = g_ptr_array_index (releases, j);
			if (!as_store_cab_set_release_blobs (rel, tmp_path, error))
				return FALSE;
		}
	}

	/* add any remaining components to the store */
	for (i = 0; i < apps->len; i++) {
		AsApp *app_tmp = AS_APP (g_ptr_array_index (apps, i));
		as_store_add_app (store, app_tmp);
	}

	/* delete temp files */
	for (i = 0; i < filelist->len; i++) {
		const gchar *fn;
		g_autofree gchar *tmp_fn = NULL;
		fn = g_ptr_array_index (filelist, i);
		tmp_fn = g_build_filename (tmp_path, fn, NULL);
		g_unlink (tmp_fn);
	}
	g_rmdir (tmp_path);

	/* success */
	return TRUE;
}

/**
 * as_store_cab_from_bytes:
 **/
gboolean
as_store_cab_from_bytes (AsStore *store,
			 GBytes *bytes,
			 GCancellable *cancellable,
			 GError **error)
{
	g_autoptr(GInputStream) input_stream = NULL;
	input_stream = g_memory_input_stream_new_from_bytes (bytes);
	return as_store_cab_from_stream (store, input_stream,
					 g_bytes_get_size (bytes),
					 cancellable, error);
}

/**
 * as_store_cab_from_file:
 **/
gboolean
as_store_cab_from_file (AsStore *store,
			GFile *file,
			GCancellable *cancellable,
			GError **error)
{
	guint64 size;
	g_autoptr(GError) error_local = NULL;
	g_autoptr(GFileInfo) info = NULL;
	g_autoptr(GInputStream) input_stream = NULL;
	g_autofree gchar *filename = NULL;
	g_autofree gchar *origin = NULL;

	/* set origin */
	origin = g_file_get_basename (file);
	as_store_set_origin (store, origin);

	/* get size */
	info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_SIZE,
				  G_FILE_QUERY_INFO_NONE, cancellable, &error_local);
	if (info == NULL) {
		filename = g_file_get_path (file);
		g_set_error (error,
			     AS_STORE_ERROR,
			     AS_STORE_ERROR_FAILED,
			     "Failed to get info for %s: %s",
			     filename, error_local->message);
		return FALSE;
	}
	size = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_STANDARD_SIZE);

	/* open file */
	input_stream = G_INPUT_STREAM (g_file_read (file, cancellable, &error_local));
	if (input_stream == NULL) {
		filename = g_file_get_path (file);
		g_set_error (error,
			     AS_STORE_ERROR,
			     AS_STORE_ERROR_FAILED,
			     "Failed to open %s: %s",
			     filename, error_local->message);
		return FALSE;
	}

	/* parse */
	return as_store_cab_from_stream (store, input_stream, size, cancellable, error);
}