Line data Source code
1 : /*
2 : * gnome-keyring
3 : *
4 : * Copyright (C) 2008 Stefan Walter
5 : *
6 : * This program is free software; you can redistribute it and/or modify
7 : * it under the terms of the GNU Lesser General Public License as
8 : * published by the Free Software Foundation; either version 2.1 of
9 : * the License, or (at your option) any later version.
10 : *
11 : * This program is distributed in the hope that it will be useful, but
12 : * WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : * Lesser General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU Lesser General Public
17 : * License along with this program; if not, see
18 : * <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : /*
22 : * PORTIONS FROM: ----------------------------------------------------------
23 : *
24 : * GLIB - Library of useful routines for C programming
25 : * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
26 : *
27 : * This library is free software; you can redistribute it and/or
28 : * modify it under the terms of the GNU Lesser General Public
29 : * License as published by the Free Software Foundation; either
30 : * version 2 of the License, or (at your option) any later version.
31 : *
32 : * This library is distributed in the hope that it will be useful,
33 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35 : * Lesser General Public License for more details.
36 : *
37 : * You should have received a copy of the GNU Lesser General Public
38 : * License along with this library; if not, see
39 : * <http://www.gnu.org/licenses/>.
40 : *
41 : * --------------------------------------------------------------------------
42 : */
43 :
44 : /*
45 : * PORTIONS FROM: ----------------------------------------------------------
46 : *
47 : * Modified by the GLib Team and others 1997-2000. See the AUTHORS
48 : * file for a list of people on the GLib Team. See the ChangeLog
49 : * files for a list of changes. These files are distributed with
50 : * GLib at ftp://ftp.gtk.org/pub/gtk/.
51 : *
52 : * --------------------------------------------------------------------------
53 : */
54 :
55 : #include "config.h"
56 :
57 : #include "gkm-util.h"
58 :
59 : #include <glib.h>
60 : #include <glib-object.h>
61 : #include <glib/gstdio.h>
62 :
63 : #include <errno.h>
64 : #include <fcntl.h>
65 : #include <stdio.h>
66 : #include <string.h>
67 :
68 : /* Only access using atomic operations */
69 : static gint next_handle = 0x00000010;
70 :
71 :
72 :
73 : gulong*
74 1418 : gkm_util_ulong_alloc (gulong value)
75 : {
76 1418 : return g_slice_dup (gulong, &value);
77 : }
78 :
79 : void
80 1418 : gkm_util_ulong_free (gpointer ptr_to_ulong)
81 : {
82 1418 : g_slice_free (gulong, ptr_to_ulong);
83 1418 : }
84 :
85 : guint
86 10157 : gkm_util_ulong_hash (gconstpointer v)
87 : {
88 10157 : const signed char *p = v;
89 10157 : guint32 i, h = *p;
90 91413 : for(i = 0; i < sizeof (gulong); ++i)
91 81256 : h = (h << 5) - h + *(p++);
92 10157 : return h;
93 : }
94 :
95 : gboolean
96 3284 : gkm_util_ulong_equal (gconstpointer v1, gconstpointer v2)
97 : {
98 3284 : return *((const gulong*)v1) == *((const gulong*)v2);
99 : }
100 :
101 : CK_RV
102 11572 : gkm_util_return_data (CK_VOID_PTR output, CK_ULONG_PTR n_output,
103 : gconstpointer input, gsize n_input)
104 : {
105 11572 : g_return_val_if_fail (n_output, CKR_GENERAL_ERROR);
106 11572 : g_return_val_if_fail (input || !n_input, CKR_GENERAL_ERROR);
107 :
108 : /* Just asking for the length */
109 11572 : if (!output) {
110 5350 : *n_output = n_input;
111 5350 : return CKR_OK;
112 : }
113 :
114 : /* Buffer is too short */
115 6222 : if (n_input > *n_output) {
116 6 : *n_output = n_input;
117 6 : return CKR_BUFFER_TOO_SMALL;
118 : }
119 :
120 6216 : *n_output = n_input;
121 6216 : if (n_input)
122 6185 : memcpy (output, input, n_input);
123 6216 : return CKR_OK;
124 : }
125 :
126 : CK_ULONG
127 2686 : gkm_util_next_handle (void)
128 : {
129 2686 : return (CK_ULONG)g_atomic_int_add (&next_handle, 1);
130 : }
131 :
132 : void
133 362 : gkm_util_dispose_unref (gpointer object)
134 : {
135 362 : g_return_if_fail (G_IS_OBJECT (object));
136 362 : g_object_run_dispose (G_OBJECT (object));
137 362 : g_object_unref (object);
138 : }
139 :
140 : gchar *
141 6 : gkm_util_locate_keyrings_directory (void)
142 : {
143 : gchar *old_directory;
144 : gchar *new_directory;
145 : gchar *directory;
146 :
147 6 : old_directory = g_build_filename (g_get_home_dir (), ".gnome2", "keyrings", NULL);
148 6 : new_directory = g_build_filename (g_get_user_data_dir (), "keyrings", NULL);
149 :
150 : /*
151 : * If the new XDG directory doesn't exist, and the old one does,
152 : * use the old one, otherwise create/use the new XDG location.
153 : */
154 :
155 7 : if (!g_file_test (new_directory, G_FILE_TEST_IS_DIR) &&
156 1 : g_file_test (old_directory, G_FILE_TEST_IS_DIR)) {
157 0 : directory = old_directory;
158 0 : old_directory = NULL;
159 :
160 0 : g_message ("using old keyring directory: %s", directory);
161 : } else {
162 6 : directory = new_directory;
163 6 : new_directory = NULL;
164 :
165 6 : if (g_mkdir_with_parents (directory, S_IRWXU) < 0)
166 0 : g_warning ("unable to create keyring dir: %s", directory);
167 : }
168 :
169 6 : g_free (old_directory);
170 6 : g_free (new_directory);
171 6 : return directory;
172 : }
|