+FUSE interface skeleton.
[captive.git] / src / client / fuse / gnomevfsresult.c
1 /* $Id$
2  * Client fuse interface operation "statfs" for libcaptive
3  * Copyright (C) 2005 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include <libgnomevfs/gnome-vfs-result.h>
23 #include <glib/ghash.h>
24 #include <glib/gmessages.h>
25 #include <errno.h>
26
27 #include "gnomevfsresult.h"     /* self */
28
29
30 /* Config: */
31 #define ERRNO_VALUE_MAX 0x400
32
33
34 /* map: GINT_TO_POINTER(GnomeVFSResult) -> GINT_TO_POINTER(errno) */
35 static GHashTable *gnomevfsresult_to_errno_hash;
36
37 static void gnomevfsresult_to_errno_hash_init(void)
38 {
39 int errno_i;
40
41         if (gnomevfsresult_to_errno_hash)
42                 return;
43         gnomevfsresult_to_errno_hash=g_hash_table_new(g_direct_hash,g_direct_equal);
44         for (errno_i=1;errno_i<ERRNO_VALUE_MAX;errno_i++) {
45 GnomeVFSResult gnomevfsresult;
46
47                 /* gnome_vfs_result_from_errno_code() returns 'GNOME_VFS_ERROR_GENERIC'
48                  * for invalid input values.
49                  */
50                 gnomevfsresult=gnome_vfs_result_from_errno_code(errno_i);
51                 if (GNOME_VFS_ERROR_GENERIC==gnomevfsresult)
52                         continue;
53                 g_hash_table_insert(
54                                 gnomevfsresult_to_errno_hash,   /* hash_table */
55                                 GINT_TO_POINTER(gnomevfsresult),        /* key */
56                                 GINT_TO_POINTER(errno_i));      /* value */
57                 }
58 }
59
60 int gnomevfsresult_to_errno(GnomeVFSResult gnomevfsresult)
61 {
62 int r;
63
64         g_return_val_if_fail(sizeof(GnomeVFSResult)>sizeof(gint),EINVAL);
65
66         /* 'GNOME_VFS_OK' may map to NULL or fallback somehow or whatever weird. */
67         if (GNOME_VFS_OK==gnomevfsresult)
68                 return 0;
69
70         gnomevfsresult_to_errno_hash_init();
71         if (!(r=GPOINTER_TO_INT(g_hash_table_lookup(gnomevfsresult_to_errno_hash,GINT_TO_POINTER(gnomevfsresult))))) {
72                 g_assert_not_reached();
73                 return EINVAL;
74                 }
75         return r;
76 }