blob: bfe7dcae10f0d380eb92fd1a8c21806c2e0996eb [file] [log] [blame]
Richard Hughes943d2c92017-06-21 09:04:39 +01001/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2017 Richard Hughes <richard@hughsie.com>
4 *
5 * Licensed under the GNU General Public License Version 2
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22#include <config.h>
23
24#include <gio/gunixinputstream.h>
25
26#include "fwupd-error.h"
27
28#include "fu-common.h"
29
30/**
31 * fu_common_set_contents_bytes:
32 * @filename: A filename
33 * @bytes: The data to write
34 * @error: A #GError, or %NULL
35 *
36 * Writes a blob of data to a filename, creating the parent directories as
37 * required.
38 *
39 * Returns: %TRUE for success
40 **/
41gboolean
42fu_common_set_contents_bytes (const gchar *filename, GBytes *bytes, GError **error)
43{
44 const gchar *data;
45 gsize size;
46 g_autoptr(GFile) file = NULL;
47 g_autoptr(GFile) file_parent = NULL;
48
49 file = g_file_new_for_path (filename);
50 file_parent = g_file_get_parent (file);
51 if (!g_file_query_exists (file_parent, NULL)) {
52 if (!g_file_make_directory_with_parents (file_parent, NULL, error))
53 return FALSE;
54 }
55 data = g_bytes_get_data (bytes, &size);
56 return g_file_set_contents (filename, data, size, error);
57}
58
59
60/**
61 * fu_common_get_contents_fd:
62 * @fd: A file descriptor
63 * @count: The maximum number of bytes to read
64 * @error: A #GError, or %NULL
65 *
66 * Reads a blob from a specific file descriptor.
67 *
68 * Note: this will close the fd when done
69 *
70 * Returns: (transfer container): a #GBytes, or %NULL
71 **/
72GBytes *
73fu_common_get_contents_fd (gint fd, gsize count, GError **error)
74{
75 g_autoptr(GBytes) blob = NULL;
76 g_autoptr(GError) error_local = NULL;
77 g_autoptr(GInputStream) stream = NULL;
78
79 g_return_val_if_fail (fd > 0, NULL);
80 g_return_val_if_fail (count > 0, NULL);
81 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
82
83 /* read the entire fd to a data blob */
84 stream = g_unix_input_stream_new (fd, TRUE);
85 blob = g_input_stream_read_bytes (stream, count, NULL, &error_local);
86 if (blob == NULL) {
87 g_set_error_literal (error,
88 FWUPD_ERROR,
89 FWUPD_ERROR_INVALID_FILE,
90 error_local->message);
91 return NULL;
92 }
93 return g_steal_pointer (&blob);
94}