mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-02-02 17:26:17 +01:00
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.
This commit is contained in:
parent
e5841beba6
commit
b64fd312da
@ -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
|
||||
<SUBSECTION Standard>
|
||||
G_TYPE_IO_ERROR_ENUM
|
||||
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Author: Alexander Larsson <alexl@redhat.com>
|
||||
* Author: Marco Trevisan <marco.trevisan@canonical.com>
|
||||
*/
|
||||
|
||||
#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
|
||||
|
||||
/**
|
||||
|
@ -29,6 +29,7 @@
|
||||
|
||||
#include <glib.h>
|
||||
#include <gio/gioenums.h>
|
||||
#include <glib/gfileutils.h>
|
||||
|
||||
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
|
||||
|
@ -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 ();
|
||||
|
Loading…
Reference in New Issue
Block a user