1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
27 #include <sys/types.h>
30 #ifndef HAVE_SYSCTLBYNAME
31 #ifdef HAVE_SYS_PARAM_H
32 #include <sys/param.h>
48 #include <sys/statfs.h>
50 #if HAVE_SYS_STATVFS_H
51 #include <sys/statvfs.h>
55 #elif HAVE_SYS_MOUNT_H
57 #include <sys/param.h>
59 #include <sys/mount.h>
66 #include "gunixmounts.h"
67 #include "glocalfileprivate.h"
69 #include "gfilemonitor.h"
71 #include "gthemedicon.h"
72 #include "gcontextspecificgroup.h"
76 static const char *_resolve_dev_root (void);
81 * @include: gio/gunixmounts.h
82 * @short_description: UNIX mounts
84 * Routines for managing mounted UNIX mount points and paths.
86 * Note that `<gio/gunixmounts.h>` belongs to the UNIX-specific GIO
87 * interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
93 * @G_UNIX_MOUNT_TYPE_UNKNOWN: Unknown UNIX mount type.
94 * @G_UNIX_MOUNT_TYPE_FLOPPY: Floppy disk UNIX mount type.
95 * @G_UNIX_MOUNT_TYPE_CDROM: CDROM UNIX mount type.
96 * @G_UNIX_MOUNT_TYPE_NFS: Network File System (NFS) UNIX mount type.
97 * @G_UNIX_MOUNT_TYPE_ZIP: ZIP UNIX mount type.
98 * @G_UNIX_MOUNT_TYPE_JAZ: JAZZ UNIX mount type.
99 * @G_UNIX_MOUNT_TYPE_MEMSTICK: Memory Stick UNIX mount type.
100 * @G_UNIX_MOUNT_TYPE_CF: Compact Flash UNIX mount type.
101 * @G_UNIX_MOUNT_TYPE_SM: Smart Media UNIX mount type.
102 * @G_UNIX_MOUNT_TYPE_SDMMC: SD/MMC UNIX mount type.
103 * @G_UNIX_MOUNT_TYPE_IPOD: iPod UNIX mount type.
104 * @G_UNIX_MOUNT_TYPE_CAMERA: Digital camera UNIX mount type.
105 * @G_UNIX_MOUNT_TYPE_HD: Hard drive UNIX mount type.
107 * Types of UNIX mounts.
110 G_UNIX_MOUNT_TYPE_UNKNOWN,
111 G_UNIX_MOUNT_TYPE_FLOPPY,
112 G_UNIX_MOUNT_TYPE_CDROM,
113 G_UNIX_MOUNT_TYPE_NFS,
114 G_UNIX_MOUNT_TYPE_ZIP,
115 G_UNIX_MOUNT_TYPE_JAZ,
116 G_UNIX_MOUNT_TYPE_MEMSTICK,
117 G_UNIX_MOUNT_TYPE_CF,
118 G_UNIX_MOUNT_TYPE_SM,
119 G_UNIX_MOUNT_TYPE_SDMMC,
120 G_UNIX_MOUNT_TYPE_IPOD,
121 G_UNIX_MOUNT_TYPE_CAMERA,
125 struct _GUnixMountEntry {
129 char *filesystem_type;
131 gboolean is_read_only;
132 gboolean is_system_internal;
135 G_DEFINE_BOXED_TYPE (GUnixMountEntry, g_unix_mount_entry,
136 g_unix_mount_copy, g_unix_mount_free)
138 struct _GUnixMountPoint {
141 char *filesystem_type;
143 gboolean is_read_only;
144 gboolean is_user_mountable;
145 gboolean is_loopback;
148 G_DEFINE_BOXED_TYPE (GUnixMountPoint, g_unix_mount_point,
149 g_unix_mount_point_copy, g_unix_mount_point_free)
151 static GList *_g_get_unix_mounts (void);
152 static GList *_g_get_unix_mount_points (void);
153 static gboolean proc_mounts_watch_is_running (void);
155 G_LOCK_DEFINE_STATIC (proc_mounts_source);
157 /* Protected by proc_mounts_source lock */
158 static guint64 mount_poller_time = 0;
159 static GSource *proc_mounts_watch_source;
161 #ifdef HAVE_SYS_MNTTAB_H
162 #define MNTOPT_RO "ro"
168 #include <libmount.h>
170 #elif defined (HAVE_SYS_MNTTAB_H)
171 #include <sys/mnttab.h>
172 #if defined(__sun) && !defined(mnt_opts)
173 #define mnt_opts mnt_mntopts
177 #ifdef HAVE_SYS_VFSTAB_H
178 #include <sys/vfstab.h>
181 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
182 #include <sys/mntctl.h>
184 #include <sys/vmount.h>
188 #if (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
189 #include <sys/param.h>
190 #include <sys/ucred.h>
191 #include <sys/mount.h>
193 #ifdef HAVE_SYS_SYSCTL_H
194 #include <sys/sysctl.h>
198 #ifndef HAVE_SETMNTENT
199 #define setmntent(f,m) fopen(f,m)
201 #ifndef HAVE_ENDMNTENT
202 #define endmntent(f) fclose(f)
206 is_in (const char *value, const char *set[])
209 for (i = 0; set[i] != NULL; i++)
211 if (strcmp (set[i], value) == 0)
218 * g_unix_is_mount_path_system_internal:
219 * @mount_path: (type filename): a mount path, e.g. `/media/disk` or `/usr`
221 * Determines if @mount_path is considered an implementation of the
222 * OS. This is primarily used for hiding mountable and mounted volumes
223 * that only are used in the OS and has little to no relevance to the
226 * Returns: %TRUE if @mount_path is considered an implementation detail
230 g_unix_is_mount_path_system_internal (const char *mount_path)
232 const char *ignore_mountpoints[] = {
233 /* Includes all FHS 2.3 toplevel dirs and other specialized
234 * directories that we want to hide from the user.
236 "/", /* we already have "Filesystem root" in Nautilus */
239 "/compat/linux/proc",
268 "/var/log/audit", /* https://bugzilla.redhat.com/show_bug.cgi?id=333041 */
271 "/var/tmp", /* https://bugzilla.redhat.com/show_bug.cgi?id=335241 */
279 if (is_in (mount_path, ignore_mountpoints))
282 if (g_str_has_prefix (mount_path, "/dev/") ||
283 g_str_has_prefix (mount_path, "/proc/") ||
284 g_str_has_prefix (mount_path, "/sys/"))
287 if (g_str_has_suffix (mount_path, "/.gvfs"))
294 * g_unix_is_system_fs_type:
295 * @fs_type: a file system type, e.g. `procfs` or `tmpfs`
297 * Determines if @fs_type is considered a type of file system which is only
298 * used in implementation of the OS. This is primarily used for hiding
299 * mounted volumes that are intended as APIs for programs to read, and system
300 * administrators at a shell; rather than something that should, for example,
301 * appear in a GUI. For example, the Linux `/proc` filesystem.
303 * The list of file system types considered ‘system’ ones may change over time.
305 * Returns: %TRUE if @fs_type is considered an implementation detail of the OS.
309 g_unix_is_system_fs_type (const char *fs_type)
311 const char *ignore_fs[] = {
357 g_return_val_if_fail (fs_type != NULL && *fs_type != '\0', FALSE);
359 return is_in (fs_type, ignore_fs);
363 * g_unix_is_system_device_path:
364 * @device_path: a device path, e.g. `/dev/loop0` or `nfsd`
366 * Determines if @device_path is considered a block device path which is only
367 * used in implementation of the OS. This is primarily used for hiding
368 * mounted volumes that are intended as APIs for programs to read, and system
369 * administrators at a shell; rather than something that should, for example,
370 * appear in a GUI. For example, the Linux `/proc` filesystem.
372 * The list of device paths considered ‘system’ ones may change over time.
374 * Returns: %TRUE if @device_path is considered an implementation detail of
379 g_unix_is_system_device_path (const char *device_path)
381 const char *ignore_devices[] = {
391 g_return_val_if_fail (device_path != NULL && *device_path != '\0', FALSE);
393 return is_in (device_path, ignore_devices);
397 guess_system_internal (const char *mountpoint,
402 if (g_unix_is_system_fs_type (fs))
405 if (g_unix_is_system_device_path (device))
408 if (g_unix_is_mount_path_system_internal (mountpoint))
411 /* It is not possible to reliably detect mounts which were created by bind
412 * operation. mntent-based _g_get_unix_mounts() implementation blindly skips
413 * mounts with a device path that is repeated (e.g. mounts created by bind
414 * operation, btrfs subvolumes). This usually chooses the most important
415 * mounts (i.e. which points to the root of filesystem), but it doesn't work
416 * in all cases and also it is not ideal that those mounts are completely
417 * ignored (e.g. x-gvfs-show doesn't work for them, trash backend can't handle
418 * files on btrfs subvolumes). libmount-based _g_get_unix_mounts()
419 * implementation provides a root path. So there is no need to completely
420 * ignore those mounts, because e.g. our volume monitors can use the root path
421 * to not mengle those mounts with the "regular" mounts (i.e. which points to
422 * the root). But because those mounts usually just duplicate other mounts and
423 * are completely ignored with mntend-based implementation, let's mark them as
424 * system internal. Given the different approaches it doesn't mean that all
425 * mounts which were ignored will be system internal now, but this should work
426 * in most cases. For more info, see g_unix_mount_get_root_path() annotation,
427 * comment in mntent-based _g_get_unix_mounts() implementation and the
428 * https://gitlab.gnome.org/GNOME/glib/issues/1271 issue.
430 if (root != NULL && g_strcmp0 (root, "/") != 0)
436 /* GUnixMounts (ie: mtab) implementations {{{1 */
438 static GUnixMountEntry *
439 create_unix_mount_entry (const char *device_path,
440 const char *mount_path,
441 const char *root_path,
442 const char *filesystem_type,
444 gboolean is_read_only)
446 GUnixMountEntry *mount_entry = NULL;
448 mount_entry = g_new0 (GUnixMountEntry, 1);
449 mount_entry->device_path = g_strdup (device_path);
450 mount_entry->mount_path = g_strdup (mount_path);
451 mount_entry->root_path = g_strdup (root_path);
452 mount_entry->filesystem_type = g_strdup (filesystem_type);
453 mount_entry->options = g_strdup (options);
454 mount_entry->is_read_only = is_read_only;
456 mount_entry->is_system_internal =
457 guess_system_internal (mount_entry->mount_path,
458 mount_entry->filesystem_type,
459 mount_entry->device_path,
460 mount_entry->root_path);
465 static GUnixMountPoint *
466 create_unix_mount_point (const char *device_path,
467 const char *mount_path,
468 const char *filesystem_type,
470 gboolean is_read_only,
471 gboolean is_user_mountable,
472 gboolean is_loopback)
474 GUnixMountPoint *mount_point = NULL;
476 mount_point = g_new0 (GUnixMountPoint, 1);
477 mount_point->device_path = g_strdup (device_path);
478 mount_point->mount_path = g_strdup (mount_path);
479 mount_point->filesystem_type = g_strdup (filesystem_type);
480 mount_point->options = g_strdup (options);
481 mount_point->is_read_only = is_read_only;
482 mount_point->is_user_mountable = is_user_mountable;
483 mount_point->is_loopback = is_loopback;
488 /* mntent.h (Linux, GNU, NSS) {{{2 */
493 /* For documentation on /proc/self/mountinfo see
494 * http://www.kernel.org/doc/Documentation/filesystems/proc.txt
496 #define PROC_MOUNTINFO_PATH "/proc/self/mountinfo"
499 _g_get_unix_mounts (void)
501 struct libmnt_table *table = NULL;
502 struct libmnt_iter* iter = NULL;
503 struct libmnt_fs *fs = NULL;
504 GUnixMountEntry *mount_entry = NULL;
505 GList *return_list = NULL;
507 table = mnt_new_table ();
508 if (mnt_table_parse_mtab (table, NULL) < 0)
511 iter = mnt_new_iter (MNT_ITER_FORWARD);
512 while (mnt_table_next_fs (table, iter, &fs) == 0)
514 const char *device_path = NULL;
515 char *mount_options = NULL;
516 unsigned long mount_flags = 0;
517 gboolean is_read_only = FALSE;
519 device_path = mnt_fs_get_source (fs);
520 if (g_strcmp0 (device_path, "/dev/root") == 0)
521 device_path = _resolve_dev_root ();
523 mount_options = mnt_fs_strdup_options (fs);
526 mnt_optstr_get_flags (mount_options, &mount_flags, mnt_get_builtin_optmap (MNT_LINUX_MAP));
527 g_free (mount_options);
529 is_read_only = (mount_flags & MS_RDONLY) ? TRUE : FALSE;
531 mount_entry = create_unix_mount_entry (device_path,
532 mnt_fs_get_target (fs),
533 mnt_fs_get_root (fs),
534 mnt_fs_get_fstype (fs),
535 mnt_fs_get_options (fs),
538 return_list = g_list_prepend (return_list, mount_entry);
540 mnt_free_iter (iter);
543 mnt_free_table (table);
545 return g_list_reverse (return_list);
551 get_mtab_read_file (void)
555 return "/proc/mounts";
557 return _PATH_MOUNTED;
564 #ifndef HAVE_GETMNTENT_R
565 G_LOCK_DEFINE_STATIC(getmntent);
569 _g_get_unix_mounts (void)
571 #ifdef HAVE_GETMNTENT_R
575 struct mntent *mntent;
577 const char *read_file;
578 GUnixMountEntry *mount_entry;
579 GHashTable *mounts_hash;
582 read_file = get_mtab_read_file ();
584 file = setmntent (read_file, "r");
590 mounts_hash = g_hash_table_new (g_str_hash, g_str_equal);
592 #ifdef HAVE_GETMNTENT_R
593 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
596 while ((mntent = getmntent (file)) != NULL)
599 const char *device_path = NULL;
600 gboolean is_read_only = FALSE;
602 /* ignore any mnt_fsname that is repeated and begins with a '/'
604 * We do this to avoid being fooled by --bind mounts, since
605 * these have the same device as the location they bind to.
606 * It's not an ideal solution to the problem, but it's likely that
607 * the most important mountpoint is first and the --bind ones after
608 * that aren't as important. So it should work.
610 * The '/' is to handle procfs, tmpfs and other no device mounts.
612 if (mntent->mnt_fsname != NULL &&
613 mntent->mnt_fsname[0] == '/' &&
614 g_hash_table_lookup (mounts_hash, mntent->mnt_fsname))
617 if (g_strcmp0 (mntent->mnt_fsname, "/dev/root") == 0)
618 device_path = _resolve_dev_root ();
620 device_path = mntent->mnt_fsname;
622 #if defined (HAVE_HASMNTOPT)
623 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
627 mount_entry = create_unix_mount_entry (device_path,
634 g_hash_table_insert (mounts_hash,
635 mount_entry->device_path,
636 mount_entry->device_path);
638 return_list = g_list_prepend (return_list, mount_entry);
640 g_hash_table_destroy (mounts_hash);
644 #ifndef HAVE_GETMNTENT_R
645 G_UNLOCK (getmntent);
648 return g_list_reverse (return_list);
651 #endif /* HAVE_LIBMOUNT */
654 get_mtab_monitor_file (void)
656 static const char *mountinfo_path = NULL;
661 if (mountinfo_path != NULL)
662 return mountinfo_path;
665 /* The mtab file is still used by some distros, so it has to be monitored in
666 * order to avoid races between g_unix_mounts_get and "mounts-changed" signal:
667 * https://bugzilla.gnome.org/show_bug.cgi?id=782814
669 if (mnt_has_regular_mtab (&mountinfo_path, NULL))
671 return mountinfo_path;
674 if (stat (PROC_MOUNTINFO_PATH, &buf) == 0)
676 mountinfo_path = PROC_MOUNTINFO_PATH;
677 return mountinfo_path;
683 mountinfo_path = "/proc/mounts";
685 mountinfo_path = _PATH_MOUNTED;
688 mountinfo_path = "/etc/mtab";
691 return mountinfo_path;
695 #elif defined (HAVE_SYS_MNTTAB_H)
697 G_LOCK_DEFINE_STATIC(getmntent);
700 get_mtab_read_file (void)
703 return _PATH_MOUNTED;
705 return "/etc/mnttab";
710 get_mtab_monitor_file (void)
712 return get_mtab_read_file ();
716 _g_get_unix_mounts (void)
718 struct mnttab mntent;
720 const char *read_file;
721 GUnixMountEntry *mount_entry;
724 read_file = get_mtab_read_file ();
726 file = setmntent (read_file, "r");
733 while (! getmntent (file, &mntent))
735 gboolean is_read_only = FALSE;
737 #if defined (HAVE_HASMNTOPT)
738 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
742 mount_entry = create_unix_mount_entry (mntent.mnt_special,
749 return_list = g_list_prepend (return_list, mount_entry);
754 G_UNLOCK (getmntent);
756 return g_list_reverse (return_list);
759 /* mntctl.h (AIX) {{{2 */
760 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
763 get_mtab_monitor_file (void)
769 _g_get_unix_mounts (void)
771 struct vfs_ent *fs_info;
772 struct vmount *vmount_info;
774 unsigned int vmount_size;
778 if (mntctl (MCTL_QUERY, sizeof (vmount_size), &vmount_size) != 0)
780 g_warning ("Unable to know the number of mounted volumes");
785 vmount_info = (struct vmount*)g_malloc (vmount_size);
787 vmount_number = mntctl (MCTL_QUERY, vmount_size, vmount_info);
789 if (vmount_info->vmt_revision != VMT_REVISION)
790 g_warning ("Bad vmount structure revision number, want %d, got %d", VMT_REVISION, vmount_info->vmt_revision);
792 if (vmount_number < 0)
794 g_warning ("Unable to recover mounted volumes information");
796 g_free (vmount_info);
801 while (vmount_number > 0)
803 gboolean is_read_only = FALSE;
805 fs_info = getvfsbytype (vmount_info->vmt_gfstype);
807 /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
808 is_read_only = (vmount_info->vmt_flags & MNT_READONLY) ? 1 : 0;
810 mount_entry = create_unix_mount_entry (vmt2dataptr (vmount_info, VMT_OBJECT),
811 vmt2dataptr (vmount_info, VMT_STUB),
813 fs_info == NULL ? "unknown" : fs_info->vfsent_name,
817 return_list = g_list_prepend (return_list, mount_entry);
819 vmount_info = (struct vmount *)( (char*)vmount_info
820 + vmount_info->vmt_length);
824 g_free (vmount_info);
826 return g_list_reverse (return_list);
829 /* sys/mount.h {{{2 */
830 #elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
833 get_mtab_monitor_file (void)
839 _g_get_unix_mounts (void)
841 #if defined(USE_STATVFS)
842 struct statvfs *mntent = NULL;
843 #elif defined(USE_STATFS)
844 struct statfs *mntent = NULL;
846 #error statfs juggling failed
850 GUnixMountEntry *mount_entry;
853 /* Pass NOWAIT to avoid blocking trying to update NFS mounts. */
854 #if defined(USE_STATVFS) && defined(HAVE_GETVFSSTAT)
855 num_mounts = getvfsstat (NULL, 0, ST_NOWAIT);
856 #elif defined(USE_STATFS) && defined(HAVE_GETFSSTAT)
857 num_mounts = getfsstat (NULL, 0, MNT_NOWAIT);
859 if (num_mounts == -1)
862 bufsize = num_mounts * sizeof (*mntent);
863 mntent = g_malloc (bufsize);
864 #if defined(USE_STATVFS) && defined(HAVE_GETVFSSTAT)
865 num_mounts = getvfsstat (mntent, bufsize, ST_NOWAIT);
866 #elif defined(USE_STATFS) && defined(HAVE_GETFSSTAT)
867 num_mounts = getfsstat (mntent, bufsize, MNT_NOWAIT);
869 if (num_mounts == -1)
874 for (i = 0; i < num_mounts; i++)
876 gboolean is_read_only = FALSE;
878 #if defined(USE_STATVFS)
879 if (mntent[i].f_flag & ST_RDONLY)
880 #elif defined(USE_STATFS)
881 if (mntent[i].f_flags & MNT_RDONLY)
883 #error statfs juggling failed
887 mount_entry = create_unix_mount_entry (mntent[i].f_mntfromname,
888 mntent[i].f_mntonname,
890 mntent[i].f_fstypename,
894 return_list = g_list_prepend (return_list, mount_entry);
899 return g_list_reverse (return_list);
903 #elif defined(__INTERIX)
906 get_mtab_monitor_file (void)
912 _g_get_unix_mounts (void)
915 GList* return_list = NULL;
916 char filename[9 + NAME_MAX];
918 dirp = opendir ("/dev/fs");
921 g_warning ("unable to read /dev/fs!");
927 struct statvfs statbuf;
929 struct dirent* result;
931 if (readdir_r (dirp, &entry, &result) || result == NULL)
934 strcpy (filename, "/dev/fs/");
935 strcat (filename, entry.d_name);
937 if (statvfs (filename, &statbuf) == 0)
939 GUnixMountEntry* mount_entry = g_new0(GUnixMountEntry, 1);
941 mount_entry->mount_path = g_strdup (statbuf.f_mntonname);
942 mount_entry->device_path = g_strdup (statbuf.f_mntfromname);
943 mount_entry->filesystem_type = g_strdup (statbuf.f_fstypename);
945 if (statbuf.f_flag & ST_RDONLY)
946 mount_entry->is_read_only = TRUE;
948 return_list = g_list_prepend(return_list, mount_entry);
952 return_list = g_list_reverse (return_list);
959 /* Common code {{{2 */
961 #error No _g_get_unix_mounts() implementation for system
964 /* GUnixMountPoints (ie: fstab) implementations {{{1 */
966 /* _g_get_unix_mount_points():
968 * don't return swap and ignore mounts.
972 get_fstab_file (void)
975 return (char *) mnt_get_fstab_path ();
977 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
979 return "/etc/filesystems";
980 #elif defined(_PATH_MNTTAB)
982 #elif defined(VFSTAB)
990 /* mntent.h (Linux, GNU, NSS) {{{2 */
996 _g_get_unix_mount_points (void)
998 struct libmnt_table *table = NULL;
999 struct libmnt_iter* iter = NULL;
1000 struct libmnt_fs *fs = NULL;
1001 GUnixMountPoint *mount_point = NULL;
1002 GList *return_list = NULL;
1004 table = mnt_new_table ();
1005 if (mnt_table_parse_fstab (table, NULL) < 0)
1008 iter = mnt_new_iter (MNT_ITER_FORWARD);
1009 while (mnt_table_next_fs (table, iter, &fs) == 0)
1011 const char *device_path = NULL;
1012 const char *mount_path = NULL;
1013 const char *mount_fstype = NULL;
1014 char *mount_options = NULL;
1015 gboolean is_read_only = FALSE;
1016 gboolean is_user_mountable = FALSE;
1017 gboolean is_loopback = FALSE;
1019 mount_path = mnt_fs_get_target (fs);
1020 if ((strcmp (mount_path, "ignore") == 0) ||
1021 (strcmp (mount_path, "swap") == 0) ||
1022 (strcmp (mount_path, "none") == 0))
1025 mount_fstype = mnt_fs_get_fstype (fs);
1026 mount_options = mnt_fs_strdup_options (fs);
1029 unsigned long mount_flags = 0;
1030 unsigned long userspace_flags = 0;
1032 mnt_optstr_get_flags (mount_options, &mount_flags, mnt_get_builtin_optmap (MNT_LINUX_MAP));
1033 mnt_optstr_get_flags (mount_options, &userspace_flags, mnt_get_builtin_optmap (MNT_USERSPACE_MAP));
1035 /* We ignore bind fstab entries, as we ignore bind mounts anyway */
1036 if (mount_flags & MS_BIND)
1038 g_free (mount_options);
1042 is_read_only = (mount_flags & MS_RDONLY) != 0;
1043 is_loopback = (userspace_flags & MNT_MS_LOOP) != 0;
1045 if ((mount_fstype != NULL && g_strcmp0 ("supermount", mount_fstype) == 0) ||
1046 ((userspace_flags & MNT_MS_USER) &&
1047 (g_strstr_len (mount_options, -1, "user_xattr") == NULL)) ||
1048 (g_strstr_len (mount_options, -1, "pamconsole") == NULL) ||
1049 (userspace_flags & MNT_MS_USERS) ||
1050 (userspace_flags & MNT_MS_OWNER))
1052 is_user_mountable = TRUE;
1056 device_path = mnt_fs_get_source (fs);
1057 if (g_strcmp0 (device_path, "/dev/root") == 0)
1058 device_path = _resolve_dev_root ();
1060 mount_point = create_unix_mount_point (device_path,
1068 g_free (mount_options);
1070 return_list = g_list_prepend (return_list, mount_point);
1072 mnt_free_iter (iter);
1075 mnt_free_table (table);
1077 return g_list_reverse (return_list);
1083 _g_get_unix_mount_points (void)
1085 #ifdef HAVE_GETMNTENT_R
1089 struct mntent *mntent;
1092 GUnixMountPoint *mount_point;
1095 read_file = get_fstab_file ();
1097 file = setmntent (read_file, "r");
1103 #ifdef HAVE_GETMNTENT_R
1104 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
1107 while ((mntent = getmntent (file)) != NULL)
1110 const char *device_path = NULL;
1111 gboolean is_read_only = FALSE;
1112 gboolean is_user_mountable = FALSE;
1113 gboolean is_loopback = FALSE;
1115 if ((strcmp (mntent->mnt_dir, "ignore") == 0) ||
1116 (strcmp (mntent->mnt_dir, "swap") == 0) ||
1117 (strcmp (mntent->mnt_dir, "none") == 0))
1120 #ifdef HAVE_HASMNTOPT
1121 /* We ignore bind fstab entries, as we ignore bind mounts anyway */
1122 if (hasmntopt (mntent, "bind"))
1126 if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
1127 device_path = _resolve_dev_root ();
1129 device_path = mntent->mnt_fsname;
1131 #ifdef HAVE_HASMNTOPT
1132 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
1133 is_read_only = TRUE;
1135 if (hasmntopt (mntent, "loop") != NULL)
1140 if ((mntent->mnt_type != NULL && strcmp ("supermount", mntent->mnt_type) == 0)
1141 #ifdef HAVE_HASMNTOPT
1142 || (hasmntopt (mntent, "user") != NULL
1143 && hasmntopt (mntent, "user") != hasmntopt (mntent, "user_xattr"))
1144 || hasmntopt (mntent, "pamconsole") != NULL
1145 || hasmntopt (mntent, "users") != NULL
1146 || hasmntopt (mntent, "owner") != NULL
1149 is_user_mountable = TRUE;
1151 mount_point = create_unix_mount_point (device_path,
1159 return_list = g_list_prepend (return_list, mount_point);
1164 #ifndef HAVE_GETMNTENT_R
1165 G_UNLOCK (getmntent);
1168 return g_list_reverse (return_list);
1171 #endif /* HAVE_LIBMOUNT */
1174 #elif defined (HAVE_SYS_MNTTAB_H)
1177 _g_get_unix_mount_points (void)
1179 struct mnttab mntent;
1182 GUnixMountPoint *mount_point;
1185 read_file = get_fstab_file ();
1187 file = setmntent (read_file, "r");
1194 while (! getmntent (file, &mntent))
1196 gboolean is_read_only = FALSE;
1197 gboolean is_user_mountable = FALSE;
1198 gboolean is_loopback = FALSE;
1200 if ((strcmp (mntent.mnt_mountp, "ignore") == 0) ||
1201 (strcmp (mntent.mnt_mountp, "swap") == 0) ||
1202 (strcmp (mntent.mnt_mountp, "none") == 0))
1205 #ifdef HAVE_HASMNTOPT
1206 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
1207 is_read_only = TRUE;
1209 if (hasmntopt (&mntent, "lofs") != NULL)
1213 if ((mntent.mnt_fstype != NULL)
1214 #ifdef HAVE_HASMNTOPT
1215 || (hasmntopt (&mntent, "user") != NULL
1216 && hasmntopt (&mntent, "user") != hasmntopt (&mntent, "user_xattr"))
1217 || hasmntopt (&mntent, "pamconsole") != NULL
1218 || hasmntopt (&mntent, "users") != NULL
1219 || hasmntopt (&mntent, "owner") != NULL
1222 is_user_mountable = TRUE;
1224 mount_point = create_unix_mount_point (mntent.mnt_special,
1232 return_list = g_list_prepend (return_list, mount_point);
1236 G_UNLOCK (getmntent);
1238 return g_list_reverse (return_list);
1241 /* mntctl.h (AIX) {{{2 */
1242 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
1244 /* functions to parse /etc/filesystems on aix */
1246 /* read character, ignoring comments (begin with '*', end with '\n' */
1248 aix_fs_getc (FILE *fd)
1252 while ((c = getc (fd)) == '*')
1254 while (((c = getc (fd)) != '\n') && (c != EOF))
1259 /* eat all continuous spaces in a file */
1261 aix_fs_ignorespace (FILE *fd)
1265 while ((c = aix_fs_getc (fd)) != EOF)
1267 if (!g_ascii_isspace (c))
1277 /* read one word from file */
1279 aix_fs_getword (FILE *fd,
1284 aix_fs_ignorespace (fd);
1286 while (((c = aix_fs_getc (fd)) != EOF) && !g_ascii_isspace (c))
1290 while (((c = aix_fs_getc (fd)) != EOF) && (c != '"'))
1302 char mnt_mount[PATH_MAX];
1303 char mnt_special[PATH_MAX];
1304 char mnt_fstype[16];
1305 char mnt_options[128];
1306 } AixMountTableEntry;
1308 /* read mount points properties */
1310 aix_fs_get (FILE *fd,
1311 AixMountTableEntry *prop)
1313 static char word[PATH_MAX] = { 0 };
1314 char value[PATH_MAX];
1319 if (aix_fs_getword (fd, word) == EOF)
1323 word[strlen(word) - 1] = 0;
1324 strcpy (prop->mnt_mount, word);
1326 /* read attributes and value */
1328 while (aix_fs_getword (fd, word) != EOF)
1330 /* test if is attribute or new stanza */
1331 if (word[strlen(word) - 1] == ':')
1335 aix_fs_getword (fd, value);
1338 aix_fs_getword (fd, value);
1340 if (strcmp (word, "dev") == 0)
1341 strcpy (prop->mnt_special, value);
1342 else if (strcmp (word, "vfs") == 0)
1343 strcpy (prop->mnt_fstype, value);
1344 else if (strcmp (word, "options") == 0)
1345 strcpy(prop->mnt_options, value);
1352 _g_get_unix_mount_points (void)
1354 struct mntent *mntent;
1357 GUnixMountPoint *mount_point;
1358 AixMountTableEntry mntent;
1361 read_file = get_fstab_file ();
1363 file = setmntent (read_file, "r");
1369 while (!aix_fs_get (file, &mntent))
1371 if (strcmp ("cdrfs", mntent.mnt_fstype) == 0)
1373 mount_point = create_unix_mount_point (mntent.mnt_special,
1381 return_list = g_list_prepend (return_list, mount_point);
1387 return g_list_reverse (return_list);
1390 #elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
1393 _g_get_unix_mount_points (void)
1395 struct fstab *fstab = NULL;
1396 GUnixMountPoint *mount_point;
1398 #ifdef HAVE_SYS_SYSCTL_H
1408 #ifdef HAVE_SYS_SYSCTL_H
1409 #if defined(HAVE_SYSCTLBYNAME)
1411 size_t len = sizeof(usermnt);
1413 sysctlbyname ("vfs.usermount", &usermnt, &len, NULL, 0);
1415 #elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
1418 size_t len = sizeof(usermnt);
1421 mib[1] = VFS_USERMOUNT;
1422 sysctl (mib, 2, &usermnt, &len, NULL, 0);
1424 #elif defined(CTL_KERN) && defined(KERN_USERMOUNT)
1427 size_t len = sizeof(usermnt);
1430 mib[1] = KERN_USERMOUNT;
1431 sysctl (mib, 2, &usermnt, &len, NULL, 0);
1436 while ((fstab = getfsent ()) != NULL)
1438 gboolean is_read_only = FALSE;
1439 gboolean is_user_mountable = FALSE;
1441 if (strcmp (fstab->fs_vfstype, "swap") == 0)
1444 if (strcmp (fstab->fs_type, "ro") == 0)
1445 is_read_only = TRUE;
1447 #ifdef HAVE_SYS_SYSCTL_H
1450 uid_t uid = getuid ();
1451 if (stat (fstab->fs_file, &sb) == 0)
1453 if (uid == 0 || sb.st_uid == uid)
1454 is_user_mountable = TRUE;
1459 mount_point = create_unix_mount_point (fstab->fs_spec,
1467 return_list = g_list_prepend (return_list, mount_point);
1472 return g_list_reverse (return_list);
1475 #elif defined(__INTERIX)
1477 _g_get_unix_mount_points (void)
1479 return _g_get_unix_mounts ();
1482 /* Common code {{{2 */
1484 #error No g_get_mount_table() implementation for system
1488 get_mounts_timestamp (void)
1490 const char *monitor_file;
1492 guint64 timestamp = 0;
1494 G_LOCK (proc_mounts_source);
1496 monitor_file = get_mtab_monitor_file ();
1497 /* Don't return mtime for /proc/ files */
1498 if (monitor_file && !g_str_has_prefix (monitor_file, "/proc/"))
1500 if (stat (monitor_file, &buf) == 0)
1501 timestamp = buf.st_mtime;
1503 else if (proc_mounts_watch_is_running ())
1505 /* it's being monitored by poll, so return mount_poller_time */
1506 timestamp = mount_poller_time;
1510 /* Case of /proc/ file not being monitored - Be on the safe side and
1511 * send a new timestamp to force g_unix_mounts_changed_since() to
1512 * return TRUE so any application caches depending on it (like eg.
1513 * the one in GIO) get invalidated and don't hold possibly outdated
1514 * data - see Bug 787731 */
1515 timestamp = g_get_monotonic_time ();
1518 G_UNLOCK (proc_mounts_source);
1524 get_mount_points_timestamp (void)
1526 const char *monitor_file;
1529 monitor_file = get_fstab_file ();
1532 if (stat (monitor_file, &buf) == 0)
1533 return (guint64)buf.st_mtime;
1539 * g_unix_mounts_get:
1540 * @time_read: (out) (optional): guint64 to contain a timestamp, or %NULL
1542 * Gets a #GList of #GUnixMountEntry containing the unix mounts.
1543 * If @time_read is set, it will be filled with the mount
1544 * timestamp, allowing for checking if the mounts have changed
1545 * with g_unix_mounts_changed_since().
1547 * Returns: (element-type GUnixMountEntry) (transfer full):
1548 * a #GList of the UNIX mounts.
1551 g_unix_mounts_get (guint64 *time_read)
1554 *time_read = get_mounts_timestamp ();
1556 return _g_get_unix_mounts ();
1561 * @mount_path: (type filename): path for a possible unix mount.
1562 * @time_read: (out) (optional): guint64 to contain a timestamp.
1564 * Gets a #GUnixMountEntry for a given mount path. If @time_read
1565 * is set, it will be filled with a unix timestamp for checking
1566 * if the mounts have changed since with g_unix_mounts_changed_since().
1568 * If more mounts have the same mount path, the last matching mount
1571 * Returns: (transfer full): a #GUnixMountEntry.
1574 g_unix_mount_at (const char *mount_path,
1578 GUnixMountEntry *mount_entry, *found;
1580 mounts = g_unix_mounts_get (time_read);
1583 for (l = mounts; l != NULL; l = l->next)
1585 mount_entry = l->data;
1587 if (strcmp (mount_path, mount_entry->mount_path) == 0)
1590 g_unix_mount_free (found);
1592 found = mount_entry;
1595 g_unix_mount_free (mount_entry);
1597 g_list_free (mounts);
1604 * @file_path: (type filename): file path on some unix mount.
1605 * @time_read: (out) (optional): guint64 to contain a timestamp.
1607 * Gets a #GUnixMountEntry for a given file path. If @time_read
1608 * is set, it will be filled with a unix timestamp for checking
1609 * if the mounts have changed since with g_unix_mounts_changed_since().
1611 * If more mounts have the same mount path, the last matching mount
1614 * Returns: (transfer full): a #GUnixMountEntry.
1619 g_unix_mount_for (const char *file_path,
1622 GUnixMountEntry *entry;
1624 g_return_val_if_fail (file_path != NULL, NULL);
1626 entry = g_unix_mount_at (file_path, time_read);
1631 topdir = _g_local_file_find_topdir_for (file_path);
1634 entry = g_unix_mount_at (topdir, time_read);
1643 * g_unix_mount_points_get:
1644 * @time_read: (out) (optional): guint64 to contain a timestamp.
1646 * Gets a #GList of #GUnixMountPoint containing the unix mount points.
1647 * If @time_read is set, it will be filled with the mount timestamp,
1648 * allowing for checking if the mounts have changed with
1649 * g_unix_mount_points_changed_since().
1651 * Returns: (element-type GUnixMountPoint) (transfer full):
1652 * a #GList of the UNIX mountpoints.
1655 g_unix_mount_points_get (guint64 *time_read)
1658 *time_read = get_mount_points_timestamp ();
1660 return _g_get_unix_mount_points ();
1664 * g_unix_mounts_changed_since:
1665 * @time: guint64 to contain a timestamp.
1667 * Checks if the unix mounts have changed since a given unix time.
1669 * Returns: %TRUE if the mounts have changed since @time.
1672 g_unix_mounts_changed_since (guint64 time)
1674 return get_mounts_timestamp () != time;
1678 * g_unix_mount_points_changed_since:
1679 * @time: guint64 to contain a timestamp.
1681 * Checks if the unix mount points have changed since a given unix time.
1683 * Returns: %TRUE if the mount points have changed since @time.
1686 g_unix_mount_points_changed_since (guint64 time)
1688 return get_mount_points_timestamp () != time;
1691 /* GUnixMountMonitor {{{1 */
1695 MOUNTPOINTS_CHANGED,
1699 static guint signals[LAST_SIGNAL];
1701 struct _GUnixMountMonitor {
1704 GMainContext *context;
1707 struct _GUnixMountMonitorClass {
1708 GObjectClass parent_class;
1712 G_DEFINE_TYPE (GUnixMountMonitor, g_unix_mount_monitor, G_TYPE_OBJECT)
1714 static GContextSpecificGroup mount_monitor_group;
1715 static GFileMonitor *fstab_monitor;
1716 static GFileMonitor *mtab_monitor;
1717 static GList *mount_poller_mounts;
1718 static guint mtab_file_changed_id;
1720 /* Called with proc_mounts_source lock held. */
1722 proc_mounts_watch_is_running (void)
1724 return proc_mounts_watch_source != NULL &&
1725 !g_source_is_destroyed (proc_mounts_watch_source);
1729 fstab_file_changed (GFileMonitor *monitor,
1732 GFileMonitorEvent event_type,
1735 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1736 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1737 event_type != G_FILE_MONITOR_EVENT_DELETED)
1740 g_context_specific_group_emit (&mount_monitor_group, signals[MOUNTPOINTS_CHANGED]);
1744 mtab_file_changed_cb (gpointer user_data)
1746 mtab_file_changed_id = 0;
1747 g_context_specific_group_emit (&mount_monitor_group, signals[MOUNTS_CHANGED]);
1749 return G_SOURCE_REMOVE;
1753 mtab_file_changed (GFileMonitor *monitor,
1756 GFileMonitorEvent event_type,
1759 GMainContext *context;
1762 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1763 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1764 event_type != G_FILE_MONITOR_EVENT_DELETED)
1767 /* Skip accumulated events from file monitor which we are not able to handle
1768 * in a real time instead of emitting mounts_changed signal several times.
1769 * This should behave equally to GIOChannel based monitoring. See Bug 792235.
1771 if (mtab_file_changed_id > 0)
1774 context = g_main_context_get_thread_default ();
1776 context = g_main_context_default ();
1778 source = g_idle_source_new ();
1779 g_source_set_priority (source, G_PRIORITY_DEFAULT);
1780 g_source_set_callback (source, mtab_file_changed_cb, NULL, NULL);
1781 g_source_set_name (source, "[gio] mtab_file_changed_cb");
1782 g_source_attach (source, context);
1783 g_source_unref (source);
1787 proc_mounts_changed (GIOChannel *channel,
1791 if (cond & G_IO_ERR)
1793 G_LOCK (proc_mounts_source);
1794 mount_poller_time = (guint64) g_get_monotonic_time ();
1795 G_UNLOCK (proc_mounts_source);
1797 g_context_specific_group_emit (&mount_monitor_group, signals[MOUNTS_CHANGED]);
1804 mount_change_poller (gpointer user_data)
1806 GList *current_mounts, *new_it, *old_it;
1807 gboolean has_changed = FALSE;
1809 current_mounts = _g_get_unix_mounts ();
1811 for ( new_it = current_mounts, old_it = mount_poller_mounts;
1812 new_it != NULL && old_it != NULL;
1813 new_it = g_list_next (new_it), old_it = g_list_next (old_it) )
1815 if (g_unix_mount_compare (new_it->data, old_it->data) != 0)
1821 if (!(new_it == NULL && old_it == NULL))
1824 g_list_free_full (mount_poller_mounts, (GDestroyNotify) g_unix_mount_free);
1826 mount_poller_mounts = current_mounts;
1830 G_LOCK (proc_mounts_source);
1831 mount_poller_time = (guint64) g_get_monotonic_time ();
1832 G_UNLOCK (proc_mounts_source);
1834 g_context_specific_group_emit (&mount_monitor_group, signals[MOUNTPOINTS_CHANGED]);
1842 mount_monitor_stop (void)
1846 g_file_monitor_cancel (fstab_monitor);
1847 g_object_unref (fstab_monitor);
1850 G_LOCK (proc_mounts_source);
1851 if (proc_mounts_watch_source != NULL)
1853 g_source_destroy (proc_mounts_watch_source);
1854 proc_mounts_watch_source = NULL;
1856 G_UNLOCK (proc_mounts_source);
1860 g_file_monitor_cancel (mtab_monitor);
1861 g_object_unref (mtab_monitor);
1864 if (mtab_file_changed_id)
1866 g_source_remove (mtab_file_changed_id);
1867 mtab_file_changed_id = 0;
1870 g_list_free_full (mount_poller_mounts, (GDestroyNotify) g_unix_mount_free);
1874 mount_monitor_start (void)
1878 if (get_fstab_file () != NULL)
1880 file = g_file_new_for_path (get_fstab_file ());
1881 fstab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1882 g_object_unref (file);
1884 g_signal_connect (fstab_monitor, "changed", (GCallback)fstab_file_changed, NULL);
1887 if (get_mtab_monitor_file () != NULL)
1889 const gchar *mtab_path;
1891 mtab_path = get_mtab_monitor_file ();
1892 /* Monitoring files in /proc/ is special - can't just use GFileMonitor.
1893 * See 'man proc' for more details.
1895 if (g_str_has_prefix (mtab_path, "/proc/"))
1897 GIOChannel *proc_mounts_channel;
1898 GError *error = NULL;
1899 proc_mounts_channel = g_io_channel_new_file (mtab_path, "r", &error);
1900 if (proc_mounts_channel == NULL)
1902 g_warning ("Error creating IO channel for %s: %s (%s, %d)", mtab_path,
1903 error->message, g_quark_to_string (error->domain), error->code);
1904 g_error_free (error);
1908 G_LOCK (proc_mounts_source);
1910 proc_mounts_watch_source = g_io_create_watch (proc_mounts_channel, G_IO_ERR);
1911 mount_poller_time = (guint64) g_get_monotonic_time ();
1912 g_source_set_callback (proc_mounts_watch_source,
1913 (GSourceFunc) proc_mounts_changed,
1915 g_source_attach (proc_mounts_watch_source,
1916 g_main_context_get_thread_default ());
1917 g_source_unref (proc_mounts_watch_source);
1918 g_io_channel_unref (proc_mounts_channel);
1920 G_UNLOCK (proc_mounts_source);
1925 file = g_file_new_for_path (mtab_path);
1926 mtab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1927 g_object_unref (file);
1928 g_signal_connect (mtab_monitor, "changed", (GCallback)mtab_file_changed, NULL);
1933 G_LOCK (proc_mounts_source);
1935 proc_mounts_watch_source = g_timeout_source_new_seconds (3);
1936 mount_poller_mounts = _g_get_unix_mounts ();
1937 mount_poller_time = (guint64)g_get_monotonic_time ();
1938 g_source_set_callback (proc_mounts_watch_source,
1939 mount_change_poller,
1941 g_source_attach (proc_mounts_watch_source,
1942 g_main_context_get_thread_default ());
1943 g_source_unref (proc_mounts_watch_source);
1945 G_UNLOCK (proc_mounts_source);
1950 g_unix_mount_monitor_finalize (GObject *object)
1952 GUnixMountMonitor *monitor;
1954 monitor = G_UNIX_MOUNT_MONITOR (object);
1956 g_context_specific_group_remove (&mount_monitor_group, monitor->context, monitor, mount_monitor_stop);
1958 G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize (object);
1962 g_unix_mount_monitor_class_init (GUnixMountMonitorClass *klass)
1964 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1966 gobject_class->finalize = g_unix_mount_monitor_finalize;
1969 * GUnixMountMonitor::mounts-changed:
1970 * @monitor: the object on which the signal is emitted
1972 * Emitted when the unix mounts have changed.
1974 signals[MOUNTS_CHANGED] =
1975 g_signal_new (I_("mounts-changed"),
1976 G_TYPE_FROM_CLASS (klass),
1984 * GUnixMountMonitor::mountpoints-changed:
1985 * @monitor: the object on which the signal is emitted
1987 * Emitted when the unix mount points have changed.
1989 signals[MOUNTPOINTS_CHANGED] =
1990 g_signal_new (I_("mountpoints-changed"),
1991 G_TYPE_FROM_CLASS (klass),
2000 g_unix_mount_monitor_init (GUnixMountMonitor *monitor)
2005 * g_unix_mount_monitor_set_rate_limit:
2006 * @mount_monitor: a #GUnixMountMonitor
2007 * @limit_msec: a integer with the limit in milliseconds to
2010 * This function does nothing.
2012 * Before 2.44, this was a partially-effective way of controlling the
2013 * rate at which events would be reported under some uncommon
2014 * circumstances. Since @mount_monitor is a singleton, it also meant
2015 * that calling this function would have side effects for other users of
2020 * Deprecated:2.44:This function does nothing. Don't call it.
2023 g_unix_mount_monitor_set_rate_limit (GUnixMountMonitor *mount_monitor,
2029 * g_unix_mount_monitor_get:
2031 * Gets the #GUnixMountMonitor for the current thread-default main
2034 * The mount monitor can be used to monitor for changes to the list of
2035 * mounted filesystems as well as the list of mount points (ie: fstab
2038 * You must only call g_object_unref() on the return value from under
2039 * the same main context as you called this function.
2041 * Returns: (transfer full): the #GUnixMountMonitor.
2046 g_unix_mount_monitor_get (void)
2048 return g_context_specific_group_get (&mount_monitor_group,
2049 G_TYPE_UNIX_MOUNT_MONITOR,
2050 G_STRUCT_OFFSET(GUnixMountMonitor, context),
2051 mount_monitor_start);
2055 * g_unix_mount_monitor_new:
2057 * Deprecated alias for g_unix_mount_monitor_get().
2059 * This function was never a true constructor, which is why it was
2062 * Returns: a #GUnixMountMonitor.
2064 * Deprecated:2.44:Use g_unix_mount_monitor_get() instead.
2067 g_unix_mount_monitor_new (void)
2069 return g_unix_mount_monitor_get ();
2072 /* GUnixMount {{{1 */
2074 * g_unix_mount_free:
2075 * @mount_entry: a #GUnixMountEntry.
2077 * Frees a unix mount.
2080 g_unix_mount_free (GUnixMountEntry *mount_entry)
2082 g_return_if_fail (mount_entry != NULL);
2084 g_free (mount_entry->mount_path);
2085 g_free (mount_entry->device_path);
2086 g_free (mount_entry->root_path);
2087 g_free (mount_entry->filesystem_type);
2088 g_free (mount_entry->options);
2089 g_free (mount_entry);
2093 * g_unix_mount_copy:
2094 * @mount_entry: a #GUnixMountEntry.
2096 * Makes a copy of @mount_entry.
2098 * Returns: (transfer full): a new #GUnixMountEntry
2103 g_unix_mount_copy (GUnixMountEntry *mount_entry)
2105 GUnixMountEntry *copy;
2107 g_return_val_if_fail (mount_entry != NULL, NULL);
2109 copy = g_new0 (GUnixMountEntry, 1);
2110 copy->mount_path = g_strdup (mount_entry->mount_path);
2111 copy->device_path = g_strdup (mount_entry->device_path);
2112 copy->root_path = g_strdup (mount_entry->root_path);
2113 copy->filesystem_type = g_strdup (mount_entry->filesystem_type);
2114 copy->options = g_strdup (mount_entry->options);
2115 copy->is_read_only = mount_entry->is_read_only;
2116 copy->is_system_internal = mount_entry->is_system_internal;
2122 * g_unix_mount_point_free:
2123 * @mount_point: unix mount point to free.
2125 * Frees a unix mount point.
2128 g_unix_mount_point_free (GUnixMountPoint *mount_point)
2130 g_return_if_fail (mount_point != NULL);
2132 g_free (mount_point->mount_path);
2133 g_free (mount_point->device_path);
2134 g_free (mount_point->filesystem_type);
2135 g_free (mount_point->options);
2136 g_free (mount_point);
2140 * g_unix_mount_point_copy:
2141 * @mount_point: a #GUnixMountPoint.
2143 * Makes a copy of @mount_point.
2145 * Returns: (transfer full): a new #GUnixMountPoint
2150 g_unix_mount_point_copy (GUnixMountPoint *mount_point)
2152 GUnixMountPoint *copy;
2154 g_return_val_if_fail (mount_point != NULL, NULL);
2156 copy = g_new0 (GUnixMountPoint, 1);
2157 copy->mount_path = g_strdup (mount_point->mount_path);
2158 copy->device_path = g_strdup (mount_point->device_path);
2159 copy->filesystem_type = g_strdup (mount_point->filesystem_type);
2160 copy->options = g_strdup (mount_point->options);
2161 copy->is_read_only = mount_point->is_read_only;
2162 copy->is_user_mountable = mount_point->is_user_mountable;
2163 copy->is_loopback = mount_point->is_loopback;
2169 * g_unix_mount_compare:
2170 * @mount1: first #GUnixMountEntry to compare.
2171 * @mount2: second #GUnixMountEntry to compare.
2173 * Compares two unix mounts.
2175 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
2176 * or less than @mount2, respectively.
2179 g_unix_mount_compare (GUnixMountEntry *mount1,
2180 GUnixMountEntry *mount2)
2184 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
2186 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
2190 res = g_strcmp0 (mount1->device_path, mount2->device_path);
2194 res = g_strcmp0 (mount1->root_path, mount2->root_path);
2198 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
2202 res = g_strcmp0 (mount1->options, mount2->options);
2206 res = mount1->is_read_only - mount2->is_read_only;
2214 * g_unix_mount_get_mount_path:
2215 * @mount_entry: input #GUnixMountEntry to get the mount path for.
2217 * Gets the mount path for a unix mount.
2219 * Returns: (type filename): the mount path for @mount_entry.
2222 g_unix_mount_get_mount_path (GUnixMountEntry *mount_entry)
2224 g_return_val_if_fail (mount_entry != NULL, NULL);
2226 return mount_entry->mount_path;
2230 * g_unix_mount_get_device_path:
2231 * @mount_entry: a #GUnixMount.
2233 * Gets the device path for a unix mount.
2235 * Returns: (type filename): a string containing the device path.
2238 g_unix_mount_get_device_path (GUnixMountEntry *mount_entry)
2240 g_return_val_if_fail (mount_entry != NULL, NULL);
2242 return mount_entry->device_path;
2246 * g_unix_mount_get_root_path:
2247 * @mount_entry: a #GUnixMountEntry.
2249 * Gets the root of the mount within the filesystem. This is useful e.g. for
2250 * mounts created by bind operation, or btrfs subvolumes.
2252 * For example, the root path is equal to "/" for mount created by
2253 * "mount /dev/sda1 /mnt/foo" and "/bar" for
2254 * "mount --bind /mnt/foo/bar /mnt/bar".
2256 * Returns: (nullable): a string containing the root, or %NULL if not supported.
2261 g_unix_mount_get_root_path (GUnixMountEntry *mount_entry)
2263 g_return_val_if_fail (mount_entry != NULL, NULL);
2265 return mount_entry->root_path;
2269 * g_unix_mount_get_fs_type:
2270 * @mount_entry: a #GUnixMount.
2272 * Gets the filesystem type for the unix mount.
2274 * Returns: a string containing the file system type.
2277 g_unix_mount_get_fs_type (GUnixMountEntry *mount_entry)
2279 g_return_val_if_fail (mount_entry != NULL, NULL);
2281 return mount_entry->filesystem_type;
2285 * g_unix_mount_get_options:
2286 * @mount_entry: a #GUnixMountEntry.
2288 * Gets a comma-separated list of mount options for the unix mount. For example,
2289 * `rw,relatime,seclabel,data=ordered`.
2291 * This is similar to g_unix_mount_point_get_options(), but it takes
2292 * a #GUnixMountEntry as an argument.
2294 * Returns: (nullable): a string containing the options, or %NULL if not
2300 g_unix_mount_get_options (GUnixMountEntry *mount_entry)
2302 g_return_val_if_fail (mount_entry != NULL, NULL);
2304 return mount_entry->options;
2308 * g_unix_mount_is_readonly:
2309 * @mount_entry: a #GUnixMount.
2311 * Checks if a unix mount is mounted read only.
2313 * Returns: %TRUE if @mount_entry is read only.
2316 g_unix_mount_is_readonly (GUnixMountEntry *mount_entry)
2318 g_return_val_if_fail (mount_entry != NULL, FALSE);
2320 return mount_entry->is_read_only;
2324 * g_unix_mount_is_system_internal:
2325 * @mount_entry: a #GUnixMount.
2327 * Checks if a Unix mount is a system mount. This is the Boolean OR of
2328 * g_unix_is_system_fs_type(), g_unix_is_system_device_path() and
2329 * g_unix_is_mount_path_system_internal() on @mount_entry’s properties.
2331 * The definition of what a ‘system’ mount entry is may change over time as new
2332 * file system types and device paths are ignored.
2334 * Returns: %TRUE if the unix mount is for a system path.
2337 g_unix_mount_is_system_internal (GUnixMountEntry *mount_entry)
2339 g_return_val_if_fail (mount_entry != NULL, FALSE);
2341 return mount_entry->is_system_internal;
2344 /* GUnixMountPoint {{{1 */
2346 * g_unix_mount_point_compare:
2347 * @mount1: a #GUnixMount.
2348 * @mount2: a #GUnixMount.
2350 * Compares two unix mount points.
2352 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
2353 * or less than @mount2, respectively.
2356 g_unix_mount_point_compare (GUnixMountPoint *mount1,
2357 GUnixMountPoint *mount2)
2361 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
2363 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
2367 res = g_strcmp0 (mount1->device_path, mount2->device_path);
2371 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
2375 res = g_strcmp0 (mount1->options, mount2->options);
2379 res = mount1->is_read_only - mount2->is_read_only;
2383 res = mount1->is_user_mountable - mount2->is_user_mountable;
2387 res = mount1->is_loopback - mount2->is_loopback;
2395 * g_unix_mount_point_get_mount_path:
2396 * @mount_point: a #GUnixMountPoint.
2398 * Gets the mount path for a unix mount point.
2400 * Returns: (type filename): a string containing the mount path.
2403 g_unix_mount_point_get_mount_path (GUnixMountPoint *mount_point)
2405 g_return_val_if_fail (mount_point != NULL, NULL);
2407 return mount_point->mount_path;
2411 * g_unix_mount_point_get_device_path:
2412 * @mount_point: a #GUnixMountPoint.
2414 * Gets the device path for a unix mount point.
2416 * Returns: (type filename): a string containing the device path.
2419 g_unix_mount_point_get_device_path (GUnixMountPoint *mount_point)
2421 g_return_val_if_fail (mount_point != NULL, NULL);
2423 return mount_point->device_path;
2427 * g_unix_mount_point_get_fs_type:
2428 * @mount_point: a #GUnixMountPoint.
2430 * Gets the file system type for the mount point.
2432 * Returns: a string containing the file system type.
2435 g_unix_mount_point_get_fs_type (GUnixMountPoint *mount_point)
2437 g_return_val_if_fail (mount_point != NULL, NULL);
2439 return mount_point->filesystem_type;
2443 * g_unix_mount_point_get_options:
2444 * @mount_point: a #GUnixMountPoint.
2446 * Gets the options for the mount point.
2448 * Returns: a string containing the options.
2453 g_unix_mount_point_get_options (GUnixMountPoint *mount_point)
2455 g_return_val_if_fail (mount_point != NULL, NULL);
2457 return mount_point->options;
2461 * g_unix_mount_point_is_readonly:
2462 * @mount_point: a #GUnixMountPoint.
2464 * Checks if a unix mount point is read only.
2466 * Returns: %TRUE if a mount point is read only.
2469 g_unix_mount_point_is_readonly (GUnixMountPoint *mount_point)
2471 g_return_val_if_fail (mount_point != NULL, FALSE);
2473 return mount_point->is_read_only;
2477 * g_unix_mount_point_is_user_mountable:
2478 * @mount_point: a #GUnixMountPoint.
2480 * Checks if a unix mount point is mountable by the user.
2482 * Returns: %TRUE if the mount point is user mountable.
2485 g_unix_mount_point_is_user_mountable (GUnixMountPoint *mount_point)
2487 g_return_val_if_fail (mount_point != NULL, FALSE);
2489 return mount_point->is_user_mountable;
2493 * g_unix_mount_point_is_loopback:
2494 * @mount_point: a #GUnixMountPoint.
2496 * Checks if a unix mount point is a loopback device.
2498 * Returns: %TRUE if the mount point is a loopback. %FALSE otherwise.
2501 g_unix_mount_point_is_loopback (GUnixMountPoint *mount_point)
2503 g_return_val_if_fail (mount_point != NULL, FALSE);
2505 return mount_point->is_loopback;
2508 static GUnixMountType
2509 guess_mount_type (const char *mount_path,
2510 const char *device_path,
2511 const char *filesystem_type)
2513 GUnixMountType type;
2516 type = G_UNIX_MOUNT_TYPE_UNKNOWN;
2518 if ((strcmp (filesystem_type, "udf") == 0) ||
2519 (strcmp (filesystem_type, "iso9660") == 0) ||
2520 (strcmp (filesystem_type, "cd9660") == 0))
2521 type = G_UNIX_MOUNT_TYPE_CDROM;
2522 else if ((strcmp (filesystem_type, "nfs") == 0) ||
2523 (strcmp (filesystem_type, "nfs4") == 0))
2524 type = G_UNIX_MOUNT_TYPE_NFS;
2525 else if (g_str_has_prefix (device_path, "/vol/dev/diskette/") ||
2526 g_str_has_prefix (device_path, "/dev/fd") ||
2527 g_str_has_prefix (device_path, "/dev/floppy"))
2528 type = G_UNIX_MOUNT_TYPE_FLOPPY;
2529 else if (g_str_has_prefix (device_path, "/dev/cdrom") ||
2530 g_str_has_prefix (device_path, "/dev/acd") ||
2531 g_str_has_prefix (device_path, "/dev/cd"))
2532 type = G_UNIX_MOUNT_TYPE_CDROM;
2533 else if (g_str_has_prefix (device_path, "/vol/"))
2535 const char *name = mount_path + strlen ("/");
2537 if (g_str_has_prefix (name, "cdrom"))
2538 type = G_UNIX_MOUNT_TYPE_CDROM;
2539 else if (g_str_has_prefix (name, "floppy") ||
2540 g_str_has_prefix (device_path, "/vol/dev/diskette/"))
2541 type = G_UNIX_MOUNT_TYPE_FLOPPY;
2542 else if (g_str_has_prefix (name, "rmdisk"))
2543 type = G_UNIX_MOUNT_TYPE_ZIP;
2544 else if (g_str_has_prefix (name, "jaz"))
2545 type = G_UNIX_MOUNT_TYPE_JAZ;
2546 else if (g_str_has_prefix (name, "memstick"))
2547 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
2551 basename = g_path_get_basename (mount_path);
2553 if (g_str_has_prefix (basename, "cdr") ||
2554 g_str_has_prefix (basename, "cdwriter") ||
2555 g_str_has_prefix (basename, "burn") ||
2556 g_str_has_prefix (basename, "dvdr"))
2557 type = G_UNIX_MOUNT_TYPE_CDROM;
2558 else if (g_str_has_prefix (basename, "floppy"))
2559 type = G_UNIX_MOUNT_TYPE_FLOPPY;
2560 else if (g_str_has_prefix (basename, "zip"))
2561 type = G_UNIX_MOUNT_TYPE_ZIP;
2562 else if (g_str_has_prefix (basename, "jaz"))
2563 type = G_UNIX_MOUNT_TYPE_JAZ;
2564 else if (g_str_has_prefix (basename, "camera"))
2565 type = G_UNIX_MOUNT_TYPE_CAMERA;
2566 else if (g_str_has_prefix (basename, "memstick") ||
2567 g_str_has_prefix (basename, "memory_stick") ||
2568 g_str_has_prefix (basename, "ram"))
2569 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
2570 else if (g_str_has_prefix (basename, "compact_flash"))
2571 type = G_UNIX_MOUNT_TYPE_CF;
2572 else if (g_str_has_prefix (basename, "smart_media"))
2573 type = G_UNIX_MOUNT_TYPE_SM;
2574 else if (g_str_has_prefix (basename, "sd_mmc"))
2575 type = G_UNIX_MOUNT_TYPE_SDMMC;
2576 else if (g_str_has_prefix (basename, "ipod"))
2577 type = G_UNIX_MOUNT_TYPE_IPOD;
2582 if (type == G_UNIX_MOUNT_TYPE_UNKNOWN)
2583 type = G_UNIX_MOUNT_TYPE_HD;
2589 * g_unix_mount_guess_type:
2590 * @mount_entry: a #GUnixMount.
2592 * Guesses the type of a unix mount. If the mount type cannot be
2593 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
2595 * Returns: a #GUnixMountType.
2597 static GUnixMountType
2598 g_unix_mount_guess_type (GUnixMountEntry *mount_entry)
2600 g_return_val_if_fail (mount_entry != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
2601 g_return_val_if_fail (mount_entry->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
2602 g_return_val_if_fail (mount_entry->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
2603 g_return_val_if_fail (mount_entry->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
2605 return guess_mount_type (mount_entry->mount_path,
2606 mount_entry->device_path,
2607 mount_entry->filesystem_type);
2611 * g_unix_mount_point_guess_type:
2612 * @mount_point: a #GUnixMountPoint.
2614 * Guesses the type of a unix mount point.
2615 * If the mount type cannot be determined,
2616 * returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
2618 * Returns: a #GUnixMountType.
2620 static GUnixMountType
2621 g_unix_mount_point_guess_type (GUnixMountPoint *mount_point)
2623 g_return_val_if_fail (mount_point != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
2624 g_return_val_if_fail (mount_point->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
2625 g_return_val_if_fail (mount_point->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
2626 g_return_val_if_fail (mount_point->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
2628 return guess_mount_type (mount_point->mount_path,
2629 mount_point->device_path,
2630 mount_point->filesystem_type);
2634 type_to_icon (GUnixMountType type, gboolean is_mount_point, gboolean use_symbolic)
2636 const char *icon_name;
2640 case G_UNIX_MOUNT_TYPE_HD:
2642 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2644 icon_name = use_symbolic ? "drive-harddisk-symbolic" : "drive-harddisk";
2646 case G_UNIX_MOUNT_TYPE_FLOPPY:
2647 case G_UNIX_MOUNT_TYPE_ZIP:
2648 case G_UNIX_MOUNT_TYPE_JAZ:
2650 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2652 icon_name = use_symbolic ? "media-removable-symbolic" : "media-floppy";
2654 case G_UNIX_MOUNT_TYPE_CDROM:
2656 icon_name = use_symbolic ? "drive-optical-symbolic" : "drive-optical";
2658 icon_name = use_symbolic ? "media-optical-symbolic" : "media-optical";
2660 case G_UNIX_MOUNT_TYPE_NFS:
2661 icon_name = use_symbolic ? "folder-remote-symbolic" : "folder-remote";
2663 case G_UNIX_MOUNT_TYPE_MEMSTICK:
2665 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2667 icon_name = use_symbolic ? "media-removable-symbolic" : "media-flash";
2669 case G_UNIX_MOUNT_TYPE_CAMERA:
2671 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2673 icon_name = use_symbolic ? "camera-photo-symbolic" : "camera-photo";
2675 case G_UNIX_MOUNT_TYPE_IPOD:
2677 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2679 icon_name = use_symbolic ? "multimedia-player-symbolic" : "multimedia-player";
2681 case G_UNIX_MOUNT_TYPE_UNKNOWN:
2684 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2686 icon_name = use_symbolic ? "drive-harddisk-symbolic" : "drive-harddisk";
2694 * g_unix_mount_guess_name:
2695 * @mount_entry: a #GUnixMountEntry
2697 * Guesses the name of a Unix mount.
2698 * The result is a translated string.
2700 * Returns: A newly allocated string that must
2701 * be freed with g_free()
2704 g_unix_mount_guess_name (GUnixMountEntry *mount_entry)
2708 if (strcmp (mount_entry->mount_path, "/") == 0)
2709 name = g_strdup (_("Filesystem root"));
2711 name = g_filename_display_basename (mount_entry->mount_path);
2717 * g_unix_mount_guess_icon:
2718 * @mount_entry: a #GUnixMountEntry
2720 * Guesses the icon of a Unix mount.
2722 * Returns: (transfer full): a #GIcon
2725 g_unix_mount_guess_icon (GUnixMountEntry *mount_entry)
2727 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE, FALSE));
2731 * g_unix_mount_guess_symbolic_icon:
2732 * @mount_entry: a #GUnixMountEntry
2734 * Guesses the symbolic icon of a Unix mount.
2736 * Returns: (transfer full): a #GIcon
2741 g_unix_mount_guess_symbolic_icon (GUnixMountEntry *mount_entry)
2743 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE, TRUE));
2747 * g_unix_mount_point_guess_name:
2748 * @mount_point: a #GUnixMountPoint
2750 * Guesses the name of a Unix mount point.
2751 * The result is a translated string.
2753 * Returns: A newly allocated string that must
2754 * be freed with g_free()
2757 g_unix_mount_point_guess_name (GUnixMountPoint *mount_point)
2761 if (strcmp (mount_point->mount_path, "/") == 0)
2762 name = g_strdup (_("Filesystem root"));
2764 name = g_filename_display_basename (mount_point->mount_path);
2770 * g_unix_mount_point_guess_icon:
2771 * @mount_point: a #GUnixMountPoint
2773 * Guesses the icon of a Unix mount point.
2775 * Returns: (transfer full): a #GIcon
2778 g_unix_mount_point_guess_icon (GUnixMountPoint *mount_point)
2780 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE, FALSE));
2784 * g_unix_mount_point_guess_symbolic_icon:
2785 * @mount_point: a #GUnixMountPoint
2787 * Guesses the symbolic icon of a Unix mount point.
2789 * Returns: (transfer full): a #GIcon
2794 g_unix_mount_point_guess_symbolic_icon (GUnixMountPoint *mount_point)
2796 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE, TRUE));
2800 * g_unix_mount_guess_can_eject:
2801 * @mount_entry: a #GUnixMountEntry
2803 * Guesses whether a Unix mount can be ejected.
2805 * Returns: %TRUE if @mount_entry is deemed to be ejectable.
2808 g_unix_mount_guess_can_eject (GUnixMountEntry *mount_entry)
2810 GUnixMountType guessed_type;
2812 guessed_type = g_unix_mount_guess_type (mount_entry);
2813 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
2814 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
2821 * g_unix_mount_guess_should_display:
2822 * @mount_entry: a #GUnixMountEntry
2824 * Guesses whether a Unix mount should be displayed in the UI.
2826 * Returns: %TRUE if @mount_entry is deemed to be displayable.
2829 g_unix_mount_guess_should_display (GUnixMountEntry *mount_entry)
2831 const char *mount_path;
2832 const gchar *user_name;
2833 gsize user_name_len;
2835 /* Never display internal mountpoints */
2836 if (g_unix_mount_is_system_internal (mount_entry))
2839 /* Only display things in /media (which are generally user mountable)
2840 and home dir (fuse stuff) and /run/media/$USER */
2841 mount_path = mount_entry->mount_path;
2842 if (mount_path != NULL)
2844 const gboolean running_as_root = (getuid () == 0);
2845 gboolean is_in_runtime_dir = FALSE;
2847 /* Hide mounts within a dot path, suppose it was a purpose to hide this mount */
2848 if (g_strstr_len (mount_path, -1, "/.") != NULL)
2851 /* Check /run/media/$USER/. If running as root, display any mounts below
2853 if (running_as_root)
2855 if (strncmp (mount_path, "/run/media/", strlen ("/run/media/")) == 0)
2856 is_in_runtime_dir = TRUE;
2860 user_name = g_get_user_name ();
2861 user_name_len = strlen (user_name);
2862 if (strncmp (mount_path, "/run/media/", strlen ("/run/media/")) == 0 &&
2863 strncmp (mount_path + strlen ("/run/media/"), user_name, user_name_len) == 0 &&
2864 mount_path[strlen ("/run/media/") + user_name_len] == '/')
2865 is_in_runtime_dir = TRUE;
2868 if (is_in_runtime_dir || g_str_has_prefix (mount_path, "/media/"))
2871 /* Avoid displaying mounts that are not accessible to the user.
2873 * See http://bugzilla.gnome.org/show_bug.cgi?id=526320 for why we
2874 * want to avoid g_access() for mount points which can potentially
2875 * block or fail stat()'ing, such as network mounts.
2877 path = g_path_get_dirname (mount_path);
2878 if (g_str_has_prefix (path, "/media/"))
2880 if (g_access (path, R_OK|X_OK) != 0)
2888 if (mount_entry->device_path && mount_entry->device_path[0] == '/')
2891 if (g_stat (mount_entry->device_path, &st) == 0 &&
2892 S_ISBLK(st.st_mode) &&
2893 g_access (mount_path, R_OK|X_OK) != 0)
2899 if (g_str_has_prefix (mount_path, g_get_home_dir ()) &&
2900 mount_path[strlen (g_get_home_dir())] == G_DIR_SEPARATOR)
2908 * g_unix_mount_point_guess_can_eject:
2909 * @mount_point: a #GUnixMountPoint
2911 * Guesses whether a Unix mount point can be ejected.
2913 * Returns: %TRUE if @mount_point is deemed to be ejectable.
2916 g_unix_mount_point_guess_can_eject (GUnixMountPoint *mount_point)
2918 GUnixMountType guessed_type;
2920 guessed_type = g_unix_mount_point_guess_type (mount_point);
2921 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
2922 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
2928 /* Utility functions {{{1 */
2930 #ifdef HAVE_MNTENT_H
2931 /* borrowed from gtk/gtkfilesystemunix.c in GTK+ on 02/23/2006 */
2933 _canonicalize_filename (gchar *filename)
2936 gboolean last_was_slash = FALSE;
2943 if (*p == G_DIR_SEPARATOR)
2945 if (!last_was_slash)
2946 *q++ = G_DIR_SEPARATOR;
2948 last_was_slash = TRUE;
2952 if (last_was_slash && *p == '.')
2954 if (*(p + 1) == G_DIR_SEPARATOR ||
2957 if (*(p + 1) == '\0')
2962 else if (*(p + 1) == '.' &&
2963 (*(p + 2) == G_DIR_SEPARATOR ||
2966 if (q > filename + 1)
2969 while (q > filename + 1 &&
2970 *(q - 1) != G_DIR_SEPARATOR)
2974 if (*(p + 2) == '\0')
2982 last_was_slash = FALSE;
2988 last_was_slash = FALSE;
2995 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
3002 _resolve_symlink (const char *file)
3010 f = g_strdup (file);
3012 while (g_file_test (f, G_FILE_TEST_IS_SYMLINK))
3014 link = g_file_read_link (f, &error);
3017 g_error_free (error);
3023 dir = g_path_get_dirname (f);
3024 f1 = g_strdup_printf ("%s/%s", dir, link);
3033 _canonicalize_filename (f);
3038 _resolve_dev_root (void)
3040 static gboolean have_real_dev_root = FALSE;
3041 static char real_dev_root[256];
3042 struct stat statbuf;
3044 /* see if it's cached already */
3045 if (have_real_dev_root)
3048 /* otherwise we're going to find it right away.. */
3049 have_real_dev_root = TRUE;
3051 if (stat ("/dev/root", &statbuf) == 0)
3053 if (! S_ISLNK (statbuf.st_mode))
3055 dev_t root_dev = statbuf.st_dev;
3058 /* see if device with similar major:minor as /dev/root is mention
3059 * in /etc/mtab (it usually is)
3061 f = fopen ("/etc/mtab", "r");
3064 struct mntent *entp;
3065 #ifdef HAVE_GETMNTENT_R
3068 while ((entp = getmntent_r (f, &ent, buf, sizeof (buf))) != NULL)
3072 while ((entp = getmntent (f)) != NULL)
3075 if (stat (entp->mnt_fsname, &statbuf) == 0 &&
3076 statbuf.st_dev == root_dev)
3078 strncpy (real_dev_root, entp->mnt_fsname, sizeof (real_dev_root) - 1);
3079 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
3087 #ifndef HAVE_GETMNTENT_R
3088 G_UNLOCK (getmntent);
3092 /* no, that didn't work.. next we could scan /dev ... but I digress.. */
3098 resolved = _resolve_symlink ("/dev/root");
3099 if (resolved != NULL)
3101 strncpy (real_dev_root, resolved, sizeof (real_dev_root) - 1);
3102 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
3110 strcpy (real_dev_root, "/dev/root");
3113 return real_dev_root;
3118 /* vim:set foldmethod=marker: */