Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 1 | /* -*- 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 | **/ |
| 41 | gboolean |
| 42 | fu_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 | |
Richard Hughes | d0d2ae6 | 2017-08-08 12:22:30 +0100 | [diff] [blame^] | 59 | /** |
| 60 | * fu_common_get_contents_bytes: |
| 61 | * @filename: A filename |
| 62 | * @error: A #GError, or %NULL |
| 63 | * |
| 64 | * Reads a blob of data from a file. |
| 65 | * |
| 66 | * Returns: a #GBytes, or %NULL for failure |
| 67 | **/ |
| 68 | GBytes * |
| 69 | fu_common_get_contents_bytes (const gchar *filename, GError **error) |
| 70 | { |
| 71 | gchar *data = NULL; |
| 72 | gsize len = 0; |
| 73 | if (!g_file_get_contents (filename, &data, &len, error)) |
| 74 | return NULL; |
| 75 | return g_bytes_new_take (data, len); |
| 76 | } |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * fu_common_get_contents_fd: |
| 80 | * @fd: A file descriptor |
| 81 | * @count: The maximum number of bytes to read |
| 82 | * @error: A #GError, or %NULL |
| 83 | * |
| 84 | * Reads a blob from a specific file descriptor. |
| 85 | * |
| 86 | * Note: this will close the fd when done |
| 87 | * |
| 88 | * Returns: (transfer container): a #GBytes, or %NULL |
| 89 | **/ |
| 90 | GBytes * |
| 91 | fu_common_get_contents_fd (gint fd, gsize count, GError **error) |
| 92 | { |
| 93 | g_autoptr(GBytes) blob = NULL; |
| 94 | g_autoptr(GError) error_local = NULL; |
| 95 | g_autoptr(GInputStream) stream = NULL; |
| 96 | |
| 97 | g_return_val_if_fail (fd > 0, NULL); |
| 98 | g_return_val_if_fail (count > 0, NULL); |
| 99 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 100 | |
| 101 | /* read the entire fd to a data blob */ |
| 102 | stream = g_unix_input_stream_new (fd, TRUE); |
| 103 | blob = g_input_stream_read_bytes (stream, count, NULL, &error_local); |
| 104 | if (blob == NULL) { |
| 105 | g_set_error_literal (error, |
| 106 | FWUPD_ERROR, |
| 107 | FWUPD_ERROR_INVALID_FILE, |
| 108 | error_local->message); |
| 109 | return NULL; |
| 110 | } |
| 111 | return g_steal_pointer (&blob); |
| 112 | } |