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 "udisksmount.h"
37 #include "udisksprivate.h"
38
39 /**
40 * UDisksMount:
41 *
42 * The #UDisksMount structure contains only private data and should
43 * only be accessed using the provided API.
44 */
45 struct _UDisksMount
46 {
47 GObject parent_instance;
48
49 gchar *mount_path;
50 dev_t dev;
51 UDisksMountType type;
52 };
53
54 typedef struct _UDisksMountClass UDisksMountClass;
55
56 struct _UDisksMountClass
57 {
58 GObjectClass parent_class;
59 };
60
61 G_DEFINE_TYPE (UDisksMount, udisks_mount, G_TYPE_OBJECT);
62
63 static void
udisks_mount_finalize(GObject * object)64 udisks_mount_finalize (GObject *object)
65 {
66 UDisksMount *mount = UDISKS_MOUNT (object);
67
68 g_free (mount->mount_path);
69
70 if (G_OBJECT_CLASS (udisks_mount_parent_class)->finalize)
71 G_OBJECT_CLASS (udisks_mount_parent_class)->finalize (object);
72 }
73
74 static void
udisks_mount_init(UDisksMount * mount)75 udisks_mount_init (UDisksMount *mount)
76 {
77 }
78
79 static void
udisks_mount_class_init(UDisksMountClass * klass)80 udisks_mount_class_init (UDisksMountClass *klass)
81 {
82 GObjectClass *gobject_class;
83
84 gobject_class = G_OBJECT_CLASS (klass);
85 gobject_class->finalize = udisks_mount_finalize;
86 }
87
88 UDisksMount *
_udisks_mount_new(dev_t dev,const gchar * mount_path,UDisksMountType type)89 _udisks_mount_new (dev_t dev,
90 const gchar *mount_path,
91 UDisksMountType type)
92 {
93 UDisksMount *mount;
94
95 mount = UDISKS_MOUNT (g_object_new (UDISKS_TYPE_MOUNT, NULL));
96 mount->dev = dev;
97 mount->mount_path = g_strdup (mount_path);
98 mount->type = type;
99
100 return mount;
101 }
102
103 /**
104 * udisks_mount_get_mount_path:
105 * @mount: A #UDisksMount
106 *
107 * Gets the mount path for a #UDISKS_MOUNT_TYPE_FILESYSTEM<!-- -->-type mount.
108 *
109 * It is a programming error to call this on any other type of #UDisksMount.
110 *
111 * Returns: A string owned by @mount. Do not free.
112 */
113 const gchar *
udisks_mount_get_mount_path(UDisksMount * mount)114 udisks_mount_get_mount_path (UDisksMount *mount)
115 {
116 g_return_val_if_fail (UDISKS_IS_MOUNT (mount), NULL);
117 g_return_val_if_fail (mount->type == UDISKS_MOUNT_TYPE_FILESYSTEM, NULL);
118 return mount->mount_path;
119 }
120
121 /**
122 * udisks_mount_get_dev:
123 * @mount: A #UDisksMount.
124 *
125 * Gets the device number for @mount.
126 *
127 * Returns: A #dev_t.
128 */
129 dev_t
udisks_mount_get_dev(UDisksMount * mount)130 udisks_mount_get_dev (UDisksMount *mount)
131 {
132 g_return_val_if_fail (UDISKS_IS_MOUNT (mount), 0);
133 return mount->dev;
134 }
135
136 /**
137 * udisks_mount_compare:
138 * @mount: A #UDisksMount
139 * @other_mount: Another #UDisksMount.
140 *
141 * Comparison function for comparing two #UDisksMount objects.
142 *
143 * Returns: Negative value if @mount < @other_mount; zero if @mount = @other_mount; positive value if @mount > @other_mount.
144 */
145 gint
udisks_mount_compare(UDisksMount * mount,UDisksMount * other_mount)146 udisks_mount_compare (UDisksMount *mount,
147 UDisksMount *other_mount)
148 {
149 gint ret;
150
151 g_return_val_if_fail (UDISKS_IS_MOUNT (mount), 0);
152 g_return_val_if_fail (UDISKS_IS_MOUNT (other_mount), 0);
153
154 ret = g_strcmp0 (mount->mount_path, other_mount->mount_path);
155 if (ret != 0)
156 goto out;
157
158 ret = (other_mount->dev - mount->dev);
159 if (ret != 0)
160 goto out;
161
162 ret = other_mount->type - mount->type;
163
164 out:
165 return ret;
166 }
167
168 /**
169 * udisks_mount_get_mount_type:
170 * @mount: A #UDisksMount.
171 *
172 * Gets the #UDisksMountType for @mount.
173 *
174 * Returns: A value from the #UDisksMountType enumeration.
175 */
176 UDisksMountType
udisks_mount_get_mount_type(UDisksMount * mount)177 udisks_mount_get_mount_type (UDisksMount *mount)
178 {
179 return mount->type;
180 }
181