Branch data Line data Source code
1 : : /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 : :
3 : : /* GIO - GLib Input, Output and Streaming Library
4 : : *
5 : : * Copyright (C) 2006-2007 Red Hat, Inc.
6 : : *
7 : : * SPDX-License-Identifier: LGPL-2.1-or-later
8 : : *
9 : : * This library is free software; you can redistribute it and/or
10 : : * modify it under the terms of the GNU Lesser General Public
11 : : * License as published by the Free Software Foundation; either
12 : : * version 2.1 of the License, or (at your option) any later version.
13 : : *
14 : : * This library is distributed in the hope that it will be useful,
15 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 : : * Lesser General Public License for more details.
18 : : *
19 : : * You should have received a copy of the GNU Lesser General
20 : : * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 : : *
22 : : * Author: Alexander Larsson <alexl@redhat.com>
23 : : */
24 : :
25 : : /* Prologue {{{1 */
26 : :
27 : : #include "config.h"
28 : :
29 : : #include <sys/types.h>
30 : : #include <sys/stat.h>
31 : : #include <sys/wait.h>
32 : : #ifndef HAVE_SYSCTLBYNAME
33 : : #ifdef HAVE_SYS_PARAM_H
34 : : #include <sys/param.h>
35 : : #endif
36 : : #endif
37 : : #ifdef HAVE_POLL
38 : : #include <poll.h>
39 : : #endif
40 : : #include <stdio.h>
41 : : #include <unistd.h>
42 : : #include <sys/time.h>
43 : : #include <errno.h>
44 : : #include <stdlib.h>
45 : : #include <string.h>
46 : : #include <signal.h>
47 : : #include <gstdio.h>
48 : : #include <dirent.h>
49 : :
50 : : #if defined(__ANDROID__) && (__ANDROID_API__ < 26)
51 : : #include <mntent.h>
52 : : /* the shared object of recent bionic libc's have hasmntopt symbol, but
53 : : some a possible common build environment for android, termux ends
54 : : up with insufficient __ANDROID_API__ value for building.
55 : : */
56 : : extern char* hasmntopt(const struct mntent* mnt, const char* opt);
57 : : #endif
58 : :
59 : : #if HAVE_SYS_STATFS_H
60 : : #include <sys/statfs.h>
61 : : #endif
62 : : #if HAVE_SYS_STATVFS_H
63 : : #include <sys/statvfs.h>
64 : : #endif
65 : : #if HAVE_SYS_VFS_H
66 : : #include <sys/vfs.h>
67 : : #elif HAVE_SYS_MOUNT_H
68 : : #if HAVE_SYS_PARAM_H
69 : : #include <sys/param.h>
70 : : #endif
71 : : #include <sys/mount.h>
72 : : #endif
73 : :
74 : : #ifndef O_BINARY
75 : : #define O_BINARY 0
76 : : #endif
77 : :
78 : : #include "gunixmounts.h"
79 : : #include "gunixmounts-private.h"
80 : : #include "gfile.h"
81 : : #include "gfilemonitor.h"
82 : : #include "glibintl.h"
83 : : #include "glocalfile.h"
84 : : #include "gstdio.h"
85 : : #include "gthemedicon.h"
86 : : #include "gcontextspecificgroup.h"
87 : :
88 : :
89 : : #ifdef HAVE_MNTENT_H
90 : : static const char *_resolve_dev_root (void);
91 : : #endif
92 : :
93 : : /**
94 : : * GUnixMountType:
95 : : * @G_UNIX_MOUNT_TYPE_UNKNOWN: Unknown Unix mount type.
96 : : * @G_UNIX_MOUNT_TYPE_FLOPPY: Floppy disk Unix mount type.
97 : : * @G_UNIX_MOUNT_TYPE_CDROM: CDROM Unix mount type.
98 : : * @G_UNIX_MOUNT_TYPE_NFS: Network File System (NFS) Unix mount type.
99 : : * @G_UNIX_MOUNT_TYPE_ZIP: ZIP Unix mount type.
100 : : * @G_UNIX_MOUNT_TYPE_JAZ: JAZZ Unix mount type.
101 : : * @G_UNIX_MOUNT_TYPE_MEMSTICK: Memory Stick Unix mount type.
102 : : * @G_UNIX_MOUNT_TYPE_CF: Compact Flash Unix mount type.
103 : : * @G_UNIX_MOUNT_TYPE_SM: Smart Media Unix mount type.
104 : : * @G_UNIX_MOUNT_TYPE_SDMMC: SD/MMC Unix mount type.
105 : : * @G_UNIX_MOUNT_TYPE_IPOD: iPod Unix mount type.
106 : : * @G_UNIX_MOUNT_TYPE_CAMERA: Digital camera Unix mount type.
107 : : * @G_UNIX_MOUNT_TYPE_HD: Hard drive Unix mount type.
108 : : *
109 : : * Types of Unix mounts.
110 : : **/
111 : : typedef enum {
112 : : G_UNIX_MOUNT_TYPE_UNKNOWN,
113 : : G_UNIX_MOUNT_TYPE_FLOPPY,
114 : : G_UNIX_MOUNT_TYPE_CDROM,
115 : : G_UNIX_MOUNT_TYPE_NFS,
116 : : G_UNIX_MOUNT_TYPE_ZIP,
117 : : G_UNIX_MOUNT_TYPE_JAZ,
118 : : G_UNIX_MOUNT_TYPE_MEMSTICK,
119 : : G_UNIX_MOUNT_TYPE_CF,
120 : : G_UNIX_MOUNT_TYPE_SM,
121 : : G_UNIX_MOUNT_TYPE_SDMMC,
122 : : G_UNIX_MOUNT_TYPE_IPOD,
123 : : G_UNIX_MOUNT_TYPE_CAMERA,
124 : : G_UNIX_MOUNT_TYPE_HD
125 : : } GUnixMountType;
126 : :
127 : : struct _GUnixMountEntry {
128 : : char *mount_path;
129 : : char *device_path;
130 : : char *root_path;
131 : : char *filesystem_type;
132 : : char *options;
133 : : gboolean is_read_only;
134 : : gboolean is_system_internal;
135 : : };
136 : :
137 : 2 : G_DEFINE_BOXED_TYPE (GUnixMountEntry, g_unix_mount_entry, g_unix_mount_entry_copy, g_unix_mount_entry_free)
138 : :
139 : : struct _GUnixMountPoint {
140 : : char *mount_path;
141 : : char *device_path;
142 : : char *filesystem_type;
143 : : char *options;
144 : : gboolean is_read_only;
145 : : gboolean is_user_mountable;
146 : : gboolean is_loopback;
147 : : };
148 : :
149 : 2 : G_DEFINE_BOXED_TYPE (GUnixMountPoint, g_unix_mount_point,
150 : : g_unix_mount_point_copy, g_unix_mount_point_free)
151 : :
152 : : static GList *_g_get_unix_mounts (void);
153 : : static GUnixMountEntry **_g_unix_mounts_get_from_file (const char *table_path,
154 : : uint64_t *time_read_out,
155 : : size_t *n_entries_out);
156 : : static GList *_g_get_unix_mount_points (void);
157 : : static GUnixMountPoint **_g_unix_mount_points_get_from_file (const char *table_path,
158 : : uint64_t *time_read_out,
159 : : size_t *n_points_out);
160 : : static gboolean proc_mounts_watch_is_running (void);
161 : :
162 : : G_LOCK_DEFINE_STATIC (proc_mounts_source);
163 : :
164 : : /* Protected by proc_mounts_source lock */
165 : : static guint64 mount_poller_time = 0;
166 : : static GSource *proc_mounts_watch_source = NULL;
167 : :
168 : : #ifdef HAVE_SYS_MNTTAB_H
169 : : #define MNTOPT_RO "ro"
170 : : #endif
171 : :
172 : : #ifdef HAVE_MNTENT_H
173 : : #include <mntent.h>
174 : : #ifdef HAVE_LIBMOUNT
175 : : #include <libmount.h>
176 : : #endif
177 : : #elif defined (HAVE_SYS_MNTTAB_H)
178 : : #include <sys/mnttab.h>
179 : : #if defined(__sun) && !defined(mnt_opts)
180 : : #define mnt_opts mnt_mntopts
181 : : #endif
182 : : #endif
183 : :
184 : : #ifdef HAVE_SYS_VFSTAB_H
185 : : #include <sys/vfstab.h>
186 : : #endif
187 : :
188 : : #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
189 : : #include <sys/mntctl.h>
190 : : #include <sys/vfs.h>
191 : : #include <sys/vmount.h>
192 : : #include <fshelp.h>
193 : : #endif
194 : :
195 : : #if (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT) || defined(HAVE_GETFSENT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
196 : : #include <sys/param.h>
197 : : #include <sys/mount.h>
198 : : #include <fstab.h>
199 : : #ifdef HAVE_SYS_UCRED_H
200 : : #include <sys/ucred.h>
201 : : #endif
202 : : #ifdef HAVE_SYS_SYSCTL_H
203 : : #include <sys/sysctl.h>
204 : : #endif
205 : : #endif
206 : :
207 : : #ifndef HAVE_SETMNTENT
208 : : #define setmntent(f,m) g_fopen (f, m)
209 : : #endif
210 : : #ifndef HAVE_ENDMNTENT
211 : : #define endmntent(f) fclose(f)
212 : : #endif
213 : :
214 : : #ifdef HAVE_LIBMOUNT
215 : : /* Protected by proc_mounts_source lock */
216 : : static struct libmnt_monitor *proc_mounts_monitor = NULL;
217 : : #endif
218 : :
219 : : static guint64 get_mounts_timestamp (void);
220 : : static guint64 get_mount_points_timestamp (void);
221 : :
222 : : static int
223 : 1456 : compare_str (const char * key,
224 : : const char * const *element)
225 : : {
226 : 1456 : return strcmp (key, *element);
227 : : }
228 : :
229 : : static gboolean
230 : 327 : is_in (const char *value, const char *set[], gsize set_size)
231 : : {
232 : 327 : return bsearch (value, set, set_size, sizeof (char *), (GCompareFunc)compare_str) != NULL;
233 : : }
234 : :
235 : : /* Marked as unused because these are only used on some platform variants, but
236 : : * working out the #if sequence for that would be too much for my little brain. */
237 : : static GList *unix_mount_entry_array_free_to_list (GUnixMountEntry **entries,
238 : : size_t n_entries) G_GNUC_UNUSED;
239 : : static GList *unix_mount_point_array_free_to_list (GUnixMountPoint **points,
240 : : size_t n_points) G_GNUC_UNUSED;
241 : :
242 : : /* Helper to convert to a list for the old API.
243 : : * Steals ownership of the @entries array. */
244 : : static GList *
245 : 8 : unix_mount_entry_array_free_to_list (GUnixMountEntry **entries,
246 : : size_t n_entries)
247 : : {
248 : 8 : GList *l = NULL;
249 : :
250 : 304 : for (size_t i = 0; i < n_entries; i++)
251 : 296 : l = g_list_prepend (l, g_steal_pointer (&entries[i]));
252 : :
253 : 8 : g_free (entries);
254 : :
255 : 8 : return g_list_reverse (l);
256 : : }
257 : :
258 : : /* Helper to convert to a list for the old API.
259 : : * Steals ownership of the @entries array. */
260 : : static GList *
261 : 1 : unix_mount_point_array_free_to_list (GUnixMountPoint **points,
262 : : size_t n_points)
263 : : {
264 : 1 : GList *l = NULL;
265 : :
266 : 1 : for (size_t i = 0; i < n_points; i++)
267 : 0 : l = g_list_prepend (l, g_steal_pointer (&points[i]));
268 : :
269 : 1 : g_free (points);
270 : :
271 : 1 : return g_list_reverse (l);
272 : : }
273 : :
274 : : /**
275 : : * g_unix_is_mount_path_system_internal:
276 : : * @mount_path: (type filename): a mount path, e.g. `/media/disk` or `/usr`
277 : : *
278 : : * Determines if @mount_path is considered an implementation of the
279 : : * OS.
280 : : *
281 : : * This is primarily used for hiding mountable and mounted volumes
282 : : * that only are used in the OS and has little to no relevance to the
283 : : * casual user.
284 : : *
285 : : * Returns: true if @mount_path is considered an implementation detail
286 : : * of the OS; false otherwise
287 : : **/
288 : : gboolean
289 : 10 : g_unix_is_mount_path_system_internal (const char *mount_path)
290 : : {
291 : 10 : if (is_in (mount_path, system_mount_paths, G_N_ELEMENTS (system_mount_paths)))
292 : 1 : return TRUE;
293 : :
294 : : /* Kept separate from sorted list as they may vary */
295 : 9 : if (g_str_equal (GLIB_LOCALSTATEDIR, mount_path) ||
296 : 9 : g_str_equal (GLIB_RUNSTATEDIR, mount_path))
297 : 0 : return TRUE;
298 : :
299 : 9 : if (g_str_has_prefix (mount_path, "/dev/") ||
300 : 9 : g_str_has_prefix (mount_path, "/proc/") ||
301 : 9 : g_str_has_prefix (mount_path, "/sys/"))
302 : 0 : return TRUE;
303 : :
304 : 9 : if (g_str_has_suffix (mount_path, "/.gvfs"))
305 : 0 : return TRUE;
306 : :
307 : 9 : return FALSE;
308 : : }
309 : :
310 : : /**
311 : : * g_unix_is_system_fs_type:
312 : : * @fs_type: a file system type, e.g. `procfs` or `tmpfs`
313 : : *
314 : : * Determines if @fs_type is considered a type of file system which is only
315 : : * used in implementation of the OS.
316 : : *
317 : : * This is primarily used for hiding mounted volumes that are intended as APIs
318 : : * for programs to read, and system administrators at a shell; rather than
319 : : * something that should, for example, appear in a GUI. For example, the Linux
320 : : * `/proc` filesystem.
321 : : *
322 : : * The list of file system types considered ‘system’ ones may change over time.
323 : : *
324 : : * Returns: true if @fs_type is considered an implementation detail of the OS;
325 : : * false otherwise
326 : : * Since: 2.56
327 : : */
328 : : gboolean
329 : 305 : g_unix_is_system_fs_type (const char *fs_type)
330 : : {
331 : : /* keep sorted for bsearch */
332 : 305 : const char *ignore_fs[] = {
333 : : "adfs",
334 : : "afs",
335 : : "auto",
336 : : "autofs",
337 : : "autofs4",
338 : : "cgroup",
339 : : "cgroup2",
340 : : "configfs",
341 : : "cxfs",
342 : : "debugfs",
343 : : "devfs",
344 : : "devpts",
345 : : "devtmpfs",
346 : : "ecryptfs",
347 : : "fdescfs",
348 : : "fuse.gvfsd-fuse",
349 : : "fuse.portal",
350 : : "fusectl",
351 : : "gfs",
352 : : "gfs2",
353 : : "gpfs",
354 : : "hugetlbfs",
355 : : "kernfs",
356 : : "linprocfs",
357 : : "linsysfs",
358 : : "lustre",
359 : : "lustre_lite",
360 : : "mfs",
361 : : "mqueue",
362 : : "ncpfs",
363 : : "nfsd",
364 : : "nullfs",
365 : : "ocfs2",
366 : : "overlay",
367 : : "proc",
368 : : "procfs",
369 : : "pstore",
370 : : "ptyfs",
371 : : "rootfs",
372 : : "rpc_pipefs",
373 : : "securityfs",
374 : : "selinuxfs",
375 : : "sysfs",
376 : : "tmpfs",
377 : : "usbfs",
378 : : };
379 : :
380 : 305 : g_return_val_if_fail (fs_type != NULL && *fs_type != '\0', FALSE);
381 : :
382 : 305 : return is_in (fs_type, ignore_fs, G_N_ELEMENTS (ignore_fs));
383 : : }
384 : :
385 : : /**
386 : : * g_unix_is_system_device_path:
387 : : * @device_path: a device path, e.g. `/dev/loop0` or `nfsd`
388 : : *
389 : : * Determines if @device_path is considered a block device path which is only
390 : : * used in implementation of the OS.
391 : : *
392 : : * This is primarily used for hiding mounted volumes that are intended as APIs
393 : : * for programs to read, and system administrators at a shell; rather than
394 : : * something that should, for example, appear in a GUI. For example, the Linux
395 : : * `/proc` filesystem.
396 : : *
397 : : * The list of device paths considered ‘system’ ones may change over time.
398 : : *
399 : : * Returns: true if @device_path is considered an implementation detail of
400 : : * the OS; false otherwise
401 : : * Since: 2.56
402 : : */
403 : : gboolean
404 : 12 : g_unix_is_system_device_path (const char *device_path)
405 : : {
406 : : /* keep sorted for bsearch */
407 : 12 : const char *ignore_devices[] = {
408 : : "/dev/loop",
409 : : "/dev/vn",
410 : : "devpts",
411 : : "nfsd",
412 : : "none",
413 : : "sunrpc",
414 : : };
415 : :
416 : 12 : g_return_val_if_fail (device_path != NULL && *device_path != '\0', FALSE);
417 : :
418 : 12 : return is_in (device_path, ignore_devices, G_N_ELEMENTS (ignore_devices));
419 : : }
420 : :
421 : : static gboolean
422 : 299 : guess_system_internal (const char *mountpoint,
423 : : const char *fs,
424 : : const char *device,
425 : : const char *root)
426 : : {
427 : 299 : if (g_unix_is_system_fs_type (fs))
428 : 289 : return TRUE;
429 : :
430 : 10 : if (g_unix_is_system_device_path (device))
431 : 0 : return TRUE;
432 : :
433 : 10 : if (g_unix_is_mount_path_system_internal (mountpoint))
434 : 1 : return TRUE;
435 : :
436 : : /* It is not possible to reliably detect mounts which were created by bind
437 : : * operation. mntent-based _g_get_unix_mounts() implementation blindly skips
438 : : * mounts with a device path that is repeated (e.g. mounts created by bind
439 : : * operation, btrfs subvolumes). This usually chooses the most important
440 : : * mounts (i.e. which points to the root of filesystem), but it doesn't work
441 : : * in all cases and also it is not ideal that those mounts are completely
442 : : * ignored (e.g. x-gvfs-show doesn't work for them, trash backend can't handle
443 : : * files on btrfs subvolumes). libmount-based _g_get_unix_mounts()
444 : : * implementation provides a root path. So there is no need to completely
445 : : * ignore those mounts, because e.g. our volume monitors can use the root path
446 : : * to not mengle those mounts with the "regular" mounts (i.e. which points to
447 : : * the root). But because those mounts usually just duplicate other mounts and
448 : : * are completely ignored with mntend-based implementation, let's mark them as
449 : : * system internal. Given the different approaches it doesn't mean that all
450 : : * mounts which were ignored will be system internal now, but this should work
451 : : * in most cases. For more info, see g_unix_mount_entry_get_root_path()
452 : : * annotation, comment in mntent-based _g_get_unix_mounts() implementation and
453 : : * the https://gitlab.gnome.org/GNOME/glib/issues/1271 issue.
454 : : */
455 : 9 : if (root != NULL && g_strcmp0 (root, "/") != 0)
456 : 9 : return TRUE;
457 : :
458 : 0 : return FALSE;
459 : : }
460 : :
461 : : /* GUnixMounts (ie: mtab) implementations {{{1 */
462 : :
463 : : static GUnixMountEntry *
464 : 299 : create_unix_mount_entry (const char *device_path,
465 : : const char *mount_path,
466 : : const char *root_path,
467 : : const char *filesystem_type,
468 : : const char *options,
469 : : gboolean is_read_only)
470 : : {
471 : 299 : GUnixMountEntry *mount_entry = NULL;
472 : :
473 : 299 : mount_entry = g_new0 (GUnixMountEntry, 1);
474 : 299 : mount_entry->device_path = g_strdup (device_path);
475 : 299 : mount_entry->mount_path = g_strdup (mount_path);
476 : 299 : mount_entry->root_path = g_strdup (root_path);
477 : 299 : mount_entry->filesystem_type = g_strdup (filesystem_type);
478 : 299 : mount_entry->options = g_strdup (options);
479 : 299 : mount_entry->is_read_only = is_read_only;
480 : :
481 : 299 : mount_entry->is_system_internal =
482 : 299 : guess_system_internal (mount_entry->mount_path,
483 : 299 : mount_entry->filesystem_type,
484 : 299 : mount_entry->device_path,
485 : 299 : mount_entry->root_path);
486 : :
487 : 299 : return mount_entry;
488 : : }
489 : :
490 : : static GUnixMountPoint *
491 : 4 : create_unix_mount_point (const char *device_path,
492 : : const char *mount_path,
493 : : const char *filesystem_type,
494 : : const char *options,
495 : : gboolean is_read_only,
496 : : gboolean is_user_mountable,
497 : : gboolean is_loopback)
498 : : {
499 : 4 : GUnixMountPoint *mount_point = NULL;
500 : :
501 : 4 : mount_point = g_new0 (GUnixMountPoint, 1);
502 : 4 : mount_point->device_path = g_strdup (device_path);
503 : 4 : mount_point->mount_path = g_strdup (mount_path);
504 : 4 : mount_point->filesystem_type = g_strdup (filesystem_type);
505 : 4 : mount_point->options = g_strdup (options);
506 : 4 : mount_point->is_read_only = is_read_only;
507 : 4 : mount_point->is_user_mountable = is_user_mountable;
508 : 4 : mount_point->is_loopback = is_loopback;
509 : :
510 : 4 : return mount_point;
511 : : }
512 : :
513 : : /* mntent.h (Linux, GNU, NSS) {{{2 */
514 : : #ifdef HAVE_MNTENT_H
515 : :
516 : : #ifdef HAVE_LIBMOUNT
517 : :
518 : : /* For documentation on /proc/self/mountinfo see
519 : : * http://www.kernel.org/doc/Documentation/filesystems/proc.txt
520 : : */
521 : : #define PROC_MOUNTINFO_PATH "/proc/self/mountinfo"
522 : :
523 : : static GUnixMountEntry **
524 : 9 : _g_unix_mounts_get_from_file (const char *table_path,
525 : : uint64_t *time_read_out,
526 : : size_t *n_entries_out)
527 : : {
528 : 9 : struct libmnt_table *table = NULL;
529 : 9 : struct libmnt_iter* iter = NULL;
530 : 9 : struct libmnt_fs *fs = NULL;
531 : 9 : GUnixMountEntry *mount_entry = NULL;
532 : 9 : GPtrArray *return_array = NULL;
533 : :
534 : 9 : if (time_read_out != NULL)
535 : 1 : *time_read_out = get_mounts_timestamp ();
536 : :
537 : 9 : return_array = g_ptr_array_new_null_terminated (0, (GDestroyNotify) g_unix_mount_entry_free, TRUE);
538 : 9 : table = mnt_new_table ();
539 : 9 : if (mnt_table_parse_mtab (table, table_path) < 0)
540 : 0 : goto out;
541 : :
542 : 9 : iter = mnt_new_iter (MNT_ITER_FORWARD);
543 : 308 : while (mnt_table_next_fs (table, iter, &fs) == 0)
544 : : {
545 : 299 : const char *device_path = NULL;
546 : 299 : char *mount_options = NULL;
547 : 299 : unsigned long mount_flags = 0;
548 : 299 : gboolean is_read_only = FALSE;
549 : :
550 : 299 : device_path = mnt_fs_get_source (fs);
551 : 299 : if (g_strcmp0 (device_path, "/dev/root") == 0)
552 : 0 : device_path = _resolve_dev_root ();
553 : :
554 : 299 : mount_options = mnt_fs_strdup_options (fs);
555 : 299 : if (mount_options)
556 : : {
557 : 299 : mnt_optstr_get_flags (mount_options, &mount_flags, mnt_get_builtin_optmap (MNT_LINUX_MAP));
558 : 299 : g_free (mount_options);
559 : : }
560 : 299 : is_read_only = (mount_flags & MS_RDONLY) ? TRUE : FALSE;
561 : :
562 : 299 : mount_entry = create_unix_mount_entry (device_path,
563 : : mnt_fs_get_target (fs),
564 : : mnt_fs_get_root (fs),
565 : : mnt_fs_get_fstype (fs),
566 : : mnt_fs_get_options (fs),
567 : : is_read_only);
568 : :
569 : 299 : g_ptr_array_add (return_array, g_steal_pointer (&mount_entry));
570 : : }
571 : 9 : mnt_free_iter (iter);
572 : :
573 : 9 : out:
574 : 9 : mnt_free_table (table);
575 : :
576 : 9 : if (n_entries_out != NULL)
577 : 9 : *n_entries_out = return_array->len;
578 : :
579 : 9 : return (GUnixMountEntry **) g_ptr_array_free (g_steal_pointer (&return_array), FALSE);
580 : : }
581 : :
582 : : static GList *
583 : 8 : _g_get_unix_mounts (void)
584 : : {
585 : 8 : GUnixMountEntry **entries = NULL;
586 : 8 : size_t n_entries = 0;
587 : :
588 : 8 : entries = _g_unix_mounts_get_from_file (NULL /* default libmount filename */,
589 : : NULL, &n_entries);
590 : :
591 : 8 : return unix_mount_entry_array_free_to_list (g_steal_pointer (&entries), n_entries);
592 : : }
593 : :
594 : : #else
595 : :
596 : : static const char *
597 : : get_mtab_read_file (void)
598 : : {
599 : : #ifdef _PATH_MOUNTED
600 : : # ifdef __linux__
601 : : return "/proc/mounts";
602 : : # else
603 : : return _PATH_MOUNTED;
604 : : # endif
605 : : #else
606 : : return "/etc/mtab";
607 : : #endif
608 : : }
609 : :
610 : : #ifndef HAVE_GETMNTENT_R
611 : : G_LOCK_DEFINE_STATIC(getmntent);
612 : : #endif
613 : :
614 : : static GUnixMountEntry **
615 : : _g_unix_mounts_get_from_file (const char *table_path,
616 : : uint64_t *time_read_out,
617 : : size_t *n_entries_out)
618 : : {
619 : : #ifdef HAVE_GETMNTENT_R
620 : : struct mntent ent;
621 : : char buf[1024];
622 : : #endif
623 : : struct mntent *mntent;
624 : : FILE *file;
625 : : GUnixMountEntry *mount_entry;
626 : : GHashTable *mounts_hash;
627 : : GPtrArray *return_array = NULL;
628 : :
629 : : if (time_read_out != NULL)
630 : : *time_read_out = get_mounts_timestamp ();
631 : :
632 : : file = setmntent (table_path, "re");
633 : : if (file == NULL)
634 : : return NULL;
635 : :
636 : : return_array = g_ptr_array_new_null_terminated (0, (GDestroyNotify) g_unix_mount_entry_free, TRUE);
637 : : mounts_hash = g_hash_table_new (g_str_hash, g_str_equal);
638 : :
639 : : #ifdef HAVE_GETMNTENT_R
640 : : while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
641 : : #else
642 : : G_LOCK (getmntent);
643 : : while ((mntent = getmntent (file)) != NULL)
644 : : #endif
645 : : {
646 : : const char *device_path = NULL;
647 : : gboolean is_read_only = FALSE;
648 : :
649 : : /* ignore any mnt_fsname that is repeated and begins with a '/'
650 : : *
651 : : * We do this to avoid being fooled by --bind mounts, since
652 : : * these have the same device as the location they bind to.
653 : : * It's not an ideal solution to the problem, but it's likely that
654 : : * the most important mountpoint is first and the --bind ones after
655 : : * that aren't as important. So it should work.
656 : : *
657 : : * The '/' is to handle procfs, tmpfs and other no device mounts.
658 : : */
659 : : if (mntent->mnt_fsname != NULL &&
660 : : mntent->mnt_fsname[0] == '/' &&
661 : : g_hash_table_lookup (mounts_hash, mntent->mnt_fsname))
662 : : continue;
663 : :
664 : : if (g_strcmp0 (mntent->mnt_fsname, "/dev/root") == 0)
665 : : device_path = _resolve_dev_root ();
666 : : else
667 : : device_path = mntent->mnt_fsname;
668 : :
669 : : #if defined (HAVE_HASMNTOPT)
670 : : if (hasmntopt (mntent, MNTOPT_RO) != NULL)
671 : : is_read_only = TRUE;
672 : : #endif
673 : :
674 : : mount_entry = create_unix_mount_entry (device_path,
675 : : mntent->mnt_dir,
676 : : NULL,
677 : : mntent->mnt_type,
678 : : mntent->mnt_opts,
679 : : is_read_only);
680 : :
681 : : g_hash_table_insert (mounts_hash,
682 : : mount_entry->device_path,
683 : : mount_entry->device_path);
684 : :
685 : : g_ptr_array_add (return_array, g_steal_pointer (&mount_entry));
686 : : }
687 : : g_hash_table_destroy (mounts_hash);
688 : :
689 : : endmntent (file);
690 : :
691 : : #ifndef HAVE_GETMNTENT_R
692 : : G_UNLOCK (getmntent);
693 : : #endif
694 : :
695 : : if (n_entries_out != NULL)
696 : : *n_entries_out = return_array->len;
697 : :
698 : : return (GUnixMountEntry **) g_ptr_array_free (g_steal_pointer (&return_array), FALSE);
699 : : }
700 : :
701 : : static GList *
702 : : _g_get_unix_mounts (void)
703 : : {
704 : : GUnixMountEntry **entries = NULL;
705 : : size_t n_entries = 0;
706 : :
707 : : entries = _g_unix_mounts_get_from_file (get_mtab_read_file (), NULL, &n_entries);
708 : :
709 : : return unix_mount_entry_array_free_to_list (g_steal_pointer (&entries), n_entries);
710 : : }
711 : :
712 : : #endif /* HAVE_LIBMOUNT */
713 : :
714 : : static const char *
715 : 3 : get_mtab_monitor_file (void)
716 : : {
717 : : static const char *mountinfo_path = NULL;
718 : : #ifdef HAVE_LIBMOUNT
719 : : struct stat buf;
720 : : #endif
721 : :
722 : 3 : if (mountinfo_path != NULL)
723 : 1 : return mountinfo_path;
724 : :
725 : : #ifdef HAVE_LIBMOUNT
726 : : /* The mtab file is still used by some distros, so it has to be monitored in
727 : : * order to avoid races between g_unix_mounts_get and "mounts-changed" signal:
728 : : * https://bugzilla.gnome.org/show_bug.cgi?id=782814
729 : : */
730 : 2 : if (mnt_has_regular_mtab (&mountinfo_path, NULL))
731 : : {
732 : 0 : return mountinfo_path;
733 : : }
734 : :
735 : 2 : if (stat (PROC_MOUNTINFO_PATH, &buf) == 0)
736 : : {
737 : 2 : mountinfo_path = PROC_MOUNTINFO_PATH;
738 : 2 : return mountinfo_path;
739 : : }
740 : : #endif
741 : :
742 : : #ifdef _PATH_MOUNTED
743 : : # ifdef __linux__
744 : 0 : mountinfo_path = "/proc/mounts";
745 : : # else
746 : : mountinfo_path = _PATH_MOUNTED;
747 : : # endif
748 : : #else
749 : : mountinfo_path = "/etc/mtab";
750 : : #endif
751 : :
752 : 0 : return mountinfo_path;
753 : : }
754 : :
755 : : /* mnttab.h {{{2 */
756 : : #elif defined (HAVE_SYS_MNTTAB_H)
757 : :
758 : : G_LOCK_DEFINE_STATIC(getmntent);
759 : :
760 : : static const char *
761 : : get_mtab_read_file (void)
762 : : {
763 : : #ifdef _PATH_MOUNTED
764 : : return _PATH_MOUNTED;
765 : : #else
766 : : return "/etc/mnttab";
767 : : #endif
768 : : }
769 : :
770 : : static const char *
771 : : get_mtab_monitor_file (void)
772 : : {
773 : : return get_mtab_read_file ();
774 : : }
775 : :
776 : : static GUnixMountEntry **
777 : : _g_unix_mounts_get_from_file (const char *table_path,
778 : : uint64_t *time_read_out,
779 : : size_t *n_entries_out)
780 : : {
781 : : struct mnttab mntent;
782 : : FILE *file;
783 : : GUnixMountEntry *mount_entry;
784 : : GPtrArray *return_array = NULL;
785 : :
786 : : if (time_read_out != NULL)
787 : : *time_read_out = get_mounts_timestamp ();
788 : :
789 : : file = setmntent (table_path, "re");
790 : : if (file == NULL)
791 : : return NULL;
792 : :
793 : : return_array = g_ptr_array_new_null_terminated (0, (GDestroyNotify) g_unix_mount_entry_free, TRUE);
794 : :
795 : : G_LOCK (getmntent);
796 : : while (! getmntent (file, &mntent))
797 : : {
798 : : gboolean is_read_only = FALSE;
799 : :
800 : : #if defined (HAVE_HASMNTOPT)
801 : : if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
802 : : is_read_only = TRUE;
803 : : #endif
804 : :
805 : : mount_entry = create_unix_mount_entry (mntent.mnt_special,
806 : : mntent.mnt_mountp,
807 : : NULL,
808 : : mntent.mnt_fstype,
809 : : mntent.mnt_opts,
810 : : is_read_only);
811 : :
812 : : g_ptr_array_add (return_array, g_steal_pointer (&mount_entry));
813 : : }
814 : :
815 : : endmntent (file);
816 : :
817 : : G_UNLOCK (getmntent);
818 : :
819 : : if (n_entries_out != NULL)
820 : : *n_entries_out = return_array->len;
821 : :
822 : : return (GUnixMountEntry **) g_ptr_array_free (g_steal_pointer (&return_array), FALSE);
823 : : }
824 : :
825 : : static GList *
826 : : _g_get_unix_mounts (void)
827 : : {
828 : : GUnixMountEntry **entries = NULL;
829 : : size_t n_entries = 0;
830 : :
831 : : entries = _g_unix_mounts_get_from_file (get_mtab_read_file (), NULL, &n_entries);
832 : :
833 : : return unix_mount_entry_array_free_to_list (g_steal_pointer (&entries), n_entries);
834 : : }
835 : :
836 : : /* mntctl.h (AIX) {{{2 */
837 : : #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
838 : :
839 : : static const char *
840 : : get_mtab_monitor_file (void)
841 : : {
842 : : return NULL;
843 : : }
844 : :
845 : : static GList *
846 : : _g_get_unix_mounts (void)
847 : : {
848 : : struct vfs_ent *fs_info;
849 : : struct vmount *vmount_info;
850 : : int vmount_number;
851 : : unsigned int vmount_size;
852 : : int current;
853 : : GList *return_list;
854 : :
855 : : if (mntctl (MCTL_QUERY, sizeof (vmount_size), &vmount_size) != 0)
856 : : {
857 : : g_warning ("Unable to know the number of mounted volumes");
858 : :
859 : : return NULL;
860 : : }
861 : :
862 : : vmount_info = (struct vmount*)g_malloc (vmount_size);
863 : :
864 : : vmount_number = mntctl (MCTL_QUERY, vmount_size, vmount_info);
865 : :
866 : : if (vmount_info->vmt_revision != VMT_REVISION)
867 : : g_warning ("Bad vmount structure revision number, want %d, got %d", VMT_REVISION, vmount_info->vmt_revision);
868 : :
869 : : if (vmount_number < 0)
870 : : {
871 : : g_warning ("Unable to recover mounted volumes information");
872 : :
873 : : g_free (vmount_info);
874 : : return NULL;
875 : : }
876 : :
877 : : return_list = NULL;
878 : : while (vmount_number > 0)
879 : : {
880 : : gboolean is_read_only = FALSE;
881 : :
882 : : fs_info = getvfsbytype (vmount_info->vmt_gfstype);
883 : :
884 : : /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
885 : : is_read_only = (vmount_info->vmt_flags & MNT_READONLY) ? 1 : 0;
886 : :
887 : : mount_entry = create_unix_mount_entry (vmt2dataptr (vmount_info, VMT_OBJECT),
888 : : vmt2dataptr (vmount_info, VMT_STUB),
889 : : NULL,
890 : : fs_info == NULL ? "unknown" : fs_info->vfsent_name,
891 : : NULL,
892 : : is_read_only);
893 : :
894 : : return_list = g_list_prepend (return_list, mount_entry);
895 : :
896 : : vmount_info = (struct vmount *)( (char*)vmount_info
897 : : + vmount_info->vmt_length);
898 : : vmount_number--;
899 : : }
900 : :
901 : : g_free (vmount_info);
902 : :
903 : : return g_list_reverse (return_list);
904 : : }
905 : :
906 : : static GUnixMountEntry **
907 : : _g_unix_mounts_get_from_file (const char *table_path,
908 : : uint64_t *time_read_out,
909 : : size_t *n_entries_out)
910 : : {
911 : : /* Not supported on mntctl() systems. */
912 : : if (time_read_out != NULL)
913 : : *time_read_out = 0;
914 : : if (n_entries_out != NULL)
915 : : *n_entries_out = 0;
916 : :
917 : : return NULL;
918 : : }
919 : :
920 : : /* sys/mount.h {{{2 */
921 : : #elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
922 : :
923 : : static const char *
924 : : get_mtab_monitor_file (void)
925 : : {
926 : : return NULL;
927 : : }
928 : :
929 : : static GList *
930 : : _g_get_unix_mounts (void)
931 : : {
932 : : #if defined(USE_STATVFS)
933 : : struct statvfs *mntent = NULL;
934 : : #elif defined(USE_STATFS)
935 : : struct statfs *mntent = NULL;
936 : : #else
937 : : #error statfs juggling failed
938 : : #endif
939 : : size_t bufsize;
940 : : int num_mounts, i;
941 : : GUnixMountEntry *mount_entry;
942 : : GList *return_list;
943 : :
944 : : /* Pass NOWAIT to avoid blocking trying to update NFS mounts. */
945 : : #if defined(USE_STATVFS) && defined(HAVE_GETVFSSTAT)
946 : : num_mounts = getvfsstat (NULL, 0, ST_NOWAIT);
947 : : #elif defined(USE_STATFS) && defined(HAVE_GETFSSTAT)
948 : : num_mounts = getfsstat (NULL, 0, MNT_NOWAIT);
949 : : #endif
950 : : if (num_mounts == -1)
951 : : return NULL;
952 : :
953 : : bufsize = num_mounts * sizeof (*mntent);
954 : : mntent = g_malloc (bufsize);
955 : : #if defined(USE_STATVFS) && defined(HAVE_GETVFSSTAT)
956 : : num_mounts = getvfsstat (mntent, bufsize, ST_NOWAIT);
957 : : #elif defined(USE_STATFS) && defined(HAVE_GETFSSTAT)
958 : : num_mounts = getfsstat (mntent, bufsize, MNT_NOWAIT);
959 : : #endif
960 : : if (num_mounts == -1)
961 : : return NULL;
962 : :
963 : : return_list = NULL;
964 : :
965 : : for (i = 0; i < num_mounts; i++)
966 : : {
967 : : gboolean is_read_only = FALSE;
968 : :
969 : : #if defined(USE_STATVFS)
970 : : if (mntent[i].f_flag & ST_RDONLY)
971 : : #elif defined(USE_STATFS)
972 : : if (mntent[i].f_flags & MNT_RDONLY)
973 : : #else
974 : : #error statfs juggling failed
975 : : #endif
976 : : is_read_only = TRUE;
977 : :
978 : : mount_entry = create_unix_mount_entry (mntent[i].f_mntfromname,
979 : : mntent[i].f_mntonname,
980 : : NULL,
981 : : mntent[i].f_fstypename,
982 : : NULL,
983 : : is_read_only);
984 : :
985 : : return_list = g_list_prepend (return_list, mount_entry);
986 : : }
987 : :
988 : : g_free (mntent);
989 : :
990 : : return g_list_reverse (return_list);
991 : : }
992 : :
993 : : static GUnixMountEntry **
994 : : _g_unix_mounts_get_from_file (const char *table_path,
995 : : uint64_t *time_read_out,
996 : : size_t *n_entries_out)
997 : : {
998 : : /* Not supported on getvfsstat()/getfsstat() systems. */
999 : : if (time_read_out != NULL)
1000 : : *time_read_out = 0;
1001 : : if (n_entries_out != NULL)
1002 : : *n_entries_out = 0;
1003 : :
1004 : : return NULL;
1005 : : }
1006 : :
1007 : : /* Interix {{{2 */
1008 : : #elif defined(__INTERIX)
1009 : :
1010 : : static const char *
1011 : : get_mtab_monitor_file (void)
1012 : : {
1013 : : return NULL;
1014 : : }
1015 : :
1016 : : static GList *
1017 : : _g_get_unix_mounts (void)
1018 : : {
1019 : : DIR *dirp;
1020 : : GList* return_list = NULL;
1021 : : char filename[9 + NAME_MAX];
1022 : :
1023 : : dirp = opendir ("/dev/fs");
1024 : : if (!dirp)
1025 : : {
1026 : : g_warning ("unable to read /dev/fs!");
1027 : : return NULL;
1028 : : }
1029 : :
1030 : : while (1)
1031 : : {
1032 : : struct statvfs statbuf;
1033 : : struct dirent entry;
1034 : : struct dirent* result;
1035 : :
1036 : : if (readdir_r (dirp, &entry, &result) || result == NULL)
1037 : : break;
1038 : :
1039 : : strcpy (filename, "/dev/fs/");
1040 : : strcat (filename, entry.d_name);
1041 : :
1042 : : if (statvfs (filename, &statbuf) == 0)
1043 : : {
1044 : : GUnixMountEntry* mount_entry = g_new0(GUnixMountEntry, 1);
1045 : :
1046 : : mount_entry->mount_path = g_strdup (statbuf.f_mntonname);
1047 : : mount_entry->device_path = g_strdup (statbuf.f_mntfromname);
1048 : : mount_entry->filesystem_type = g_strdup (statbuf.f_fstypename);
1049 : :
1050 : : if (statbuf.f_flag & ST_RDONLY)
1051 : : mount_entry->is_read_only = TRUE;
1052 : :
1053 : : return_list = g_list_prepend(return_list, mount_entry);
1054 : : }
1055 : : }
1056 : :
1057 : : return_list = g_list_reverse (return_list);
1058 : :
1059 : : closedir (dirp);
1060 : :
1061 : : return return_list;
1062 : : }
1063 : :
1064 : : static GUnixMountEntry **
1065 : : _g_unix_mounts_get_from_file (const char *table_path,
1066 : : uint64_t *time_read_out,
1067 : : size_t *n_entries_out)
1068 : : {
1069 : : /* Not supported on Interix systems. */
1070 : : if (time_read_out != NULL)
1071 : : *time_read_out = 0;
1072 : : if (n_entries_out != NULL)
1073 : : *n_entries_out = 0;
1074 : :
1075 : : return NULL;
1076 : : }
1077 : :
1078 : : /* QNX {{{2 */
1079 : : #elif defined (HAVE_QNX)
1080 : :
1081 : : static char *
1082 : : get_mtab_monitor_file (void)
1083 : : {
1084 : : /* TODO: Not implemented */
1085 : : return NULL;
1086 : : }
1087 : :
1088 : : static GUnixMountEntry **
1089 : : _g_unix_mounts_get_from_file (const char *table_path,
1090 : : uint64_t *time_read_out,
1091 : : size_t *n_entries_out)
1092 : : {
1093 : : /* Not implemented, as per _g_get_unix_mounts() below */
1094 : : if (time_read_out != NULL)
1095 : : *time_read_out = 0;
1096 : : if (n_entries_out != NULL)
1097 : : *n_entries_out = 0;
1098 : :
1099 : : return NULL;
1100 : : }
1101 : :
1102 : : static GList *
1103 : : _g_get_unix_mounts (void)
1104 : : {
1105 : : /* TODO: Not implemented */
1106 : : return NULL;
1107 : : }
1108 : :
1109 : : /* Common code {{{2 */
1110 : : #else
1111 : : #error No _g_get_unix_mounts() implementation for system
1112 : : #endif
1113 : :
1114 : : /* GUnixMountPoints (ie: fstab) implementations {{{1 */
1115 : :
1116 : : /* _g_get_unix_mount_points():
1117 : : * read the fstab.
1118 : : * don't return swap and ignore mounts.
1119 : : */
1120 : :
1121 : : static char *
1122 : 4 : get_fstab_file (void)
1123 : : {
1124 : : #ifdef HAVE_LIBMOUNT
1125 : 4 : return (char *) mnt_get_fstab_path ();
1126 : : #else
1127 : : #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
1128 : : /* AIX */
1129 : : return "/etc/filesystems";
1130 : : #elif defined(_PATH_MNTTAB)
1131 : : return _PATH_MNTTAB;
1132 : : #elif defined(VFSTAB)
1133 : : return VFSTAB;
1134 : : #else
1135 : : return "/etc/fstab";
1136 : : #endif
1137 : : #endif
1138 : : }
1139 : :
1140 : : /* mntent.h (Linux, GNU, NSS) {{{2 */
1141 : : #ifdef HAVE_MNTENT_H
1142 : :
1143 : : #ifdef HAVE_LIBMOUNT
1144 : :
1145 : : static GUnixMountPoint **
1146 : 2 : _g_unix_mount_points_get_from_file (const char *table_path,
1147 : : uint64_t *time_read_out,
1148 : : size_t *n_points_out)
1149 : : {
1150 : 2 : struct libmnt_table *table = NULL;
1151 : 2 : struct libmnt_iter* iter = NULL;
1152 : 2 : struct libmnt_fs *fs = NULL;
1153 : 2 : GUnixMountPoint *mount_point = NULL;
1154 : 2 : GPtrArray *return_array = NULL;
1155 : :
1156 : 2 : if (time_read_out != NULL)
1157 : 1 : *time_read_out = get_mount_points_timestamp ();
1158 : :
1159 : 2 : return_array = g_ptr_array_new_null_terminated (0, (GDestroyNotify) g_unix_mount_point_free, TRUE);
1160 : 2 : table = mnt_new_table ();
1161 : 2 : if (mnt_table_parse_fstab (table, table_path) < 0)
1162 : 0 : goto out;
1163 : :
1164 : 2 : iter = mnt_new_iter (MNT_ITER_FORWARD);
1165 : 8 : while (mnt_table_next_fs (table, iter, &fs) == 0)
1166 : : {
1167 : 6 : const char *device_path = NULL;
1168 : 6 : const char *mount_path = NULL;
1169 : 6 : const char *mount_fstype = NULL;
1170 : 6 : char *mount_options = NULL;
1171 : 6 : gboolean is_read_only = FALSE;
1172 : 6 : gboolean is_user_mountable = FALSE;
1173 : 6 : gboolean is_loopback = FALSE;
1174 : :
1175 : 6 : mount_path = mnt_fs_get_target (fs);
1176 : 6 : if ((strcmp (mount_path, "ignore") == 0) ||
1177 : 6 : (strcmp (mount_path, "swap") == 0) ||
1178 : 6 : (strcmp (mount_path, "none") == 0))
1179 : 2 : continue;
1180 : :
1181 : 4 : mount_fstype = mnt_fs_get_fstype (fs);
1182 : 4 : mount_options = mnt_fs_strdup_options (fs);
1183 : 4 : if (mount_options)
1184 : : {
1185 : 4 : unsigned long mount_flags = 0;
1186 : 4 : unsigned long userspace_flags = 0;
1187 : :
1188 : 4 : mnt_optstr_get_flags (mount_options, &mount_flags, mnt_get_builtin_optmap (MNT_LINUX_MAP));
1189 : 4 : mnt_optstr_get_flags (mount_options, &userspace_flags, mnt_get_builtin_optmap (MNT_USERSPACE_MAP));
1190 : :
1191 : : /* We ignore bind fstab entries, as we ignore bind mounts anyway */
1192 : 4 : if (mount_flags & MS_BIND)
1193 : : {
1194 : 0 : g_free (mount_options);
1195 : 0 : continue;
1196 : : }
1197 : :
1198 : 4 : is_read_only = (mount_flags & MS_RDONLY) != 0;
1199 : 4 : is_loopback = (userspace_flags & MNT_MS_LOOP) != 0;
1200 : :
1201 : 4 : if ((mount_fstype != NULL && g_strcmp0 ("supermount", mount_fstype) == 0) ||
1202 : 4 : ((userspace_flags & MNT_MS_USER) &&
1203 : 0 : (g_strstr_len (mount_options, -1, "user_xattr") == NULL)) ||
1204 : 4 : (userspace_flags & MNT_MS_USERS) ||
1205 : 4 : (userspace_flags & MNT_MS_OWNER))
1206 : : {
1207 : 0 : is_user_mountable = TRUE;
1208 : : }
1209 : : }
1210 : :
1211 : 4 : device_path = mnt_fs_get_source (fs);
1212 : 4 : if (g_strcmp0 (device_path, "/dev/root") == 0)
1213 : 0 : device_path = _resolve_dev_root ();
1214 : :
1215 : 4 : mount_point = create_unix_mount_point (device_path,
1216 : : mount_path,
1217 : : mount_fstype,
1218 : : mount_options,
1219 : : is_read_only,
1220 : : is_user_mountable,
1221 : : is_loopback);
1222 : 4 : if (mount_options)
1223 : 4 : g_free (mount_options);
1224 : :
1225 : 4 : g_ptr_array_add (return_array, g_steal_pointer (&mount_point));
1226 : : }
1227 : 2 : mnt_free_iter (iter);
1228 : :
1229 : 2 : out:
1230 : 2 : mnt_free_table (table);
1231 : :
1232 : 2 : if (n_points_out != NULL)
1233 : 2 : *n_points_out = return_array->len;
1234 : :
1235 : 2 : return (GUnixMountPoint **) g_ptr_array_free (g_steal_pointer (&return_array), FALSE);
1236 : : }
1237 : :
1238 : : static GList *
1239 : 1 : _g_get_unix_mount_points (void)
1240 : : {
1241 : 1 : GUnixMountPoint **points = NULL;
1242 : 1 : size_t n_points = 0;
1243 : :
1244 : 1 : points = _g_unix_mount_points_get_from_file (NULL /* default libmount filename */,
1245 : : NULL, &n_points);
1246 : :
1247 : 1 : return unix_mount_point_array_free_to_list (g_steal_pointer (&points), n_points);
1248 : : }
1249 : :
1250 : : #else
1251 : :
1252 : : static GUnixMountPoint **
1253 : : _g_unix_mount_points_get_from_file (const char *table_path,
1254 : : uint64_t *time_read_out,
1255 : : size_t *n_points_out)
1256 : : {
1257 : : #ifdef HAVE_GETMNTENT_R
1258 : : struct mntent ent;
1259 : : char buf[1024];
1260 : : #endif
1261 : : struct mntent *mntent;
1262 : : FILE *file;
1263 : : GUnixMountPoint *mount_point;
1264 : : GPtrArray *return_array = NULL;
1265 : :
1266 : : if (time_read_out != NULL)
1267 : : *time_read_out = get_mount_points_timestamp ();
1268 : :
1269 : : file = setmntent (table_path, "re");
1270 : : if (file == NULL)
1271 : : {
1272 : : if (n_points_out != NULL)
1273 : : *n_points_out = 0;
1274 : : return NULL;
1275 : : }
1276 : :
1277 : : return_array = g_ptr_array_new_null_terminated (0, (GDestroyNotify) g_unix_mount_point_free, TRUE);
1278 : :
1279 : : #ifdef HAVE_GETMNTENT_R
1280 : : while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
1281 : : #else
1282 : : G_LOCK (getmntent);
1283 : : while ((mntent = getmntent (file)) != NULL)
1284 : : #endif
1285 : : {
1286 : : const char *device_path = NULL;
1287 : : gboolean is_read_only = FALSE;
1288 : : gboolean is_user_mountable = FALSE;
1289 : : gboolean is_loopback = FALSE;
1290 : :
1291 : : if ((strcmp (mntent->mnt_dir, "ignore") == 0) ||
1292 : : (strcmp (mntent->mnt_dir, "swap") == 0) ||
1293 : : (strcmp (mntent->mnt_dir, "none") == 0))
1294 : : continue;
1295 : :
1296 : : #ifdef HAVE_HASMNTOPT
1297 : : /* We ignore bind fstab entries, as we ignore bind mounts anyway */
1298 : : if (hasmntopt (mntent, "bind"))
1299 : : continue;
1300 : : #endif
1301 : :
1302 : : if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
1303 : : device_path = _resolve_dev_root ();
1304 : : else
1305 : : device_path = mntent->mnt_fsname;
1306 : :
1307 : : #ifdef HAVE_HASMNTOPT
1308 : : if (hasmntopt (mntent, MNTOPT_RO) != NULL)
1309 : : is_read_only = TRUE;
1310 : :
1311 : : if (hasmntopt (mntent, "loop") != NULL)
1312 : : is_loopback = TRUE;
1313 : :
1314 : : #endif
1315 : :
1316 : : if ((mntent->mnt_type != NULL && strcmp ("supermount", mntent->mnt_type) == 0)
1317 : : #ifdef HAVE_HASMNTOPT
1318 : : || (hasmntopt (mntent, "user") != NULL
1319 : : && hasmntopt (mntent, "user") != hasmntopt (mntent, "user_xattr"))
1320 : : || hasmntopt (mntent, "users") != NULL
1321 : : || hasmntopt (mntent, "owner") != NULL
1322 : : #endif
1323 : : )
1324 : : is_user_mountable = TRUE;
1325 : :
1326 : : mount_point = create_unix_mount_point (device_path,
1327 : : mntent->mnt_dir,
1328 : : mntent->mnt_type,
1329 : : mntent->mnt_opts,
1330 : : is_read_only,
1331 : : is_user_mountable,
1332 : : is_loopback);
1333 : :
1334 : : g_ptr_array_add (return_array, g_steal_pointer (&mount_point));
1335 : : }
1336 : :
1337 : : endmntent (file);
1338 : :
1339 : : #ifndef HAVE_GETMNTENT_R
1340 : : G_UNLOCK (getmntent);
1341 : : #endif
1342 : :
1343 : : if (n_points_out != NULL)
1344 : : *n_points_out = return_array->len;
1345 : :
1346 : : return (GUnixMountPoint **) g_ptr_array_free (g_steal_pointer (&return_array), FALSE);
1347 : : }
1348 : :
1349 : : static GList *
1350 : : _g_get_unix_mount_points (void)
1351 : : {
1352 : : GUnixMountPoint **points = NULL;
1353 : : size_t n_points = 0;
1354 : :
1355 : : points = _g_unix_mount_points_get_from_file (get_fstab_file (),
1356 : : NULL, &n_points);
1357 : :
1358 : : return unix_mount_point_array_free_to_list (g_steal_pointer (&points), n_points);
1359 : : }
1360 : :
1361 : : #endif /* HAVE_LIBMOUNT */
1362 : :
1363 : : /* mnttab.h {{{2 */
1364 : : #elif defined (HAVE_SYS_MNTTAB_H)
1365 : :
1366 : : static GUnixMountPoint **
1367 : : _g_unix_mount_points_get_from_file (const char *table_path,
1368 : : uint64_t *time_read_out,
1369 : : size_t *n_points_out)
1370 : : {
1371 : : struct mnttab mntent;
1372 : : FILE *file;
1373 : : GUnixMountPoint *mount_point;
1374 : : GPtrArray *return_array = NULL;
1375 : :
1376 : : if (time_read_out != NULL)
1377 : : *time_read_out = get_mount_points_timestamp ();
1378 : :
1379 : : file = setmntent (table_path, "re");
1380 : : if (file == NULL)
1381 : : {
1382 : : if (n_points_out != NULL)
1383 : : *n_points_out = 0;
1384 : : return NULL;
1385 : : }
1386 : :
1387 : : return_array = g_ptr_array_new_null_terminated (0, (GDestroyNotify) g_unix_mount_point_free, TRUE);
1388 : :
1389 : : G_LOCK (getmntent);
1390 : : while (! getmntent (file, &mntent))
1391 : : {
1392 : : gboolean is_read_only = FALSE;
1393 : : gboolean is_user_mountable = FALSE;
1394 : : gboolean is_loopback = FALSE;
1395 : :
1396 : : if ((strcmp (mntent.mnt_mountp, "ignore") == 0) ||
1397 : : (strcmp (mntent.mnt_mountp, "swap") == 0) ||
1398 : : (strcmp (mntent.mnt_mountp, "none") == 0))
1399 : : continue;
1400 : :
1401 : : #ifdef HAVE_HASMNTOPT
1402 : : if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
1403 : : is_read_only = TRUE;
1404 : :
1405 : : if (hasmntopt (&mntent, "lofs") != NULL)
1406 : : is_loopback = TRUE;
1407 : : #endif
1408 : :
1409 : : if ((mntent.mnt_fstype != NULL)
1410 : : #ifdef HAVE_HASMNTOPT
1411 : : || (hasmntopt (&mntent, "user") != NULL
1412 : : && hasmntopt (&mntent, "user") != hasmntopt (&mntent, "user_xattr"))
1413 : : || hasmntopt (&mntent, "users") != NULL
1414 : : || hasmntopt (&mntent, "owner") != NULL
1415 : : #endif
1416 : : )
1417 : : is_user_mountable = TRUE;
1418 : :
1419 : : mount_point = create_unix_mount_point (mntent.mnt_special,
1420 : : mntent.mnt_mountp,
1421 : : mntent.mnt_fstype,
1422 : : mntent.mnt_mntopts,
1423 : : is_read_only,
1424 : : is_user_mountable,
1425 : : is_loopback);
1426 : :
1427 : : g_ptr_array_add (return_array, g_steal_pointer (&mount_point));
1428 : : }
1429 : :
1430 : : endmntent (file);
1431 : : G_UNLOCK (getmntent);
1432 : :
1433 : : if (n_points_out != NULL)
1434 : : *n_points_out = return_array->len;
1435 : :
1436 : : return (GUnixMountPoint **) g_ptr_array_free (g_steal_pointer (&return_array), FALSE);
1437 : : }
1438 : :
1439 : : static GList *
1440 : : _g_get_unix_mount_points (void)
1441 : : {
1442 : : GUnixMountPoint **points = NULL;
1443 : : size_t n_points = 0;
1444 : :
1445 : : points = _g_unix_mount_points_get_from_file (get_fstab_file (),
1446 : : NULL, &n_points);
1447 : :
1448 : : return unix_mount_point_array_free_to_list (g_steal_pointer (&points), n_points);
1449 : : }
1450 : :
1451 : : /* mntctl.h (AIX) {{{2 */
1452 : : #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
1453 : :
1454 : : /* functions to parse /etc/filesystems on aix */
1455 : :
1456 : : /* read character, ignoring comments (begin with '*', end with '\n' */
1457 : : static int
1458 : : aix_fs_getc (FILE *fd)
1459 : : {
1460 : : int c;
1461 : :
1462 : : while ((c = getc (fd)) == '*')
1463 : : {
1464 : : while (((c = getc (fd)) != '\n') && (c != EOF))
1465 : : ;
1466 : : }
1467 : : }
1468 : :
1469 : : /* eat all continuous spaces in a file */
1470 : : static int
1471 : : aix_fs_ignorespace (FILE *fd)
1472 : : {
1473 : : int c;
1474 : :
1475 : : while ((c = aix_fs_getc (fd)) != EOF)
1476 : : {
1477 : : if (!g_ascii_isspace (c))
1478 : : {
1479 : : ungetc (c,fd);
1480 : : return c;
1481 : : }
1482 : : }
1483 : :
1484 : : return EOF;
1485 : : }
1486 : :
1487 : : /* read one word from file */
1488 : : static int
1489 : : aix_fs_getword (FILE *fd,
1490 : : char *word)
1491 : : {
1492 : : int c;
1493 : :
1494 : : aix_fs_ignorespace (fd);
1495 : :
1496 : : while (((c = aix_fs_getc (fd)) != EOF) && !g_ascii_isspace (c))
1497 : : {
1498 : : if (c == '"')
1499 : : {
1500 : : while (((c = aix_fs_getc (fd)) != EOF) && (c != '"'))
1501 : : *word++ = c;
1502 : : else
1503 : : *word++ = c;
1504 : : }
1505 : : }
1506 : : *word = 0;
1507 : :
1508 : : return c;
1509 : : }
1510 : :
1511 : : typedef struct {
1512 : : char mnt_mount[PATH_MAX];
1513 : : char mnt_special[PATH_MAX];
1514 : : char mnt_fstype[16];
1515 : : char mnt_options[128];
1516 : : } AixMountTableEntry;
1517 : :
1518 : : /* read mount points properties */
1519 : : static int
1520 : : aix_fs_get (FILE *fd,
1521 : : AixMountTableEntry *prop)
1522 : : {
1523 : : static char word[PATH_MAX] = { 0 };
1524 : : char value[PATH_MAX];
1525 : :
1526 : : /* read stanza */
1527 : : if (word[0] == 0)
1528 : : {
1529 : : if (aix_fs_getword (fd, word) == EOF)
1530 : : return EOF;
1531 : : }
1532 : :
1533 : : word[strlen(word) - 1] = 0;
1534 : : g_strlcpy (prop->mnt_mount, word, sizeof (prop->mnt_mount));
1535 : :
1536 : : /* read attributes and value */
1537 : :
1538 : : while (aix_fs_getword (fd, word) != EOF)
1539 : : {
1540 : : /* test if is attribute or new stanza */
1541 : : if (word[strlen(word) - 1] == ':')
1542 : : return 0;
1543 : :
1544 : : /* read "=" */
1545 : : aix_fs_getword (fd, value);
1546 : :
1547 : : /* read value */
1548 : : aix_fs_getword (fd, value);
1549 : :
1550 : : if (strcmp (word, "dev") == 0)
1551 : : g_strlcpy (prop->mnt_special, value, sizeof (prop->mnt_special));
1552 : : else if (strcmp (word, "vfs") == 0)
1553 : : g_strlcpy (prop->mnt_fstype, value, sizeof (prop->mnt_fstype));
1554 : : else if (strcmp (word, "options") == 0)
1555 : : g_strlcpy (prop->mnt_options, value, sizeof (prop->mnt_options));
1556 : : }
1557 : :
1558 : : return 0;
1559 : : }
1560 : :
1561 : : static GUnixMountPoint **
1562 : : _g_unix_mount_points_get_from_file (const char *table_path,
1563 : : uint64_t *time_read_out,
1564 : : size_t *n_points_out)
1565 : : {
1566 : : struct mntent *mntent;
1567 : : FILE *file;
1568 : : GUnixMountPoint *mount_point;
1569 : : AixMountTableEntry mntent;
1570 : : GPtrArray *return_array = NULL;
1571 : :
1572 : : if (time_read_out != NULL)
1573 : : *time_read_out = get_mount_points_timestamp ();
1574 : :
1575 : : file = setmntent (table_path, "re");
1576 : : if (file == NULL)
1577 : : {
1578 : : if (n_points_out != NULL)
1579 : : *n_points_out = 0;
1580 : : return NULL;
1581 : : }
1582 : :
1583 : : return_array = g_ptr_array_new_null_terminated (0, (GDestroyNotify) g_unix_mount_point_free, TRUE);
1584 : :
1585 : : while (!aix_fs_get (file, &mntent))
1586 : : {
1587 : : if (strcmp ("cdrfs", mntent.mnt_fstype) == 0)
1588 : : {
1589 : : mount_point = create_unix_mount_point (mntent.mnt_special,
1590 : : mntent.mnt_mount,
1591 : : mntent.mnt_fstype,
1592 : : mntent.mnt_options,
1593 : : TRUE,
1594 : : TRUE,
1595 : : FALSE);
1596 : :
1597 : : g_ptr_array_add (return_array, g_steal_pointer (&mount_point));
1598 : : }
1599 : : }
1600 : :
1601 : : endmntent (file);
1602 : :
1603 : : if (n_points_out != NULL)
1604 : : *n_points_out = return_array->len;
1605 : :
1606 : : return (GUnixMountPoint **) g_ptr_array_free (g_steal_pointer (&return_array), FALSE);
1607 : : }
1608 : :
1609 : : static GList *
1610 : : _g_get_unix_mount_points (void)
1611 : : {
1612 : : GUnixMountPoint **points = NULL;
1613 : : size_t n_points = 0;
1614 : :
1615 : : points = _g_unix_mount_points_get_from_file (get_fstab_file (),
1616 : : NULL, &n_points);
1617 : :
1618 : : return unix_mount_point_array_free_to_list (g_steal_pointer (&points), n_points);
1619 : : }
1620 : :
1621 : : #elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT) || defined(HAVE_GETFSENT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
1622 : :
1623 : : static GList *
1624 : : _g_get_unix_mount_points (void)
1625 : : {
1626 : : struct fstab *fstab = NULL;
1627 : : GUnixMountPoint *mount_point;
1628 : : GList *return_list = NULL;
1629 : : G_LOCK_DEFINE_STATIC (fsent);
1630 : : #ifdef HAVE_SYS_SYSCTL_H
1631 : : uid_t uid = getuid ();
1632 : : int usermnt = 0;
1633 : : struct stat sb;
1634 : : #endif
1635 : :
1636 : : #ifdef HAVE_SYS_SYSCTL_H
1637 : : #if defined(HAVE_SYSCTLBYNAME)
1638 : : {
1639 : : size_t len = sizeof(usermnt);
1640 : :
1641 : : sysctlbyname ("vfs.usermount", &usermnt, &len, NULL, 0);
1642 : : }
1643 : : #elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
1644 : : {
1645 : : int mib[2];
1646 : : size_t len = sizeof(usermnt);
1647 : :
1648 : : mib[0] = CTL_VFS;
1649 : : mib[1] = VFS_USERMOUNT;
1650 : : sysctl (mib, 2, &usermnt, &len, NULL, 0);
1651 : : }
1652 : : #elif defined(CTL_KERN) && defined(KERN_USERMOUNT)
1653 : : {
1654 : : int mib[2];
1655 : : size_t len = sizeof(usermnt);
1656 : :
1657 : : mib[0] = CTL_KERN;
1658 : : mib[1] = KERN_USERMOUNT;
1659 : : sysctl (mib, 2, &usermnt, &len, NULL, 0);
1660 : : }
1661 : : #endif
1662 : : #endif
1663 : :
1664 : : G_LOCK (fsent);
1665 : : if (!setfsent ())
1666 : : {
1667 : : G_UNLOCK (fsent);
1668 : : return NULL;
1669 : : }
1670 : :
1671 : : while ((fstab = getfsent ()) != NULL)
1672 : : {
1673 : : gboolean is_read_only = FALSE;
1674 : : gboolean is_user_mountable = FALSE;
1675 : :
1676 : : if (strcmp (fstab->fs_vfstype, "swap") == 0)
1677 : : continue;
1678 : :
1679 : : if (strcmp (fstab->fs_type, "ro") == 0)
1680 : : is_read_only = TRUE;
1681 : :
1682 : : #ifdef HAVE_SYS_SYSCTL_H
1683 : : if (usermnt != 0)
1684 : : {
1685 : : if (uid == 0 ||
1686 : : (stat (fstab->fs_file, &sb) == 0 && sb.st_uid == uid))
1687 : : {
1688 : : is_user_mountable = TRUE;
1689 : : }
1690 : : }
1691 : : #endif
1692 : :
1693 : : mount_point = create_unix_mount_point (fstab->fs_spec,
1694 : : fstab->fs_file,
1695 : : fstab->fs_vfstype,
1696 : : fstab->fs_mntops,
1697 : : is_read_only,
1698 : : is_user_mountable,
1699 : : FALSE);
1700 : :
1701 : : return_list = g_list_prepend (return_list, mount_point);
1702 : : }
1703 : :
1704 : : endfsent ();
1705 : : G_UNLOCK (fsent);
1706 : :
1707 : : return g_list_reverse (return_list);
1708 : : }
1709 : :
1710 : : static GUnixMountPoint **
1711 : : _g_unix_mount_points_get_from_file (const char *table_path,
1712 : : uint64_t *time_read_out,
1713 : : size_t *n_points_out)
1714 : : {
1715 : : /* Not supported on getfsent() systems. */
1716 : : if (time_read_out != NULL)
1717 : : *time_read_out = 0;
1718 : : if (n_points_out != NULL)
1719 : : *n_points_out = 0;
1720 : : return NULL;
1721 : : }
1722 : :
1723 : : /* Common code {{{2 */
1724 : : #else
1725 : : #error No g_get_mount_table() implementation for system
1726 : : #endif
1727 : :
1728 : : static guint64
1729 : 1 : get_mounts_timestamp (void)
1730 : : {
1731 : : const char *monitor_file;
1732 : : struct stat buf;
1733 : 1 : guint64 timestamp = 0;
1734 : :
1735 : 1 : G_LOCK (proc_mounts_source);
1736 : :
1737 : 1 : monitor_file = get_mtab_monitor_file ();
1738 : : /* Don't return mtime for /proc/ files */
1739 : 1 : if (monitor_file && !g_str_has_prefix (monitor_file, "/proc/"))
1740 : : {
1741 : 0 : if (stat (monitor_file, &buf) == 0)
1742 : 0 : timestamp = buf.st_mtime;
1743 : : }
1744 : 1 : else if (proc_mounts_watch_is_running ())
1745 : : {
1746 : : /* it's being monitored by poll, so return mount_poller_time */
1747 : 0 : timestamp = mount_poller_time;
1748 : : }
1749 : : else
1750 : : {
1751 : : /* Case of /proc/ file not being monitored - Be on the safe side and
1752 : : * send a new timestamp to force g_unix_mount_entries_changed_since() to
1753 : : * return TRUE so any application caches depending on it (like eg.
1754 : : * the one in GIO) get invalidated and don't hold possibly outdated
1755 : : * data - see Bug 787731 */
1756 : 1 : timestamp = g_get_monotonic_time ();
1757 : : }
1758 : :
1759 : 1 : G_UNLOCK (proc_mounts_source);
1760 : :
1761 : 1 : return timestamp;
1762 : : }
1763 : :
1764 : : static guint64
1765 : 2 : get_mount_points_timestamp (void)
1766 : : {
1767 : : const char *monitor_file;
1768 : : struct stat buf;
1769 : :
1770 : 2 : monitor_file = get_fstab_file ();
1771 : 2 : if (monitor_file)
1772 : : {
1773 : 2 : if (stat (monitor_file, &buf) == 0)
1774 : 2 : return (guint64)buf.st_mtime;
1775 : : }
1776 : 0 : return 0;
1777 : : }
1778 : :
1779 : : /**
1780 : : * g_unix_mounts_get:
1781 : : * @time_read: (out) (optional): return location for a timestamp
1782 : : *
1783 : : * Gets a list of [struct@GioUnix.MountEntry] instances representing the Unix
1784 : : * mounts.
1785 : : *
1786 : : * If @time_read is set, it will be filled with the mount timestamp, allowing
1787 : : * for checking if the mounts have changed with
1788 : : * [func@GioUnix.mount_entries_changed_since].
1789 : : *
1790 : : * Returns: (element-type GUnixMountEntry) (transfer full): a list of the
1791 : : * Unix mounts
1792 : : * Deprecated: 2.84: Use [func@GioUnix.mount_entries_get] instead.
1793 : : */
1794 : : GList *
1795 : 0 : g_unix_mounts_get (guint64 *time_read)
1796 : : {
1797 : 0 : return g_unix_mount_entries_get (time_read);
1798 : : }
1799 : :
1800 : : /**
1801 : : * g_unix_mount_entries_get:
1802 : : * @time_read: (out) (optional): return location for a timestamp
1803 : : *
1804 : : * Gets a list of [struct@GioUnix.MountEntry] instances representing the Unix
1805 : : * mounts.
1806 : : *
1807 : : * If @time_read is set, it will be filled with the mount timestamp, allowing
1808 : : * for checking if the mounts have changed with
1809 : : * [func@GioUnix.mount_entries_changed_since].
1810 : : *
1811 : : * Returns: (element-type GUnixMountEntry) (transfer full): a list of the
1812 : : * Unix mounts
1813 : : * Since: 2.84
1814 : : */
1815 : : GList *
1816 : 8 : g_unix_mount_entries_get (guint64 *time_read)
1817 : : {
1818 : 8 : if (time_read)
1819 : 0 : *time_read = get_mounts_timestamp ();
1820 : :
1821 : 8 : return _g_get_unix_mounts ();
1822 : : }
1823 : :
1824 : : /**
1825 : : * g_unix_mounts_get_from_file:
1826 : : * @table_path: path to the mounts table file (for example `/proc/self/mountinfo`)
1827 : : * @time_read_out: (optional) (out caller-allocates): return location for the
1828 : : * modification time of @table_path
1829 : : * @n_entries_out: (optional) (out caller-allocates): return location for the
1830 : : * number of mount entries returned
1831 : : *
1832 : : * Gets an array of [struct@GioUnix.MountEntry]s containing the Unix mounts
1833 : : * listed in @table_path.
1834 : : *
1835 : : * This is a generalized version of [func@GioUnix.mount_entries_get], mainly
1836 : : * intended for internal testing use. Note that [func@GioUnix.mount_entries_get]
1837 : : * may parse multiple hierarchical table files, so this function is not a direct
1838 : : * superset of its functionality.
1839 : : *
1840 : : * If there is an error reading or parsing the file, `NULL` will be returned
1841 : : * and both out parameters will be set to `0`.
1842 : : *
1843 : : * Returns: (transfer full) (array length=n_entries_out) (nullable): mount
1844 : : * entries, or `NULL` if there was an error loading them
1845 : : * Since: 2.82
1846 : : * Deprecated: 2.84: Use [func@GioUnix.mount_entries_get_from_file] instead.
1847 : : */
1848 : : GUnixMountEntry **
1849 : 1 : g_unix_mounts_get_from_file (const char *table_path,
1850 : : uint64_t *time_read_out,
1851 : : size_t *n_entries_out)
1852 : : {
1853 : 1 : return g_unix_mount_entries_get_from_file (table_path, time_read_out, n_entries_out);
1854 : : }
1855 : :
1856 : : /**
1857 : : * g_unix_mount_entries_get_from_file:
1858 : : * @table_path: path to the mounts table file (for example `/proc/self/mountinfo`)
1859 : : * @time_read_out: (optional) (out caller-allocates): return location for the
1860 : : * modification time of @table_path
1861 : : * @n_entries_out: (optional) (out caller-allocates): return location for the
1862 : : * number of mount entries returned
1863 : : *
1864 : : * Gets an array of [struct@GioUnix.MountEntry]s containing the Unix mounts
1865 : : * listed in @table_path.
1866 : : *
1867 : : * This is a generalized version of [func@GioUnix.mount_entries_get], mainly
1868 : : * intended for internal testing use. Note that [func@GioUnix.mount_entries_get]
1869 : : * may parse multiple hierarchical table files, so this function is not a direct
1870 : : * superset of its functionality.
1871 : : *
1872 : : * If there is an error reading or parsing the file, `NULL` will be returned
1873 : : * and both out parameters will be set to `0`.
1874 : : *
1875 : : * Returns: (transfer full) (array length=n_entries_out) (nullable): mount
1876 : : * entries, or `NULL` if there was an error loading them
1877 : : * Since: 2.84
1878 : : */
1879 : : GUnixMountEntry **
1880 : 1 : g_unix_mount_entries_get_from_file (const char *table_path,
1881 : : uint64_t *time_read_out,
1882 : : size_t *n_entries_out)
1883 : : {
1884 : 1 : return _g_unix_mounts_get_from_file (table_path, time_read_out, n_entries_out);
1885 : : }
1886 : :
1887 : : /**
1888 : : * g_unix_mount_at:
1889 : : * @mount_path: (type filename): path for a possible Unix mount
1890 : : * @time_read: (out) (optional): return location for a timestamp
1891 : : *
1892 : : * Gets a [struct@GioUnix.MountEntry] for a given mount path.
1893 : : *
1894 : : * If @time_read is set, it will be filled with a Unix timestamp for checking
1895 : : * if the mounts have changed since with
1896 : : * [func@GioUnix.mount_entries_changed_since].
1897 : : *
1898 : : * If more mounts have the same mount path, the last matching mount
1899 : : * is returned.
1900 : : *
1901 : : * This will return `NULL` if there is no mount point at @mount_path.
1902 : : *
1903 : : * Returns: (transfer full) (nullable): a [struct@GioUnix.MountEntry]
1904 : : * Deprecated: 2.84: Use [func@GioUnix.MountEntry.at] instead.
1905 : : **/
1906 : : GUnixMountEntry *
1907 : 0 : g_unix_mount_at (const char *mount_path,
1908 : : guint64 *time_read)
1909 : : {
1910 : 0 : return g_unix_mount_entry_at (mount_path, time_read);
1911 : : }
1912 : :
1913 : : /**
1914 : : * g_unix_mount_entry_at:
1915 : : * @mount_path: (type filename): path for a possible Unix mount
1916 : : * @time_read: (out) (optional): return location for a timestamp
1917 : : *
1918 : : * Gets a [struct@GioUnix.MountEntry] for a given mount path.
1919 : : *
1920 : : * If @time_read is set, it will be filled with a Unix timestamp for checking
1921 : : * if the mounts have changed since with
1922 : : * [func@GioUnix.mount_entries_changed_since].
1923 : : *
1924 : : * If more mounts have the same mount path, the last matching mount
1925 : : * is returned.
1926 : : *
1927 : : * This will return `NULL` if there is no mount point at @mount_path.
1928 : : *
1929 : : * Returns: (transfer full) (nullable): a [struct@GioUnix.MountEntry]
1930 : : * Since: 2.84
1931 : : **/
1932 : : GUnixMountEntry *
1933 : 7 : g_unix_mount_entry_at (const char *mount_path,
1934 : : guint64 *time_read)
1935 : : {
1936 : : GList *mounts, *l;
1937 : : GUnixMountEntry *mount_entry, *found;
1938 : :
1939 : 7 : mounts = g_unix_mount_entries_get (time_read);
1940 : :
1941 : 7 : found = NULL;
1942 : 266 : for (l = mounts; l != NULL; l = l->next)
1943 : : {
1944 : 259 : mount_entry = l->data;
1945 : :
1946 : 259 : if (strcmp (mount_path, mount_entry->mount_path) == 0)
1947 : : {
1948 : 4 : if (found != NULL)
1949 : 0 : g_unix_mount_entry_free (found);
1950 : :
1951 : 4 : found = mount_entry;
1952 : : }
1953 : : else
1954 : 255 : g_unix_mount_entry_free (mount_entry);
1955 : : }
1956 : 7 : g_list_free (mounts);
1957 : :
1958 : 7 : return found;
1959 : : }
1960 : :
1961 : : /**
1962 : : * g_unix_mount_for:
1963 : : * @file_path: (type filename): file path on some Unix mount
1964 : : * @time_read: (out) (optional): return location for a timestamp
1965 : : *
1966 : : * Gets a [struct@GioUnix.MountEntry] for a given file path.
1967 : : *
1968 : : * If @time_read is set, it will be filled with a Unix timestamp for checking
1969 : : * if the mounts have changed since with
1970 : : * [func@GioUnix.mount_entries_changed_since].
1971 : : *
1972 : : * If more mounts have the same mount path, the last matching mount
1973 : : * is returned.
1974 : : *
1975 : : * This will return `NULL` if looking up the mount entry fails, if
1976 : : * @file_path doesn’t exist or there is an I/O error.
1977 : : *
1978 : : * Returns: (transfer full) (nullable): a [struct@GioUnix.MountEntry]
1979 : : * Since: 2.52
1980 : : * Deprecated: 2.84: Use [func@GioUnix.MountEntry.for] instead.
1981 : : **/
1982 : : GUnixMountEntry *
1983 : 0 : g_unix_mount_for (const char *file_path,
1984 : : guint64 *time_read)
1985 : : {
1986 : 0 : return g_unix_mount_entry_for (file_path, time_read);
1987 : : }
1988 : :
1989 : : /**
1990 : : * g_unix_mount_entry_for:
1991 : : * @file_path: (type filename): file path on some Unix mount
1992 : : * @time_read: (out) (optional): return location for a timestamp
1993 : : *
1994 : : * Gets a [struct@GioUnix.MountEntry] for a given file path.
1995 : : *
1996 : : * If @time_read is set, it will be filled with a Unix timestamp for checking
1997 : : * if the mounts have changed since with
1998 : : * [func@GioUnix.mount_entries_changed_since].
1999 : : *
2000 : : * If more mounts have the same mount path, the last matching mount
2001 : : * is returned.
2002 : : *
2003 : : * This will return `NULL` if looking up the mount entry fails, if
2004 : : * @file_path doesn’t exist or there is an I/O error.
2005 : : *
2006 : : * Returns: (transfer full) (nullable): a [struct@GioUnix.MountEntry]
2007 : : * Since: 2.84
2008 : : **/
2009 : : GUnixMountEntry *
2010 : 2 : g_unix_mount_entry_for (const char *file_path,
2011 : : guint64 *time_read)
2012 : : {
2013 : : GUnixMountEntry *entry;
2014 : :
2015 : 2 : g_return_val_if_fail (file_path != NULL, NULL);
2016 : :
2017 : 2 : entry = g_unix_mount_entry_at (file_path, time_read);
2018 : 2 : if (entry == NULL)
2019 : : {
2020 : : char *topdir;
2021 : :
2022 : 2 : topdir = _g_local_file_find_topdir_for (file_path);
2023 : 2 : if (topdir != NULL)
2024 : : {
2025 : 2 : entry = g_unix_mount_entry_at (topdir, time_read);
2026 : 2 : g_free (topdir);
2027 : : }
2028 : : }
2029 : :
2030 : 2 : return entry;
2031 : : }
2032 : :
2033 : : static gpointer
2034 : 0 : copy_mount_point_cb (gconstpointer src,
2035 : : gpointer data)
2036 : : {
2037 : 0 : GUnixMountPoint *src_mount_point = (GUnixMountPoint *) src;
2038 : 0 : return g_unix_mount_point_copy (src_mount_point);
2039 : : }
2040 : :
2041 : : /**
2042 : : * g_unix_mount_points_get:
2043 : : * @time_read: (out) (optional): return location for a timestamp
2044 : : *
2045 : : * Gets a list of [struct@GioUnix.MountPoint] instances representing the Unix
2046 : : * mount points.
2047 : : *
2048 : : * If @time_read is set, it will be filled with the mount timestamp, allowing
2049 : : * for checking if the mounts have changed with
2050 : : * [func@GioUnix.mount_points_changed_since].
2051 : : *
2052 : : * Returns: (element-type GUnixMountPoint) (transfer full): a list of the Unix
2053 : : * mount points
2054 : : **/
2055 : : GList *
2056 : 1 : g_unix_mount_points_get (guint64 *time_read)
2057 : : {
2058 : : static GList *mnt_pts_last = NULL;
2059 : : static guint64 time_read_last = 0;
2060 : 1 : GList *mnt_pts = NULL;
2061 : : guint64 time_read_now;
2062 : : G_LOCK_DEFINE_STATIC (unix_mount_points);
2063 : :
2064 : 1 : G_LOCK (unix_mount_points);
2065 : :
2066 : 1 : time_read_now = get_mount_points_timestamp ();
2067 : 1 : if (time_read_now != time_read_last || mnt_pts_last == NULL)
2068 : : {
2069 : 1 : time_read_last = time_read_now;
2070 : 1 : g_list_free_full (mnt_pts_last, (GDestroyNotify) g_unix_mount_point_free);
2071 : 1 : mnt_pts_last = _g_get_unix_mount_points ();
2072 : : }
2073 : 1 : mnt_pts = g_list_copy_deep (mnt_pts_last, copy_mount_point_cb, NULL);
2074 : :
2075 : 1 : G_UNLOCK (unix_mount_points);
2076 : :
2077 : 1 : if (time_read)
2078 : 0 : *time_read = time_read_now;
2079 : :
2080 : 1 : return mnt_pts;
2081 : : }
2082 : :
2083 : : /**
2084 : : * g_unix_mount_points_get_from_file:
2085 : : * @table_path: path to the mount points table file (for example `/etc/fstab`)
2086 : : * @time_read_out: (optional) (out caller-allocates): return location for the
2087 : : * modification time of @table_path
2088 : : * @n_points_out: (optional) (out caller-allocates): return location for the
2089 : : * number of mount points returned
2090 : : *
2091 : : * Gets an array of [struct@GioUnix.MountPoint]s containing the Unix mount
2092 : : * points listed in @table_path.
2093 : : *
2094 : : * This is a generalized version of [func@GioUnix.mount_points_get], mainly
2095 : : * intended for internal testing use. Note that [func@GioUnix.mount_points_get]
2096 : : * may parse multiple hierarchical table files, so this function is not a direct
2097 : : * superset of its functionality.
2098 : : *
2099 : : * If there is an error reading or parsing the file, `NULL` will be returned
2100 : : * and both out parameters will be set to `0`.
2101 : : *
2102 : : * Returns: (transfer full) (array length=n_points_out) (nullable): mount
2103 : : * points, or `NULL` if there was an error loading them
2104 : : * Since: 2.82
2105 : : */
2106 : : GUnixMountPoint **
2107 : 1 : g_unix_mount_points_get_from_file (const char *table_path,
2108 : : uint64_t *time_read_out,
2109 : : size_t *n_points_out)
2110 : : {
2111 : 1 : return _g_unix_mount_points_get_from_file (table_path, time_read_out, n_points_out);
2112 : : }
2113 : :
2114 : : /**
2115 : : * g_unix_mount_point_at:
2116 : : * @mount_path: (type filename): path for a possible Unix mount point
2117 : : * @time_read: (out) (optional): return location for a timestamp
2118 : : *
2119 : : * Gets a [struct@GioUnix.MountPoint] for a given mount path.
2120 : : *
2121 : : * If @time_read is set, it will be filled with a Unix timestamp for checking if
2122 : : * the mount points have changed since with
2123 : : * [func@GioUnix.mount_points_changed_since].
2124 : : *
2125 : : * If more mount points have the same mount path, the last matching mount point
2126 : : * is returned.
2127 : : *
2128 : : * Returns: (transfer full) (nullable): a [struct@GioUnix.MountPoint], or `NULL`
2129 : : * if no match is found
2130 : : * Since: 2.66
2131 : : **/
2132 : : GUnixMountPoint *
2133 : 0 : g_unix_mount_point_at (const char *mount_path,
2134 : : guint64 *time_read)
2135 : : {
2136 : : GList *mount_points, *l;
2137 : : GUnixMountPoint *mount_point, *found;
2138 : :
2139 : 0 : mount_points = g_unix_mount_points_get (time_read);
2140 : :
2141 : 0 : found = NULL;
2142 : 0 : for (l = mount_points; l != NULL; l = l->next)
2143 : : {
2144 : 0 : mount_point = l->data;
2145 : :
2146 : 0 : if (strcmp (mount_path, mount_point->mount_path) == 0)
2147 : : {
2148 : 0 : if (found != NULL)
2149 : 0 : g_unix_mount_point_free (found);
2150 : :
2151 : 0 : found = mount_point;
2152 : : }
2153 : : else
2154 : 0 : g_unix_mount_point_free (mount_point);
2155 : : }
2156 : 0 : g_list_free (mount_points);
2157 : :
2158 : 0 : return found;
2159 : : }
2160 : :
2161 : : /**
2162 : : * g_unix_mounts_changed_since:
2163 : : * @time: a timestamp
2164 : : *
2165 : : * Checks if the Unix mounts have changed since a given Unix time.
2166 : : *
2167 : : * Returns: true if the mounts have changed since @time; false otherwise
2168 : : * Deprecated: 2.84: Use [func@GioUnix.mount_entries_changed_since] instead.
2169 : : **/
2170 : : gboolean
2171 : 0 : g_unix_mounts_changed_since (guint64 time)
2172 : : {
2173 : 0 : return g_unix_mount_entries_changed_since (time);
2174 : : }
2175 : :
2176 : : /**
2177 : : * g_unix_mount_entries_changed_since:
2178 : : * @time: a timestamp
2179 : : *
2180 : : * Checks if the Unix mounts have changed since a given Unix time.
2181 : : *
2182 : : * This can only work reliably if a [class@GioUnix.MountMonitor] is running in
2183 : : * the process, otherwise changes in the mount entries file (such as
2184 : : * `/proc/self/mountinfo` on Linux) cannot be detected and, as a result, this
2185 : : * function has to conservatively always return `TRUE`.
2186 : : *
2187 : : * It is more efficient to use [signal@GioUnix.MountMonitor::mounts-changed] to
2188 : : * be signalled of changes to the mount entries, rather than polling using this
2189 : : * function. This function is more appropriate for infrequently determining
2190 : : * cache validity.
2191 : : *
2192 : : * Returns: true if the mounts have changed since @time; false otherwise
2193 : : * Since 2.84
2194 : : **/
2195 : : gboolean
2196 : 0 : g_unix_mount_entries_changed_since (guint64 time)
2197 : : {
2198 : 0 : return get_mounts_timestamp () != time;
2199 : : }
2200 : :
2201 : : /**
2202 : : * g_unix_mount_points_changed_since:
2203 : : * @time: a timestamp
2204 : : *
2205 : : * Checks if the Unix mount points have changed since a given Unix time.
2206 : : *
2207 : : * Unlike [func@GioUnix.mount_entries_changed_since], this function can work
2208 : : * reliably without a [class@GioUnix.MountMonitor] running, as it accesses the
2209 : : * static mount point information (such as `/etc/fstab` on Linux), which has a
2210 : : * valid modification time.
2211 : : *
2212 : : * It is more efficient to use [signal@GioUnix.MountMonitor::mountpoints-changed]
2213 : : * to be signalled of changes to the mount points, rather than polling using
2214 : : * this function. This function is more appropriate for infrequently determining
2215 : : * cache validity.
2216 : : *
2217 : : * Returns: true if the mount points have changed since @time; false otherwise
2218 : : **/
2219 : : gboolean
2220 : 0 : g_unix_mount_points_changed_since (guint64 time)
2221 : : {
2222 : 0 : return get_mount_points_timestamp () != time;
2223 : : }
2224 : :
2225 : : /* GUnixMountMonitor {{{1 */
2226 : :
2227 : : enum {
2228 : : MOUNTS_CHANGED,
2229 : : MOUNTPOINTS_CHANGED,
2230 : : LAST_SIGNAL
2231 : : };
2232 : :
2233 : : static guint signals[LAST_SIGNAL];
2234 : :
2235 : : struct _GUnixMountMonitor {
2236 : : GObject parent;
2237 : :
2238 : : GMainContext *context;
2239 : : };
2240 : :
2241 : : struct _GUnixMountMonitorClass {
2242 : : GObjectClass parent_class;
2243 : : };
2244 : :
2245 : :
2246 : 7 : G_DEFINE_TYPE (GUnixMountMonitor, g_unix_mount_monitor, G_TYPE_OBJECT)
2247 : :
2248 : : static GContextSpecificGroup mount_monitor_group;
2249 : : static GFileMonitor *fstab_monitor;
2250 : : static GFileMonitor *mtab_monitor;
2251 : : static GList *mount_poller_mounts;
2252 : : static guint mtab_file_changed_id;
2253 : :
2254 : : /* Called with proc_mounts_source lock held. */
2255 : : static gboolean
2256 : 1 : proc_mounts_watch_is_running (void)
2257 : : {
2258 : 1 : return proc_mounts_watch_source != NULL &&
2259 : 0 : !g_source_is_destroyed (proc_mounts_watch_source);
2260 : : }
2261 : :
2262 : : static void
2263 : 0 : fstab_file_changed (GFileMonitor *monitor,
2264 : : GFile *file,
2265 : : GFile *other_file,
2266 : : GFileMonitorEvent event_type,
2267 : : gpointer user_data)
2268 : : {
2269 : 0 : if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
2270 : 0 : event_type != G_FILE_MONITOR_EVENT_CREATED &&
2271 : : event_type != G_FILE_MONITOR_EVENT_DELETED)
2272 : 0 : return;
2273 : :
2274 : 0 : g_context_specific_group_emit (&mount_monitor_group, signals[MOUNTPOINTS_CHANGED]);
2275 : : }
2276 : :
2277 : : static gboolean
2278 : 0 : mtab_file_changed_cb (gpointer user_data)
2279 : : {
2280 : 0 : mtab_file_changed_id = 0;
2281 : 0 : g_context_specific_group_emit (&mount_monitor_group, signals[MOUNTS_CHANGED]);
2282 : :
2283 : 0 : return G_SOURCE_REMOVE;
2284 : : }
2285 : :
2286 : : static void
2287 : 0 : mtab_file_changed (GFileMonitor *monitor,
2288 : : GFile *file,
2289 : : GFile *other_file,
2290 : : GFileMonitorEvent event_type,
2291 : : gpointer user_data)
2292 : : {
2293 : : GMainContext *context;
2294 : : GSource *source;
2295 : :
2296 : 0 : if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
2297 : 0 : event_type != G_FILE_MONITOR_EVENT_CREATED &&
2298 : : event_type != G_FILE_MONITOR_EVENT_DELETED)
2299 : 0 : return;
2300 : :
2301 : : /* Skip accumulated events from file monitor which we are not able to handle
2302 : : * in a real time instead of emitting mounts_changed signal several times.
2303 : : * This should behave equally to GIOChannel based monitoring. See Bug 792235.
2304 : : */
2305 : 0 : if (mtab_file_changed_id > 0)
2306 : 0 : return;
2307 : :
2308 : 0 : context = g_main_context_get_thread_default ();
2309 : 0 : if (!context)
2310 : 0 : context = g_main_context_default ();
2311 : :
2312 : 0 : source = g_idle_source_new ();
2313 : 0 : g_source_set_priority (source, G_PRIORITY_DEFAULT);
2314 : 0 : g_source_set_callback (source, mtab_file_changed_cb, NULL, NULL);
2315 : 0 : g_source_set_static_name (source, "[gio] mtab_file_changed_cb");
2316 : 0 : g_source_attach (source, context);
2317 : 0 : g_source_unref (source);
2318 : : }
2319 : :
2320 : : static gboolean
2321 : 0 : proc_mounts_changed (GIOChannel *channel,
2322 : : GIOCondition cond,
2323 : : gpointer user_data)
2324 : : {
2325 : 0 : gboolean has_changed = FALSE;
2326 : :
2327 : : #ifdef HAVE_LIBMOUNT
2328 : 0 : if (cond & G_IO_IN)
2329 : : {
2330 : 0 : G_LOCK (proc_mounts_source);
2331 : 0 : if (proc_mounts_monitor != NULL)
2332 : : {
2333 : : int ret;
2334 : :
2335 : : /* The mnt_monitor_next_change function needs to be used to avoid false-positives. */
2336 : 0 : ret = mnt_monitor_next_change (proc_mounts_monitor, NULL, NULL);
2337 : 0 : if (ret == 0)
2338 : : {
2339 : 0 : has_changed = TRUE;
2340 : 0 : ret = mnt_monitor_event_cleanup (proc_mounts_monitor);
2341 : : }
2342 : :
2343 : 0 : if (ret < 0)
2344 : 0 : g_debug ("mnt_monitor_next_change failed: %s", g_strerror (-ret));
2345 : : }
2346 : 0 : G_UNLOCK (proc_mounts_source);
2347 : : }
2348 : : #endif
2349 : :
2350 : 0 : if (cond & G_IO_ERR)
2351 : 0 : has_changed = TRUE;
2352 : :
2353 : 0 : if (has_changed)
2354 : : {
2355 : 0 : G_LOCK (proc_mounts_source);
2356 : 0 : mount_poller_time = (guint64) g_get_monotonic_time ();
2357 : 0 : G_UNLOCK (proc_mounts_source);
2358 : :
2359 : 0 : g_context_specific_group_emit (&mount_monitor_group, signals[MOUNTS_CHANGED]);
2360 : : }
2361 : :
2362 : 0 : return TRUE;
2363 : : }
2364 : :
2365 : : static gboolean
2366 : 0 : mount_change_poller (gpointer user_data)
2367 : : {
2368 : : GList *current_mounts, *new_it, *old_it;
2369 : 0 : gboolean has_changed = FALSE;
2370 : :
2371 : 0 : current_mounts = _g_get_unix_mounts ();
2372 : :
2373 : 0 : for ( new_it = current_mounts, old_it = mount_poller_mounts;
2374 : 0 : new_it != NULL && old_it != NULL;
2375 : 0 : new_it = g_list_next (new_it), old_it = g_list_next (old_it) )
2376 : : {
2377 : 0 : if (g_unix_mount_entry_compare (new_it->data, old_it->data) != 0)
2378 : : {
2379 : 0 : has_changed = TRUE;
2380 : 0 : break;
2381 : : }
2382 : : }
2383 : 0 : if (!(new_it == NULL && old_it == NULL))
2384 : 0 : has_changed = TRUE;
2385 : :
2386 : 0 : g_list_free_full (mount_poller_mounts, (GDestroyNotify) g_unix_mount_entry_free);
2387 : :
2388 : 0 : mount_poller_mounts = current_mounts;
2389 : :
2390 : 0 : if (has_changed)
2391 : : {
2392 : 0 : G_LOCK (proc_mounts_source);
2393 : 0 : mount_poller_time = (guint64) g_get_monotonic_time ();
2394 : 0 : G_UNLOCK (proc_mounts_source);
2395 : :
2396 : 0 : g_context_specific_group_emit (&mount_monitor_group, signals[MOUNTPOINTS_CHANGED]);
2397 : : }
2398 : :
2399 : 0 : return TRUE;
2400 : : }
2401 : :
2402 : :
2403 : : static void
2404 : 1 : mount_monitor_stop (void)
2405 : : {
2406 : 1 : if (fstab_monitor)
2407 : : {
2408 : 1 : g_file_monitor_cancel (fstab_monitor);
2409 : 1 : g_object_unref (fstab_monitor);
2410 : : }
2411 : :
2412 : 1 : G_LOCK (proc_mounts_source);
2413 : 1 : if (proc_mounts_watch_source != NULL)
2414 : : {
2415 : 1 : g_source_destroy (proc_mounts_watch_source);
2416 : 1 : proc_mounts_watch_source = NULL;
2417 : : }
2418 : :
2419 : : #ifdef HAVE_LIBMOUNT
2420 : 1 : g_clear_pointer (&proc_mounts_monitor, mnt_unref_monitor);
2421 : : #endif
2422 : 1 : G_UNLOCK (proc_mounts_source);
2423 : :
2424 : 1 : if (mtab_monitor)
2425 : : {
2426 : 0 : g_file_monitor_cancel (mtab_monitor);
2427 : 0 : g_object_unref (mtab_monitor);
2428 : : }
2429 : :
2430 : 1 : if (mtab_file_changed_id)
2431 : : {
2432 : 0 : g_source_remove (mtab_file_changed_id);
2433 : 0 : mtab_file_changed_id = 0;
2434 : : }
2435 : :
2436 : 1 : g_list_free_full (mount_poller_mounts, (GDestroyNotify) g_unix_mount_entry_free);
2437 : 1 : }
2438 : :
2439 : : static void
2440 : 1 : mount_monitor_start (void)
2441 : : {
2442 : : GFile *file;
2443 : :
2444 : 1 : if (get_fstab_file () != NULL)
2445 : : {
2446 : 1 : file = g_file_new_for_path (get_fstab_file ());
2447 : 1 : fstab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
2448 : 1 : g_object_unref (file);
2449 : :
2450 : 1 : g_signal_connect (fstab_monitor, "changed", (GCallback)fstab_file_changed, NULL);
2451 : : }
2452 : :
2453 : 1 : if (get_mtab_monitor_file () != NULL)
2454 : : {
2455 : : const gchar *mtab_path;
2456 : :
2457 : 1 : mtab_path = get_mtab_monitor_file ();
2458 : : /* Monitoring files in /proc/ is special - can't just use GFileMonitor.
2459 : : * See 'man proc' for more details.
2460 : : */
2461 : 1 : if (g_str_has_prefix (mtab_path, "/proc/"))
2462 : : {
2463 : 1 : GIOChannel *proc_mounts_channel = NULL;
2464 : 1 : GError *error = NULL;
2465 : : #ifdef HAVE_LIBMOUNT
2466 : : int ret;
2467 : :
2468 : 1 : G_LOCK (proc_mounts_source);
2469 : :
2470 : 1 : proc_mounts_monitor = mnt_new_monitor ();
2471 : 1 : ret = mnt_monitor_enable_kernel (proc_mounts_monitor, TRUE);
2472 : 1 : if (ret < 0)
2473 : 0 : g_warning ("mnt_monitor_enable_kernel failed: %s", g_strerror (-ret));
2474 : :
2475 : 1 : ret = mnt_monitor_enable_userspace (proc_mounts_monitor, TRUE, NULL);
2476 : 1 : if (ret < 0)
2477 : 0 : g_warning ("mnt_monitor_enable_userspace failed: %s", g_strerror (-ret));
2478 : :
2479 : : #ifdef HAVE_MNT_MONITOR_VEIL_KERNEL
2480 : 1 : ret = mnt_monitor_veil_kernel (proc_mounts_monitor, TRUE);
2481 : 1 : if (ret < 0)
2482 : 0 : g_warning ("mnt_monitor_veil_kernel failed: %s", g_strerror (-ret));
2483 : : #endif
2484 : :
2485 : 1 : ret = mnt_monitor_get_fd (proc_mounts_monitor);
2486 : 1 : if (ret >= 0)
2487 : : {
2488 : 1 : proc_mounts_channel = g_io_channel_unix_new (ret);
2489 : : }
2490 : : else
2491 : : {
2492 : 0 : g_debug ("mnt_monitor_get_fd failed: %s", g_strerror (-ret));
2493 : 0 : g_clear_pointer (&proc_mounts_monitor, mnt_unref_monitor);
2494 : :
2495 : : /* The mnt_monitor_get_fd function failed e.g. inotify limits are
2496 : : * exceeded. Let's try to silently fallback to the old behavior.
2497 : : * See: https://gitlab.gnome.org/GNOME/tracker-miners/-/issues/315
2498 : : */
2499 : : }
2500 : :
2501 : 1 : G_UNLOCK (proc_mounts_source);
2502 : : #endif
2503 : 1 : if (proc_mounts_channel == NULL)
2504 : 0 : proc_mounts_channel = g_io_channel_new_file (mtab_path, "r", &error);
2505 : :
2506 : 1 : if (error != NULL)
2507 : : {
2508 : 0 : g_warning ("Error creating IO channel for %s: %s (%s, %d)", mtab_path,
2509 : : error->message, g_quark_to_string (error->domain), error->code);
2510 : 0 : g_error_free (error);
2511 : : }
2512 : : else
2513 : : {
2514 : 1 : G_LOCK (proc_mounts_source);
2515 : :
2516 : : #ifdef HAVE_LIBMOUNT
2517 : 1 : if (proc_mounts_monitor != NULL)
2518 : 1 : proc_mounts_watch_source = g_io_create_watch (proc_mounts_channel, G_IO_IN);
2519 : : #endif
2520 : 1 : if (proc_mounts_watch_source == NULL)
2521 : 0 : proc_mounts_watch_source = g_io_create_watch (proc_mounts_channel, G_IO_ERR);
2522 : :
2523 : 1 : mount_poller_time = (guint64) g_get_monotonic_time ();
2524 : 1 : g_source_set_callback (proc_mounts_watch_source,
2525 : : (GSourceFunc) proc_mounts_changed,
2526 : : NULL, NULL);
2527 : 1 : g_source_attach (proc_mounts_watch_source,
2528 : : g_main_context_get_thread_default ());
2529 : 1 : g_source_unref (proc_mounts_watch_source);
2530 : 1 : g_io_channel_unref (proc_mounts_channel);
2531 : :
2532 : 1 : G_UNLOCK (proc_mounts_source);
2533 : : }
2534 : : }
2535 : : else
2536 : : {
2537 : 0 : file = g_file_new_for_path (mtab_path);
2538 : 0 : mtab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
2539 : 0 : g_object_unref (file);
2540 : 0 : g_signal_connect (mtab_monitor, "changed", (GCallback)mtab_file_changed, NULL);
2541 : : }
2542 : : }
2543 : : else
2544 : : {
2545 : 0 : G_LOCK (proc_mounts_source);
2546 : :
2547 : 0 : proc_mounts_watch_source = g_timeout_source_new_seconds (3);
2548 : 0 : mount_poller_mounts = _g_get_unix_mounts ();
2549 : 0 : mount_poller_time = (guint64)g_get_monotonic_time ();
2550 : 0 : g_source_set_callback (proc_mounts_watch_source,
2551 : : mount_change_poller,
2552 : : NULL, NULL);
2553 : 0 : g_source_attach (proc_mounts_watch_source,
2554 : : g_main_context_get_thread_default ());
2555 : 0 : g_source_unref (proc_mounts_watch_source);
2556 : :
2557 : 0 : G_UNLOCK (proc_mounts_source);
2558 : : }
2559 : 1 : }
2560 : :
2561 : : static void
2562 : 1 : g_unix_mount_monitor_finalize (GObject *object)
2563 : : {
2564 : : GUnixMountMonitor *monitor;
2565 : :
2566 : 1 : monitor = G_UNIX_MOUNT_MONITOR (object);
2567 : :
2568 : 1 : g_context_specific_group_remove (&mount_monitor_group, monitor->context, monitor, mount_monitor_stop);
2569 : :
2570 : 1 : G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize (object);
2571 : 1 : }
2572 : :
2573 : : static void
2574 : 2 : g_unix_mount_monitor_class_init (GUnixMountMonitorClass *klass)
2575 : : {
2576 : 2 : GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
2577 : :
2578 : 2 : gobject_class->finalize = g_unix_mount_monitor_finalize;
2579 : :
2580 : : /**
2581 : : * GUnixMountMonitor::mounts-changed:
2582 : : * @monitor: the object on which the signal is emitted
2583 : : *
2584 : : * Emitted when the Unix mount entries have changed.
2585 : : */
2586 : 2 : signals[MOUNTS_CHANGED] =
2587 : 2 : g_signal_new (I_("mounts-changed"),
2588 : : G_TYPE_FROM_CLASS (klass),
2589 : : G_SIGNAL_RUN_LAST,
2590 : : 0,
2591 : : NULL, NULL,
2592 : : NULL,
2593 : : G_TYPE_NONE, 0);
2594 : :
2595 : : /**
2596 : : * GUnixMountMonitor::mountpoints-changed:
2597 : : * @monitor: the object on which the signal is emitted
2598 : : *
2599 : : * Emitted when the Unix mount points have changed.
2600 : : */
2601 : 2 : signals[MOUNTPOINTS_CHANGED] =
2602 : 2 : g_signal_new (I_("mountpoints-changed"),
2603 : : G_TYPE_FROM_CLASS (klass),
2604 : : G_SIGNAL_RUN_LAST,
2605 : : 0,
2606 : : NULL, NULL,
2607 : : NULL,
2608 : : G_TYPE_NONE, 0);
2609 : 2 : }
2610 : :
2611 : : static void
2612 : 1 : g_unix_mount_monitor_init (GUnixMountMonitor *monitor)
2613 : : {
2614 : 1 : }
2615 : :
2616 : : /**
2617 : : * g_unix_mount_monitor_set_rate_limit:
2618 : : * @mount_monitor: a [class@GioUnix.MountMonitor]
2619 : : * @limit_msec: a integer with the limit (in milliseconds) to poll for changes
2620 : : *
2621 : : * This function does nothing.
2622 : : *
2623 : : * Before 2.44, this was a partially-effective way of controlling the
2624 : : * rate at which events would be reported under some uncommon
2625 : : * circumstances. Since @mount_monitor is a singleton, it also meant
2626 : : * that calling this function would have side effects for other users of
2627 : : * the monitor.
2628 : : *
2629 : : * Since: 2.18
2630 : : * Deprecated: 2.44: This function does nothing. Don’t call it.
2631 : : */
2632 : : void
2633 : 0 : g_unix_mount_monitor_set_rate_limit (GUnixMountMonitor *mount_monitor,
2634 : : gint limit_msec)
2635 : : {
2636 : 0 : }
2637 : :
2638 : : /**
2639 : : * g_unix_mount_monitor_get:
2640 : : *
2641 : : * Gets the [class@GioUnix.MountMonitor] for the current thread-default main
2642 : : * context.
2643 : : *
2644 : : * The mount monitor can be used to monitor for changes to the list of
2645 : : * mounted filesystems as well as the list of mount points (ie: fstab
2646 : : * entries).
2647 : : *
2648 : : * You must only call [method@GObject.Object.unref] on the return value from
2649 : : * under the same main context as you called this function.
2650 : : *
2651 : : * Returns: (transfer full): the [class@GioUnix.MountMonitor]
2652 : : * Since: 2.44
2653 : : **/
2654 : : GUnixMountMonitor *
2655 : 1 : g_unix_mount_monitor_get (void)
2656 : : {
2657 : 1 : return g_context_specific_group_get (&mount_monitor_group,
2658 : : G_TYPE_UNIX_MOUNT_MONITOR,
2659 : : G_STRUCT_OFFSET(GUnixMountMonitor, context),
2660 : : mount_monitor_start);
2661 : : }
2662 : :
2663 : : /**
2664 : : * g_unix_mount_monitor_new:
2665 : : *
2666 : : * Deprecated alias for [func@GioUnix.MountMonitor.get].
2667 : : *
2668 : : * This function was never a true constructor, which is why it was
2669 : : * renamed.
2670 : : *
2671 : : * Returns: a [class@GioUnix.MountMonitor]
2672 : : * Deprecated: 2.44: Use [func@GioUnix.MountMonitor.get] instead.
2673 : : */
2674 : : GUnixMountMonitor *
2675 : 0 : g_unix_mount_monitor_new (void)
2676 : : {
2677 : 0 : return g_unix_mount_monitor_get ();
2678 : : }
2679 : :
2680 : : /* GUnixMount {{{1 */
2681 : : /**
2682 : : * g_unix_mount_free:
2683 : : * @mount_entry: a [struct@GioUnix.MountEntry]
2684 : : *
2685 : : * Frees a Unix mount.
2686 : : *
2687 : : * Deprecated: 2.84: Use [method@GioUnix.MountEntry.free] instead.
2688 : : */
2689 : : void
2690 : 3 : g_unix_mount_free (GUnixMountEntry *mount_entry)
2691 : : {
2692 : 3 : g_unix_mount_entry_free (mount_entry);
2693 : 3 : }
2694 : :
2695 : : /**
2696 : : * g_unix_mount_entry_free:
2697 : : * @mount_entry: a [struct@GioUnix.MountEntry]
2698 : : *
2699 : : * Frees a Unix mount.
2700 : : *
2701 : : * Since: 2.84
2702 : : */
2703 : : void
2704 : 299 : g_unix_mount_entry_free (GUnixMountEntry *mount_entry)
2705 : : {
2706 : 299 : g_return_if_fail (mount_entry != NULL);
2707 : :
2708 : 299 : g_free (mount_entry->mount_path);
2709 : 299 : g_free (mount_entry->device_path);
2710 : 299 : g_free (mount_entry->root_path);
2711 : 299 : g_free (mount_entry->filesystem_type);
2712 : 299 : g_free (mount_entry->options);
2713 : 299 : g_free (mount_entry);
2714 : : }
2715 : :
2716 : : /**
2717 : : * g_unix_mount_copy:
2718 : : * @mount_entry: a [struct@GioUnix.MountEntry]
2719 : : *
2720 : : * Makes a copy of @mount_entry.
2721 : : *
2722 : : * Returns: (transfer full): a new [struct@GioUnix.MountEntry]
2723 : : * Since: 2.54
2724 : : * Deprecated: 2.84: Use [method@GioUnix.MountEntry.copy] instead.
2725 : : */
2726 : : GUnixMountEntry *
2727 : 0 : g_unix_mount_copy (GUnixMountEntry *mount_entry)
2728 : : {
2729 : 0 : return g_unix_mount_entry_copy (mount_entry);
2730 : : }
2731 : :
2732 : : /**
2733 : : * g_unix_mount_entry_copy:
2734 : : * @mount_entry: a [struct@GioUnix.MountEntry]
2735 : : *
2736 : : * Makes a copy of @mount_entry.
2737 : : *
2738 : : * Returns: (transfer full): a new [struct@GioUnix.MountEntry]
2739 : : * Since: 2.84
2740 : : */
2741 : : GUnixMountEntry *
2742 : 0 : g_unix_mount_entry_copy (GUnixMountEntry *mount_entry)
2743 : : {
2744 : : GUnixMountEntry *copy;
2745 : :
2746 : 0 : g_return_val_if_fail (mount_entry != NULL, NULL);
2747 : :
2748 : 0 : copy = g_new0 (GUnixMountEntry, 1);
2749 : 0 : copy->mount_path = g_strdup (mount_entry->mount_path);
2750 : 0 : copy->device_path = g_strdup (mount_entry->device_path);
2751 : 0 : copy->root_path = g_strdup (mount_entry->root_path);
2752 : 0 : copy->filesystem_type = g_strdup (mount_entry->filesystem_type);
2753 : 0 : copy->options = g_strdup (mount_entry->options);
2754 : 0 : copy->is_read_only = mount_entry->is_read_only;
2755 : 0 : copy->is_system_internal = mount_entry->is_system_internal;
2756 : :
2757 : 0 : return copy;
2758 : : }
2759 : :
2760 : : /**
2761 : : * g_unix_mount_point_free:
2762 : : * @mount_point: Unix mount point to free.
2763 : : *
2764 : : * Frees a Unix mount point.
2765 : : */
2766 : : void
2767 : 4 : g_unix_mount_point_free (GUnixMountPoint *mount_point)
2768 : : {
2769 : 4 : g_return_if_fail (mount_point != NULL);
2770 : :
2771 : 4 : g_free (mount_point->mount_path);
2772 : 4 : g_free (mount_point->device_path);
2773 : 4 : g_free (mount_point->filesystem_type);
2774 : 4 : g_free (mount_point->options);
2775 : 4 : g_free (mount_point);
2776 : : }
2777 : :
2778 : : /**
2779 : : * g_unix_mount_point_copy:
2780 : : * @mount_point: a [struct@GioUnix.MountPoint]
2781 : : *
2782 : : * Makes a copy of @mount_point.
2783 : : *
2784 : : * Returns: (transfer full): a new [struct@GioUnix.MountPoint]
2785 : : * Since: 2.54
2786 : : */
2787 : : GUnixMountPoint*
2788 : 0 : g_unix_mount_point_copy (GUnixMountPoint *mount_point)
2789 : : {
2790 : : GUnixMountPoint *copy;
2791 : :
2792 : 0 : g_return_val_if_fail (mount_point != NULL, NULL);
2793 : :
2794 : 0 : copy = g_new0 (GUnixMountPoint, 1);
2795 : 0 : copy->mount_path = g_strdup (mount_point->mount_path);
2796 : 0 : copy->device_path = g_strdup (mount_point->device_path);
2797 : 0 : copy->filesystem_type = g_strdup (mount_point->filesystem_type);
2798 : 0 : copy->options = g_strdup (mount_point->options);
2799 : 0 : copy->is_read_only = mount_point->is_read_only;
2800 : 0 : copy->is_user_mountable = mount_point->is_user_mountable;
2801 : 0 : copy->is_loopback = mount_point->is_loopback;
2802 : :
2803 : 0 : return copy;
2804 : : }
2805 : :
2806 : : /**
2807 : : * g_unix_mount_compare:
2808 : : * @mount1: first [struct@GioUnix.MountEntry] to compare
2809 : : * @mount2: second [struct@GioUnix.MountEntry] to compare
2810 : : *
2811 : : * Compares two Unix mounts.
2812 : : *
2813 : : * Returns: `1`, `0` or `-1` if @mount1 is greater than, equal to,
2814 : : * or less than @mount2, respectively
2815 : : * Deprecated: 2.84: Use [method@GioUnix.MountEntry.compare] instead.
2816 : : */
2817 : : gint
2818 : 0 : g_unix_mount_compare (GUnixMountEntry *mount1,
2819 : : GUnixMountEntry *mount2)
2820 : : {
2821 : 0 : return g_unix_mount_entry_compare (mount1, mount2);
2822 : : }
2823 : :
2824 : : /**
2825 : : * g_unix_mount_entry_compare:
2826 : : * @mount1: first [struct@GioUnix.MountEntry] to compare
2827 : : * @mount2: second [struct@GioUnix.MountEntry] to compare
2828 : : *
2829 : : * Compares two Unix mounts.
2830 : : *
2831 : : * Returns: `1`, `0` or `-1` if @mount1 is greater than, equal to,
2832 : : * or less than @mount2, respectively
2833 : : * Since: 2.84
2834 : : */
2835 : : gint
2836 : 138 : g_unix_mount_entry_compare (GUnixMountEntry *mount1,
2837 : : GUnixMountEntry *mount2)
2838 : : {
2839 : : int res;
2840 : :
2841 : 138 : g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
2842 : :
2843 : 138 : res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
2844 : 138 : if (res != 0)
2845 : 138 : return res;
2846 : :
2847 : 0 : res = g_strcmp0 (mount1->device_path, mount2->device_path);
2848 : 0 : if (res != 0)
2849 : 0 : return res;
2850 : :
2851 : 0 : res = g_strcmp0 (mount1->root_path, mount2->root_path);
2852 : 0 : if (res != 0)
2853 : 0 : return res;
2854 : :
2855 : 0 : res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
2856 : 0 : if (res != 0)
2857 : 0 : return res;
2858 : :
2859 : 0 : res = g_strcmp0 (mount1->options, mount2->options);
2860 : 0 : if (res != 0)
2861 : 0 : return res;
2862 : :
2863 : 0 : res = mount1->is_read_only - mount2->is_read_only;
2864 : 0 : if (res != 0)
2865 : 0 : return res;
2866 : :
2867 : 0 : return 0;
2868 : : }
2869 : :
2870 : : /**
2871 : : * g_unix_mount_get_mount_path:
2872 : : * @mount_entry: a [struct@GioUnix.MountEntry] to get the mount path for
2873 : : *
2874 : : * Gets the mount path for a Unix mount.
2875 : : *
2876 : : * Returns: (type filename): the mount path for @mount_entry
2877 : : * Deprecated: 2.84: Use [method@GioUnix.MountEntry.get_mount_path] instead.
2878 : : */
2879 : : const gchar *
2880 : 3 : g_unix_mount_get_mount_path (GUnixMountEntry *mount_entry)
2881 : : {
2882 : 3 : return g_unix_mount_entry_get_mount_path (mount_entry);
2883 : : }
2884 : :
2885 : : /**
2886 : : * g_unix_mount_entry_get_mount_path:
2887 : : * @mount_entry: a [struct@GioUnix.MountEntry] to get the mount path for
2888 : : *
2889 : : * Gets the mount path for a Unix mount.
2890 : : *
2891 : : * Returns: (type filename): the mount path for @mount_entry
2892 : : * Since: 2.84
2893 : : */
2894 : : const gchar *
2895 : 42 : g_unix_mount_entry_get_mount_path (GUnixMountEntry *mount_entry)
2896 : : {
2897 : 42 : g_return_val_if_fail (mount_entry != NULL, NULL);
2898 : :
2899 : 42 : return mount_entry->mount_path;
2900 : : }
2901 : :
2902 : : /**
2903 : : * g_unix_mount_get_device_path:
2904 : : * @mount_entry: a [struct@GioUnix.MountEntry]
2905 : : *
2906 : : * Gets the device path for a Unix mount.
2907 : : *
2908 : : * Returns: (type filename): a string containing the device path
2909 : : * Deprecated: 2.84: Use [method@GioUnix.MountEntry.get_device_path] instead.
2910 : : */
2911 : : const gchar *
2912 : 3 : g_unix_mount_get_device_path (GUnixMountEntry *mount_entry)
2913 : : {
2914 : 3 : return g_unix_mount_entry_get_device_path (mount_entry);
2915 : : }
2916 : :
2917 : : /**
2918 : : * g_unix_mount_entry_get_device_path:
2919 : : * @mount_entry: a [struct@GioUnix.MountEntry]
2920 : : *
2921 : : * Gets the device path for a Unix mount.
2922 : : *
2923 : : * Returns: (type filename): a string containing the device path
2924 : : * Since: 2.84
2925 : : */
2926 : : const gchar *
2927 : 4 : g_unix_mount_entry_get_device_path (GUnixMountEntry *mount_entry)
2928 : : {
2929 : 4 : g_return_val_if_fail (mount_entry != NULL, NULL);
2930 : :
2931 : 4 : return mount_entry->device_path;
2932 : : }
2933 : :
2934 : : /**
2935 : : * g_unix_mount_get_root_path:
2936 : : * @mount_entry: a [struct@GioUnix.MountEntry]
2937 : : *
2938 : : * Gets the root of the mount within the filesystem.
2939 : : *
2940 : : * This is useful e.g. for mounts created by bind operation, or btrfs subvolumes.
2941 : : *
2942 : : * For example, the root path is equal to `/` for a mount created by
2943 : : * `mount /dev/sda1 /mnt/foo` and `/bar` for
2944 : : * `mount --bind /mnt/foo/bar /mnt/bar`.
2945 : : *
2946 : : * Returns: (nullable): a string containing the root, or `NULL` if not supported
2947 : : * Since: 2.60
2948 : : * Deprecated: 2.84: Use [method@GioUnix.MountEntry.get_root_path] instead.
2949 : : */
2950 : : const gchar *
2951 : 3 : g_unix_mount_get_root_path (GUnixMountEntry *mount_entry)
2952 : : {
2953 : 3 : return g_unix_mount_entry_get_root_path (mount_entry);
2954 : : }
2955 : :
2956 : : /**
2957 : : * g_unix_mount_entry_get_root_path:
2958 : : * @mount_entry: a [struct@GioUnix.MountEntry]
2959 : : *
2960 : : * Gets the root of the mount within the filesystem. This is useful e.g. for
2961 : : * mounts created by bind operation, or btrfs subvolumes.
2962 : : *
2963 : : * For example, the root path is equal to `/` for a mount created by
2964 : : * `mount /dev/sda1 /mnt/foo` and `/bar` for
2965 : : * `mount --bind /mnt/foo/bar /mnt/bar`.
2966 : : *
2967 : : * Returns: (nullable): a string containing the root, or `NULL` if not supported
2968 : : * Since: 2.84
2969 : : */
2970 : : const gchar *
2971 : 4 : g_unix_mount_entry_get_root_path (GUnixMountEntry *mount_entry)
2972 : : {
2973 : 4 : g_return_val_if_fail (mount_entry != NULL, NULL);
2974 : :
2975 : 4 : return mount_entry->root_path;
2976 : : }
2977 : :
2978 : : /**
2979 : : * g_unix_mount_get_fs_type:
2980 : : * @mount_entry: a [struct@GioUnix.MountEntry]
2981 : : *
2982 : : * Gets the filesystem type for the Unix mount.
2983 : : *
2984 : : * Returns: a string containing the file system type
2985 : : * Deprecated: 2.84: Use [method@GioUnix.MountEntry.get_fs_type] instead.
2986 : : */
2987 : : const gchar *
2988 : 3 : g_unix_mount_get_fs_type (GUnixMountEntry *mount_entry)
2989 : : {
2990 : 3 : return g_unix_mount_entry_get_fs_type (mount_entry);
2991 : : }
2992 : :
2993 : : /**
2994 : : * g_unix_mount_entry_get_fs_type:
2995 : : * @mount_entry: a [struct@GioUnix.MountEntry]
2996 : : *
2997 : : * Gets the filesystem type for the Unix mount.
2998 : : *
2999 : : * Returns: a string containing the file system type
3000 : : * Since: 2.84
3001 : : */
3002 : : const gchar *
3003 : 4 : g_unix_mount_entry_get_fs_type (GUnixMountEntry *mount_entry)
3004 : : {
3005 : 4 : g_return_val_if_fail (mount_entry != NULL, NULL);
3006 : :
3007 : 4 : return mount_entry->filesystem_type;
3008 : : }
3009 : :
3010 : : /**
3011 : : * g_unix_mount_get_options:
3012 : : * @mount_entry: a [struct@GioUnix.MountEntry]
3013 : : *
3014 : : * Gets a comma separated list of mount options for the Unix mount.
3015 : : *
3016 : : * For example: `rw,relatime,seclabel,data=ordered`.
3017 : : *
3018 : : * This is similar to [method@GioUnix.MountPoint.get_options], but it takes
3019 : : * a [struct@GioUnix.MountEntry] as an argument.
3020 : : *
3021 : : * Returns: (nullable): a string containing the options, or `NULL` if not
3022 : : * available.
3023 : : * Since: 2.58
3024 : : * Deprecated: 2.84: Use [method@GioUnix.MountEntry.get_options] instead.
3025 : : */
3026 : : const gchar *
3027 : 3 : g_unix_mount_get_options (GUnixMountEntry *mount_entry)
3028 : : {
3029 : 3 : return g_unix_mount_entry_get_options (mount_entry);
3030 : : }
3031 : :
3032 : : /**
3033 : : * g_unix_mount_entry_get_options:
3034 : : * @mount_entry: a [struct@GioUnix.MountEntry]
3035 : : *
3036 : : * Gets a comma separated list of mount options for the Unix mount.
3037 : : *
3038 : : * For example: `rw,relatime,seclabel,data=ordered`.
3039 : : *
3040 : : * This is similar to [method@GioUnix.MountPoint.get_options], but it takes
3041 : : * a [struct@GioUnix.MountEntry] as an argument.
3042 : : *
3043 : : * Returns: (nullable): a string containing the options, or `NULL` if not
3044 : : * available.
3045 : : * Since: 2.84
3046 : : */
3047 : : const gchar *
3048 : 6 : g_unix_mount_entry_get_options (GUnixMountEntry *mount_entry)
3049 : : {
3050 : 6 : g_return_val_if_fail (mount_entry != NULL, NULL);
3051 : :
3052 : 6 : return mount_entry->options;
3053 : : }
3054 : :
3055 : : /**
3056 : : * g_unix_mount_is_readonly:
3057 : : * @mount_entry: a [struct@GioUnix.MountEntry]
3058 : : *
3059 : : * Checks if a Unix mount is mounted read only.
3060 : : *
3061 : : * Returns: true if @mount_entry is read only; false otherwise
3062 : : * Deprecated: 2.84: Use [method@GioUnix.MountEntry.is_readonly] instead.
3063 : : */
3064 : : gboolean
3065 : 0 : g_unix_mount_is_readonly (GUnixMountEntry *mount_entry)
3066 : : {
3067 : 0 : return g_unix_mount_entry_is_readonly (mount_entry);
3068 : : }
3069 : :
3070 : : /**
3071 : : * g_unix_mount_entry_is_readonly:
3072 : : * @mount_entry: a [struct@GioUnix.MountEntry]
3073 : : *
3074 : : * Checks if a Unix mount is mounted read only.
3075 : : *
3076 : : * Returns: true if @mount_entry is read only; false otherwise
3077 : : * Since: 2.84
3078 : : */
3079 : : gboolean
3080 : 0 : g_unix_mount_entry_is_readonly (GUnixMountEntry *mount_entry)
3081 : : {
3082 : 0 : g_return_val_if_fail (mount_entry != NULL, FALSE);
3083 : :
3084 : 0 : return mount_entry->is_read_only;
3085 : : }
3086 : :
3087 : : /**
3088 : : * g_unix_mount_is_system_internal:
3089 : : * @mount_entry: a [struct@GioUnix.MountEntry]
3090 : : *
3091 : : * Checks if a Unix mount is a system mount.
3092 : : *
3093 : : * This is the Boolean OR of
3094 : : * [func@GioUnix.is_system_fs_type], [func@GioUnix.is_system_device_path] and
3095 : : * [func@GioUnix.is_mount_path_system_internal] on @mount_entry’s properties.
3096 : : *
3097 : : * The definition of what a ‘system’ mount entry is may change over time as new
3098 : : * file system types and device paths are ignored.
3099 : : *
3100 : : * Returns: true if the Unix mount is for a system path; false otherwise
3101 : : * Deprecated: 2.84: Use [method@GioUnix.MountEntry.is_system_internal] instead.
3102 : : */
3103 : : gboolean
3104 : 0 : g_unix_mount_is_system_internal (GUnixMountEntry *mount_entry)
3105 : : {
3106 : 0 : return g_unix_mount_entry_is_system_internal (mount_entry);
3107 : : }
3108 : :
3109 : : /**
3110 : : * g_unix_mount_entry_is_system_internal:
3111 : : * @mount_entry: a [struct@GioUnix.MountEntry]
3112 : : *
3113 : : * Checks if a Unix mount is a system mount.
3114 : : *
3115 : : * This is the Boolean OR of
3116 : : * [func@GioUnix.is_system_fs_type], [func@GioUnix.is_system_device_path] and
3117 : : * [func@GioUnix.is_mount_path_system_internal] on @mount_entry’s properties.
3118 : : *
3119 : : * The definition of what a ‘system’ mount entry is may change over time as new
3120 : : * file system types and device paths are ignored.
3121 : : *
3122 : : * Returns: true if the Unix mount is for a system path; false otherwise
3123 : : * Since: 2.84
3124 : : */
3125 : : gboolean
3126 : 40 : g_unix_mount_entry_is_system_internal (GUnixMountEntry *mount_entry)
3127 : : {
3128 : 40 : g_return_val_if_fail (mount_entry != NULL, FALSE);
3129 : :
3130 : 40 : return mount_entry->is_system_internal;
3131 : : }
3132 : :
3133 : : /* GUnixMountPoint {{{1 */
3134 : : /**
3135 : : * g_unix_mount_point_compare:
3136 : : * @mount1: a [struct@GioUnix.MountPoint]
3137 : : * @mount2: a [struct@GioUnix.MountPoint]
3138 : : *
3139 : : * Compares two Unix mount points.
3140 : : *
3141 : : * Returns: `1`, `0` or `-1` if @mount1 is greater than, equal to,
3142 : : * or less than @mount2, respectively
3143 : : */
3144 : : gint
3145 : 0 : g_unix_mount_point_compare (GUnixMountPoint *mount1,
3146 : : GUnixMountPoint *mount2)
3147 : : {
3148 : : int res;
3149 : :
3150 : 0 : g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
3151 : :
3152 : 0 : res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
3153 : 0 : if (res != 0)
3154 : 0 : return res;
3155 : :
3156 : 0 : res = g_strcmp0 (mount1->device_path, mount2->device_path);
3157 : 0 : if (res != 0)
3158 : 0 : return res;
3159 : :
3160 : 0 : res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
3161 : 0 : if (res != 0)
3162 : 0 : return res;
3163 : :
3164 : 0 : res = g_strcmp0 (mount1->options, mount2->options);
3165 : 0 : if (res != 0)
3166 : 0 : return res;
3167 : :
3168 : 0 : res = mount1->is_read_only - mount2->is_read_only;
3169 : 0 : if (res != 0)
3170 : 0 : return res;
3171 : :
3172 : 0 : res = mount1->is_user_mountable - mount2->is_user_mountable;
3173 : 0 : if (res != 0)
3174 : 0 : return res;
3175 : :
3176 : 0 : res = mount1->is_loopback - mount2->is_loopback;
3177 : 0 : if (res != 0)
3178 : 0 : return res;
3179 : :
3180 : 0 : return 0;
3181 : : }
3182 : :
3183 : : /**
3184 : : * g_unix_mount_point_get_mount_path:
3185 : : * @mount_point: a [struct@GioUnix.MountPoint]
3186 : : *
3187 : : * Gets the mount path for a Unix mount point.
3188 : : *
3189 : : * Returns: (type filename): a string containing the mount path
3190 : : */
3191 : : const gchar *
3192 : 0 : g_unix_mount_point_get_mount_path (GUnixMountPoint *mount_point)
3193 : : {
3194 : 0 : g_return_val_if_fail (mount_point != NULL, NULL);
3195 : :
3196 : 0 : return mount_point->mount_path;
3197 : : }
3198 : :
3199 : : /**
3200 : : * g_unix_mount_point_get_device_path:
3201 : : * @mount_point: a [struct@GioUnix.MountPoint]
3202 : : *
3203 : : * Gets the device path for a Unix mount point.
3204 : : *
3205 : : * Returns: (type filename): a string containing the device path
3206 : : */
3207 : : const gchar *
3208 : 4 : g_unix_mount_point_get_device_path (GUnixMountPoint *mount_point)
3209 : : {
3210 : 4 : g_return_val_if_fail (mount_point != NULL, NULL);
3211 : :
3212 : 4 : return mount_point->device_path;
3213 : : }
3214 : :
3215 : : /**
3216 : : * g_unix_mount_point_get_fs_type:
3217 : : * @mount_point: a [struct@GioUnix.MountPoint]
3218 : : *
3219 : : * Gets the file system type for the mount point.
3220 : : *
3221 : : * Returns: a string containing the file system type
3222 : : */
3223 : : const gchar *
3224 : 4 : g_unix_mount_point_get_fs_type (GUnixMountPoint *mount_point)
3225 : : {
3226 : 4 : g_return_val_if_fail (mount_point != NULL, NULL);
3227 : :
3228 : 4 : return mount_point->filesystem_type;
3229 : : }
3230 : :
3231 : : /**
3232 : : * g_unix_mount_point_get_options:
3233 : : * @mount_point: a [struct@GioUnix.MountPoint]
3234 : : *
3235 : : * Gets the options for the mount point.
3236 : : *
3237 : : * Returns: (nullable): a string containing the options
3238 : : * Since: 2.32
3239 : : */
3240 : : const gchar *
3241 : 4 : g_unix_mount_point_get_options (GUnixMountPoint *mount_point)
3242 : : {
3243 : 4 : g_return_val_if_fail (mount_point != NULL, NULL);
3244 : :
3245 : 4 : return mount_point->options;
3246 : : }
3247 : :
3248 : : /**
3249 : : * g_unix_mount_point_is_readonly:
3250 : : * @mount_point: a [struct@GioUnix.MountPoint]
3251 : : *
3252 : : * Checks if a Unix mount point is read only.
3253 : : *
3254 : : * Returns: true if a mount point is read only; false otherwise
3255 : : */
3256 : : gboolean
3257 : 4 : g_unix_mount_point_is_readonly (GUnixMountPoint *mount_point)
3258 : : {
3259 : 4 : g_return_val_if_fail (mount_point != NULL, FALSE);
3260 : :
3261 : 4 : return mount_point->is_read_only;
3262 : : }
3263 : :
3264 : : /**
3265 : : * g_unix_mount_point_is_user_mountable:
3266 : : * @mount_point: a [struct@GioUnix.MountPoint]
3267 : : *
3268 : : * Checks if a Unix mount point is mountable by the user.
3269 : : *
3270 : : * Returns: true if the mount point is user mountable; false otherwise
3271 : : */
3272 : : gboolean
3273 : 4 : g_unix_mount_point_is_user_mountable (GUnixMountPoint *mount_point)
3274 : : {
3275 : 4 : g_return_val_if_fail (mount_point != NULL, FALSE);
3276 : :
3277 : 4 : return mount_point->is_user_mountable;
3278 : : }
3279 : :
3280 : : /**
3281 : : * g_unix_mount_point_is_loopback:
3282 : : * @mount_point: a [struct@GioUnix.MountPoint]
3283 : : *
3284 : : * Checks if a Unix mount point is a loopback device.
3285 : : *
3286 : : * Returns: true if the mount point is a loopback device; false otherwise
3287 : : */
3288 : : gboolean
3289 : 4 : g_unix_mount_point_is_loopback (GUnixMountPoint *mount_point)
3290 : : {
3291 : 4 : g_return_val_if_fail (mount_point != NULL, FALSE);
3292 : :
3293 : 4 : return mount_point->is_loopback;
3294 : : }
3295 : :
3296 : : static GUnixMountType
3297 : 12 : guess_mount_type (const char *mount_path,
3298 : : const char *device_path,
3299 : : const char *filesystem_type)
3300 : : {
3301 : : GUnixMountType type;
3302 : : char *basename;
3303 : :
3304 : 12 : type = G_UNIX_MOUNT_TYPE_UNKNOWN;
3305 : :
3306 : 12 : if ((strcmp (filesystem_type, "udf") == 0) ||
3307 : 12 : (strcmp (filesystem_type, "iso9660") == 0) ||
3308 : 12 : (strcmp (filesystem_type, "cd9660") == 0))
3309 : 0 : type = G_UNIX_MOUNT_TYPE_CDROM;
3310 : 12 : else if ((strcmp (filesystem_type, "nfs") == 0) ||
3311 : 12 : (strcmp (filesystem_type, "nfs4") == 0))
3312 : 0 : type = G_UNIX_MOUNT_TYPE_NFS;
3313 : 12 : else if (g_str_has_prefix (device_path, "/vol/dev/diskette/") ||
3314 : 12 : g_str_has_prefix (device_path, "/dev/fd") ||
3315 : 12 : g_str_has_prefix (device_path, "/dev/floppy"))
3316 : 0 : type = G_UNIX_MOUNT_TYPE_FLOPPY;
3317 : 12 : else if (g_str_has_prefix (device_path, "/dev/cdrom") ||
3318 : 12 : g_str_has_prefix (device_path, "/dev/acd") ||
3319 : 12 : g_str_has_prefix (device_path, "/dev/cd"))
3320 : 0 : type = G_UNIX_MOUNT_TYPE_CDROM;
3321 : 12 : else if (g_str_has_prefix (device_path, "/vol/"))
3322 : : {
3323 : 0 : const char *name = mount_path + strlen ("/");
3324 : :
3325 : 0 : if (g_str_has_prefix (name, "cdrom"))
3326 : 0 : type = G_UNIX_MOUNT_TYPE_CDROM;
3327 : 0 : else if (g_str_has_prefix (name, "floppy") ||
3328 : 0 : g_str_has_prefix (device_path, "/vol/dev/diskette/"))
3329 : 0 : type = G_UNIX_MOUNT_TYPE_FLOPPY;
3330 : 0 : else if (g_str_has_prefix (name, "rmdisk"))
3331 : 0 : type = G_UNIX_MOUNT_TYPE_ZIP;
3332 : 0 : else if (g_str_has_prefix (name, "jaz"))
3333 : 0 : type = G_UNIX_MOUNT_TYPE_JAZ;
3334 : 0 : else if (g_str_has_prefix (name, "memstick"))
3335 : 0 : type = G_UNIX_MOUNT_TYPE_MEMSTICK;
3336 : : }
3337 : : else
3338 : : {
3339 : 12 : basename = g_path_get_basename (mount_path);
3340 : :
3341 : 12 : if (g_str_has_prefix (basename, "cdr") ||
3342 : 12 : g_str_has_prefix (basename, "cdwriter") ||
3343 : 12 : g_str_has_prefix (basename, "burn") ||
3344 : 12 : g_str_has_prefix (basename, "dvdr"))
3345 : 0 : type = G_UNIX_MOUNT_TYPE_CDROM;
3346 : 12 : else if (g_str_has_prefix (basename, "floppy"))
3347 : 0 : type = G_UNIX_MOUNT_TYPE_FLOPPY;
3348 : 12 : else if (g_str_has_prefix (basename, "zip"))
3349 : 0 : type = G_UNIX_MOUNT_TYPE_ZIP;
3350 : 12 : else if (g_str_has_prefix (basename, "jaz"))
3351 : 0 : type = G_UNIX_MOUNT_TYPE_JAZ;
3352 : 12 : else if (g_str_has_prefix (basename, "camera"))
3353 : 0 : type = G_UNIX_MOUNT_TYPE_CAMERA;
3354 : 12 : else if (g_str_has_prefix (basename, "memstick") ||
3355 : 12 : g_str_has_prefix (basename, "memory_stick") ||
3356 : 12 : g_str_has_prefix (basename, "ram"))
3357 : 0 : type = G_UNIX_MOUNT_TYPE_MEMSTICK;
3358 : 12 : else if (g_str_has_prefix (basename, "compact_flash"))
3359 : 0 : type = G_UNIX_MOUNT_TYPE_CF;
3360 : 12 : else if (g_str_has_prefix (basename, "smart_media"))
3361 : 0 : type = G_UNIX_MOUNT_TYPE_SM;
3362 : 12 : else if (g_str_has_prefix (basename, "sd_mmc"))
3363 : 0 : type = G_UNIX_MOUNT_TYPE_SDMMC;
3364 : 12 : else if (g_str_has_prefix (basename, "ipod"))
3365 : 0 : type = G_UNIX_MOUNT_TYPE_IPOD;
3366 : :
3367 : 12 : g_free (basename);
3368 : : }
3369 : :
3370 : 12 : if (type == G_UNIX_MOUNT_TYPE_UNKNOWN)
3371 : 12 : type = G_UNIX_MOUNT_TYPE_HD;
3372 : :
3373 : 12 : return type;
3374 : : }
3375 : :
3376 : : /**
3377 : : * g_unix_mount_entry_guess_type:
3378 : : * @mount_entry: a [struct@GioUnix.MountEntry]
3379 : : *
3380 : : * Guesses the type of a Unix mount entry.
3381 : : *
3382 : : * If the mount type cannot be determined, returns
3383 : : * [enum@GioUnix.MountType.UNKNOWN].
3384 : : *
3385 : : * Returns: a [enum@GioUnix.MountType]
3386 : : */
3387 : : static GUnixMountType
3388 : 0 : g_unix_mount_entry_guess_type (GUnixMountEntry *mount_entry)
3389 : : {
3390 : 0 : g_return_val_if_fail (mount_entry != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
3391 : 0 : g_return_val_if_fail (mount_entry->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
3392 : 0 : g_return_val_if_fail (mount_entry->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
3393 : 0 : g_return_val_if_fail (mount_entry->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
3394 : :
3395 : 0 : return guess_mount_type (mount_entry->mount_path,
3396 : 0 : mount_entry->device_path,
3397 : 0 : mount_entry->filesystem_type);
3398 : : }
3399 : :
3400 : : /**
3401 : : * g_unix_mount_point_guess_type:
3402 : : * @mount_point: a [struct@GioUnix.MountPoint]
3403 : : *
3404 : : * Guesses the type of a Unix mount point.
3405 : : *
3406 : : * If the mount type cannot be determined, returns
3407 : : * [enum@GioUnix.MountType.UNKNOWN].
3408 : : *
3409 : : * Returns: a [enum@GioUnix.MountType]
3410 : : */
3411 : : static GUnixMountType
3412 : 12 : g_unix_mount_point_guess_type (GUnixMountPoint *mount_point)
3413 : : {
3414 : 12 : g_return_val_if_fail (mount_point != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
3415 : 12 : g_return_val_if_fail (mount_point->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
3416 : 12 : g_return_val_if_fail (mount_point->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
3417 : 12 : g_return_val_if_fail (mount_point->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
3418 : :
3419 : 12 : return guess_mount_type (mount_point->mount_path,
3420 : 12 : mount_point->device_path,
3421 : 12 : mount_point->filesystem_type);
3422 : : }
3423 : :
3424 : : static const char *
3425 : 8 : type_to_icon (GUnixMountType type, gboolean is_mount_point, gboolean use_symbolic)
3426 : : {
3427 : : const char *icon_name;
3428 : :
3429 : 8 : switch (type)
3430 : : {
3431 : 8 : case G_UNIX_MOUNT_TYPE_HD:
3432 : 8 : if (is_mount_point)
3433 : 8 : icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
3434 : : else
3435 : 0 : icon_name = use_symbolic ? "drive-harddisk-symbolic" : "drive-harddisk";
3436 : 8 : break;
3437 : 0 : case G_UNIX_MOUNT_TYPE_FLOPPY:
3438 : : case G_UNIX_MOUNT_TYPE_ZIP:
3439 : : case G_UNIX_MOUNT_TYPE_JAZ:
3440 : 0 : if (is_mount_point)
3441 : 0 : icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
3442 : : else
3443 : 0 : icon_name = use_symbolic ? "media-removable-symbolic" : "media-floppy";
3444 : 0 : break;
3445 : 0 : case G_UNIX_MOUNT_TYPE_CDROM:
3446 : 0 : if (is_mount_point)
3447 : 0 : icon_name = use_symbolic ? "drive-optical-symbolic" : "drive-optical";
3448 : : else
3449 : 0 : icon_name = use_symbolic ? "media-optical-symbolic" : "media-optical";
3450 : 0 : break;
3451 : 0 : case G_UNIX_MOUNT_TYPE_NFS:
3452 : 0 : icon_name = use_symbolic ? "folder-remote-symbolic" : "folder-remote";
3453 : 0 : break;
3454 : 0 : case G_UNIX_MOUNT_TYPE_MEMSTICK:
3455 : 0 : if (is_mount_point)
3456 : 0 : icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
3457 : : else
3458 : 0 : icon_name = use_symbolic ? "media-removable-symbolic" : "media-flash";
3459 : 0 : break;
3460 : 0 : case G_UNIX_MOUNT_TYPE_CAMERA:
3461 : 0 : if (is_mount_point)
3462 : 0 : icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
3463 : : else
3464 : 0 : icon_name = use_symbolic ? "camera-photo-symbolic" : "camera-photo";
3465 : 0 : break;
3466 : 0 : case G_UNIX_MOUNT_TYPE_IPOD:
3467 : 0 : if (is_mount_point)
3468 : 0 : icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
3469 : : else
3470 : 0 : icon_name = use_symbolic ? "multimedia-player-symbolic" : "multimedia-player";
3471 : 0 : break;
3472 : 0 : case G_UNIX_MOUNT_TYPE_UNKNOWN:
3473 : : default:
3474 : 0 : if (is_mount_point)
3475 : 0 : icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
3476 : : else
3477 : 0 : icon_name = use_symbolic ? "drive-harddisk-symbolic" : "drive-harddisk";
3478 : 0 : break;
3479 : : }
3480 : :
3481 : 8 : return icon_name;
3482 : : }
3483 : :
3484 : : /**
3485 : : * g_unix_mount_guess_name:
3486 : : * @mount_entry: a [struct@GioUnix.MountEntry]
3487 : : *
3488 : : * Guesses the name of a Unix mount entry.
3489 : : *
3490 : : * The result is a translated string.
3491 : : *
3492 : : * Returns: (transfer full): a newly allocated translated string
3493 : : * Deprecated: 2.84: Use [method@GioUnix.MountEntry.guess_name] instead.
3494 : : */
3495 : : gchar *
3496 : 0 : g_unix_mount_guess_name (GUnixMountEntry *mount_entry)
3497 : : {
3498 : 0 : return g_unix_mount_entry_guess_name (mount_entry);
3499 : : }
3500 : :
3501 : : /**
3502 : : * g_unix_mount_entry_guess_name:
3503 : : * @mount_entry: a [struct@GioUnix.MountEntry]
3504 : : *
3505 : : * Guesses the name of a Unix mount entry.
3506 : : *
3507 : : * The result is a translated string.
3508 : : *
3509 : : * Returns: (transfer full): a newly allocated translated string
3510 : : * Since: 2.84
3511 : : */
3512 : : gchar *
3513 : 0 : g_unix_mount_entry_guess_name (GUnixMountEntry *mount_entry)
3514 : : {
3515 : : char *name;
3516 : :
3517 : 0 : if (strcmp (mount_entry->mount_path, "/") == 0)
3518 : 0 : name = g_strdup (_("Filesystem root"));
3519 : : else
3520 : 0 : name = g_filename_display_basename (mount_entry->mount_path);
3521 : :
3522 : 0 : return name;
3523 : : }
3524 : :
3525 : : /**
3526 : : * g_unix_mount_guess_icon:
3527 : : * @mount_entry: a [struct@GioUnix.MountEntry]
3528 : : *
3529 : : * Guesses the icon of a Unix mount entry.
3530 : : *
3531 : : * Returns: (transfer full): a [iface@Gio.Icon]
3532 : : * Deprecated: 2.84: Use [method@GioUnix.MountEntry.guess_icon] instead.
3533 : : */
3534 : : GIcon *
3535 : 0 : g_unix_mount_guess_icon (GUnixMountEntry *mount_entry)
3536 : : {
3537 : 0 : return g_unix_mount_entry_guess_icon (mount_entry);
3538 : : }
3539 : :
3540 : : /**
3541 : : * g_unix_mount_entry_guess_icon:
3542 : : * @mount_entry: a [struct@GioUnix.MountEntry]
3543 : : *
3544 : : * Guesses the icon of a Unix mount entry.
3545 : : *
3546 : : * Returns: (transfer full): a [iface@Gio.Icon]
3547 : : * Since: 2.84
3548 : : */
3549 : : GIcon *
3550 : 0 : g_unix_mount_entry_guess_icon (GUnixMountEntry *mount_entry)
3551 : : {
3552 : 0 : return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_entry_guess_type (mount_entry), FALSE, FALSE));
3553 : : }
3554 : :
3555 : : /**
3556 : : * g_unix_mount_guess_symbolic_icon:
3557 : : * @mount_entry: a [struct@GioUnix.MountEntry]
3558 : : *
3559 : : * Guesses the symbolic icon of a Unix mount entry.
3560 : : *
3561 : : * Returns: (transfer full): a [iface@Gio.Icon]
3562 : : * Since: 2.34
3563 : : * Deprecated: 2.84: Use [method@GioUnix.MountEntry.guess_symbolic_icon] instead.
3564 : : */
3565 : : GIcon *
3566 : 0 : g_unix_mount_guess_symbolic_icon (GUnixMountEntry *mount_entry)
3567 : : {
3568 : 0 : return g_unix_mount_entry_guess_symbolic_icon (mount_entry);
3569 : : }
3570 : :
3571 : : /**
3572 : : * g_unix_mount_entry_guess_symbolic_icon:
3573 : : * @mount_entry: a [struct@GioUnix.MountEntry]
3574 : : *
3575 : : * Guesses the symbolic icon of a Unix mount entry.
3576 : : *
3577 : : * Returns: (transfer full): a [iface@Gio.Icon]
3578 : : * Since: 2.84
3579 : : */
3580 : : GIcon *
3581 : 0 : g_unix_mount_entry_guess_symbolic_icon (GUnixMountEntry *mount_entry)
3582 : : {
3583 : 0 : return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_entry_guess_type (mount_entry), FALSE, TRUE));
3584 : : }
3585 : :
3586 : : /**
3587 : : * g_unix_mount_point_guess_name:
3588 : : * @mount_point: a [struct@GioUnix.MountPoint]
3589 : : *
3590 : : * Guesses the name of a Unix mount point.
3591 : : *
3592 : : * The result is a translated string.
3593 : : *
3594 : : * Returns: (transfer full): a newly allocated translated string
3595 : : */
3596 : : gchar *
3597 : 4 : g_unix_mount_point_guess_name (GUnixMountPoint *mount_point)
3598 : : {
3599 : : char *name;
3600 : :
3601 : 4 : if (strcmp (mount_point->mount_path, "/") == 0)
3602 : 2 : name = g_strdup (_("Filesystem root"));
3603 : : else
3604 : 3 : name = g_filename_display_basename (mount_point->mount_path);
3605 : :
3606 : 4 : return name;
3607 : : }
3608 : :
3609 : : /**
3610 : : * g_unix_mount_point_guess_icon:
3611 : : * @mount_point: a [struct@GioUnix.MountPoint]
3612 : : *
3613 : : * Guesses the icon of a Unix mount point.
3614 : : *
3615 : : * Returns: (transfer full): a [iface@Gio.Icon]
3616 : : */
3617 : : GIcon *
3618 : 4 : g_unix_mount_point_guess_icon (GUnixMountPoint *mount_point)
3619 : : {
3620 : 4 : return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE, FALSE));
3621 : : }
3622 : :
3623 : : /**
3624 : : * g_unix_mount_point_guess_symbolic_icon:
3625 : : * @mount_point: a [struct@GioUnix.MountPoint]
3626 : : *
3627 : : * Guesses the symbolic icon of a Unix mount point.
3628 : : *
3629 : : * Returns: (transfer full): a [iface@Gio.Icon]
3630 : : * Since: 2.34
3631 : : */
3632 : : GIcon *
3633 : 4 : g_unix_mount_point_guess_symbolic_icon (GUnixMountPoint *mount_point)
3634 : : {
3635 : 4 : return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE, TRUE));
3636 : : }
3637 : :
3638 : : /**
3639 : : * g_unix_mount_guess_can_eject:
3640 : : * @mount_entry: a [struct@GioUnix.MountEntry]
3641 : : *
3642 : : * Guesses whether a Unix mount entry can be ejected.
3643 : : *
3644 : : * Returns: true if @mount_entry is deemed to be ejectable; false otherwise
3645 : : * Deprecated: 2.84: Use [method@GioUnix.MountEntry.guess_can_eject] instead.
3646 : : */
3647 : : gboolean
3648 : 0 : g_unix_mount_guess_can_eject (GUnixMountEntry *mount_entry)
3649 : : {
3650 : 0 : return g_unix_mount_entry_guess_can_eject (mount_entry);
3651 : : }
3652 : :
3653 : : /**
3654 : : * g_unix_mount_entry_guess_can_eject:
3655 : : * @mount_entry: a [struct@GioUnix.MountEntry]
3656 : : *
3657 : : * Guesses whether a Unix mount entry can be ejected.
3658 : : *
3659 : : * Returns: true if @mount_entry is deemed to be ejectable; false otherwise
3660 : : * Since: 2.84
3661 : : */
3662 : : gboolean
3663 : 0 : g_unix_mount_entry_guess_can_eject (GUnixMountEntry *mount_entry)
3664 : : {
3665 : : GUnixMountType guessed_type;
3666 : :
3667 : 0 : guessed_type = g_unix_mount_entry_guess_type (mount_entry);
3668 : 0 : if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
3669 : : guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
3670 : 0 : return TRUE;
3671 : :
3672 : 0 : return FALSE;
3673 : : }
3674 : :
3675 : : /**
3676 : : * g_unix_mount_guess_should_display:
3677 : : * @mount_entry: a [struct@GioUnix.MountEntry]
3678 : : *
3679 : : * Guesses whether a Unix mount entry should be displayed in the UI.
3680 : : *
3681 : : * Returns: true if @mount_entry is deemed to be displayable; false otherwise
3682 : : * Deprecated: 2.84: Use [method@GioUnix.MountEntry.guess_should_display] instead.
3683 : : */
3684 : : gboolean
3685 : 0 : g_unix_mount_guess_should_display (GUnixMountEntry *mount_entry)
3686 : : {
3687 : 0 : return g_unix_mount_entry_guess_should_display (mount_entry);
3688 : : }
3689 : :
3690 : : /**
3691 : : * g_unix_mount_entry_guess_should_display:
3692 : : * @mount_entry: a [struct@GioUnix.MountEntry]
3693 : : *
3694 : : * Guesses whether a Unix mount entry should be displayed in the UI.
3695 : : *
3696 : : * Returns: true if @mount_entry is deemed to be displayable; false otherwise
3697 : : * Since: 2.84
3698 : : */
3699 : : gboolean
3700 : 37 : g_unix_mount_entry_guess_should_display (GUnixMountEntry *mount_entry)
3701 : : {
3702 : : const char *mount_path;
3703 : : const gchar *user_name;
3704 : : gsize user_name_len;
3705 : :
3706 : : /* Never display internal mountpoints */
3707 : 37 : if (g_unix_mount_entry_is_system_internal (mount_entry))
3708 : 37 : return FALSE;
3709 : :
3710 : : /* Only display things in /media (which are generally user mountable)
3711 : : and home dir (fuse stuff) and /run/media/$USER */
3712 : 0 : mount_path = mount_entry->mount_path;
3713 : 0 : if (mount_path != NULL)
3714 : : {
3715 : 0 : const gboolean running_as_root = (getuid () == 0);
3716 : 0 : gboolean is_in_runtime_dir = FALSE;
3717 : :
3718 : : /* Hide mounts within a dot path, suppose it was a purpose to hide this mount */
3719 : 0 : if (g_strstr_len (mount_path, -1, "/.") != NULL)
3720 : 0 : return FALSE;
3721 : :
3722 : : /* Check /run/media/$USER/. If running as root, display any mounts below
3723 : : * /run/media/. */
3724 : 0 : if (running_as_root)
3725 : : {
3726 : 0 : if (strncmp (mount_path, "/run/media/", strlen ("/run/media/")) == 0)
3727 : 0 : is_in_runtime_dir = TRUE;
3728 : : }
3729 : : else
3730 : : {
3731 : 0 : user_name = g_get_user_name ();
3732 : 0 : user_name_len = strlen (user_name);
3733 : 0 : if (strncmp (mount_path, "/run/media/", strlen ("/run/media/")) == 0 &&
3734 : 0 : strncmp (mount_path + strlen ("/run/media/"), user_name, user_name_len) == 0 &&
3735 : 0 : mount_path[strlen ("/run/media/") + user_name_len] == '/')
3736 : 0 : is_in_runtime_dir = TRUE;
3737 : : }
3738 : :
3739 : 0 : if (is_in_runtime_dir || g_str_has_prefix (mount_path, "/media/"))
3740 : : {
3741 : : char *path;
3742 : : /* Avoid displaying mounts that are not accessible to the user.
3743 : : *
3744 : : * See http://bugzilla.gnome.org/show_bug.cgi?id=526320 for why we
3745 : : * want to avoid g_access() for mount points which can potentially
3746 : : * block or fail stat()'ing, such as network mounts.
3747 : : */
3748 : 0 : path = g_path_get_dirname (mount_path);
3749 : 0 : if (g_str_has_prefix (path, "/media/"))
3750 : : {
3751 : 0 : if (g_access (path, R_OK|X_OK) != 0)
3752 : : {
3753 : 0 : g_free (path);
3754 : 0 : return FALSE;
3755 : : }
3756 : : }
3757 : 0 : g_free (path);
3758 : :
3759 : 0 : if (mount_entry->device_path && mount_entry->device_path[0] == '/')
3760 : : {
3761 : : struct stat st;
3762 : 0 : if (g_stat (mount_entry->device_path, &st) == 0 &&
3763 : 0 : S_ISBLK(st.st_mode) &&
3764 : 0 : g_access (mount_path, R_OK|X_OK) != 0)
3765 : 0 : return FALSE;
3766 : : }
3767 : 0 : return TRUE;
3768 : : }
3769 : :
3770 : 0 : if (g_str_has_prefix (mount_path, g_get_home_dir ()) &&
3771 : 0 : mount_path[strlen (g_get_home_dir())] == G_DIR_SEPARATOR)
3772 : 0 : return TRUE;
3773 : : }
3774 : :
3775 : 0 : return FALSE;
3776 : : }
3777 : :
3778 : : /**
3779 : : * g_unix_mount_point_guess_can_eject:
3780 : : * @mount_point: a [struct@GioUnix.MountPoint]
3781 : : *
3782 : : * Guesses whether a Unix mount point can be ejected.
3783 : : *
3784 : : * Returns: true if @mount_point is deemed to be ejectable; false otherwise
3785 : : */
3786 : : gboolean
3787 : 4 : g_unix_mount_point_guess_can_eject (GUnixMountPoint *mount_point)
3788 : : {
3789 : : GUnixMountType guessed_type;
3790 : :
3791 : 4 : guessed_type = g_unix_mount_point_guess_type (mount_point);
3792 : 4 : if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
3793 : : guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
3794 : 0 : return TRUE;
3795 : :
3796 : 4 : return FALSE;
3797 : : }
3798 : :
3799 : : /* Utility functions {{{1 */
3800 : :
3801 : : #ifdef HAVE_MNTENT_H
3802 : : /* borrowed from gtk/gtkfilesystemunix.c in GTK on 02/23/2006 */
3803 : : static void
3804 : 0 : _canonicalize_filename (gchar *filename)
3805 : : {
3806 : : gchar *p, *q;
3807 : 0 : gboolean last_was_slash = FALSE;
3808 : :
3809 : 0 : p = filename;
3810 : 0 : q = filename;
3811 : :
3812 : 0 : while (*p)
3813 : : {
3814 : 0 : if (*p == G_DIR_SEPARATOR)
3815 : : {
3816 : 0 : if (!last_was_slash)
3817 : 0 : *q++ = G_DIR_SEPARATOR;
3818 : :
3819 : 0 : last_was_slash = TRUE;
3820 : : }
3821 : : else
3822 : : {
3823 : 0 : if (last_was_slash && *p == '.')
3824 : : {
3825 : 0 : if (*(p + 1) == G_DIR_SEPARATOR ||
3826 : 0 : *(p + 1) == '\0')
3827 : : {
3828 : 0 : if (*(p + 1) == '\0')
3829 : 0 : break;
3830 : :
3831 : 0 : p += 1;
3832 : : }
3833 : 0 : else if (*(p + 1) == '.' &&
3834 : 0 : (*(p + 2) == G_DIR_SEPARATOR ||
3835 : 0 : *(p + 2) == '\0'))
3836 : : {
3837 : 0 : if (q > filename + 1)
3838 : : {
3839 : 0 : q--;
3840 : 0 : while (q > filename + 1 &&
3841 : 0 : *(q - 1) != G_DIR_SEPARATOR)
3842 : 0 : q--;
3843 : : }
3844 : :
3845 : 0 : if (*(p + 2) == '\0')
3846 : 0 : break;
3847 : :
3848 : 0 : p += 2;
3849 : : }
3850 : : else
3851 : : {
3852 : 0 : *q++ = *p;
3853 : 0 : last_was_slash = FALSE;
3854 : : }
3855 : : }
3856 : : else
3857 : : {
3858 : 0 : *q++ = *p;
3859 : 0 : last_was_slash = FALSE;
3860 : : }
3861 : : }
3862 : :
3863 : 0 : p++;
3864 : : }
3865 : :
3866 : 0 : if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
3867 : 0 : q--;
3868 : :
3869 : 0 : *q = '\0';
3870 : 0 : }
3871 : :
3872 : : static char *
3873 : 0 : _resolve_symlink (const char *file)
3874 : : {
3875 : : GError *error;
3876 : : char *dir;
3877 : : char *link;
3878 : : char *f;
3879 : : char *f1;
3880 : :
3881 : 0 : f = g_strdup (file);
3882 : :
3883 : 0 : while (g_file_test (f, G_FILE_TEST_IS_SYMLINK))
3884 : : {
3885 : 0 : link = g_file_read_link (f, &error);
3886 : 0 : if (link == NULL)
3887 : : {
3888 : 0 : g_error_free (error);
3889 : 0 : g_free (f);
3890 : 0 : f = NULL;
3891 : 0 : goto out;
3892 : : }
3893 : :
3894 : 0 : dir = g_path_get_dirname (f);
3895 : 0 : f1 = g_strdup_printf ("%s/%s", dir, link);
3896 : 0 : g_free (dir);
3897 : 0 : g_free (link);
3898 : 0 : g_free (f);
3899 : 0 : f = f1;
3900 : : }
3901 : :
3902 : 0 : out:
3903 : 0 : if (f != NULL)
3904 : 0 : _canonicalize_filename (f);
3905 : 0 : return f;
3906 : : }
3907 : :
3908 : : static const char *
3909 : 0 : _resolve_dev_root (void)
3910 : : {
3911 : : static gboolean have_real_dev_root = FALSE;
3912 : : static char real_dev_root[256];
3913 : : struct stat statbuf;
3914 : :
3915 : : /* see if it's cached already */
3916 : 0 : if (have_real_dev_root)
3917 : 0 : goto found;
3918 : :
3919 : : /* otherwise we're going to find it right away.. */
3920 : 0 : have_real_dev_root = TRUE;
3921 : :
3922 : 0 : if (stat ("/dev/root", &statbuf) == 0)
3923 : : {
3924 : 0 : if (! S_ISLNK (statbuf.st_mode))
3925 : : {
3926 : 0 : dev_t root_dev = statbuf.st_dev;
3927 : : FILE *f;
3928 : :
3929 : : /* see if device with similar major:minor as /dev/root is mention
3930 : : * in /etc/mtab (it usually is)
3931 : : */
3932 : 0 : f = g_fopen ("/etc/mtab", "re");
3933 : 0 : if (f != NULL)
3934 : : {
3935 : : struct mntent *entp;
3936 : : #ifdef HAVE_GETMNTENT_R
3937 : : struct mntent ent;
3938 : : char buf[1024];
3939 : 0 : while ((entp = getmntent_r (f, &ent, buf, sizeof (buf))) != NULL)
3940 : : {
3941 : : #else
3942 : : G_LOCK (getmntent);
3943 : : while ((entp = getmntent (f)) != NULL)
3944 : : {
3945 : : #endif
3946 : 0 : if (stat (entp->mnt_fsname, &statbuf) == 0 &&
3947 : 0 : statbuf.st_dev == root_dev)
3948 : : {
3949 : 0 : strncpy (real_dev_root, entp->mnt_fsname, sizeof (real_dev_root) - 1);
3950 : 0 : real_dev_root[sizeof (real_dev_root) - 1] = '\0';
3951 : 0 : fclose (f);
3952 : 0 : goto found;
3953 : : }
3954 : : }
3955 : :
3956 : : /* endmntent() calls fclose() for us, but scan-build doesn’t know that */
3957 : : #if !G_ANALYZER_ANALYZING
3958 : 0 : endmntent (f);
3959 : : #else
3960 : : fclose (f);
3961 : : #endif
3962 : :
3963 : : #ifndef HAVE_GETMNTENT_R
3964 : : G_UNLOCK (getmntent);
3965 : : #endif
3966 : : }
3967 : :
3968 : : /* no, that didn't work.. next we could scan /dev ... but I digress.. */
3969 : :
3970 : : }
3971 : : else
3972 : : {
3973 : : char *resolved;
3974 : 0 : resolved = _resolve_symlink ("/dev/root");
3975 : 0 : if (resolved != NULL)
3976 : : {
3977 : 0 : strncpy (real_dev_root, resolved, sizeof (real_dev_root) - 1);
3978 : 0 : real_dev_root[sizeof (real_dev_root) - 1] = '\0';
3979 : 0 : g_free (resolved);
3980 : 0 : goto found;
3981 : : }
3982 : : }
3983 : : }
3984 : :
3985 : : /* bah sucks.. */
3986 : 0 : strcpy (real_dev_root, "/dev/root");
3987 : :
3988 : 0 : found:
3989 : 0 : return real_dev_root;
3990 : : }
3991 : : #endif
3992 : :
3993 : : /* Epilogue {{{1 */
3994 : : /* vim:set foldmethod=marker: */
|