1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
4 * This file is part of gsignond
6 * Copyright (C) 2012 Intel Corporation.
8 * Contact: Jussi Laako <jussi.laako@linux.intel.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 #include <glib/gstdio.h>
30 #include "gsignond/gsignond-log.h"
31 #include "gsignond/gsignond-storage-manager.h"
32 #include "gsignond/gsignond-utils.h"
34 #define GSIGNOND_STORAGE_MANAGER_GET_PRIVATE(obj) \
35 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
36 GSIGNOND_TYPE_STORAGE_MANAGER, \
37 GSignondStorageManagerPrivate))
39 struct _GSignondStorageManagerPrivate
50 static GParamSpec *properties[N_PROPERTIES] = { NULL, };
52 G_DEFINE_TYPE (GSignondStorageManager, gsignond_storage_manager, G_TYPE_OBJECT);
55 _set_config (GSignondStorageManager *self, GSignondConfig *config)
57 g_assert (self->config == NULL);
58 self->config = config;
60 const gchar *secure_dir = gsignond_config_get_string (
62 GSIGNOND_CONFIG_GENERAL_SECURE_DIR);
64 self->location = g_build_filename (secure_dir,
68 self->location = g_build_filename (g_get_user_data_dir (),
71 DBG ("secure dir %s", self->location);
75 _set_property (GObject *object, guint prop_id, const GValue *value,
78 GSignondStorageManager *self =
79 GSIGNOND_STORAGE_MANAGER (object);
83 g_assert (self->config == NULL);
84 _set_config (self, GSIGNOND_CONFIG (g_value_dup_object (value)));
87 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
92 _get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
94 GSignondStorageManager *self =
95 GSIGNOND_STORAGE_MANAGER (object);
99 g_value_set_object (value, self->config);
102 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
107 _dispose (GObject *object)
109 GSignondStorageManager *self =
110 GSIGNOND_STORAGE_MANAGER (object);
112 /* unmount mounted filesystem */
113 if (gsignond_storage_manager_filesystem_is_mounted (self)) {
114 gsignond_storage_manager_unmount_filesystem (self);
118 g_object_unref (self->config);
122 G_OBJECT_CLASS (gsignond_storage_manager_parent_class)->dispose (object);
126 _finalize (GObject *object)
128 GSignondStorageManager *self =
129 GSIGNOND_STORAGE_MANAGER (object);
131 if (self->location) {
132 g_free (self->location);
133 self->location = NULL;
136 G_OBJECT_CLASS (gsignond_storage_manager_parent_class)->finalize (object);
140 _initialize_storage (GSignondStorageManager *self)
142 g_return_val_if_fail (self != NULL, FALSE);
143 g_return_val_if_fail (self->location, FALSE);
145 if (g_mkdir_with_parents (self->location, S_IRWXU))
152 _delete_storage (GSignondStorageManager *self)
154 g_return_val_if_fail (self != NULL, FALSE);
155 g_return_val_if_fail (self->location, FALSE);
157 return gsignond_wipe_directory (self->location);
161 _storage_is_initialized (GSignondStorageManager *self)
163 g_return_val_if_fail (self != NULL, FALSE);
164 g_return_val_if_fail (self->location, FALSE);
166 if (g_access (self->location, 0)) /* 0 should equal to F_OK */
173 _mount_filesystem (GSignondStorageManager *self)
175 g_return_val_if_fail (self != NULL, NULL);
177 return self->location;
181 _unmount_filesystem (GSignondStorageManager *self)
183 g_return_val_if_fail (self != NULL, FALSE);
189 _filesystem_is_mounted (GSignondStorageManager *self)
191 return _storage_is_initialized (self);
195 gsignond_storage_manager_class_init (GSignondStorageManagerClass *klass)
197 GObjectClass *base = G_OBJECT_CLASS (klass);
199 base->set_property = _set_property;
200 base->get_property = _get_property;
201 base->dispose = _dispose;
202 base->finalize = _finalize;
203 properties[PROP_CONFIG] = g_param_spec_object ("config",
205 "Configuration object",
206 GSIGNOND_TYPE_CONFIG,
207 G_PARAM_CONSTRUCT_ONLY|
209 G_PARAM_STATIC_STRINGS);
210 g_object_class_install_properties (base, N_PROPERTIES, properties);
212 /*g_type_class_add_private (klass, sizeof(GSignondStorageManagerPrivate));*/
214 klass->initialize_storage = _initialize_storage;
215 klass->delete_storage = _delete_storage;
216 klass->storage_is_initialized = _storage_is_initialized;
217 klass->mount_filesystem = _mount_filesystem;
218 klass->unmount_filesystem = _unmount_filesystem;
219 klass->filesystem_is_mounted = _filesystem_is_mounted;
223 gsignond_storage_manager_init (GSignondStorageManager *self)
225 /*self->priv = GSIGNOND_STORAGE_MANAGER_GET_PRIVATE (self);*/
227 self->location = NULL;
232 * gsignond_storage_manager_initialize_storage:
233 * @self: object instance.
235 * Initialize encryption storage. Initiali key should be set using
236 * #gsignond_storage_manager_set_encryption_key before calling this.
241 gsignond_storage_manager_initialize_storage (GSignondStorageManager *self)
243 return GSIGNOND_STORAGE_MANAGER_GET_CLASS (self)->
244 initialize_storage (self);
248 * gsignond_storage_manager_delete_storage:
249 * @self: object instance.
251 * Destroys all the encryption keys and wipes the storage.
256 gsignond_storage_manager_delete_storage (GSignondStorageManager *self)
258 return GSIGNOND_STORAGE_MANAGER_GET_CLASS (self)->
259 delete_storage (self);
263 * gsignond_storage_manager_storage_is_initialized:
264 * @self: object instance.
266 * Checks if the storage exists, and if possible if it has been initialized.
268 * Returns: storage has been initialized?
271 gsignond_storage_manager_storage_is_initialized (GSignondStorageManager *self)
273 return GSIGNOND_STORAGE_MANAGER_GET_CLASS (self)->
274 storage_is_initialized (self);
278 * gsignond_storage_manager_mount_filesystem:
279 * @self: object instance.
281 * Mounts an encrypted storage and returns filesystem path of the storage
284 * Returns: (transfer none): path of the storage mount point.
287 gsignond_storage_manager_mount_filesystem (GSignondStorageManager *self)
289 return GSIGNOND_STORAGE_MANAGER_GET_CLASS (self)->
290 mount_filesystem (self);
294 * gsignond_storage_manager_unmount_filesystem:
295 * @self: object instance.
297 * Unmounts a previously mounted storage filesystem.
302 gsignond_storage_manager_unmount_filesystem (GSignondStorageManager *self)
304 return GSIGNOND_STORAGE_MANAGER_GET_CLASS (self)->
305 unmount_filesystem (self);
309 * gsignond_storage_manager_filesystem_is_mounted:
310 * @self: object instance.
312 * Checks if the filesystem is currently mounted.
314 * Returns: filesystem is currently mounted?
317 gsignond_storage_manager_filesystem_is_mounted (GSignondStorageManager *self)
319 return GSIGNOND_STORAGE_MANAGER_GET_CLASS (self)->
320 filesystem_is_mounted (self);