1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2  *
3  * Copyright (C) 2008 David Zeuthen <zeuthen@gmail.com>
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; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20 
21 #include "config.h"
22 #include <glib/gi18n-lib.h>
23 
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <signal.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <mntent.h>
32 
33 #include <glib.h>
34 #include <glib-object.h>
35 
36 #include "udiskscrypttabentry.h"
37 #include "udisksprivate.h"
38 
39 /**
40  * UDisksCrypttabEntry:
41  *
42  * The #UDisksCrypttabEntry structure contains only private data and should
43  * only be accessed using the provided API.
44  */
45 struct _UDisksCrypttabEntry
46 {
47   GObject parent_instance;
48 
49   gchar *name;
50   gchar *device;
51   gchar *passphrase_path;
52   gchar *options;
53 };
54 
55 typedef struct _UDisksCrypttabEntryClass UDisksCrypttabEntryClass;
56 
57 struct _UDisksCrypttabEntryClass
58 {
59   GObjectClass parent_class;
60 };
61 
62 G_DEFINE_TYPE (UDisksCrypttabEntry, udisks_crypttab_entry, G_TYPE_OBJECT);
63 
64 static void
udisks_crypttab_entry_finalize(GObject * object)65 udisks_crypttab_entry_finalize (GObject *object)
66 {
67   UDisksCrypttabEntry *entry = UDISKS_CRYPTTAB_ENTRY (object);
68 
69   g_free (entry->name);
70   g_free (entry->device);
71   g_free (entry->passphrase_path);
72   g_free (entry->options);
73 
74   if (G_OBJECT_CLASS (udisks_crypttab_entry_parent_class)->finalize)
75     G_OBJECT_CLASS (udisks_crypttab_entry_parent_class)->finalize (object);
76 }
77 
78 static void
udisks_crypttab_entry_init(UDisksCrypttabEntry * crypttab_entry)79 udisks_crypttab_entry_init (UDisksCrypttabEntry *crypttab_entry)
80 {
81 }
82 
83 static void
udisks_crypttab_entry_class_init(UDisksCrypttabEntryClass * klass)84 udisks_crypttab_entry_class_init (UDisksCrypttabEntryClass *klass)
85 {
86   GObjectClass *gobject_class;
87 
88   gobject_class = G_OBJECT_CLASS (klass);
89   gobject_class->finalize = udisks_crypttab_entry_finalize;
90 }
91 
92 UDisksCrypttabEntry *
_udisks_crypttab_entry_new(const gchar * name,const gchar * device,const gchar * passphrase_path,const gchar * options)93 _udisks_crypttab_entry_new (const gchar *name,
94                             const gchar *device,
95                             const gchar *passphrase_path,
96                             const gchar *options)
97 {
98   UDisksCrypttabEntry *entry;
99 
100   entry = UDISKS_CRYPTTAB_ENTRY (g_object_new (UDISKS_TYPE_CRYPTTAB_ENTRY, NULL));
101   entry->name = g_strdup (name);
102   entry->device = g_strdup (device);
103   entry->passphrase_path = g_strdup (passphrase_path);
104   entry->options = g_strdup (options);
105 
106   return entry;
107 }
108 
109 /**
110  * udisks_crypttab_entry_compare:
111  * @entry: A #UDisksCrypttabEntry
112  * @other_entry: Another #UDisksCrypttabEntry.
113  *
114  * Comparison function for comparing two #UDisksCrypttabEntry objects.
115  *
116  * Returns: Negative value if @entry < @other_entry; zero if @entry = @other_entry; positive value if @entry > @other_entry.
117  */
118 gint
udisks_crypttab_entry_compare(UDisksCrypttabEntry * entry,UDisksCrypttabEntry * other_entry)119 udisks_crypttab_entry_compare (UDisksCrypttabEntry  *entry,
120                                UDisksCrypttabEntry  *other_entry)
121 {
122   gint ret;
123 
124   g_return_val_if_fail (UDISKS_IS_CRYPTTAB_ENTRY (entry), 0);
125   g_return_val_if_fail (UDISKS_IS_CRYPTTAB_ENTRY (other_entry), 0);
126 
127   ret = g_strcmp0 (other_entry->name, entry->name);
128   if (ret != 0)
129     goto out;
130 
131   ret = g_strcmp0 (other_entry->device, entry->device);
132   if (ret != 0)
133     goto out;
134 
135   ret = g_strcmp0 (other_entry->passphrase_path, entry->passphrase_path);
136   if (ret != 0)
137     goto out;
138 
139   ret = g_strcmp0 (other_entry->options, entry->options);
140 
141  out:
142   return ret;
143 }
144 
145 /**
146  * udisks_crypttab_entry_get_name:
147  * @entry: A #UDisksCrypttabEntry.
148  *
149  * Gets the name field of @entry.
150  *
151  * Returns: The name field.
152  */
153 const gchar *
udisks_crypttab_entry_get_name(UDisksCrypttabEntry * entry)154 udisks_crypttab_entry_get_name (UDisksCrypttabEntry *entry)
155 {
156   g_return_val_if_fail (UDISKS_IS_CRYPTTAB_ENTRY (entry), NULL);
157   return entry->name;
158 }
159 
160 /**
161  * udisks_crypttab_entry_get_device:
162  * @entry: A #UDisksCrypttabEntry.
163  *
164  * Gets the device field of @entry.
165  *
166  * Returns: The device field.
167  */
168 const gchar *
udisks_crypttab_entry_get_device(UDisksCrypttabEntry * entry)169 udisks_crypttab_entry_get_device (UDisksCrypttabEntry *entry)
170 {
171   g_return_val_if_fail (UDISKS_IS_CRYPTTAB_ENTRY (entry), NULL);
172   return entry->device;
173 }
174 
175 /**
176  * udisks_crypttab_entry_get_passphrase_path:
177  * @entry: A #UDisksCrypttabEntry.
178  *
179  * Gets the passphrase path field of @entry.
180  *
181  * Returns: The passphrase path field.
182  */
183 const gchar *
udisks_crypttab_entry_get_passphrase_path(UDisksCrypttabEntry * entry)184 udisks_crypttab_entry_get_passphrase_path (UDisksCrypttabEntry *entry)
185 {
186   g_return_val_if_fail (UDISKS_IS_CRYPTTAB_ENTRY (entry), NULL);
187   return entry->passphrase_path;
188 }
189 
190 /**
191  * udisks_crypttab_entry_get_options:
192  * @entry: A #UDisksCrypttabEntry.
193  *
194  * Gets the options field of @entry.
195  *
196  * Returns: The options field.
197  */
198 const gchar *
udisks_crypttab_entry_get_options(UDisksCrypttabEntry * entry)199 udisks_crypttab_entry_get_options (UDisksCrypttabEntry *entry)
200 {
201   g_return_val_if_fail (UDISKS_IS_CRYPTTAB_ENTRY (entry), NULL);
202   return entry->options;
203 }
204 
205