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
48 #include "gunixmounts.h"
50 #include "gfilemonitor.h"
52 #include "gthemedicon.h"
56 static const char *_resolve_dev_root (void);
60 * @include: gio/gunixmounts.h
61 * @short_description: Unix Mounts
63 * Routines for managing mounted UNIX mount points and paths.
69 * @G_UNIX_MOUNT_TYPE_UNKNOWN: Unknown UNIX mount type.
70 * @G_UNIX_MOUNT_TYPE_FLOPPY: Floppy disk UNIX mount type.
71 * @G_UNIX_MOUNT_TYPE_CDROM: CDROM UNIX mount type.
72 * @G_UNIX_MOUNT_TYPE_NFS: Network File System (NFS) UNIX mount type.
73 * @G_UNIX_MOUNT_TYPE_ZIP: ZIP UNIX mount type.
74 * @G_UNIX_MOUNT_TYPE_JAZ: JAZZ UNIX mount type.
75 * @G_UNIX_MOUNT_TYPE_MEMSTICK: Memory Stick UNIX mount type.
76 * @G_UNIX_MOUNT_TYPE_CF: Compact Flash UNIX mount type.
77 * @G_UNIX_MOUNT_TYPE_SM: Smart Media UNIX mount type.
78 * @G_UNIX_MOUNT_TYPE_SDMMC: SD/MMC UNIX mount type.
79 * @G_UNIX_MOUNT_TYPE_IPOD: iPod UNIX mount type.
80 * @G_UNIX_MOUNT_TYPE_CAMERA: Digital camera UNIX mount type.
81 * @G_UNIX_MOUNT_TYPE_HD: Hard drive UNIX mount type.
83 * Types of UNIX mounts.
86 G_UNIX_MOUNT_TYPE_UNKNOWN,
87 G_UNIX_MOUNT_TYPE_FLOPPY,
88 G_UNIX_MOUNT_TYPE_CDROM,
89 G_UNIX_MOUNT_TYPE_NFS,
90 G_UNIX_MOUNT_TYPE_ZIP,
91 G_UNIX_MOUNT_TYPE_JAZ,
92 G_UNIX_MOUNT_TYPE_MEMSTICK,
95 G_UNIX_MOUNT_TYPE_SDMMC,
96 G_UNIX_MOUNT_TYPE_IPOD,
97 G_UNIX_MOUNT_TYPE_CAMERA,
101 struct _GUnixMountEntry {
104 char *filesystem_type;
105 gboolean is_read_only;
106 gboolean is_system_internal;
109 struct _GUnixMountPoint {
112 char *filesystem_type;
113 gboolean is_read_only;
114 gboolean is_user_mountable;
115 gboolean is_loopback;
124 static guint signals[LAST_SIGNAL];
126 struct _GUnixMountMonitor {
129 GFileMonitor *fstab_monitor;
130 GFileMonitor *mtab_monitor;
133 struct _GUnixMountMonitorClass {
134 GObjectClass parent_class;
137 static GUnixMountMonitor *the_mount_monitor = NULL;
139 static GList *_g_get_unix_mounts (void);
140 static GList *_g_get_unix_mount_points (void);
142 G_DEFINE_TYPE (GUnixMountMonitor, g_unix_mount_monitor, G_TYPE_OBJECT);
144 #define MOUNT_POLL_INTERVAL 4000
146 #ifdef HAVE_SYS_MNTTAB_H
147 #define MNTOPT_RO "ro"
152 #elif defined (HAVE_SYS_MNTTAB_H)
153 #include <sys/mnttab.h>
156 #ifdef HAVE_SYS_VFSTAB_H
157 #include <sys/vfstab.h>
160 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
161 #include <sys/mntctl.h>
163 #include <sys/vmount.h>
167 #if defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
168 #include <sys/ucred.h>
169 #include <sys/mount.h>
171 #ifdef HAVE_SYS_SYSCTL_H
172 #include <sys/sysctl.h>
176 #ifndef HAVE_SETMNTENT
177 #define setmntent(f,m) fopen(f,m)
179 #ifndef HAVE_ENDMNTENT
180 #define endmntent(f) fclose(f)
184 is_in (const char *value, const char *set[])
187 for (i = 0; set[i] != NULL; i++)
189 if (strcmp (set[i], value) == 0)
196 * g_unix_is_mount_path_system_internal:
197 * @mount_path: a mount path, e.g. <filename>/media/disk</filename>
198 * or <filename>/usr</filename>
200 * Determines if @mount_path is considered an implementation of the
201 * OS. This is primarily used for hiding mountable and mounted volumes
202 * that only are used in the OS and has little to no relevance to the
205 * Returns: %TRUE if @mount_path is considered an implementation detail
209 g_unix_is_mount_path_system_internal (const char *mount_path)
211 const char *ignore_mountpoints[] = {
212 /* Includes all FHS 2.3 toplevel dirs and other specilized
213 * directories that we want to hide from the user.
215 "/", /* we already have "Filesystem root" in Nautilus */
233 "/var/log/audit", /* https://bugzilla.redhat.com/show_bug.cgi?id=333041 */
234 "/var/tmp", /* https://bugzilla.redhat.com/show_bug.cgi?id=335241 */
241 if (is_in (mount_path, ignore_mountpoints))
244 if (g_str_has_prefix (mount_path, "/dev") ||
245 g_str_has_prefix (mount_path, "/proc") ||
246 g_str_has_prefix (mount_path, "/sys"))
249 if (strstr (mount_path, "/.gvfs") != NULL)
256 guess_system_internal (const char *mountpoint,
260 const char *ignore_fs[] = {
279 const char *ignore_devices[] = {
289 if (is_in (fs, ignore_fs))
292 if (is_in (device, ignore_devices))
295 if (g_unix_is_mount_path_system_internal (mountpoint))
304 get_mtab_read_file (void)
308 return "/proc/mounts";
310 return _PATH_MOUNTED;
318 get_mtab_monitor_file (void)
321 return _PATH_MOUNTED;
327 #ifndef HAVE_GETMNTENT_R
328 G_LOCK_DEFINE_STATIC(getmntent);
332 _g_get_unix_mounts ()
334 #ifdef HAVE_GETMNTENT_R
338 struct mntent *mntent;
341 GUnixMountEntry *mount_entry;
342 GHashTable *mounts_hash;
345 read_file = get_mtab_read_file ();
347 file = setmntent (read_file, "r");
353 mounts_hash = g_hash_table_new (g_str_hash, g_str_equal);
355 #ifdef HAVE_GETMNTENT_R
356 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
359 while ((mntent = getmntent (file)) != NULL)
362 /* ignore any mnt_fsname that is repeated and begins with a '/'
364 * We do this to avoid being fooled by --bind mounts, since
365 * these have the same device as the location they bind to.
366 * Its not an ideal solution to the problem, but it's likely that
367 * the most important mountpoint is first and the --bind ones after
368 * that aren't as important. So it should work.
370 * The '/' is to handle procfs, tmpfs and other no device mounts.
372 if (mntent->mnt_fsname != NULL &&
373 mntent->mnt_fsname[0] == '/' &&
374 g_hash_table_lookup (mounts_hash, mntent->mnt_fsname))
377 mount_entry = g_new0 (GUnixMountEntry, 1);
378 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
379 if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
380 mount_entry->device_path = g_strdup (_resolve_dev_root ());
382 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
383 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
385 #if defined (HAVE_HASMNTOPT)
386 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
387 mount_entry->is_read_only = TRUE;
390 mount_entry->is_system_internal =
391 guess_system_internal (mount_entry->mount_path,
392 mount_entry->filesystem_type,
393 mount_entry->device_path);
395 g_hash_table_insert (mounts_hash,
396 mount_entry->device_path,
397 mount_entry->device_path);
399 return_list = g_list_prepend (return_list, mount_entry);
401 g_hash_table_destroy (mounts_hash);
405 #ifndef HAVE_GETMNTENT_R
406 G_UNLOCK (getmntent);
409 return g_list_reverse (return_list);
412 #elif defined (HAVE_SYS_MNTTAB_H)
414 G_LOCK_DEFINE_STATIC(getmntent);
417 get_mtab_read_file (void)
420 return _PATH_MOUNTED;
422 return "/etc/mnttab";
427 get_mtab_monitor_file (void)
429 return get_mtab_read_file ();
433 _g_get_unix_mounts (void)
435 struct mnttab mntent;
438 GUnixMountEntry *mount_entry;
441 read_file = get_mtab_read_file ();
443 file = setmntent (read_file, "r");
450 while (! getmntent (file, &mntent))
452 mount_entry = g_new0 (GUnixMountEntry, 1);
454 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
455 mount_entry->device_path = g_strdup (mntent.mnt_special);
456 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
458 #if defined (HAVE_HASMNTOPT)
459 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
460 mount_entry->is_read_only = TRUE;
463 mount_entry->is_system_internal =
464 guess_system_internal (mount_entry->mount_path,
465 mount_entry->filesystem_type,
466 mount_entry->device_path);
468 return_list = g_list_prepend (return_list, mount_entry);
473 G_UNLOCK (getmntent);
475 return g_list_reverse (return_list);
478 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
481 get_mtab_monitor_file (void)
487 _g_get_unix_mounts (void)
489 struct vfs_ent *fs_info;
490 struct vmount *vmount_info;
492 unsigned int vmount_size;
496 if (mntctl (MCTL_QUERY, sizeof (vmount_size), &vmount_size) != 0)
498 g_warning ("Unable to know the number of mounted volumes\n");
503 vmount_info = (struct vmount*)g_malloc (vmount_size);
505 vmount_number = mntctl (MCTL_QUERY, vmount_size, vmount_info);
507 if (vmount_info->vmt_revision != VMT_REVISION)
508 g_warning ("Bad vmount structure revision number, want %d, got %d\n", VMT_REVISION, vmount_info->vmt_revision);
510 if (vmount_number < 0)
512 g_warning ("Unable to recover mounted volumes information\n");
514 g_free (vmount_info);
519 while (vmount_number > 0)
521 mount_entry = g_new0 (GUnixMountEntry, 1);
523 mount_entry->device_path = g_strdup (vmt2dataptr (vmount_info, VMT_OBJECT));
524 mount_entry->mount_path = g_strdup (vmt2dataptr (vmount_info, VMT_STUB));
525 /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
526 mount_entry->is_read_only = (vmount_info->vmt_flags & MNT_READONLY) ? 1 : 0;
528 fs_info = getvfsbytype (vmount_info->vmt_gfstype);
531 mount_entry->filesystem_type = g_strdup ("unknown");
533 mount_entry->filesystem_type = g_strdup (fs_info->vfsent_name);
535 mount_entry->is_system_internal =
536 guess_system_internal (mount_entry->mount_path,
537 mount_entry->filesystem_type,
538 mount_entry->device_path);
540 return_list = g_list_prepend (return_list, mount_entry);
542 vmount_info = (struct vmount *)( (char*)vmount_info
543 + vmount_info->vmt_length);
548 g_free (vmount_info);
550 return g_list_reverse (return_list);
553 #elif defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
556 get_mtab_monitor_file (void)
562 _g_get_unix_mounts (void)
564 struct statfs *mntent = NULL;
566 GUnixMountEntry *mount_entry;
569 /* Pass MNT_NOWAIT to avoid blocking trying to update NFS mounts. */
570 if ((num_mounts = getmntinfo (&mntent, MNT_NOWAIT)) == 0)
575 for (i = 0; i < num_mounts; i++)
577 mount_entry = g_new0 (GUnixMountEntry, 1);
579 mount_entry->mount_path = g_strdup (mntent[i].f_mntonname);
580 mount_entry->device_path = g_strdup (mntent[i].f_mntfromname);
581 mount_entry->filesystem_type = g_strdup (mntent[i].f_fstypename);
582 if (mntent[i].f_flags & MNT_RDONLY)
583 mount_entry->is_read_only = TRUE;
585 mount_entry->is_system_internal =
586 guess_system_internal (mount_entry->mount_path,
587 mount_entry->filesystem_type,
588 mount_entry->device_path);
590 return_list = g_list_prepend (return_list, mount_entry);
593 return g_list_reverse (return_list);
596 #error No _g_get_unix_mounts() implementation for system
599 /* _g_get_unix_mount_points():
601 * don't return swap and ignore mounts.
605 get_fstab_file (void)
607 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
609 return "/etc/filesystems";
610 #elif defined(_PATH_MNTTAB)
612 #elif defined(VFSTAB)
621 _g_get_unix_mount_points (void)
623 #ifdef HAVE_GETMNTENT_R
627 struct mntent *mntent;
630 GUnixMountPoint *mount_entry;
633 read_file = get_fstab_file ();
635 file = setmntent (read_file, "r");
641 #ifdef HAVE_GETMNTENT_R
642 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
645 while ((mntent = getmntent (file)) != NULL)
648 if ((strcmp (mntent->mnt_dir, "ignore") == 0) ||
649 (strcmp (mntent->mnt_dir, "swap") == 0))
652 mount_entry = g_new0 (GUnixMountPoint, 1);
653 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
654 if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
655 mount_entry->device_path = g_strdup (_resolve_dev_root ());
657 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
658 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
660 #ifdef HAVE_HASMNTOPT
661 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
662 mount_entry->is_read_only = TRUE;
664 if (hasmntopt (mntent, "loop") != NULL)
665 mount_entry->is_loopback = TRUE;
669 if ((mntent->mnt_type != NULL && strcmp ("supermount", mntent->mnt_type) == 0)
670 #ifdef HAVE_HASMNTOPT
671 || (hasmntopt (mntent, "user") != NULL
672 && hasmntopt (mntent, "user") != hasmntopt (mntent, "user_xattr"))
673 || hasmntopt (mntent, "pamconsole") != NULL
674 || hasmntopt (mntent, "users") != NULL
675 || hasmntopt (mntent, "owner") != NULL
678 mount_entry->is_user_mountable = TRUE;
680 return_list = g_list_prepend (return_list, mount_entry);
685 #ifndef HAVE_GETMNTENT_R
686 G_UNLOCK (getmntent);
689 return g_list_reverse (return_list);
692 #elif defined (HAVE_SYS_MNTTAB_H)
695 _g_get_unix_mount_points (void)
697 struct mnttab mntent;
700 GUnixMountPoint *mount_entry;
703 read_file = get_fstab_file ();
705 file = setmntent (read_file, "r");
712 while (! getmntent (file, &mntent))
714 if ((strcmp (mntent.mnt_mountp, "ignore") == 0) ||
715 (strcmp (mntent.mnt_mountp, "swap") == 0))
718 mount_entry = g_new0 (GUnixMountPoint, 1);
720 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
721 mount_entry->device_path = g_strdup (mntent.mnt_special);
722 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
724 #ifdef HAVE_HASMNTOPT
725 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
726 mount_entry->is_read_only = TRUE;
728 if (hasmntopt (&mntent, "lofs") != NULL)
729 mount_entry->is_loopback = TRUE;
732 if ((mntent.mnt_fstype != NULL)
733 #ifdef HAVE_HASMNTOPT
734 || (hasmntopt (&mntent, "user") != NULL
735 && hasmntopt (&mntent, "user") != hasmntopt (&mntent, "user_xattr"))
736 || hasmntopt (&mntent, "pamconsole") != NULL
737 || hasmntopt (&mntent, "users") != NULL
738 || hasmntopt (&mntent, "owner") != NULL
741 mount_entry->is_user_mountable = TRUE;
744 return_list = g_list_prepend (return_list, mount_entry);
748 G_UNLOCK (getmntent);
750 return g_list_reverse (return_list);
752 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
754 /* functions to parse /etc/filesystems on aix */
756 /* read character, ignoring comments (begin with '*', end with '\n' */
758 aix_fs_getc (FILE *fd)
762 while ((c = getc (fd)) == '*')
764 while (((c = getc (fd)) != '\n') && (c != EOF))
769 /* eat all continuous spaces in a file */
771 aix_fs_ignorespace (FILE *fd)
775 while ((c = aix_fs_getc (fd)) != EOF)
777 if (!g_ascii_isspace (c))
787 /* read one word from file */
789 aix_fs_getword (FILE *fd,
794 aix_fs_ignorespace (fd);
796 while (((c = aix_fs_getc (fd)) != EOF) && !g_ascii_isspace (c))
800 while (((c = aix_fs_getc (fd)) != EOF) && (c != '"'))
812 char mnt_mount[PATH_MAX];
813 char mnt_special[PATH_MAX];
815 char mnt_options[128];
816 } AixMountTableEntry;
818 /* read mount points properties */
820 aix_fs_get (FILE *fd,
821 AixMountTableEntry *prop)
823 static char word[PATH_MAX] = { 0 };
824 char value[PATH_MAX];
829 if (aix_fs_getword (fd, word) == EOF)
833 word[strlen(word) - 1] = 0;
834 strcpy (prop->mnt_mount, word);
836 /* read attributes and value */
838 while (aix_fs_getword (fd, word) != EOF)
840 /* test if is attribute or new stanza */
841 if (word[strlen(word) - 1] == ':')
845 aix_fs_getword (fd, value);
848 aix_fs_getword (fd, value);
850 if (strcmp (word, "dev") == 0)
851 strcpy (prop->mnt_special, value);
852 else if (strcmp (word, "vfs") == 0)
853 strcpy (prop->mnt_fstype, value);
854 else if (strcmp (word, "options") == 0)
855 strcpy(prop->mnt_options, value);
862 _g_get_unix_mount_points (void)
864 struct mntent *mntent;
867 GUnixMountPoint *mount_entry;
868 AixMountTableEntry mntent;
871 read_file = get_fstab_file ();
873 file = setmntent (read_file, "r");
879 while (!aix_fs_get (file, &mntent))
881 if (strcmp ("cdrfs", mntent.mnt_fstype) == 0)
883 mount_entry = g_new0 (GUnixMountPoint, 1);
886 mount_entry->mount_path = g_strdup (mntent.mnt_mount);
887 mount_entry->device_path = g_strdup (mntent.mnt_special);
888 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
889 mount_entry->is_read_only = TRUE;
890 mount_entry->is_user_mountable = TRUE;
892 return_list = g_list_prepend (return_list, mount_entry);
898 return g_list_reverse (return_list);
901 #elif defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
904 _g_get_unix_mount_points (void)
906 struct fstab *fstab = NULL;
907 GUnixMountPoint *mount_entry;
909 #ifdef HAVE_SYS_SYSCTL_H
911 size_t len = sizeof(usermnt);
920 #ifdef HAVE_SYS_SYSCTL_H
921 #if defined(HAVE_SYSCTLBYNAME)
922 sysctlbyname ("vfs.usermount", &usermnt, &len, NULL, 0);
923 #elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
928 mib[1] = VFS_USERMOUNT;
929 sysctl (mib, 2, &usermnt, &len, NULL, 0);
931 #elif defined(CTL_KERN) && defined(KERN_USERMOUNT)
936 mib[1] = KERN_USERMOUNT;
937 sysctl (mib, 2, &usermnt, &len, NULL, 0);
942 while ((fstab = getfsent ()) != NULL)
944 if (strcmp (fstab->fs_vfstype, "swap") == 0)
947 mount_entry = g_new0 (GUnixMountPoint, 1);
949 mount_entry->mount_path = g_strdup (fstab->fs_file);
950 mount_entry->device_path = g_strdup (fstab->fs_spec);
951 mount_entry->filesystem_type = g_strdup (fstab->fs_vfstype);
953 if (strcmp (fstab->fs_type, "ro") == 0)
954 mount_entry->is_read_only = TRUE;
956 #ifdef HAVE_SYS_SYSCTL_H
959 uid_t uid = getuid ();
960 if (stat (fstab->fs_file, &sb) == 0)
962 if (uid == 0 || sb.st_uid == uid)
963 mount_entry->is_user_mountable = TRUE;
968 return_list = g_list_prepend (return_list, mount_entry);
973 return g_list_reverse (return_list);
976 #error No g_get_mount_table() implementation for system
980 get_mounts_timestamp (void)
982 const char *monitor_file;
985 monitor_file = get_mtab_monitor_file ();
988 if (stat (monitor_file, &buf) == 0)
989 return (guint64)buf.st_mtime;
995 get_mount_points_timestamp (void)
997 const char *monitor_file;
1000 monitor_file = get_fstab_file ();
1003 if (stat (monitor_file, &buf) == 0)
1004 return (guint64)buf.st_mtime;
1010 * g_unix_mounts_get:
1011 * @time_read: guint64 to contain a timestamp.
1013 * Gets a #GList of strings containing the unix mounts.
1014 * If @time_read is set, it will be filled with the mount
1015 * timestamp, allowing for checking if the mounts have changed
1016 * with g_unix_mounts_changed_since().
1018 * Returns: a #GList of the UNIX mounts.
1021 g_unix_mounts_get (guint64 *time_read)
1024 *time_read = get_mounts_timestamp ();
1026 return _g_get_unix_mounts ();
1031 * @mount_path: path for a possible unix mount.
1032 * @time_read: guint64 to contain a timestamp.
1034 * Gets a #GUnixMountEntry for a given mount path. If @time_read
1035 * is set, it will be filled with a unix timestamp for checking
1036 * if the mounts have changed since with g_unix_mounts_changed_since().
1038 * Returns: a #GUnixMount.
1041 g_unix_mount_at (const char *mount_path,
1045 GUnixMountEntry *mount_entry, *found;
1047 mounts = g_unix_mounts_get (time_read);
1050 for (l = mounts; l != NULL; l = l->next)
1052 mount_entry = l->data;
1054 if (strcmp (mount_path, mount_entry->mount_path) == 0)
1055 found = mount_entry;
1057 g_unix_mount_free (mount_entry);
1060 g_list_free (mounts);
1066 * g_unix_mount_points_get:
1067 * @time_read: guint64 to contain a timestamp.
1069 * Gets a #GList of strings containing the unix mount points.
1070 * If @time_read is set, it will be filled with the mount timestamp,
1071 * allowing for checking if the mounts have changed with
1072 * g_unix_mounts_points_changed_since().
1074 * Returns: a #GList of the UNIX mountpoints.
1077 g_unix_mount_points_get (guint64 *time_read)
1080 *time_read = get_mount_points_timestamp ();
1082 return _g_get_unix_mount_points ();
1086 * g_unix_mounts_changed_since:
1087 * @time: guint64 to contain a timestamp.
1089 * Checks if the unix mounts have changed since a given unix time.
1091 * Returns: %TRUE if the mounts have changed since @time.
1094 g_unix_mounts_changed_since (guint64 time)
1096 return get_mounts_timestamp () != time;
1100 * g_unix_mount_points_changed_since:
1101 * @time: guint64 to contain a timestamp.
1103 * Checks if the unix mount points have changed since a given unix time.
1105 * Returns: %TRUE if the mount points have changed since @time.
1108 g_unix_mount_points_changed_since (guint64 time)
1110 return get_mount_points_timestamp () != time;
1114 g_unix_mount_monitor_finalize (GObject *object)
1116 GUnixMountMonitor *monitor;
1118 monitor = G_UNIX_MOUNT_MONITOR (object);
1120 if (monitor->fstab_monitor)
1122 g_file_monitor_cancel (monitor->fstab_monitor);
1123 g_object_unref (monitor->fstab_monitor);
1126 if (monitor->mtab_monitor)
1128 g_file_monitor_cancel (monitor->mtab_monitor);
1129 g_object_unref (monitor->mtab_monitor);
1132 the_mount_monitor = NULL;
1134 if (G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize)
1135 (*G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize) (object);
1140 g_unix_mount_monitor_class_init (GUnixMountMonitorClass *klass)
1142 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1144 gobject_class->finalize = g_unix_mount_monitor_finalize;
1146 * GUnixMountMonitor::mounts-changed:
1148 * Emitted when the unix mounts have changed.
1150 signals[MOUNTS_CHANGED] =
1151 g_signal_new ("mounts_changed",
1152 G_TYPE_FROM_CLASS (klass),
1156 g_cclosure_marshal_VOID__VOID,
1159 * GUnixMountMonitor::mountpoints-changed:
1161 * Emitted when the unix mount points have changed.
1163 signals[MOUNTPOINTS_CHANGED] =
1164 g_signal_new ("mountpoints_changed",
1165 G_TYPE_FROM_CLASS (klass),
1169 g_cclosure_marshal_VOID__VOID,
1174 fstab_file_changed (GFileMonitor *monitor,
1177 GFileMonitorEvent event_type,
1180 GUnixMountMonitor *mount_monitor;
1182 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1183 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1184 event_type != G_FILE_MONITOR_EVENT_DELETED)
1187 mount_monitor = user_data;
1188 g_signal_emit (mount_monitor, signals[MOUNTPOINTS_CHANGED], 0);
1192 mtab_file_changed (GFileMonitor *monitor,
1195 GFileMonitorEvent event_type,
1198 GUnixMountMonitor *mount_monitor;
1200 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1201 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1202 event_type != G_FILE_MONITOR_EVENT_DELETED)
1205 mount_monitor = user_data;
1206 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1210 g_unix_mount_monitor_init (GUnixMountMonitor *monitor)
1214 if (get_fstab_file () != NULL)
1216 file = g_file_new_for_path (get_fstab_file ());
1217 monitor->fstab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1218 g_object_unref (file);
1220 g_signal_connect (monitor->fstab_monitor, "changed", (GCallback)fstab_file_changed, monitor);
1223 if (get_mtab_monitor_file () != NULL)
1225 file = g_file_new_for_path (get_mtab_monitor_file ());
1226 monitor->mtab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1227 g_object_unref (file);
1229 g_signal_connect (monitor->mtab_monitor, "changed", (GCallback)mtab_file_changed, monitor);
1234 * g_unix_mount_monitor_set_rate_limit:
1235 * @mount_monitor: a #GUnixMountMonitor.
1236 * @limit_msecs: a integer with the limit in milliseconds to
1239 * Sets the rate limit to which the @mount_monitor will report
1240 * consecutive change events to the mount and mount point entry files.
1245 g_unix_mount_monitor_set_rate_limit (GUnixMountMonitor *mount_monitor,
1248 g_return_if_fail (G_IS_UNIX_MOUNT_MONITOR (mount_monitor));
1250 if (mount_monitor->fstab_monitor != NULL)
1251 g_file_monitor_set_rate_limit (mount_monitor->fstab_monitor, limit_msec);
1253 if (mount_monitor->mtab_monitor != NULL)
1254 g_file_monitor_set_rate_limit (mount_monitor->mtab_monitor, limit_msec);
1258 * g_unix_mount_monitor_new:
1260 * Gets a new #GUnixMountMonitor. The default rate limit for which the
1261 * monitor will report consecutive changes for the mount and mount
1262 * point entry files is the default for a #GFileMonitor. Use
1263 * g_unix_mount_monitor_set_rate_limit() to change this.
1265 * Returns: a #GUnixMountMonitor.
1268 g_unix_mount_monitor_new (void)
1270 if (the_mount_monitor == NULL)
1272 the_mount_monitor = g_object_new (G_TYPE_UNIX_MOUNT_MONITOR, NULL);
1273 return the_mount_monitor;
1276 return g_object_ref (the_mount_monitor);
1280 * g_unix_mount_free:
1281 * @mount_entry: a #GUnixMount.
1283 * Frees a unix mount.
1286 g_unix_mount_free (GUnixMountEntry *mount_entry)
1288 g_return_if_fail (mount_entry != NULL);
1290 g_free (mount_entry->mount_path);
1291 g_free (mount_entry->device_path);
1292 g_free (mount_entry->filesystem_type);
1293 g_free (mount_entry);
1297 * g_unix_mount_point_free:
1298 * @mount_point: unix mount point to free.
1300 * Frees a unix mount point.
1303 g_unix_mount_point_free (GUnixMountPoint *mount_point)
1305 g_return_if_fail (mount_point != NULL);
1307 g_free (mount_point->mount_path);
1308 g_free (mount_point->device_path);
1309 g_free (mount_point->filesystem_type);
1310 g_free (mount_point);
1314 strcmp_null (const char *str1,
1319 if (str1 == NULL && str2 != NULL)
1321 if (str1 != NULL && str2 == NULL)
1323 return strcmp (str1, str2);
1327 * g_unix_mount_compare:
1328 * @mount1: first #GUnixMountEntry to compare.
1329 * @mount2: second #GUnixMountEntry to compare.
1331 * Compares two unix mounts.
1333 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1334 * or less than @mount2, respectively.
1337 g_unix_mount_compare (GUnixMountEntry *mount1,
1338 GUnixMountEntry *mount2)
1342 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1344 res = strcmp_null (mount1->mount_path, mount2->mount_path);
1348 res = strcmp_null (mount1->device_path, mount2->device_path);
1352 res = strcmp_null (mount1->filesystem_type, mount2->filesystem_type);
1356 res = mount1->is_read_only - mount2->is_read_only;
1364 * g_unix_mount_get_mount_path:
1365 * @mount_entry: input #GUnixMountEntry to get the mount path for.
1367 * Gets the mount path for a unix mount.
1369 * Returns: the mount path for @mount_entry.
1372 g_unix_mount_get_mount_path (GUnixMountEntry *mount_entry)
1374 g_return_val_if_fail (mount_entry != NULL, NULL);
1376 return mount_entry->mount_path;
1380 * g_unix_mount_get_device_path:
1381 * @mount_entry: a #GUnixMount.
1383 * Gets the device path for a unix mount.
1385 * Returns: a string containing the device path.
1388 g_unix_mount_get_device_path (GUnixMountEntry *mount_entry)
1390 g_return_val_if_fail (mount_entry != NULL, NULL);
1392 return mount_entry->device_path;
1396 * g_unix_mount_get_fs_type:
1397 * @mount_entry: a #GUnixMount.
1399 * Gets the filesystem type for the unix mount.
1401 * Returns: a string containing the file system type.
1404 g_unix_mount_get_fs_type (GUnixMountEntry *mount_entry)
1406 g_return_val_if_fail (mount_entry != NULL, NULL);
1408 return mount_entry->filesystem_type;
1412 * g_unix_mount_is_readonly:
1413 * @mount_entry: a #GUnixMount.
1415 * Checks if a unix mount is mounted read only.
1417 * Returns: %TRUE if @mount_entry is read only.
1420 g_unix_mount_is_readonly (GUnixMountEntry *mount_entry)
1422 g_return_val_if_fail (mount_entry != NULL, FALSE);
1424 return mount_entry->is_read_only;
1428 * g_unix_mount_is_system_internal:
1429 * @mount_entry: a #GUnixMount.
1431 * Checks if a unix mount is a system path.
1433 * Returns: %TRUE if the unix mount is for a system path.
1436 g_unix_mount_is_system_internal (GUnixMountEntry *mount_entry)
1438 g_return_val_if_fail (mount_entry != NULL, FALSE);
1440 return mount_entry->is_system_internal;
1444 * g_unix_mount_point_compare:
1445 * @mount1: a #GUnixMount.
1446 * @mount2: a #GUnixMount.
1448 * Compares two unix mount points.
1450 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1451 * or less than @mount2, respectively.
1454 g_unix_mount_point_compare (GUnixMountPoint *mount1,
1455 GUnixMountPoint *mount2)
1459 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1461 res = strcmp_null (mount1->mount_path, mount2->mount_path);
1465 res = strcmp_null (mount1->device_path, mount2->device_path);
1469 res = strcmp_null (mount1->filesystem_type, mount2->filesystem_type);
1473 res = mount1->is_read_only - mount2->is_read_only;
1477 res = mount1->is_user_mountable - mount2->is_user_mountable;
1481 res = mount1->is_loopback - mount2->is_loopback;
1489 * g_unix_mount_point_get_mount_path:
1490 * @mount_point: a #GUnixMountPoint.
1492 * Gets the mount path for a unix mount point.
1494 * Returns: a string containing the mount path.
1497 g_unix_mount_point_get_mount_path (GUnixMountPoint *mount_point)
1499 g_return_val_if_fail (mount_point != NULL, NULL);
1501 return mount_point->mount_path;
1505 * g_unix_mount_point_get_device_path:
1506 * @mount_point: a #GUnixMountPoint.
1508 * Gets the device path for a unix mount point.
1510 * Returns: a string containing the device path.
1513 g_unix_mount_point_get_device_path (GUnixMountPoint *mount_point)
1515 g_return_val_if_fail (mount_point != NULL, NULL);
1517 return mount_point->device_path;
1521 * g_unix_mount_point_get_fs_type:
1522 * @mount_point: a #GUnixMountPoint.
1524 * Gets the file system type for the mount point.
1526 * Returns: a string containing the file system type.
1529 g_unix_mount_point_get_fs_type (GUnixMountPoint *mount_point)
1531 g_return_val_if_fail (mount_point != NULL, NULL);
1533 return mount_point->filesystem_type;
1537 * g_unix_mount_point_is_readonly:
1538 * @mount_point: a #GUnixMountPoint.
1540 * Checks if a unix mount point is read only.
1542 * Returns: %TRUE if a mount point is read only.
1545 g_unix_mount_point_is_readonly (GUnixMountPoint *mount_point)
1547 g_return_val_if_fail (mount_point != NULL, FALSE);
1549 return mount_point->is_read_only;
1553 * g_unix_mount_point_is_user_mountable:
1554 * @mount_point: a #GUnixMountPoint.
1556 * Checks if a unix mount point is mountable by the user.
1558 * Returns: %TRUE if the mount point is user mountable.
1561 g_unix_mount_point_is_user_mountable (GUnixMountPoint *mount_point)
1563 g_return_val_if_fail (mount_point != NULL, FALSE);
1565 return mount_point->is_user_mountable;
1569 * g_unix_mount_point_is_loopback:
1570 * @mount_point: a #GUnixMountPoint.
1572 * Checks if a unix mount point is a loopback device.
1574 * Returns: %TRUE if the mount point is a loopback. %FALSE otherwise.
1577 g_unix_mount_point_is_loopback (GUnixMountPoint *mount_point)
1579 g_return_val_if_fail (mount_point != NULL, FALSE);
1581 return mount_point->is_loopback;
1584 static GUnixMountType
1585 guess_mount_type (const char *mount_path,
1586 const char *device_path,
1587 const char *filesystem_type)
1589 GUnixMountType type;
1592 type = G_UNIX_MOUNT_TYPE_UNKNOWN;
1594 if ((strcmp (filesystem_type, "udf") == 0) ||
1595 (strcmp (filesystem_type, "iso9660") == 0) ||
1596 (strcmp (filesystem_type, "cd9660") == 0))
1597 type = G_UNIX_MOUNT_TYPE_CDROM;
1598 else if ((strcmp (filesystem_type, "nfs") == 0) ||
1599 (strcmp (filesystem_type, "nfs4") == 0))
1600 type = G_UNIX_MOUNT_TYPE_NFS;
1601 else if (g_str_has_prefix (device_path, "/vol/dev/diskette/") ||
1602 g_str_has_prefix (device_path, "/dev/fd") ||
1603 g_str_has_prefix (device_path, "/dev/floppy"))
1604 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1605 else if (g_str_has_prefix (device_path, "/dev/cdrom") ||
1606 g_str_has_prefix (device_path, "/dev/acd") ||
1607 g_str_has_prefix (device_path, "/dev/cd"))
1608 type = G_UNIX_MOUNT_TYPE_CDROM;
1609 else if (g_str_has_prefix (device_path, "/vol/"))
1611 const char *name = mount_path + strlen ("/");
1613 if (g_str_has_prefix (name, "cdrom"))
1614 type = G_UNIX_MOUNT_TYPE_CDROM;
1615 else if (g_str_has_prefix (name, "floppy") ||
1616 g_str_has_prefix (device_path, "/vol/dev/diskette/"))
1617 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1618 else if (g_str_has_prefix (name, "rmdisk"))
1619 type = G_UNIX_MOUNT_TYPE_ZIP;
1620 else if (g_str_has_prefix (name, "jaz"))
1621 type = G_UNIX_MOUNT_TYPE_JAZ;
1622 else if (g_str_has_prefix (name, "memstick"))
1623 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1627 basename = g_path_get_basename (mount_path);
1629 if (g_str_has_prefix (basename, "cdr") ||
1630 g_str_has_prefix (basename, "cdwriter") ||
1631 g_str_has_prefix (basename, "burn") ||
1632 g_str_has_prefix (basename, "dvdr"))
1633 type = G_UNIX_MOUNT_TYPE_CDROM;
1634 else if (g_str_has_prefix (basename, "floppy"))
1635 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1636 else if (g_str_has_prefix (basename, "zip"))
1637 type = G_UNIX_MOUNT_TYPE_ZIP;
1638 else if (g_str_has_prefix (basename, "jaz"))
1639 type = G_UNIX_MOUNT_TYPE_JAZ;
1640 else if (g_str_has_prefix (basename, "camera"))
1641 type = G_UNIX_MOUNT_TYPE_CAMERA;
1642 else if (g_str_has_prefix (basename, "memstick") ||
1643 g_str_has_prefix (basename, "memory_stick") ||
1644 g_str_has_prefix (basename, "ram"))
1645 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1646 else if (g_str_has_prefix (basename, "compact_flash"))
1647 type = G_UNIX_MOUNT_TYPE_CF;
1648 else if (g_str_has_prefix (basename, "smart_media"))
1649 type = G_UNIX_MOUNT_TYPE_SM;
1650 else if (g_str_has_prefix (basename, "sd_mmc"))
1651 type = G_UNIX_MOUNT_TYPE_SDMMC;
1652 else if (g_str_has_prefix (basename, "ipod"))
1653 type = G_UNIX_MOUNT_TYPE_IPOD;
1658 if (type == G_UNIX_MOUNT_TYPE_UNKNOWN)
1659 type = G_UNIX_MOUNT_TYPE_HD;
1665 * g_unix_mount_guess_type:
1666 * @mount_entry: a #GUnixMount.
1668 * Guesses the type of a unix mount. If the mount type cannot be
1669 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1671 * Returns: a #GUnixMountType.
1673 static GUnixMountType
1674 g_unix_mount_guess_type (GUnixMountEntry *mount_entry)
1676 g_return_val_if_fail (mount_entry != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1677 g_return_val_if_fail (mount_entry->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1678 g_return_val_if_fail (mount_entry->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1679 g_return_val_if_fail (mount_entry->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1681 return guess_mount_type (mount_entry->mount_path,
1682 mount_entry->device_path,
1683 mount_entry->filesystem_type);
1687 * g_unix_mount_point_guess_type:
1688 * @mount_point: a #GUnixMountPoint.
1690 * Guesses the type of a unix mount point.
1691 * If the mount type cannot be determined,
1692 * returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1694 * Returns: a #GUnixMountType.
1696 static GUnixMountType
1697 g_unix_mount_point_guess_type (GUnixMountPoint *mount_point)
1699 g_return_val_if_fail (mount_point != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1700 g_return_val_if_fail (mount_point->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1701 g_return_val_if_fail (mount_point->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1702 g_return_val_if_fail (mount_point->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1704 return guess_mount_type (mount_point->mount_path,
1705 mount_point->device_path,
1706 mount_point->filesystem_type);
1710 type_to_icon (GUnixMountType type, gboolean is_mount_point)
1712 const char *icon_name;
1716 case G_UNIX_MOUNT_TYPE_HD:
1718 icon_name = "drive-removable-media";
1720 icon_name = "drive-harddisk";
1722 case G_UNIX_MOUNT_TYPE_FLOPPY:
1723 case G_UNIX_MOUNT_TYPE_ZIP:
1724 case G_UNIX_MOUNT_TYPE_JAZ:
1726 icon_name = "drive-removable-media";
1728 icon_name = "media-floppy";
1730 case G_UNIX_MOUNT_TYPE_CDROM:
1732 icon_name = "drive-optical";
1734 icon_name = "media-optical";
1736 case G_UNIX_MOUNT_TYPE_NFS:
1737 /* TODO: Would like a better icon here... */
1739 icon_name = "drive-removable-media";
1741 icon_name = "drive-harddisk";
1743 case G_UNIX_MOUNT_TYPE_MEMSTICK:
1745 icon_name = "drive-removable-media";
1747 icon_name = "media-flash";
1749 case G_UNIX_MOUNT_TYPE_CAMERA:
1751 icon_name = "drive-removable-media";
1753 icon_name = "camera-photo";
1755 case G_UNIX_MOUNT_TYPE_IPOD:
1757 icon_name = "drive-removable-media";
1759 icon_name = "multimedia-player";
1761 case G_UNIX_MOUNT_TYPE_UNKNOWN:
1764 icon_name = "drive-removable-media";
1766 icon_name = "drive-harddisk";
1774 * g_unix_mount_guess_name:
1775 * @mount_entry: a #GUnixMountEntry
1777 * Guesses the name of a Unix mount.
1778 * The result is a translated string.
1780 * Returns: A newly allocated string that must
1781 * be freed with g_free()
1784 g_unix_mount_guess_name (GUnixMountEntry *mount_entry)
1788 if (strcmp (mount_entry->mount_path, "/") == 0)
1789 name = g_strdup (_("Filesystem root"));
1791 name = g_filename_display_basename (mount_entry->mount_path);
1797 * g_unix_mount_guess_icon:
1798 * @mount_entry: a #GUnixMountEntry
1800 * Guesses the icon of a Unix mount.
1805 g_unix_mount_guess_icon (GUnixMountEntry *mount_entry)
1807 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE));
1811 * g_unix_mount_point_guess_name:
1812 * @mount_point: a #GUnixMountPoint
1814 * Guesses the name of a Unix mount point.
1815 * The result is a translated string.
1817 * Returns: A newly allocated string that must
1818 * be freed with g_free()
1821 g_unix_mount_point_guess_name (GUnixMountPoint *mount_point)
1825 if (strcmp (mount_point->mount_path, "/") == 0)
1826 name = g_strdup (_("Filesystem root"));
1828 name = g_filename_display_basename (mount_point->mount_path);
1834 * g_unix_mount_point_guess_icon:
1835 * @mount_point: a #GUnixMountPoint
1837 * Guesses the icon of a Unix mount point.
1842 g_unix_mount_point_guess_icon (GUnixMountPoint *mount_point)
1844 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE));
1848 * g_unix_mount_guess_can_eject:
1849 * @mount_entry: a #GUnixMountEntry
1851 * Guesses whether a Unix mount can be ejected.
1853 * Returns: %TRUE if @mount_entry is deemed to be ejectable.
1856 g_unix_mount_guess_can_eject (GUnixMountEntry *mount_entry)
1858 GUnixMountType guessed_type;
1860 guessed_type = g_unix_mount_guess_type (mount_entry);
1861 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
1862 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
1869 * g_unix_mount_guess_should_display:
1870 * @mount_entry: a #GUnixMountEntry
1872 * Guesses whether a Unix mount should be displayed in the UI.
1874 * Returns: %TRUE if @mount_entry is deemed to be displayable.
1877 g_unix_mount_guess_should_display (GUnixMountEntry *mount_entry)
1879 const char *mount_path;
1881 /* Never display internal mountpoints */
1882 if (g_unix_mount_is_system_internal (mount_entry))
1885 /* Only display things in /media (which are generally user mountable)
1886 and home dir (fuse stuff) */
1887 mount_path = mount_entry->mount_path;
1888 if (mount_path != NULL)
1890 if (strstr (mount_path, "/.gvfs") != NULL)
1893 if (g_str_has_prefix (mount_path, "/media/"))
1896 if (g_str_has_prefix (mount_path, g_get_home_dir ()))
1904 * g_unix_mount_point_guess_can_eject:
1905 * @mount_point: a #GUnixMountPoint
1907 * Guesses whether a Unix mount point can be ejected.
1909 * Returns: %TRUE if @mount_point is deemed to be ejectable.
1912 g_unix_mount_point_guess_can_eject (GUnixMountPoint *mount_point)
1914 GUnixMountType guessed_type;
1916 guessed_type = g_unix_mount_point_guess_type (mount_point);
1917 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
1918 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
1925 /* borrowed from gtk/gtkfilesystemunix.c in GTK+ on 02/23/2006 */
1927 _canonicalize_filename (gchar *filename)
1930 gboolean last_was_slash = FALSE;
1937 if (*p == G_DIR_SEPARATOR)
1939 if (!last_was_slash)
1940 *q++ = G_DIR_SEPARATOR;
1942 last_was_slash = TRUE;
1946 if (last_was_slash && *p == '.')
1948 if (*(p + 1) == G_DIR_SEPARATOR ||
1951 if (*(p + 1) == '\0')
1956 else if (*(p + 1) == '.' &&
1957 (*(p + 2) == G_DIR_SEPARATOR ||
1960 if (q > filename + 1)
1963 while (q > filename + 1 &&
1964 *(q - 1) != G_DIR_SEPARATOR)
1968 if (*(p + 2) == '\0')
1976 last_was_slash = FALSE;
1982 last_was_slash = FALSE;
1989 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
1996 _resolve_symlink (const char *file)
2004 f = g_strdup (file);
2006 while (g_file_test (f, G_FILE_TEST_IS_SYMLINK)) {
2007 link = g_file_read_link (f, &error);
2009 g_error_free (error);
2015 dir = g_path_get_dirname (f);
2016 f1 = g_strdup_printf ("%s/%s", dir, link);
2025 _canonicalize_filename (f);
2029 #ifdef HAVE_MNTENT_H
2031 _resolve_dev_root (void)
2033 static gboolean have_real_dev_root = FALSE;
2034 static char real_dev_root[256];
2035 struct stat statbuf;
2037 /* see if it's cached already */
2038 if (have_real_dev_root)
2041 /* otherwise we're going to find it right away.. */
2042 have_real_dev_root = TRUE;
2044 if (stat ("/dev/root", &statbuf) == 0) {
2045 if (! S_ISLNK (statbuf.st_mode)) {
2046 dev_t root_dev = statbuf.st_dev;
2050 /* see if device with similar major:minor as /dev/root is mention
2051 * in /etc/mtab (it usually is)
2053 f = fopen ("/etc/mtab", "r");
2055 struct mntent *entp;
2056 #ifdef HAVE_GETMNTENT_R
2058 while ((entp = getmntent_r (f, &ent, buf, sizeof (buf))) != NULL) {
2061 while ((entp = getmntent (f)) != NULL) {
2063 if (stat (entp->mnt_fsname, &statbuf) == 0 &&
2064 statbuf.st_dev == root_dev) {
2065 strncpy (real_dev_root, entp->mnt_fsname, sizeof (real_dev_root) - 1);
2066 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2074 #ifndef HAVE_GETMNTENT_R
2075 G_UNLOCK (getmntent);
2079 /* no, that didn't work.. next we could scan /dev ... but I digress.. */
2083 resolved = _resolve_symlink ("/dev/root");
2084 if (resolved != NULL) {
2085 strncpy (real_dev_root, resolved, sizeof (real_dev_root) - 1);
2086 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2094 strcpy (real_dev_root, "/dev/root");
2097 return real_dev_root;
2101 #define __G_UNIX_MOUNTS_C__
2102 #include "gioaliasdef.c"