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 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, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * 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>
34 #ifdef HAVE_SYS_POLL_H
49 #include "gunixmounts.h"
51 #include "gfilemonitor.h"
53 #include "gthemedicon.h"
57 static const char *_resolve_dev_root (void);
61 * @include: gio/gunixmounts.h
62 * @short_description: Unix Mounts
64 * Routines for managing mounted UNIX mount points and paths.
66 * Note that <filename><gio/gunixmounts.h></filename> belongs to the
67 * UNIX-specific GIO interfaces, thus you have to use the
68 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
73 * @G_UNIX_MOUNT_TYPE_UNKNOWN: Unknown UNIX mount type.
74 * @G_UNIX_MOUNT_TYPE_FLOPPY: Floppy disk UNIX mount type.
75 * @G_UNIX_MOUNT_TYPE_CDROM: CDROM UNIX mount type.
76 * @G_UNIX_MOUNT_TYPE_NFS: Network File System (NFS) UNIX mount type.
77 * @G_UNIX_MOUNT_TYPE_ZIP: ZIP UNIX mount type.
78 * @G_UNIX_MOUNT_TYPE_JAZ: JAZZ UNIX mount type.
79 * @G_UNIX_MOUNT_TYPE_MEMSTICK: Memory Stick UNIX mount type.
80 * @G_UNIX_MOUNT_TYPE_CF: Compact Flash UNIX mount type.
81 * @G_UNIX_MOUNT_TYPE_SM: Smart Media UNIX mount type.
82 * @G_UNIX_MOUNT_TYPE_SDMMC: SD/MMC UNIX mount type.
83 * @G_UNIX_MOUNT_TYPE_IPOD: iPod UNIX mount type.
84 * @G_UNIX_MOUNT_TYPE_CAMERA: Digital camera UNIX mount type.
85 * @G_UNIX_MOUNT_TYPE_HD: Hard drive UNIX mount type.
87 * Types of UNIX mounts.
90 G_UNIX_MOUNT_TYPE_UNKNOWN,
91 G_UNIX_MOUNT_TYPE_FLOPPY,
92 G_UNIX_MOUNT_TYPE_CDROM,
93 G_UNIX_MOUNT_TYPE_NFS,
94 G_UNIX_MOUNT_TYPE_ZIP,
95 G_UNIX_MOUNT_TYPE_JAZ,
96 G_UNIX_MOUNT_TYPE_MEMSTICK,
99 G_UNIX_MOUNT_TYPE_SDMMC,
100 G_UNIX_MOUNT_TYPE_IPOD,
101 G_UNIX_MOUNT_TYPE_CAMERA,
105 struct _GUnixMountEntry {
108 char *filesystem_type;
109 gboolean is_read_only;
110 gboolean is_system_internal;
113 struct _GUnixMountPoint {
116 char *filesystem_type;
117 gboolean is_read_only;
118 gboolean is_user_mountable;
119 gboolean is_loopback;
128 static guint signals[LAST_SIGNAL];
130 struct _GUnixMountMonitor {
133 GFileMonitor *fstab_monitor;
134 GFileMonitor *mtab_monitor;
137 struct _GUnixMountMonitorClass {
138 GObjectClass parent_class;
141 static GUnixMountMonitor *the_mount_monitor = NULL;
143 static GList *_g_get_unix_mounts (void);
144 static GList *_g_get_unix_mount_points (void);
146 G_DEFINE_TYPE (GUnixMountMonitor, g_unix_mount_monitor, G_TYPE_OBJECT);
148 #define MOUNT_POLL_INTERVAL 4000
150 #ifdef HAVE_SYS_MNTTAB_H
151 #define MNTOPT_RO "ro"
156 #elif defined (HAVE_SYS_MNTTAB_H)
157 #include <sys/mnttab.h>
160 #ifdef HAVE_SYS_VFSTAB_H
161 #include <sys/vfstab.h>
164 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
165 #include <sys/mntctl.h>
167 #include <sys/vmount.h>
171 #if defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
172 #include <sys/ucred.h>
173 #include <sys/mount.h>
175 #ifdef HAVE_SYS_SYSCTL_H
176 #include <sys/sysctl.h>
180 #ifndef HAVE_SETMNTENT
181 #define setmntent(f,m) fopen(f,m)
183 #ifndef HAVE_ENDMNTENT
184 #define endmntent(f) fclose(f)
188 is_in (const char *value, const char *set[])
191 for (i = 0; set[i] != NULL; i++)
193 if (strcmp (set[i], value) == 0)
200 * g_unix_is_mount_path_system_internal:
201 * @mount_path: a mount path, e.g. <filename>/media/disk</filename>
202 * or <filename>/usr</filename>
204 * Determines if @mount_path is considered an implementation of the
205 * OS. This is primarily used for hiding mountable and mounted volumes
206 * that only are used in the OS and has little to no relevance to the
209 * Returns: %TRUE if @mount_path is considered an implementation detail
213 g_unix_is_mount_path_system_internal (const char *mount_path)
215 const char *ignore_mountpoints[] = {
216 /* Includes all FHS 2.3 toplevel dirs and other specilized
217 * directories that we want to hide from the user.
219 "/", /* we already have "Filesystem root" in Nautilus */
237 "/var/log/audit", /* https://bugzilla.redhat.com/show_bug.cgi?id=333041 */
238 "/var/tmp", /* https://bugzilla.redhat.com/show_bug.cgi?id=335241 */
245 if (is_in (mount_path, ignore_mountpoints))
248 if (g_str_has_prefix (mount_path, "/dev") ||
249 g_str_has_prefix (mount_path, "/proc") ||
250 g_str_has_prefix (mount_path, "/sys"))
253 if (strstr (mount_path, "/.gvfs") != NULL)
260 guess_system_internal (const char *mountpoint,
264 const char *ignore_fs[] = {
285 const char *ignore_devices[] = {
295 if (is_in (fs, ignore_fs))
298 if (is_in (device, ignore_devices))
301 if (g_unix_is_mount_path_system_internal (mountpoint))
310 get_mtab_read_file (void)
314 return "/proc/mounts";
316 return _PATH_MOUNTED;
324 get_mtab_monitor_file (void)
327 return _PATH_MOUNTED;
333 #ifndef HAVE_GETMNTENT_R
334 G_LOCK_DEFINE_STATIC(getmntent);
338 _g_get_unix_mounts (void)
340 #ifdef HAVE_GETMNTENT_R
344 struct mntent *mntent;
347 GUnixMountEntry *mount_entry;
348 GHashTable *mounts_hash;
351 read_file = get_mtab_read_file ();
353 file = setmntent (read_file, "r");
359 mounts_hash = g_hash_table_new (g_str_hash, g_str_equal);
361 #ifdef HAVE_GETMNTENT_R
362 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
365 while ((mntent = getmntent (file)) != NULL)
368 /* ignore any mnt_fsname that is repeated and begins with a '/'
370 * We do this to avoid being fooled by --bind mounts, since
371 * these have the same device as the location they bind to.
372 * Its not an ideal solution to the problem, but it's likely that
373 * the most important mountpoint is first and the --bind ones after
374 * that aren't as important. So it should work.
376 * The '/' is to handle procfs, tmpfs and other no device mounts.
378 if (mntent->mnt_fsname != NULL &&
379 mntent->mnt_fsname[0] == '/' &&
380 g_hash_table_lookup (mounts_hash, mntent->mnt_fsname))
383 mount_entry = g_new0 (GUnixMountEntry, 1);
384 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
385 if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
386 mount_entry->device_path = g_strdup (_resolve_dev_root ());
388 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
389 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
391 #if defined (HAVE_HASMNTOPT)
392 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
393 mount_entry->is_read_only = TRUE;
396 mount_entry->is_system_internal =
397 guess_system_internal (mount_entry->mount_path,
398 mount_entry->filesystem_type,
399 mount_entry->device_path);
401 g_hash_table_insert (mounts_hash,
402 mount_entry->device_path,
403 mount_entry->device_path);
405 return_list = g_list_prepend (return_list, mount_entry);
407 g_hash_table_destroy (mounts_hash);
411 #ifndef HAVE_GETMNTENT_R
412 G_UNLOCK (getmntent);
415 return g_list_reverse (return_list);
418 #elif defined (HAVE_SYS_MNTTAB_H)
420 G_LOCK_DEFINE_STATIC(getmntent);
423 get_mtab_read_file (void)
426 return _PATH_MOUNTED;
428 return "/etc/mnttab";
433 get_mtab_monitor_file (void)
435 return get_mtab_read_file ();
439 _g_get_unix_mounts (void)
441 struct mnttab mntent;
444 GUnixMountEntry *mount_entry;
447 read_file = get_mtab_read_file ();
449 file = setmntent (read_file, "r");
456 while (! getmntent (file, &mntent))
458 mount_entry = g_new0 (GUnixMountEntry, 1);
460 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
461 mount_entry->device_path = g_strdup (mntent.mnt_special);
462 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
464 #if defined (HAVE_HASMNTOPT)
465 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
466 mount_entry->is_read_only = TRUE;
469 mount_entry->is_system_internal =
470 guess_system_internal (mount_entry->mount_path,
471 mount_entry->filesystem_type,
472 mount_entry->device_path);
474 return_list = g_list_prepend (return_list, mount_entry);
479 G_UNLOCK (getmntent);
481 return g_list_reverse (return_list);
484 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
487 get_mtab_monitor_file (void)
493 _g_get_unix_mounts (void)
495 struct vfs_ent *fs_info;
496 struct vmount *vmount_info;
498 unsigned int vmount_size;
502 if (mntctl (MCTL_QUERY, sizeof (vmount_size), &vmount_size) != 0)
504 g_warning ("Unable to know the number of mounted volumes\n");
509 vmount_info = (struct vmount*)g_malloc (vmount_size);
511 vmount_number = mntctl (MCTL_QUERY, vmount_size, vmount_info);
513 if (vmount_info->vmt_revision != VMT_REVISION)
514 g_warning ("Bad vmount structure revision number, want %d, got %d\n", VMT_REVISION, vmount_info->vmt_revision);
516 if (vmount_number < 0)
518 g_warning ("Unable to recover mounted volumes information\n");
520 g_free (vmount_info);
525 while (vmount_number > 0)
527 mount_entry = g_new0 (GUnixMountEntry, 1);
529 mount_entry->device_path = g_strdup (vmt2dataptr (vmount_info, VMT_OBJECT));
530 mount_entry->mount_path = g_strdup (vmt2dataptr (vmount_info, VMT_STUB));
531 /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
532 mount_entry->is_read_only = (vmount_info->vmt_flags & MNT_READONLY) ? 1 : 0;
534 fs_info = getvfsbytype (vmount_info->vmt_gfstype);
537 mount_entry->filesystem_type = g_strdup ("unknown");
539 mount_entry->filesystem_type = g_strdup (fs_info->vfsent_name);
541 mount_entry->is_system_internal =
542 guess_system_internal (mount_entry->mount_path,
543 mount_entry->filesystem_type,
544 mount_entry->device_path);
546 return_list = g_list_prepend (return_list, mount_entry);
548 vmount_info = (struct vmount *)( (char*)vmount_info
549 + vmount_info->vmt_length);
553 g_free (vmount_info);
555 return g_list_reverse (return_list);
558 #elif defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
561 get_mtab_monitor_file (void)
567 _g_get_unix_mounts (void)
569 struct statfs *mntent = NULL;
571 GUnixMountEntry *mount_entry;
574 /* Pass MNT_NOWAIT to avoid blocking trying to update NFS mounts. */
575 if ((num_mounts = getmntinfo (&mntent, MNT_NOWAIT)) == 0)
580 for (i = 0; i < num_mounts; i++)
582 mount_entry = g_new0 (GUnixMountEntry, 1);
584 mount_entry->mount_path = g_strdup (mntent[i].f_mntonname);
585 mount_entry->device_path = g_strdup (mntent[i].f_mntfromname);
586 mount_entry->filesystem_type = g_strdup (mntent[i].f_fstypename);
587 if (mntent[i].f_flags & MNT_RDONLY)
588 mount_entry->is_read_only = TRUE;
590 mount_entry->is_system_internal =
591 guess_system_internal (mount_entry->mount_path,
592 mount_entry->filesystem_type,
593 mount_entry->device_path);
595 return_list = g_list_prepend (return_list, mount_entry);
598 return g_list_reverse (return_list);
601 #error No _g_get_unix_mounts() implementation for system
604 /* _g_get_unix_mount_points():
606 * don't return swap and ignore mounts.
610 get_fstab_file (void)
612 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
614 return "/etc/filesystems";
615 #elif defined(_PATH_MNTTAB)
617 #elif defined(VFSTAB)
626 _g_get_unix_mount_points (void)
628 #ifdef HAVE_GETMNTENT_R
632 struct mntent *mntent;
635 GUnixMountPoint *mount_entry;
638 read_file = get_fstab_file ();
640 file = setmntent (read_file, "r");
646 #ifdef HAVE_GETMNTENT_R
647 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
650 while ((mntent = getmntent (file)) != NULL)
653 if ((strcmp (mntent->mnt_dir, "ignore") == 0) ||
654 (strcmp (mntent->mnt_dir, "swap") == 0))
657 mount_entry = g_new0 (GUnixMountPoint, 1);
658 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
659 if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
660 mount_entry->device_path = g_strdup (_resolve_dev_root ());
662 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
663 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
665 #ifdef HAVE_HASMNTOPT
666 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
667 mount_entry->is_read_only = TRUE;
669 if (hasmntopt (mntent, "loop") != NULL)
670 mount_entry->is_loopback = TRUE;
674 if ((mntent->mnt_type != NULL && strcmp ("supermount", mntent->mnt_type) == 0)
675 #ifdef HAVE_HASMNTOPT
676 || (hasmntopt (mntent, "user") != NULL
677 && hasmntopt (mntent, "user") != hasmntopt (mntent, "user_xattr"))
678 || hasmntopt (mntent, "pamconsole") != NULL
679 || hasmntopt (mntent, "users") != NULL
680 || hasmntopt (mntent, "owner") != NULL
683 mount_entry->is_user_mountable = TRUE;
685 return_list = g_list_prepend (return_list, mount_entry);
690 #ifndef HAVE_GETMNTENT_R
691 G_UNLOCK (getmntent);
694 return g_list_reverse (return_list);
697 #elif defined (HAVE_SYS_MNTTAB_H)
700 _g_get_unix_mount_points (void)
702 struct mnttab mntent;
705 GUnixMountPoint *mount_entry;
708 read_file = get_fstab_file ();
710 file = setmntent (read_file, "r");
717 while (! getmntent (file, &mntent))
719 if ((strcmp (mntent.mnt_mountp, "ignore") == 0) ||
720 (strcmp (mntent.mnt_mountp, "swap") == 0))
723 mount_entry = g_new0 (GUnixMountPoint, 1);
725 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
726 mount_entry->device_path = g_strdup (mntent.mnt_special);
727 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
729 #ifdef HAVE_HASMNTOPT
730 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
731 mount_entry->is_read_only = TRUE;
733 if (hasmntopt (&mntent, "lofs") != NULL)
734 mount_entry->is_loopback = TRUE;
737 if ((mntent.mnt_fstype != NULL)
738 #ifdef HAVE_HASMNTOPT
739 || (hasmntopt (&mntent, "user") != NULL
740 && hasmntopt (&mntent, "user") != hasmntopt (&mntent, "user_xattr"))
741 || hasmntopt (&mntent, "pamconsole") != NULL
742 || hasmntopt (&mntent, "users") != NULL
743 || hasmntopt (&mntent, "owner") != NULL
746 mount_entry->is_user_mountable = TRUE;
748 return_list = g_list_prepend (return_list, mount_entry);
752 G_UNLOCK (getmntent);
754 return g_list_reverse (return_list);
756 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
758 /* functions to parse /etc/filesystems on aix */
760 /* read character, ignoring comments (begin with '*', end with '\n' */
762 aix_fs_getc (FILE *fd)
766 while ((c = getc (fd)) == '*')
768 while (((c = getc (fd)) != '\n') && (c != EOF))
773 /* eat all continuous spaces in a file */
775 aix_fs_ignorespace (FILE *fd)
779 while ((c = aix_fs_getc (fd)) != EOF)
781 if (!g_ascii_isspace (c))
791 /* read one word from file */
793 aix_fs_getword (FILE *fd,
798 aix_fs_ignorespace (fd);
800 while (((c = aix_fs_getc (fd)) != EOF) && !g_ascii_isspace (c))
804 while (((c = aix_fs_getc (fd)) != EOF) && (c != '"'))
816 char mnt_mount[PATH_MAX];
817 char mnt_special[PATH_MAX];
819 char mnt_options[128];
820 } AixMountTableEntry;
822 /* read mount points properties */
824 aix_fs_get (FILE *fd,
825 AixMountTableEntry *prop)
827 static char word[PATH_MAX] = { 0 };
828 char value[PATH_MAX];
833 if (aix_fs_getword (fd, word) == EOF)
837 word[strlen(word) - 1] = 0;
838 strcpy (prop->mnt_mount, word);
840 /* read attributes and value */
842 while (aix_fs_getword (fd, word) != EOF)
844 /* test if is attribute or new stanza */
845 if (word[strlen(word) - 1] == ':')
849 aix_fs_getword (fd, value);
852 aix_fs_getword (fd, value);
854 if (strcmp (word, "dev") == 0)
855 strcpy (prop->mnt_special, value);
856 else if (strcmp (word, "vfs") == 0)
857 strcpy (prop->mnt_fstype, value);
858 else if (strcmp (word, "options") == 0)
859 strcpy(prop->mnt_options, value);
866 _g_get_unix_mount_points (void)
868 struct mntent *mntent;
871 GUnixMountPoint *mount_entry;
872 AixMountTableEntry mntent;
875 read_file = get_fstab_file ();
877 file = setmntent (read_file, "r");
883 while (!aix_fs_get (file, &mntent))
885 if (strcmp ("cdrfs", mntent.mnt_fstype) == 0)
887 mount_entry = g_new0 (GUnixMountPoint, 1);
889 mount_entry->mount_path = g_strdup (mntent.mnt_mount);
890 mount_entry->device_path = g_strdup (mntent.mnt_special);
891 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
892 mount_entry->is_read_only = TRUE;
893 mount_entry->is_user_mountable = TRUE;
895 return_list = g_list_prepend (return_list, mount_entry);
901 return g_list_reverse (return_list);
904 #elif defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
907 _g_get_unix_mount_points (void)
909 struct fstab *fstab = NULL;
910 GUnixMountPoint *mount_entry;
912 #ifdef HAVE_SYS_SYSCTL_H
914 size_t len = sizeof(usermnt);
923 #ifdef HAVE_SYS_SYSCTL_H
924 #if defined(HAVE_SYSCTLBYNAME)
925 sysctlbyname ("vfs.usermount", &usermnt, &len, NULL, 0);
926 #elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
931 mib[1] = VFS_USERMOUNT;
932 sysctl (mib, 2, &usermnt, &len, NULL, 0);
934 #elif defined(CTL_KERN) && defined(KERN_USERMOUNT)
939 mib[1] = KERN_USERMOUNT;
940 sysctl (mib, 2, &usermnt, &len, NULL, 0);
945 while ((fstab = getfsent ()) != NULL)
947 if (strcmp (fstab->fs_vfstype, "swap") == 0)
950 mount_entry = g_new0 (GUnixMountPoint, 1);
952 mount_entry->mount_path = g_strdup (fstab->fs_file);
953 mount_entry->device_path = g_strdup (fstab->fs_spec);
954 mount_entry->filesystem_type = g_strdup (fstab->fs_vfstype);
956 if (strcmp (fstab->fs_type, "ro") == 0)
957 mount_entry->is_read_only = TRUE;
959 #ifdef HAVE_SYS_SYSCTL_H
962 uid_t uid = getuid ();
963 if (stat (fstab->fs_file, &sb) == 0)
965 if (uid == 0 || sb.st_uid == uid)
966 mount_entry->is_user_mountable = TRUE;
971 return_list = g_list_prepend (return_list, mount_entry);
976 return g_list_reverse (return_list);
979 #error No g_get_mount_table() implementation for system
983 get_mounts_timestamp (void)
985 const char *monitor_file;
988 monitor_file = get_mtab_monitor_file ();
991 if (stat (monitor_file, &buf) == 0)
992 return (guint64)buf.st_mtime;
998 get_mount_points_timestamp (void)
1000 const char *monitor_file;
1003 monitor_file = get_fstab_file ();
1006 if (stat (monitor_file, &buf) == 0)
1007 return (guint64)buf.st_mtime;
1013 * g_unix_mounts_get:
1014 * @time_read: guint64 to contain a timestamp.
1016 * Gets a #GList of strings containing the unix mounts.
1017 * If @time_read is set, it will be filled with the mount
1018 * timestamp, allowing for checking if the mounts have changed
1019 * with g_unix_mounts_changed_since().
1021 * Returns: a #GList of the UNIX mounts.
1024 g_unix_mounts_get (guint64 *time_read)
1027 *time_read = get_mounts_timestamp ();
1029 return _g_get_unix_mounts ();
1034 * @mount_path: path for a possible unix mount.
1035 * @time_read: guint64 to contain a timestamp.
1037 * Gets a #GUnixMountEntry for a given mount path. If @time_read
1038 * is set, it will be filled with a unix timestamp for checking
1039 * if the mounts have changed since with g_unix_mounts_changed_since().
1041 * Returns: a #GUnixMount.
1044 g_unix_mount_at (const char *mount_path,
1048 GUnixMountEntry *mount_entry, *found;
1050 mounts = g_unix_mounts_get (time_read);
1053 for (l = mounts; l != NULL; l = l->next)
1055 mount_entry = l->data;
1057 if (!found && strcmp (mount_path, mount_entry->mount_path) == 0)
1058 found = mount_entry;
1060 g_unix_mount_free (mount_entry);
1062 g_list_free (mounts);
1068 * g_unix_mount_points_get:
1069 * @time_read: guint64 to contain a timestamp.
1071 * Gets a #GList of strings containing the unix mount points.
1072 * If @time_read is set, it will be filled with the mount timestamp,
1073 * allowing for checking if the mounts have changed with
1074 * g_unix_mounts_points_changed_since().
1076 * Returns: a #GList of the UNIX mountpoints.
1079 g_unix_mount_points_get (guint64 *time_read)
1082 *time_read = get_mount_points_timestamp ();
1084 return _g_get_unix_mount_points ();
1088 * g_unix_mounts_changed_since:
1089 * @time: guint64 to contain a timestamp.
1091 * Checks if the unix mounts have changed since a given unix time.
1093 * Returns: %TRUE if the mounts have changed since @time.
1096 g_unix_mounts_changed_since (guint64 time)
1098 return get_mounts_timestamp () != time;
1102 * g_unix_mount_points_changed_since:
1103 * @time: guint64 to contain a timestamp.
1105 * Checks if the unix mount points have changed since a given unix time.
1107 * Returns: %TRUE if the mount points have changed since @time.
1110 g_unix_mount_points_changed_since (guint64 time)
1112 return get_mount_points_timestamp () != time;
1116 g_unix_mount_monitor_finalize (GObject *object)
1118 GUnixMountMonitor *monitor;
1120 monitor = G_UNIX_MOUNT_MONITOR (object);
1122 if (monitor->fstab_monitor)
1124 g_file_monitor_cancel (monitor->fstab_monitor);
1125 g_object_unref (monitor->fstab_monitor);
1128 if (monitor->mtab_monitor)
1130 g_file_monitor_cancel (monitor->mtab_monitor);
1131 g_object_unref (monitor->mtab_monitor);
1134 the_mount_monitor = NULL;
1136 G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize (object);
1141 g_unix_mount_monitor_class_init (GUnixMountMonitorClass *klass)
1143 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1145 gobject_class->finalize = g_unix_mount_monitor_finalize;
1148 * GUnixMountMonitor::mounts-changed:
1149 * @monitor: the object on which the signal is emitted
1151 * Emitted when the unix mounts have changed.
1153 signals[MOUNTS_CHANGED] =
1154 g_signal_new ("mounts-changed",
1155 G_TYPE_FROM_CLASS (klass),
1159 g_cclosure_marshal_VOID__VOID,
1163 * GUnixMountMonitor::mountpoints-changed:
1164 * @monitor: the object on which the signal is emitted
1166 * Emitted when the unix mount points have changed.
1168 signals[MOUNTPOINTS_CHANGED] =
1169 g_signal_new ("mountpoints-changed",
1170 G_TYPE_FROM_CLASS (klass),
1174 g_cclosure_marshal_VOID__VOID,
1179 fstab_file_changed (GFileMonitor *monitor,
1182 GFileMonitorEvent event_type,
1185 GUnixMountMonitor *mount_monitor;
1187 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1188 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1189 event_type != G_FILE_MONITOR_EVENT_DELETED)
1192 mount_monitor = user_data;
1193 g_signal_emit (mount_monitor, signals[MOUNTPOINTS_CHANGED], 0);
1197 mtab_file_changed (GFileMonitor *monitor,
1200 GFileMonitorEvent event_type,
1203 GUnixMountMonitor *mount_monitor;
1205 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1206 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1207 event_type != G_FILE_MONITOR_EVENT_DELETED)
1210 mount_monitor = user_data;
1211 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1215 g_unix_mount_monitor_init (GUnixMountMonitor *monitor)
1219 if (get_fstab_file () != NULL)
1221 file = g_file_new_for_path (get_fstab_file ());
1222 monitor->fstab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1223 g_object_unref (file);
1225 g_signal_connect (monitor->fstab_monitor, "changed", (GCallback)fstab_file_changed, monitor);
1228 if (get_mtab_monitor_file () != NULL)
1230 file = g_file_new_for_path (get_mtab_monitor_file ());
1231 monitor->mtab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1232 g_object_unref (file);
1234 g_signal_connect (monitor->mtab_monitor, "changed", (GCallback)mtab_file_changed, monitor);
1239 * g_unix_mount_monitor_set_rate_limit:
1240 * @mount_monitor: a #GUnixMountMonitor
1241 * @limit_msec: a integer with the limit in milliseconds to
1244 * Sets the rate limit to which the @mount_monitor will report
1245 * consecutive change events to the mount and mount point entry files.
1250 g_unix_mount_monitor_set_rate_limit (GUnixMountMonitor *mount_monitor,
1253 g_return_if_fail (G_IS_UNIX_MOUNT_MONITOR (mount_monitor));
1255 if (mount_monitor->fstab_monitor != NULL)
1256 g_file_monitor_set_rate_limit (mount_monitor->fstab_monitor, limit_msec);
1258 if (mount_monitor->mtab_monitor != NULL)
1259 g_file_monitor_set_rate_limit (mount_monitor->mtab_monitor, limit_msec);
1263 * g_unix_mount_monitor_new:
1265 * Gets a new #GUnixMountMonitor. The default rate limit for which the
1266 * monitor will report consecutive changes for the mount and mount
1267 * point entry files is the default for a #GFileMonitor. Use
1268 * g_unix_mount_monitor_set_rate_limit() to change this.
1270 * Returns: a #GUnixMountMonitor.
1273 g_unix_mount_monitor_new (void)
1275 if (the_mount_monitor == NULL)
1277 the_mount_monitor = g_object_new (G_TYPE_UNIX_MOUNT_MONITOR, NULL);
1278 return the_mount_monitor;
1281 return g_object_ref (the_mount_monitor);
1285 * g_unix_mount_free:
1286 * @mount_entry: a #GUnixMount.
1288 * Frees a unix mount.
1291 g_unix_mount_free (GUnixMountEntry *mount_entry)
1293 g_return_if_fail (mount_entry != NULL);
1295 g_free (mount_entry->mount_path);
1296 g_free (mount_entry->device_path);
1297 g_free (mount_entry->filesystem_type);
1298 g_free (mount_entry);
1302 * g_unix_mount_point_free:
1303 * @mount_point: unix mount point to free.
1305 * Frees a unix mount point.
1308 g_unix_mount_point_free (GUnixMountPoint *mount_point)
1310 g_return_if_fail (mount_point != NULL);
1312 g_free (mount_point->mount_path);
1313 g_free (mount_point->device_path);
1314 g_free (mount_point->filesystem_type);
1315 g_free (mount_point);
1319 * g_unix_mount_compare:
1320 * @mount1: first #GUnixMountEntry to compare.
1321 * @mount2: second #GUnixMountEntry to compare.
1323 * Compares two unix mounts.
1325 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1326 * or less than @mount2, respectively.
1329 g_unix_mount_compare (GUnixMountEntry *mount1,
1330 GUnixMountEntry *mount2)
1334 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1336 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1340 res = g_strcmp0 (mount1->device_path, mount2->device_path);
1344 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1348 res = mount1->is_read_only - mount2->is_read_only;
1356 * g_unix_mount_get_mount_path:
1357 * @mount_entry: input #GUnixMountEntry to get the mount path for.
1359 * Gets the mount path for a unix mount.
1361 * Returns: the mount path for @mount_entry.
1364 g_unix_mount_get_mount_path (GUnixMountEntry *mount_entry)
1366 g_return_val_if_fail (mount_entry != NULL, NULL);
1368 return mount_entry->mount_path;
1372 * g_unix_mount_get_device_path:
1373 * @mount_entry: a #GUnixMount.
1375 * Gets the device path for a unix mount.
1377 * Returns: a string containing the device path.
1380 g_unix_mount_get_device_path (GUnixMountEntry *mount_entry)
1382 g_return_val_if_fail (mount_entry != NULL, NULL);
1384 return mount_entry->device_path;
1388 * g_unix_mount_get_fs_type:
1389 * @mount_entry: a #GUnixMount.
1391 * Gets the filesystem type for the unix mount.
1393 * Returns: a string containing the file system type.
1396 g_unix_mount_get_fs_type (GUnixMountEntry *mount_entry)
1398 g_return_val_if_fail (mount_entry != NULL, NULL);
1400 return mount_entry->filesystem_type;
1404 * g_unix_mount_is_readonly:
1405 * @mount_entry: a #GUnixMount.
1407 * Checks if a unix mount is mounted read only.
1409 * Returns: %TRUE if @mount_entry is read only.
1412 g_unix_mount_is_readonly (GUnixMountEntry *mount_entry)
1414 g_return_val_if_fail (mount_entry != NULL, FALSE);
1416 return mount_entry->is_read_only;
1420 * g_unix_mount_is_system_internal:
1421 * @mount_entry: a #GUnixMount.
1423 * Checks if a unix mount is a system path.
1425 * Returns: %TRUE if the unix mount is for a system path.
1428 g_unix_mount_is_system_internal (GUnixMountEntry *mount_entry)
1430 g_return_val_if_fail (mount_entry != NULL, FALSE);
1432 return mount_entry->is_system_internal;
1436 * g_unix_mount_point_compare:
1437 * @mount1: a #GUnixMount.
1438 * @mount2: a #GUnixMount.
1440 * Compares two unix mount points.
1442 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1443 * or less than @mount2, respectively.
1446 g_unix_mount_point_compare (GUnixMountPoint *mount1,
1447 GUnixMountPoint *mount2)
1451 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1453 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1457 res = g_strcmp0 (mount1->device_path, mount2->device_path);
1461 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1465 res = mount1->is_read_only - mount2->is_read_only;
1469 res = mount1->is_user_mountable - mount2->is_user_mountable;
1473 res = mount1->is_loopback - mount2->is_loopback;
1481 * g_unix_mount_point_get_mount_path:
1482 * @mount_point: a #GUnixMountPoint.
1484 * Gets the mount path for a unix mount point.
1486 * Returns: a string containing the mount path.
1489 g_unix_mount_point_get_mount_path (GUnixMountPoint *mount_point)
1491 g_return_val_if_fail (mount_point != NULL, NULL);
1493 return mount_point->mount_path;
1497 * g_unix_mount_point_get_device_path:
1498 * @mount_point: a #GUnixMountPoint.
1500 * Gets the device path for a unix mount point.
1502 * Returns: a string containing the device path.
1505 g_unix_mount_point_get_device_path (GUnixMountPoint *mount_point)
1507 g_return_val_if_fail (mount_point != NULL, NULL);
1509 return mount_point->device_path;
1513 * g_unix_mount_point_get_fs_type:
1514 * @mount_point: a #GUnixMountPoint.
1516 * Gets the file system type for the mount point.
1518 * Returns: a string containing the file system type.
1521 g_unix_mount_point_get_fs_type (GUnixMountPoint *mount_point)
1523 g_return_val_if_fail (mount_point != NULL, NULL);
1525 return mount_point->filesystem_type;
1529 * g_unix_mount_point_is_readonly:
1530 * @mount_point: a #GUnixMountPoint.
1532 * Checks if a unix mount point is read only.
1534 * Returns: %TRUE if a mount point is read only.
1537 g_unix_mount_point_is_readonly (GUnixMountPoint *mount_point)
1539 g_return_val_if_fail (mount_point != NULL, FALSE);
1541 return mount_point->is_read_only;
1545 * g_unix_mount_point_is_user_mountable:
1546 * @mount_point: a #GUnixMountPoint.
1548 * Checks if a unix mount point is mountable by the user.
1550 * Returns: %TRUE if the mount point is user mountable.
1553 g_unix_mount_point_is_user_mountable (GUnixMountPoint *mount_point)
1555 g_return_val_if_fail (mount_point != NULL, FALSE);
1557 return mount_point->is_user_mountable;
1561 * g_unix_mount_point_is_loopback:
1562 * @mount_point: a #GUnixMountPoint.
1564 * Checks if a unix mount point is a loopback device.
1566 * Returns: %TRUE if the mount point is a loopback. %FALSE otherwise.
1569 g_unix_mount_point_is_loopback (GUnixMountPoint *mount_point)
1571 g_return_val_if_fail (mount_point != NULL, FALSE);
1573 return mount_point->is_loopback;
1576 static GUnixMountType
1577 guess_mount_type (const char *mount_path,
1578 const char *device_path,
1579 const char *filesystem_type)
1581 GUnixMountType type;
1584 type = G_UNIX_MOUNT_TYPE_UNKNOWN;
1586 if ((strcmp (filesystem_type, "udf") == 0) ||
1587 (strcmp (filesystem_type, "iso9660") == 0) ||
1588 (strcmp (filesystem_type, "cd9660") == 0))
1589 type = G_UNIX_MOUNT_TYPE_CDROM;
1590 else if ((strcmp (filesystem_type, "nfs") == 0) ||
1591 (strcmp (filesystem_type, "nfs4") == 0))
1592 type = G_UNIX_MOUNT_TYPE_NFS;
1593 else if (g_str_has_prefix (device_path, "/vol/dev/diskette/") ||
1594 g_str_has_prefix (device_path, "/dev/fd") ||
1595 g_str_has_prefix (device_path, "/dev/floppy"))
1596 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1597 else if (g_str_has_prefix (device_path, "/dev/cdrom") ||
1598 g_str_has_prefix (device_path, "/dev/acd") ||
1599 g_str_has_prefix (device_path, "/dev/cd"))
1600 type = G_UNIX_MOUNT_TYPE_CDROM;
1601 else if (g_str_has_prefix (device_path, "/vol/"))
1603 const char *name = mount_path + strlen ("/");
1605 if (g_str_has_prefix (name, "cdrom"))
1606 type = G_UNIX_MOUNT_TYPE_CDROM;
1607 else if (g_str_has_prefix (name, "floppy") ||
1608 g_str_has_prefix (device_path, "/vol/dev/diskette/"))
1609 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1610 else if (g_str_has_prefix (name, "rmdisk"))
1611 type = G_UNIX_MOUNT_TYPE_ZIP;
1612 else if (g_str_has_prefix (name, "jaz"))
1613 type = G_UNIX_MOUNT_TYPE_JAZ;
1614 else if (g_str_has_prefix (name, "memstick"))
1615 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1619 basename = g_path_get_basename (mount_path);
1621 if (g_str_has_prefix (basename, "cdr") ||
1622 g_str_has_prefix (basename, "cdwriter") ||
1623 g_str_has_prefix (basename, "burn") ||
1624 g_str_has_prefix (basename, "dvdr"))
1625 type = G_UNIX_MOUNT_TYPE_CDROM;
1626 else if (g_str_has_prefix (basename, "floppy"))
1627 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1628 else if (g_str_has_prefix (basename, "zip"))
1629 type = G_UNIX_MOUNT_TYPE_ZIP;
1630 else if (g_str_has_prefix (basename, "jaz"))
1631 type = G_UNIX_MOUNT_TYPE_JAZ;
1632 else if (g_str_has_prefix (basename, "camera"))
1633 type = G_UNIX_MOUNT_TYPE_CAMERA;
1634 else if (g_str_has_prefix (basename, "memstick") ||
1635 g_str_has_prefix (basename, "memory_stick") ||
1636 g_str_has_prefix (basename, "ram"))
1637 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1638 else if (g_str_has_prefix (basename, "compact_flash"))
1639 type = G_UNIX_MOUNT_TYPE_CF;
1640 else if (g_str_has_prefix (basename, "smart_media"))
1641 type = G_UNIX_MOUNT_TYPE_SM;
1642 else if (g_str_has_prefix (basename, "sd_mmc"))
1643 type = G_UNIX_MOUNT_TYPE_SDMMC;
1644 else if (g_str_has_prefix (basename, "ipod"))
1645 type = G_UNIX_MOUNT_TYPE_IPOD;
1650 if (type == G_UNIX_MOUNT_TYPE_UNKNOWN)
1651 type = G_UNIX_MOUNT_TYPE_HD;
1657 * g_unix_mount_guess_type:
1658 * @mount_entry: a #GUnixMount.
1660 * Guesses the type of a unix mount. If the mount type cannot be
1661 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1663 * Returns: a #GUnixMountType.
1665 static GUnixMountType
1666 g_unix_mount_guess_type (GUnixMountEntry *mount_entry)
1668 g_return_val_if_fail (mount_entry != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1669 g_return_val_if_fail (mount_entry->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1670 g_return_val_if_fail (mount_entry->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1671 g_return_val_if_fail (mount_entry->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1673 return guess_mount_type (mount_entry->mount_path,
1674 mount_entry->device_path,
1675 mount_entry->filesystem_type);
1679 * g_unix_mount_point_guess_type:
1680 * @mount_point: a #GUnixMountPoint.
1682 * Guesses the type of a unix mount point.
1683 * If the mount type cannot be determined,
1684 * returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1686 * Returns: a #GUnixMountType.
1688 static GUnixMountType
1689 g_unix_mount_point_guess_type (GUnixMountPoint *mount_point)
1691 g_return_val_if_fail (mount_point != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1692 g_return_val_if_fail (mount_point->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1693 g_return_val_if_fail (mount_point->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1694 g_return_val_if_fail (mount_point->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1696 return guess_mount_type (mount_point->mount_path,
1697 mount_point->device_path,
1698 mount_point->filesystem_type);
1702 type_to_icon (GUnixMountType type, gboolean is_mount_point)
1704 const char *icon_name;
1708 case G_UNIX_MOUNT_TYPE_HD:
1710 icon_name = "drive-removable-media";
1712 icon_name = "drive-harddisk";
1714 case G_UNIX_MOUNT_TYPE_FLOPPY:
1715 case G_UNIX_MOUNT_TYPE_ZIP:
1716 case G_UNIX_MOUNT_TYPE_JAZ:
1718 icon_name = "drive-removable-media";
1720 icon_name = "media-floppy";
1722 case G_UNIX_MOUNT_TYPE_CDROM:
1724 icon_name = "drive-optical";
1726 icon_name = "media-optical";
1728 case G_UNIX_MOUNT_TYPE_NFS:
1729 /* TODO: Would like a better icon here... */
1731 icon_name = "drive-removable-media";
1733 icon_name = "drive-harddisk";
1735 case G_UNIX_MOUNT_TYPE_MEMSTICK:
1737 icon_name = "drive-removable-media";
1739 icon_name = "media-flash";
1741 case G_UNIX_MOUNT_TYPE_CAMERA:
1743 icon_name = "drive-removable-media";
1745 icon_name = "camera-photo";
1747 case G_UNIX_MOUNT_TYPE_IPOD:
1749 icon_name = "drive-removable-media";
1751 icon_name = "multimedia-player";
1753 case G_UNIX_MOUNT_TYPE_UNKNOWN:
1756 icon_name = "drive-removable-media";
1758 icon_name = "drive-harddisk";
1766 * g_unix_mount_guess_name:
1767 * @mount_entry: a #GUnixMountEntry
1769 * Guesses the name of a Unix mount.
1770 * The result is a translated string.
1772 * Returns: A newly allocated string that must
1773 * be freed with g_free()
1776 g_unix_mount_guess_name (GUnixMountEntry *mount_entry)
1780 if (strcmp (mount_entry->mount_path, "/") == 0)
1781 name = g_strdup (_("Filesystem root"));
1783 name = g_filename_display_basename (mount_entry->mount_path);
1789 * g_unix_mount_guess_icon:
1790 * @mount_entry: a #GUnixMountEntry
1792 * Guesses the icon of a Unix mount.
1797 g_unix_mount_guess_icon (GUnixMountEntry *mount_entry)
1799 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE));
1803 * g_unix_mount_point_guess_name:
1804 * @mount_point: a #GUnixMountPoint
1806 * Guesses the name of a Unix mount point.
1807 * The result is a translated string.
1809 * Returns: A newly allocated string that must
1810 * be freed with g_free()
1813 g_unix_mount_point_guess_name (GUnixMountPoint *mount_point)
1817 if (strcmp (mount_point->mount_path, "/") == 0)
1818 name = g_strdup (_("Filesystem root"));
1820 name = g_filename_display_basename (mount_point->mount_path);
1826 * g_unix_mount_point_guess_icon:
1827 * @mount_point: a #GUnixMountPoint
1829 * Guesses the icon of a Unix mount point.
1834 g_unix_mount_point_guess_icon (GUnixMountPoint *mount_point)
1836 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE));
1840 * g_unix_mount_guess_can_eject:
1841 * @mount_entry: a #GUnixMountEntry
1843 * Guesses whether a Unix mount can be ejected.
1845 * Returns: %TRUE if @mount_entry is deemed to be ejectable.
1848 g_unix_mount_guess_can_eject (GUnixMountEntry *mount_entry)
1850 GUnixMountType guessed_type;
1852 guessed_type = g_unix_mount_guess_type (mount_entry);
1853 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
1854 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
1861 * g_unix_mount_guess_should_display:
1862 * @mount_entry: a #GUnixMountEntry
1864 * Guesses whether a Unix mount should be displayed in the UI.
1866 * Returns: %TRUE if @mount_entry is deemed to be displayable.
1869 g_unix_mount_guess_should_display (GUnixMountEntry *mount_entry)
1871 const char *mount_path;
1873 /* Never display internal mountpoints */
1874 if (g_unix_mount_is_system_internal (mount_entry))
1877 /* Only display things in /media (which are generally user mountable)
1878 and home dir (fuse stuff) */
1879 mount_path = mount_entry->mount_path;
1880 if (mount_path != NULL)
1882 if (g_str_has_prefix (mount_path, "/media/"))
1885 /* Avoid displaying mounts that are not accessible to the user.
1887 * See http://bugzilla.gnome.org/show_bug.cgi?id=526320 for why we
1888 * want to avoid g_access() for mount points which can potentially
1889 * block or fail stat()'ing, such as network mounts.
1891 path = g_path_get_dirname (mount_path);
1892 if (g_str_has_prefix (path, "/media/"))
1894 if (g_access (path, R_OK|X_OK) != 0)
1902 if (mount_entry->device_path && mount_entry->device_path[0] == '/')
1905 if (g_stat (mount_entry->device_path, &st) == 0 &&
1906 S_ISBLK(st.st_mode) &&
1907 g_access (mount_path, R_OK|X_OK) != 0)
1913 if (g_str_has_prefix (mount_path, g_get_home_dir ()) &&
1914 mount_path[strlen (g_get_home_dir())] == G_DIR_SEPARATOR)
1922 * g_unix_mount_point_guess_can_eject:
1923 * @mount_point: a #GUnixMountPoint
1925 * Guesses whether a Unix mount point can be ejected.
1927 * Returns: %TRUE if @mount_point is deemed to be ejectable.
1930 g_unix_mount_point_guess_can_eject (GUnixMountPoint *mount_point)
1932 GUnixMountType guessed_type;
1934 guessed_type = g_unix_mount_point_guess_type (mount_point);
1935 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
1936 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
1943 /* borrowed from gtk/gtkfilesystemunix.c in GTK+ on 02/23/2006 */
1945 _canonicalize_filename (gchar *filename)
1948 gboolean last_was_slash = FALSE;
1955 if (*p == G_DIR_SEPARATOR)
1957 if (!last_was_slash)
1958 *q++ = G_DIR_SEPARATOR;
1960 last_was_slash = TRUE;
1964 if (last_was_slash && *p == '.')
1966 if (*(p + 1) == G_DIR_SEPARATOR ||
1969 if (*(p + 1) == '\0')
1974 else if (*(p + 1) == '.' &&
1975 (*(p + 2) == G_DIR_SEPARATOR ||
1978 if (q > filename + 1)
1981 while (q > filename + 1 &&
1982 *(q - 1) != G_DIR_SEPARATOR)
1986 if (*(p + 2) == '\0')
1994 last_was_slash = FALSE;
2000 last_was_slash = FALSE;
2007 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
2014 _resolve_symlink (const char *file)
2022 f = g_strdup (file);
2024 while (g_file_test (f, G_FILE_TEST_IS_SYMLINK))
2026 link = g_file_read_link (f, &error);
2029 g_error_free (error);
2035 dir = g_path_get_dirname (f);
2036 f1 = g_strdup_printf ("%s/%s", dir, link);
2045 _canonicalize_filename (f);
2049 #ifdef HAVE_MNTENT_H
2051 _resolve_dev_root (void)
2053 static gboolean have_real_dev_root = FALSE;
2054 static char real_dev_root[256];
2055 struct stat statbuf;
2057 /* see if it's cached already */
2058 if (have_real_dev_root)
2061 /* otherwise we're going to find it right away.. */
2062 have_real_dev_root = TRUE;
2064 if (stat ("/dev/root", &statbuf) == 0)
2066 if (! S_ISLNK (statbuf.st_mode))
2068 dev_t root_dev = statbuf.st_dev;
2072 /* see if device with similar major:minor as /dev/root is mention
2073 * in /etc/mtab (it usually is)
2075 f = fopen ("/etc/mtab", "r");
2078 struct mntent *entp;
2079 #ifdef HAVE_GETMNTENT_R
2081 while ((entp = getmntent_r (f, &ent, buf, sizeof (buf))) != NULL)
2085 while ((entp = getmntent (f)) != NULL)
2088 if (stat (entp->mnt_fsname, &statbuf) == 0 &&
2089 statbuf.st_dev == root_dev)
2091 strncpy (real_dev_root, entp->mnt_fsname, sizeof (real_dev_root) - 1);
2092 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2100 #ifndef HAVE_GETMNTENT_R
2101 G_UNLOCK (getmntent);
2105 /* no, that didn't work.. next we could scan /dev ... but I digress.. */
2111 resolved = _resolve_symlink ("/dev/root");
2112 if (resolved != NULL)
2114 strncpy (real_dev_root, resolved, sizeof (real_dev_root) - 1);
2115 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2123 strcpy (real_dev_root, "/dev/root");
2126 return real_dev_root;
2130 #define __G_UNIX_MOUNTS_C__
2131 #include "gioaliasdef.c"