From b64fd312da70491b5b24c69c4a7eec7ee6551d5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Tue, 14 Jun 2022 19:45:53 +0200 Subject: [PATCH] gerror: Add an utility function to get the GIO Error from GFileError When GIO functions are using GLib file utils functions we expect to return a GIO Error, so provide a way to map such error values. --- docs/reference/gio/gio-sections-common.txt | 1 + gio/gioerror.c | 66 ++++++++++++++++++++++ gio/gioerror.h | 3 + gio/tests/error.c | 63 +++++++++++++++++++++ 4 files changed, 133 insertions(+) diff --git a/docs/reference/gio/gio-sections-common.txt b/docs/reference/gio/gio-sections-common.txt index a1537f0df..b7c9e933c 100644 --- a/docs/reference/gio/gio-sections-common.txt +++ b/docs/reference/gio/gio-sections-common.txt @@ -1373,6 +1373,7 @@ g_io_scheduler_job_send_to_mainloop_async G_IO_ERROR GIOErrorEnum g_io_error_from_errno +g_io_error_from_file_error g_io_error_from_win32_error G_TYPE_IO_ERROR_ENUM diff --git a/gio/gioerror.c b/gio/gioerror.c index 37c8122dc..bcb5e4bc1 100644 --- a/gio/gioerror.c +++ b/gio/gioerror.c @@ -1,6 +1,7 @@ /* GIO - GLib Input, Output and Streaming Library * * Copyright (C) 2006-2007 Red Hat, Inc. + * Copyright (C) 2022 Canonical Ltd. * * SPDX-License-Identifier: LGPL-2.1-or-later * @@ -18,6 +19,7 @@ * Public License along with this library; if not, see . * * Author: Alexander Larsson + * Author: Marco Trevisan */ #include "config.h" @@ -345,6 +347,70 @@ g_io_error_from_errno (gint err_no) } } +/** + * g_io_error_from_file_error: + * @file_error: a #GFileError. + * + * Converts #GFileError error codes into GIO error codes. + * + * Returns: #GIOErrorEnum value for the given #GFileError error value. + * + * Since: 2.74 + **/ +GIOErrorEnum +g_io_error_from_file_error (GFileError file_error) +{ + switch (file_error) + { + case G_FILE_ERROR_EXIST: + return G_IO_ERROR_EXISTS; + case G_FILE_ERROR_ISDIR: + return G_IO_ERROR_IS_DIRECTORY; + case G_FILE_ERROR_ACCES: + return G_IO_ERROR_PERMISSION_DENIED; + case G_FILE_ERROR_NAMETOOLONG: + return G_IO_ERROR_FILENAME_TOO_LONG; + case G_FILE_ERROR_NOENT: + return G_IO_ERROR_NOT_FOUND; + case G_FILE_ERROR_NOTDIR: + return G_IO_ERROR_NOT_DIRECTORY; + case G_FILE_ERROR_NXIO: + return G_IO_ERROR_NOT_REGULAR_FILE; + case G_FILE_ERROR_NODEV: + return G_IO_ERROR_NO_SUCH_DEVICE; + case G_FILE_ERROR_ROFS: + return G_IO_ERROR_READ_ONLY; + case G_FILE_ERROR_TXTBSY: + return G_IO_ERROR_BUSY; + case G_FILE_ERROR_LOOP: + return G_IO_ERROR_TOO_MANY_LINKS; + case G_FILE_ERROR_NOSPC: + case G_FILE_ERROR_NOMEM: + return G_IO_ERROR_NO_SPACE; + case G_FILE_ERROR_MFILE: + case G_FILE_ERROR_NFILE: + return G_IO_ERROR_TOO_MANY_OPEN_FILES; + case G_FILE_ERROR_INVAL: + return G_IO_ERROR_INVALID_ARGUMENT; + case G_FILE_ERROR_PIPE: + return G_IO_ERROR_BROKEN_PIPE; + case G_FILE_ERROR_AGAIN: + return G_IO_ERROR_WOULD_BLOCK; + case G_FILE_ERROR_PERM: + return G_IO_ERROR_PERMISSION_DENIED; + case G_FILE_ERROR_NOSYS: + return G_IO_ERROR_NOT_SUPPORTED; + case G_FILE_ERROR_BADF: + case G_FILE_ERROR_FAILED: + case G_FILE_ERROR_FAULT: + case G_FILE_ERROR_INTR: + case G_FILE_ERROR_IO: + return G_IO_ERROR_FAILED; + default: + g_return_val_if_reached (G_IO_ERROR_FAILED); + } +} + #ifdef G_OS_WIN32 /** diff --git a/gio/gioerror.h b/gio/gioerror.h index cb136fc9a..958c4a931 100644 --- a/gio/gioerror.h +++ b/gio/gioerror.h @@ -29,6 +29,7 @@ #include #include +#include G_BEGIN_DECLS @@ -44,6 +45,8 @@ GLIB_AVAILABLE_IN_ALL GQuark g_io_error_quark (void); GLIB_AVAILABLE_IN_ALL GIOErrorEnum g_io_error_from_errno (gint err_no); +GLIB_AVAILABLE_IN_2_74 +GIOErrorEnum g_io_error_from_file_error (GFileError file_error); #ifdef G_OS_WIN32 GLIB_AVAILABLE_IN_ALL diff --git a/gio/tests/error.c b/gio/tests/error.c index a38008eee..dc79a4d82 100644 --- a/gio/tests/error.c +++ b/gio/tests/error.c @@ -706,6 +706,68 @@ test_error_from_errno (void) #endif } +static void +test_error_from_file_error (void) +{ + g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, + "*should not be reached*"); + g_assert_cmpuint (g_io_error_from_file_error (-1), ==, + G_IO_ERROR_FAILED); + g_test_assert_expected_messages (); + + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_EXIST), ==, + G_IO_ERROR_EXISTS); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_ISDIR), ==, + G_IO_ERROR_IS_DIRECTORY); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_ACCES), ==, + G_IO_ERROR_PERMISSION_DENIED); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_NAMETOOLONG), ==, + G_IO_ERROR_FILENAME_TOO_LONG); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_NOENT), ==, + G_IO_ERROR_NOT_FOUND); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_NOTDIR), ==, + G_IO_ERROR_NOT_DIRECTORY); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_NXIO), ==, + G_IO_ERROR_NOT_REGULAR_FILE); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_NODEV), ==, + G_IO_ERROR_NO_SUCH_DEVICE); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_ROFS), ==, + G_IO_ERROR_READ_ONLY); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_TXTBSY), ==, + G_IO_ERROR_BUSY); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_LOOP), ==, + G_IO_ERROR_TOO_MANY_LINKS); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_NOSPC), ==, + G_IO_ERROR_NO_SPACE); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_NOMEM), ==, + G_IO_ERROR_NO_SPACE); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_MFILE), ==, + G_IO_ERROR_TOO_MANY_OPEN_FILES); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_NFILE), ==, + G_IO_ERROR_TOO_MANY_OPEN_FILES); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_INVAL), ==, + G_IO_ERROR_INVALID_ARGUMENT); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_PIPE), ==, + G_IO_ERROR_BROKEN_PIPE); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_AGAIN), ==, + G_IO_ERROR_WOULD_BLOCK); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_PERM), ==, + G_IO_ERROR_PERMISSION_DENIED); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_NOSYS), ==, + G_IO_ERROR_NOT_SUPPORTED); + + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_BADF), ==, + G_IO_ERROR_FAILED); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_FAILED), ==, + G_IO_ERROR_FAILED); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_FAULT), ==, + G_IO_ERROR_FAILED); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_INTR), ==, + G_IO_ERROR_FAILED); + g_assert_cmpuint (g_io_error_from_file_error (G_FILE_ERROR_IO), ==, + G_IO_ERROR_FAILED); +} + static void test_error_from_win32_error (void) { @@ -787,6 +849,7 @@ main (int argc, g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL); g_test_add_func ("/error/from-errno", test_error_from_errno); + g_test_add_func ("/error/from-file-error", test_error_from_file_error); g_test_add_func ("/error/from-win32-error", test_error_from_win32_error); return g_test_run ();