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 ()
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);
554 g_free (vmount_info);
556 return g_list_reverse (return_list);
559 #elif defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
562 get_mtab_monitor_file (void)
568 _g_get_unix_mounts (void)
570 struct statfs *mntent = NULL;
572 GUnixMountEntry *mount_entry;
575 /* Pass MNT_NOWAIT to avoid blocking trying to update NFS mounts. */
576 if ((num_mounts = getmntinfo (&mntent, MNT_NOWAIT)) == 0)
581 for (i = 0; i < num_mounts; i++)
583 mount_entry = g_new0 (GUnixMountEntry, 1);
585 mount_entry->mount_path = g_strdup (mntent[i].f_mntonname);
586 mount_entry->device_path = g_strdup (mntent[i].f_mntfromname);
587 mount_entry->filesystem_type = g_strdup (mntent[i].f_fstypename);
588 if (mntent[i].f_flags & MNT_RDONLY)
589 mount_entry->is_read_only = TRUE;
591 mount_entry->is_system_internal =
592 guess_system_internal (mount_entry->mount_path,
593 mount_entry->filesystem_type,
594 mount_entry->device_path);
596 return_list = g_list_prepend (return_list, mount_entry);
599 return g_list_reverse (return_list);
602 #error No _g_get_unix_mounts() implementation for system
605 /* _g_get_unix_mount_points():
607 * don't return swap and ignore mounts.
611 get_fstab_file (void)
613 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
615 return "/etc/filesystems";
616 #elif defined(_PATH_MNTTAB)
618 #elif defined(VFSTAB)
627 _g_get_unix_mount_points (void)
629 #ifdef HAVE_GETMNTENT_R
633 struct mntent *mntent;
636 GUnixMountPoint *mount_entry;
639 read_file = get_fstab_file ();
641 file = setmntent (read_file, "r");
647 #ifdef HAVE_GETMNTENT_R
648 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
651 while ((mntent = getmntent (file)) != NULL)
654 if ((strcmp (mntent->mnt_dir, "ignore") == 0) ||
655 (strcmp (mntent->mnt_dir, "swap") == 0))
658 mount_entry = g_new0 (GUnixMountPoint, 1);
659 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
660 if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
661 mount_entry->device_path = g_strdup (_resolve_dev_root ());
663 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
664 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
666 #ifdef HAVE_HASMNTOPT
667 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
668 mount_entry->is_read_only = TRUE;
670 if (hasmntopt (mntent, "loop") != NULL)
671 mount_entry->is_loopback = TRUE;
675 if ((mntent->mnt_type != NULL && strcmp ("supermount", mntent->mnt_type) == 0)
676 #ifdef HAVE_HASMNTOPT
677 || (hasmntopt (mntent, "user") != NULL
678 && hasmntopt (mntent, "user") != hasmntopt (mntent, "user_xattr"))
679 || hasmntopt (mntent, "pamconsole") != NULL
680 || hasmntopt (mntent, "users") != NULL
681 || hasmntopt (mntent, "owner") != NULL
684 mount_entry->is_user_mountable = TRUE;
686 return_list = g_list_prepend (return_list, mount_entry);
691 #ifndef HAVE_GETMNTENT_R
692 G_UNLOCK (getmntent);
695 return g_list_reverse (return_list);
698 #elif defined (HAVE_SYS_MNTTAB_H)
701 _g_get_unix_mount_points (void)
703 struct mnttab mntent;
706 GUnixMountPoint *mount_entry;
709 read_file = get_fstab_file ();
711 file = setmntent (read_file, "r");
718 while (! getmntent (file, &mntent))
720 if ((strcmp (mntent.mnt_mountp, "ignore") == 0) ||
721 (strcmp (mntent.mnt_mountp, "swap") == 0))
724 mount_entry = g_new0 (GUnixMountPoint, 1);
726 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
727 mount_entry->device_path = g_strdup (mntent.mnt_special);
728 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
730 #ifdef HAVE_HASMNTOPT
731 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
732 mount_entry->is_read_only = TRUE;
734 if (hasmntopt (&mntent, "lofs") != NULL)
735 mount_entry->is_loopback = TRUE;
738 if ((mntent.mnt_fstype != NULL)
739 #ifdef HAVE_HASMNTOPT
740 || (hasmntopt (&mntent, "user") != NULL
741 && hasmntopt (&mntent, "user") != hasmntopt (&mntent, "user_xattr"))
742 || hasmntopt (&mntent, "pamconsole") != NULL
743 || hasmntopt (&mntent, "users") != NULL
744 || hasmntopt (&mntent, "owner") != NULL
747 mount_entry->is_user_mountable = TRUE;
750 return_list = g_list_prepend (return_list, mount_entry);
754 G_UNLOCK (getmntent);
756 return g_list_reverse (return_list);
758 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
760 /* functions to parse /etc/filesystems on aix */
762 /* read character, ignoring comments (begin with '*', end with '\n' */
764 aix_fs_getc (FILE *fd)
768 while ((c = getc (fd)) == '*')
770 while (((c = getc (fd)) != '\n') && (c != EOF))
775 /* eat all continuous spaces in a file */
777 aix_fs_ignorespace (FILE *fd)
781 while ((c = aix_fs_getc (fd)) != EOF)
783 if (!g_ascii_isspace (c))
793 /* read one word from file */
795 aix_fs_getword (FILE *fd,
800 aix_fs_ignorespace (fd);
802 while (((c = aix_fs_getc (fd)) != EOF) && !g_ascii_isspace (c))
806 while (((c = aix_fs_getc (fd)) != EOF) && (c != '"'))
818 char mnt_mount[PATH_MAX];
819 char mnt_special[PATH_MAX];
821 char mnt_options[128];
822 } AixMountTableEntry;
824 /* read mount points properties */
826 aix_fs_get (FILE *fd,
827 AixMountTableEntry *prop)
829 static char word[PATH_MAX] = { 0 };
830 char value[PATH_MAX];
835 if (aix_fs_getword (fd, word) == EOF)
839 word[strlen(word) - 1] = 0;
840 strcpy (prop->mnt_mount, word);
842 /* read attributes and value */
844 while (aix_fs_getword (fd, word) != EOF)
846 /* test if is attribute or new stanza */
847 if (word[strlen(word) - 1] == ':')
851 aix_fs_getword (fd, value);
854 aix_fs_getword (fd, value);
856 if (strcmp (word, "dev") == 0)
857 strcpy (prop->mnt_special, value);
858 else if (strcmp (word, "vfs") == 0)
859 strcpy (prop->mnt_fstype, value);
860 else if (strcmp (word, "options") == 0)
861 strcpy(prop->mnt_options, value);
868 _g_get_unix_mount_points (void)
870 struct mntent *mntent;
873 GUnixMountPoint *mount_entry;
874 AixMountTableEntry mntent;
877 read_file = get_fstab_file ();
879 file = setmntent (read_file, "r");
885 while (!aix_fs_get (file, &mntent))
887 if (strcmp ("cdrfs", mntent.mnt_fstype) == 0)
889 mount_entry = g_new0 (GUnixMountPoint, 1);
892 mount_entry->mount_path = g_strdup (mntent.mnt_mount);
893 mount_entry->device_path = g_strdup (mntent.mnt_special);
894 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
895 mount_entry->is_read_only = TRUE;
896 mount_entry->is_user_mountable = TRUE;
898 return_list = g_list_prepend (return_list, mount_entry);
904 return g_list_reverse (return_list);
907 #elif defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
910 _g_get_unix_mount_points (void)
912 struct fstab *fstab = NULL;
913 GUnixMountPoint *mount_entry;
915 #ifdef HAVE_SYS_SYSCTL_H
917 size_t len = sizeof(usermnt);
926 #ifdef HAVE_SYS_SYSCTL_H
927 #if defined(HAVE_SYSCTLBYNAME)
928 sysctlbyname ("vfs.usermount", &usermnt, &len, NULL, 0);
929 #elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
934 mib[1] = VFS_USERMOUNT;
935 sysctl (mib, 2, &usermnt, &len, NULL, 0);
937 #elif defined(CTL_KERN) && defined(KERN_USERMOUNT)
942 mib[1] = KERN_USERMOUNT;
943 sysctl (mib, 2, &usermnt, &len, NULL, 0);
948 while ((fstab = getfsent ()) != NULL)
950 if (strcmp (fstab->fs_vfstype, "swap") == 0)
953 mount_entry = g_new0 (GUnixMountPoint, 1);
955 mount_entry->mount_path = g_strdup (fstab->fs_file);
956 mount_entry->device_path = g_strdup (fstab->fs_spec);
957 mount_entry->filesystem_type = g_strdup (fstab->fs_vfstype);
959 if (strcmp (fstab->fs_type, "ro") == 0)
960 mount_entry->is_read_only = TRUE;
962 #ifdef HAVE_SYS_SYSCTL_H
965 uid_t uid = getuid ();
966 if (stat (fstab->fs_file, &sb) == 0)
968 if (uid == 0 || sb.st_uid == uid)
969 mount_entry->is_user_mountable = TRUE;
974 return_list = g_list_prepend (return_list, mount_entry);
979 return g_list_reverse (return_list);
982 #error No g_get_mount_table() implementation for system
986 get_mounts_timestamp (void)
988 const char *monitor_file;
991 monitor_file = get_mtab_monitor_file ();
994 if (stat (monitor_file, &buf) == 0)
995 return (guint64)buf.st_mtime;
1001 get_mount_points_timestamp (void)
1003 const char *monitor_file;
1006 monitor_file = get_fstab_file ();
1009 if (stat (monitor_file, &buf) == 0)
1010 return (guint64)buf.st_mtime;
1016 * g_unix_mounts_get:
1017 * @time_read: guint64 to contain a timestamp.
1019 * Gets a #GList of strings containing the unix mounts.
1020 * If @time_read is set, it will be filled with the mount
1021 * timestamp, allowing for checking if the mounts have changed
1022 * with g_unix_mounts_changed_since().
1024 * Returns: a #GList of the UNIX mounts.
1027 g_unix_mounts_get (guint64 *time_read)
1030 *time_read = get_mounts_timestamp ();
1032 return _g_get_unix_mounts ();
1037 * @mount_path: path for a possible unix mount.
1038 * @time_read: guint64 to contain a timestamp.
1040 * Gets a #GUnixMountEntry for a given mount path. If @time_read
1041 * is set, it will be filled with a unix timestamp for checking
1042 * if the mounts have changed since with g_unix_mounts_changed_since().
1044 * Returns: a #GUnixMount.
1047 g_unix_mount_at (const char *mount_path,
1051 GUnixMountEntry *mount_entry, *found;
1053 mounts = g_unix_mounts_get (time_read);
1056 for (l = mounts; l != NULL; l = l->next)
1058 mount_entry = l->data;
1060 if (!found && strcmp (mount_path, mount_entry->mount_path) == 0)
1061 found = mount_entry;
1063 g_unix_mount_free (mount_entry);
1066 g_list_free (mounts);
1072 * g_unix_mount_points_get:
1073 * @time_read: guint64 to contain a timestamp.
1075 * Gets a #GList of strings containing the unix mount points.
1076 * If @time_read is set, it will be filled with the mount timestamp,
1077 * allowing for checking if the mounts have changed with
1078 * g_unix_mounts_points_changed_since().
1080 * Returns: a #GList of the UNIX mountpoints.
1083 g_unix_mount_points_get (guint64 *time_read)
1086 *time_read = get_mount_points_timestamp ();
1088 return _g_get_unix_mount_points ();
1092 * g_unix_mounts_changed_since:
1093 * @time: guint64 to contain a timestamp.
1095 * Checks if the unix mounts have changed since a given unix time.
1097 * Returns: %TRUE if the mounts have changed since @time.
1100 g_unix_mounts_changed_since (guint64 time)
1102 return get_mounts_timestamp () != time;
1106 * g_unix_mount_points_changed_since:
1107 * @time: guint64 to contain a timestamp.
1109 * Checks if the unix mount points have changed since a given unix time.
1111 * Returns: %TRUE if the mount points have changed since @time.
1114 g_unix_mount_points_changed_since (guint64 time)
1116 return get_mount_points_timestamp () != time;
1120 g_unix_mount_monitor_finalize (GObject *object)
1122 GUnixMountMonitor *monitor;
1124 monitor = G_UNIX_MOUNT_MONITOR (object);
1126 if (monitor->fstab_monitor)
1128 g_file_monitor_cancel (monitor->fstab_monitor);
1129 g_object_unref (monitor->fstab_monitor);
1132 if (monitor->mtab_monitor)
1134 g_file_monitor_cancel (monitor->mtab_monitor);
1135 g_object_unref (monitor->mtab_monitor);
1138 the_mount_monitor = NULL;
1140 G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize (object);
1145 g_unix_mount_monitor_class_init (GUnixMountMonitorClass *klass)
1147 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1149 gobject_class->finalize = g_unix_mount_monitor_finalize;
1151 * GUnixMountMonitor::mounts-changed:
1152 * @monitor: the object on which the signal is emitted
1154 * Emitted when the unix mounts have changed.
1156 signals[MOUNTS_CHANGED] =
1157 g_signal_new ("mounts-changed",
1158 G_TYPE_FROM_CLASS (klass),
1162 g_cclosure_marshal_VOID__VOID,
1165 * GUnixMountMonitor::mountpoints-changed:
1166 * @monitor: the object on which the signal is emitted
1168 * Emitted when the unix mount points have changed.
1170 signals[MOUNTPOINTS_CHANGED] =
1171 g_signal_new ("mountpoints-changed",
1172 G_TYPE_FROM_CLASS (klass),
1176 g_cclosure_marshal_VOID__VOID,
1181 fstab_file_changed (GFileMonitor *monitor,
1184 GFileMonitorEvent event_type,
1187 GUnixMountMonitor *mount_monitor;
1189 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1190 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1191 event_type != G_FILE_MONITOR_EVENT_DELETED)
1194 mount_monitor = user_data;
1195 g_signal_emit (mount_monitor, signals[MOUNTPOINTS_CHANGED], 0);
1199 mtab_file_changed (GFileMonitor *monitor,
1202 GFileMonitorEvent event_type,
1205 GUnixMountMonitor *mount_monitor;
1207 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1208 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1209 event_type != G_FILE_MONITOR_EVENT_DELETED)
1212 mount_monitor = user_data;
1213 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1217 g_unix_mount_monitor_init (GUnixMountMonitor *monitor)
1221 if (get_fstab_file () != NULL)
1223 file = g_file_new_for_path (get_fstab_file ());
1224 monitor->fstab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1225 g_object_unref (file);
1227 g_signal_connect (monitor->fstab_monitor, "changed", (GCallback)fstab_file_changed, monitor);
1230 if (get_mtab_monitor_file () != NULL)
1232 file = g_file_new_for_path (get_mtab_monitor_file ());
1233 monitor->mtab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1234 g_object_unref (file);
1236 g_signal_connect (monitor->mtab_monitor, "changed", (GCallback)mtab_file_changed, monitor);
1241 * g_unix_mount_monitor_set_rate_limit:
1242 * @mount_monitor: a #GUnixMountMonitor
1243 * @limit_msec: a integer with the limit in milliseconds to
1246 * Sets the rate limit to which the @mount_monitor will report
1247 * consecutive change events to the mount and mount point entry files.
1252 g_unix_mount_monitor_set_rate_limit (GUnixMountMonitor *mount_monitor,
1255 g_return_if_fail (G_IS_UNIX_MOUNT_MONITOR (mount_monitor));
1257 if (mount_monitor->fstab_monitor != NULL)
1258 g_file_monitor_set_rate_limit (mount_monitor->fstab_monitor, limit_msec);
1260 if (mount_monitor->mtab_monitor != NULL)
1261 g_file_monitor_set_rate_limit (mount_monitor->mtab_monitor, limit_msec);
1265 * g_unix_mount_monitor_new:
1267 * Gets a new #GUnixMountMonitor. The default rate limit for which the
1268 * monitor will report consecutive changes for the mount and mount
1269 * point entry files is the default for a #GFileMonitor. Use
1270 * g_unix_mount_monitor_set_rate_limit() to change this.
1272 * Returns: a #GUnixMountMonitor.
1275 g_unix_mount_monitor_new (void)
1277 if (the_mount_monitor == NULL)
1279 the_mount_monitor = g_object_new (G_TYPE_UNIX_MOUNT_MONITOR, NULL);
1280 return the_mount_monitor;
1283 return g_object_ref (the_mount_monitor);
1287 * g_unix_mount_free:
1288 * @mount_entry: a #GUnixMount.
1290 * Frees a unix mount.
1293 g_unix_mount_free (GUnixMountEntry *mount_entry)
1295 g_return_if_fail (mount_entry != NULL);
1297 g_free (mount_entry->mount_path);
1298 g_free (mount_entry->device_path);
1299 g_free (mount_entry->filesystem_type);
1300 g_free (mount_entry);
1304 * g_unix_mount_point_free:
1305 * @mount_point: unix mount point to free.
1307 * Frees a unix mount point.
1310 g_unix_mount_point_free (GUnixMountPoint *mount_point)
1312 g_return_if_fail (mount_point != NULL);
1314 g_free (mount_point->mount_path);
1315 g_free (mount_point->device_path);
1316 g_free (mount_point->filesystem_type);
1317 g_free (mount_point);
1321 * g_unix_mount_compare:
1322 * @mount1: first #GUnixMountEntry to compare.
1323 * @mount2: second #GUnixMountEntry to compare.
1325 * Compares two unix mounts.
1327 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1328 * or less than @mount2, respectively.
1331 g_unix_mount_compare (GUnixMountEntry *mount1,
1332 GUnixMountEntry *mount2)
1336 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1338 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1342 res = g_strcmp0 (mount1->device_path, mount2->device_path);
1346 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1350 res = mount1->is_read_only - mount2->is_read_only;
1358 * g_unix_mount_get_mount_path:
1359 * @mount_entry: input #GUnixMountEntry to get the mount path for.
1361 * Gets the mount path for a unix mount.
1363 * Returns: the mount path for @mount_entry.
1366 g_unix_mount_get_mount_path (GUnixMountEntry *mount_entry)
1368 g_return_val_if_fail (mount_entry != NULL, NULL);
1370 return mount_entry->mount_path;
1374 * g_unix_mount_get_device_path:
1375 * @mount_entry: a #GUnixMount.
1377 * Gets the device path for a unix mount.
1379 * Returns: a string containing the device path.
1382 g_unix_mount_get_device_path (GUnixMountEntry *mount_entry)
1384 g_return_val_if_fail (mount_entry != NULL, NULL);
1386 return mount_entry->device_path;
1390 * g_unix_mount_get_fs_type:
1391 * @mount_entry: a #GUnixMount.
1393 * Gets the filesystem type for the unix mount.
1395 * Returns: a string containing the file system type.
1398 g_unix_mount_get_fs_type (GUnixMountEntry *mount_entry)
1400 g_return_val_if_fail (mount_entry != NULL, NULL);
1402 return mount_entry->filesystem_type;
1406 * g_unix_mount_is_readonly:
1407 * @mount_entry: a #GUnixMount.
1409 * Checks if a unix mount is mounted read only.
1411 * Returns: %TRUE if @mount_entry is read only.
1414 g_unix_mount_is_readonly (GUnixMountEntry *mount_entry)
1416 g_return_val_if_fail (mount_entry != NULL, FALSE);
1418 return mount_entry->is_read_only;
1422 * g_unix_mount_is_system_internal:
1423 * @mount_entry: a #GUnixMount.
1425 * Checks if a unix mount is a system path.
1427 * Returns: %TRUE if the unix mount is for a system path.
1430 g_unix_mount_is_system_internal (GUnixMountEntry *mount_entry)
1432 g_return_val_if_fail (mount_entry != NULL, FALSE);
1434 return mount_entry->is_system_internal;
1438 * g_unix_mount_point_compare:
1439 * @mount1: a #GUnixMount.
1440 * @mount2: a #GUnixMount.
1442 * Compares two unix mount points.
1444 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1445 * or less than @mount2, respectively.
1448 g_unix_mount_point_compare (GUnixMountPoint *mount1,
1449 GUnixMountPoint *mount2)
1453 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1455 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1459 res = g_strcmp0 (mount1->device_path, mount2->device_path);
1463 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1467 res = mount1->is_read_only - mount2->is_read_only;
1471 res = mount1->is_user_mountable - mount2->is_user_mountable;
1475 res = mount1->is_loopback - mount2->is_loopback;
1483 * g_unix_mount_point_get_mount_path:
1484 * @mount_point: a #GUnixMountPoint.
1486 * Gets the mount path for a unix mount point.
1488 * Returns: a string containing the mount path.
1491 g_unix_mount_point_get_mount_path (GUnixMountPoint *mount_point)
1493 g_return_val_if_fail (mount_point != NULL, NULL);
1495 return mount_point->mount_path;
1499 * g_unix_mount_point_get_device_path:
1500 * @mount_point: a #GUnixMountPoint.
1502 * Gets the device path for a unix mount point.
1504 * Returns: a string containing the device path.
1507 g_unix_mount_point_get_device_path (GUnixMountPoint *mount_point)
1509 g_return_val_if_fail (mount_point != NULL, NULL);
1511 return mount_point->device_path;
1515 * g_unix_mount_point_get_fs_type:
1516 * @mount_point: a #GUnixMountPoint.
1518 * Gets the file system type for the mount point.
1520 * Returns: a string containing the file system type.
1523 g_unix_mount_point_get_fs_type (GUnixMountPoint *mount_point)
1525 g_return_val_if_fail (mount_point != NULL, NULL);
1527 return mount_point->filesystem_type;
1531 * g_unix_mount_point_is_readonly:
1532 * @mount_point: a #GUnixMountPoint.
1534 * Checks if a unix mount point is read only.
1536 * Returns: %TRUE if a mount point is read only.
1539 g_unix_mount_point_is_readonly (GUnixMountPoint *mount_point)
1541 g_return_val_if_fail (mount_point != NULL, FALSE);
1543 return mount_point->is_read_only;
1547 * g_unix_mount_point_is_user_mountable:
1548 * @mount_point: a #GUnixMountPoint.
1550 * Checks if a unix mount point is mountable by the user.
1552 * Returns: %TRUE if the mount point is user mountable.
1555 g_unix_mount_point_is_user_mountable (GUnixMountPoint *mount_point)
1557 g_return_val_if_fail (mount_point != NULL, FALSE);
1559 return mount_point->is_user_mountable;
1563 * g_unix_mount_point_is_loopback:
1564 * @mount_point: a #GUnixMountPoint.
1566 * Checks if a unix mount point is a loopback device.
1568 * Returns: %TRUE if the mount point is a loopback. %FALSE otherwise.
1571 g_unix_mount_point_is_loopback (GUnixMountPoint *mount_point)
1573 g_return_val_if_fail (mount_point != NULL, FALSE);
1575 return mount_point->is_loopback;
1578 static GUnixMountType
1579 guess_mount_type (const char *mount_path,
1580 const char *device_path,
1581 const char *filesystem_type)
1583 GUnixMountType type;
1586 type = G_UNIX_MOUNT_TYPE_UNKNOWN;
1588 if ((strcmp (filesystem_type, "udf") == 0) ||
1589 (strcmp (filesystem_type, "iso9660") == 0) ||
1590 (strcmp (filesystem_type, "cd9660") == 0))
1591 type = G_UNIX_MOUNT_TYPE_CDROM;
1592 else if ((strcmp (filesystem_type, "nfs") == 0) ||
1593 (strcmp (filesystem_type, "nfs4") == 0))
1594 type = G_UNIX_MOUNT_TYPE_NFS;
1595 else if (g_str_has_prefix (device_path, "/vol/dev/diskette/") ||
1596 g_str_has_prefix (device_path, "/dev/fd") ||
1597 g_str_has_prefix (device_path, "/dev/floppy"))
1598 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1599 else if (g_str_has_prefix (device_path, "/dev/cdrom") ||
1600 g_str_has_prefix (device_path, "/dev/acd") ||
1601 g_str_has_prefix (device_path, "/dev/cd"))
1602 type = G_UNIX_MOUNT_TYPE_CDROM;
1603 else if (g_str_has_prefix (device_path, "/vol/"))
1605 const char *name = mount_path + strlen ("/");
1607 if (g_str_has_prefix (name, "cdrom"))
1608 type = G_UNIX_MOUNT_TYPE_CDROM;
1609 else if (g_str_has_prefix (name, "floppy") ||
1610 g_str_has_prefix (device_path, "/vol/dev/diskette/"))
1611 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1612 else if (g_str_has_prefix (name, "rmdisk"))
1613 type = G_UNIX_MOUNT_TYPE_ZIP;
1614 else if (g_str_has_prefix (name, "jaz"))
1615 type = G_UNIX_MOUNT_TYPE_JAZ;
1616 else if (g_str_has_prefix (name, "memstick"))
1617 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1621 basename = g_path_get_basename (mount_path);
1623 if (g_str_has_prefix (basename, "cdr") ||
1624 g_str_has_prefix (basename, "cdwriter") ||
1625 g_str_has_prefix (basename, "burn") ||
1626 g_str_has_prefix (basename, "dvdr"))
1627 type = G_UNIX_MOUNT_TYPE_CDROM;
1628 else if (g_str_has_prefix (basename, "floppy"))
1629 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1630 else if (g_str_has_prefix (basename, "zip"))
1631 type = G_UNIX_MOUNT_TYPE_ZIP;
1632 else if (g_str_has_prefix (basename, "jaz"))
1633 type = G_UNIX_MOUNT_TYPE_JAZ;
1634 else if (g_str_has_prefix (basename, "camera"))
1635 type = G_UNIX_MOUNT_TYPE_CAMERA;
1636 else if (g_str_has_prefix (basename, "memstick") ||
1637 g_str_has_prefix (basename, "memory_stick") ||
1638 g_str_has_prefix (basename, "ram"))
1639 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1640 else if (g_str_has_prefix (basename, "compact_flash"))
1641 type = G_UNIX_MOUNT_TYPE_CF;
1642 else if (g_str_has_prefix (basename, "smart_media"))
1643 type = G_UNIX_MOUNT_TYPE_SM;
1644 else if (g_str_has_prefix (basename, "sd_mmc"))
1645 type = G_UNIX_MOUNT_TYPE_SDMMC;
1646 else if (g_str_has_prefix (basename, "ipod"))
1647 type = G_UNIX_MOUNT_TYPE_IPOD;
1652 if (type == G_UNIX_MOUNT_TYPE_UNKNOWN)
1653 type = G_UNIX_MOUNT_TYPE_HD;
1659 * g_unix_mount_guess_type:
1660 * @mount_entry: a #GUnixMount.
1662 * Guesses the type of a unix mount. If the mount type cannot be
1663 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1665 * Returns: a #GUnixMountType.
1667 static GUnixMountType
1668 g_unix_mount_guess_type (GUnixMountEntry *mount_entry)
1670 g_return_val_if_fail (mount_entry != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1671 g_return_val_if_fail (mount_entry->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1672 g_return_val_if_fail (mount_entry->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1673 g_return_val_if_fail (mount_entry->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1675 return guess_mount_type (mount_entry->mount_path,
1676 mount_entry->device_path,
1677 mount_entry->filesystem_type);
1681 * g_unix_mount_point_guess_type:
1682 * @mount_point: a #GUnixMountPoint.
1684 * Guesses the type of a unix mount point.
1685 * If the mount type cannot be determined,
1686 * returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1688 * Returns: a #GUnixMountType.
1690 static GUnixMountType
1691 g_unix_mount_point_guess_type (GUnixMountPoint *mount_point)
1693 g_return_val_if_fail (mount_point != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1694 g_return_val_if_fail (mount_point->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1695 g_return_val_if_fail (mount_point->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1696 g_return_val_if_fail (mount_point->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1698 return guess_mount_type (mount_point->mount_path,
1699 mount_point->device_path,
1700 mount_point->filesystem_type);
1704 type_to_icon (GUnixMountType type, gboolean is_mount_point)
1706 const char *icon_name;
1710 case G_UNIX_MOUNT_TYPE_HD:
1712 icon_name = "drive-removable-media";
1714 icon_name = "drive-harddisk";
1716 case G_UNIX_MOUNT_TYPE_FLOPPY:
1717 case G_UNIX_MOUNT_TYPE_ZIP:
1718 case G_UNIX_MOUNT_TYPE_JAZ:
1720 icon_name = "drive-removable-media";
1722 icon_name = "media-floppy";
1724 case G_UNIX_MOUNT_TYPE_CDROM:
1726 icon_name = "drive-optical";
1728 icon_name = "media-optical";
1730 case G_UNIX_MOUNT_TYPE_NFS:
1731 /* TODO: Would like a better icon here... */
1733 icon_name = "drive-removable-media";
1735 icon_name = "drive-harddisk";
1737 case G_UNIX_MOUNT_TYPE_MEMSTICK:
1739 icon_name = "drive-removable-media";
1741 icon_name = "media-flash";
1743 case G_UNIX_MOUNT_TYPE_CAMERA:
1745 icon_name = "drive-removable-media";
1747 icon_name = "camera-photo";
1749 case G_UNIX_MOUNT_TYPE_IPOD:
1751 icon_name = "drive-removable-media";
1753 icon_name = "multimedia-player";
1755 case G_UNIX_MOUNT_TYPE_UNKNOWN:
1758 icon_name = "drive-removable-media";
1760 icon_name = "drive-harddisk";
1768 * g_unix_mount_guess_name:
1769 * @mount_entry: a #GUnixMountEntry
1771 * Guesses the name of a Unix mount.
1772 * The result is a translated string.
1774 * Returns: A newly allocated string that must
1775 * be freed with g_free()
1778 g_unix_mount_guess_name (GUnixMountEntry *mount_entry)
1782 if (strcmp (mount_entry->mount_path, "/") == 0)
1783 name = g_strdup (_("Filesystem root"));
1785 name = g_filename_display_basename (mount_entry->mount_path);
1791 * g_unix_mount_guess_icon:
1792 * @mount_entry: a #GUnixMountEntry
1794 * Guesses the icon of a Unix mount.
1799 g_unix_mount_guess_icon (GUnixMountEntry *mount_entry)
1801 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE));
1805 * g_unix_mount_point_guess_name:
1806 * @mount_point: a #GUnixMountPoint
1808 * Guesses the name of a Unix mount point.
1809 * The result is a translated string.
1811 * Returns: A newly allocated string that must
1812 * be freed with g_free()
1815 g_unix_mount_point_guess_name (GUnixMountPoint *mount_point)
1819 if (strcmp (mount_point->mount_path, "/") == 0)
1820 name = g_strdup (_("Filesystem root"));
1822 name = g_filename_display_basename (mount_point->mount_path);
1828 * g_unix_mount_point_guess_icon:
1829 * @mount_point: a #GUnixMountPoint
1831 * Guesses the icon of a Unix mount point.
1836 g_unix_mount_point_guess_icon (GUnixMountPoint *mount_point)
1838 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE));
1842 * g_unix_mount_guess_can_eject:
1843 * @mount_entry: a #GUnixMountEntry
1845 * Guesses whether a Unix mount can be ejected.
1847 * Returns: %TRUE if @mount_entry is deemed to be ejectable.
1850 g_unix_mount_guess_can_eject (GUnixMountEntry *mount_entry)
1852 GUnixMountType guessed_type;
1854 guessed_type = g_unix_mount_guess_type (mount_entry);
1855 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
1856 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
1863 * g_unix_mount_guess_should_display:
1864 * @mount_entry: a #GUnixMountEntry
1866 * Guesses whether a Unix mount should be displayed in the UI.
1868 * Returns: %TRUE if @mount_entry is deemed to be displayable.
1871 g_unix_mount_guess_should_display (GUnixMountEntry *mount_entry)
1873 const char *mount_path;
1875 /* Never display internal mountpoints */
1876 if (g_unix_mount_is_system_internal (mount_entry))
1879 /* Only display things in /media (which are generally user mountable)
1880 and home dir (fuse stuff) */
1881 mount_path = mount_entry->mount_path;
1882 if (mount_path != NULL)
1884 if (g_str_has_prefix (mount_path, "/media/")) {
1886 /* Avoid displaying mounts that are not accessible to the user.
1888 * See http://bugzilla.gnome.org/show_bug.cgi?id=526320 for why we
1889 * want to avoid g_access() for mount points which can potentially
1890 * block or fail stat()'ing, such as network mounts.
1892 path = g_path_get_dirname (mount_path);
1893 if (g_str_has_prefix (path, "/media/"))
1895 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 ()) && mount_path[strlen (g_get_home_dir())] == G_DIR_SEPARATOR)
1921 * g_unix_mount_point_guess_can_eject:
1922 * @mount_point: a #GUnixMountPoint
1924 * Guesses whether a Unix mount point can be ejected.
1926 * Returns: %TRUE if @mount_point is deemed to be ejectable.
1929 g_unix_mount_point_guess_can_eject (GUnixMountPoint *mount_point)
1931 GUnixMountType guessed_type;
1933 guessed_type = g_unix_mount_point_guess_type (mount_point);
1934 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
1935 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
1942 /* borrowed from gtk/gtkfilesystemunix.c in GTK+ on 02/23/2006 */
1944 _canonicalize_filename (gchar *filename)
1947 gboolean last_was_slash = FALSE;
1954 if (*p == G_DIR_SEPARATOR)
1956 if (!last_was_slash)
1957 *q++ = G_DIR_SEPARATOR;
1959 last_was_slash = TRUE;
1963 if (last_was_slash && *p == '.')
1965 if (*(p + 1) == G_DIR_SEPARATOR ||
1968 if (*(p + 1) == '\0')
1973 else if (*(p + 1) == '.' &&
1974 (*(p + 2) == G_DIR_SEPARATOR ||
1977 if (q > filename + 1)
1980 while (q > filename + 1 &&
1981 *(q - 1) != G_DIR_SEPARATOR)
1985 if (*(p + 2) == '\0')
1993 last_was_slash = FALSE;
1999 last_was_slash = FALSE;
2006 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
2013 _resolve_symlink (const char *file)
2021 f = g_strdup (file);
2023 while (g_file_test (f, G_FILE_TEST_IS_SYMLINK)) {
2024 link = g_file_read_link (f, &error);
2026 g_error_free (error);
2032 dir = g_path_get_dirname (f);
2033 f1 = g_strdup_printf ("%s/%s", dir, link);
2042 _canonicalize_filename (f);
2046 #ifdef HAVE_MNTENT_H
2048 _resolve_dev_root (void)
2050 static gboolean have_real_dev_root = FALSE;
2051 static char real_dev_root[256];
2052 struct stat statbuf;
2054 /* see if it's cached already */
2055 if (have_real_dev_root)
2058 /* otherwise we're going to find it right away.. */
2059 have_real_dev_root = TRUE;
2061 if (stat ("/dev/root", &statbuf) == 0) {
2062 if (! S_ISLNK (statbuf.st_mode)) {
2063 dev_t root_dev = statbuf.st_dev;
2067 /* see if device with similar major:minor as /dev/root is mention
2068 * in /etc/mtab (it usually is)
2070 f = fopen ("/etc/mtab", "r");
2072 struct mntent *entp;
2073 #ifdef HAVE_GETMNTENT_R
2075 while ((entp = getmntent_r (f, &ent, buf, sizeof (buf))) != NULL) {
2078 while ((entp = getmntent (f)) != NULL) {
2080 if (stat (entp->mnt_fsname, &statbuf) == 0 &&
2081 statbuf.st_dev == root_dev) {
2082 strncpy (real_dev_root, entp->mnt_fsname, sizeof (real_dev_root) - 1);
2083 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2091 #ifndef HAVE_GETMNTENT_R
2092 G_UNLOCK (getmntent);
2096 /* no, that didn't work.. next we could scan /dev ... but I digress.. */
2100 resolved = _resolve_symlink ("/dev/root");
2101 if (resolved != NULL) {
2102 strncpy (real_dev_root, resolved, sizeof (real_dev_root) - 1);
2103 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2111 strcpy (real_dev_root, "/dev/root");
2114 return real_dev_root;
2118 #define __G_UNIX_MOUNTS_C__
2119 #include "gioaliasdef.c"