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>
48 #include <sys/statfs.h>
50 #if HAVE_SYS_STATVFS_H
51 #include <sys/statvfs.h>
55 #elif HAVE_SYS_MOUNT_H
57 #include <sys/param.h>
59 #include <sys/mount.h>
66 #include "gunixmounts.h"
68 #include "gfilemonitor.h"
70 #include "gthemedicon.h"
74 static const char *_resolve_dev_root (void);
79 * @include: gio/gunixmounts.h
80 * @short_description: UNIX mounts
82 * Routines for managing mounted UNIX mount points and paths.
84 * Note that <filename><gio/gunixmounts.h></filename> belongs to the
85 * UNIX-specific GIO interfaces, thus you have to use the
86 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
91 * @G_UNIX_MOUNT_TYPE_UNKNOWN: Unknown UNIX mount type.
92 * @G_UNIX_MOUNT_TYPE_FLOPPY: Floppy disk UNIX mount type.
93 * @G_UNIX_MOUNT_TYPE_CDROM: CDROM UNIX mount type.
94 * @G_UNIX_MOUNT_TYPE_NFS: Network File System (NFS) UNIX mount type.
95 * @G_UNIX_MOUNT_TYPE_ZIP: ZIP UNIX mount type.
96 * @G_UNIX_MOUNT_TYPE_JAZ: JAZZ UNIX mount type.
97 * @G_UNIX_MOUNT_TYPE_MEMSTICK: Memory Stick UNIX mount type.
98 * @G_UNIX_MOUNT_TYPE_CF: Compact Flash UNIX mount type.
99 * @G_UNIX_MOUNT_TYPE_SM: Smart Media UNIX mount type.
100 * @G_UNIX_MOUNT_TYPE_SDMMC: SD/MMC UNIX mount type.
101 * @G_UNIX_MOUNT_TYPE_IPOD: iPod UNIX mount type.
102 * @G_UNIX_MOUNT_TYPE_CAMERA: Digital camera UNIX mount type.
103 * @G_UNIX_MOUNT_TYPE_HD: Hard drive UNIX mount type.
105 * Types of UNIX mounts.
108 G_UNIX_MOUNT_TYPE_UNKNOWN,
109 G_UNIX_MOUNT_TYPE_FLOPPY,
110 G_UNIX_MOUNT_TYPE_CDROM,
111 G_UNIX_MOUNT_TYPE_NFS,
112 G_UNIX_MOUNT_TYPE_ZIP,
113 G_UNIX_MOUNT_TYPE_JAZ,
114 G_UNIX_MOUNT_TYPE_MEMSTICK,
115 G_UNIX_MOUNT_TYPE_CF,
116 G_UNIX_MOUNT_TYPE_SM,
117 G_UNIX_MOUNT_TYPE_SDMMC,
118 G_UNIX_MOUNT_TYPE_IPOD,
119 G_UNIX_MOUNT_TYPE_CAMERA,
123 struct _GUnixMountEntry {
126 char *filesystem_type;
127 gboolean is_read_only;
128 gboolean is_system_internal;
131 struct _GUnixMountPoint {
134 char *filesystem_type;
136 gboolean is_read_only;
137 gboolean is_user_mountable;
138 gboolean is_loopback;
147 static guint signals[LAST_SIGNAL];
149 struct _GUnixMountMonitor {
152 GFileMonitor *fstab_monitor;
153 GFileMonitor *mtab_monitor;
155 GSource *proc_mounts_watch_source;
158 struct _GUnixMountMonitorClass {
159 GObjectClass parent_class;
162 static GUnixMountMonitor *the_mount_monitor = NULL;
164 static GList *_g_get_unix_mounts (void);
165 static GList *_g_get_unix_mount_points (void);
167 G_DEFINE_TYPE (GUnixMountMonitor, g_unix_mount_monitor, G_TYPE_OBJECT);
169 #define MOUNT_POLL_INTERVAL 4000
171 #ifdef HAVE_SYS_MNTTAB_H
172 #define MNTOPT_RO "ro"
177 #elif defined (HAVE_SYS_MNTTAB_H)
178 #include <sys/mnttab.h>
181 #ifdef HAVE_SYS_VFSTAB_H
182 #include <sys/vfstab.h>
185 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
186 #include <sys/mntctl.h>
188 #include <sys/vmount.h>
192 #if (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
193 #include <sys/ucred.h>
194 #include <sys/mount.h>
196 #ifdef HAVE_SYS_SYSCTL_H
197 #include <sys/sysctl.h>
201 #ifndef HAVE_SETMNTENT
202 #define setmntent(f,m) fopen(f,m)
204 #ifndef HAVE_ENDMNTENT
205 #define endmntent(f) fclose(f)
209 is_in (const char *value, const char *set[])
212 for (i = 0; set[i] != NULL; i++)
214 if (strcmp (set[i], value) == 0)
221 * g_unix_is_mount_path_system_internal:
222 * @mount_path: a mount path, e.g. <filename>/media/disk</filename>
223 * or <filename>/usr</filename>
225 * Determines if @mount_path is considered an implementation of the
226 * OS. This is primarily used for hiding mountable and mounted volumes
227 * that only are used in the OS and has little to no relevance to the
230 * Returns: %TRUE if @mount_path is considered an implementation detail
234 g_unix_is_mount_path_system_internal (const char *mount_path)
236 const char *ignore_mountpoints[] = {
237 /* Includes all FHS 2.3 toplevel dirs and other specilized
238 * directories that we want to hide from the user.
240 "/", /* we already have "Filesystem root" in Nautilus */
263 "/var/log/audit", /* https://bugzilla.redhat.com/show_bug.cgi?id=333041 */
266 "/var/tmp", /* https://bugzilla.redhat.com/show_bug.cgi?id=335241 */
274 if (is_in (mount_path, ignore_mountpoints))
277 if (g_str_has_prefix (mount_path, "/dev/") ||
278 g_str_has_prefix (mount_path, "/proc/") ||
279 g_str_has_prefix (mount_path, "/sys/"))
282 if (g_str_has_suffix (mount_path, "/.gvfs"))
289 guess_system_internal (const char *mountpoint,
293 const char *ignore_fs[] = {
314 const char *ignore_devices[] = {
324 if (is_in (fs, ignore_fs))
327 if (is_in (device, ignore_devices))
330 if (g_unix_is_mount_path_system_internal (mountpoint))
339 get_mtab_read_file (void)
343 return "/proc/mounts";
345 return _PATH_MOUNTED;
353 get_mtab_monitor_file (void)
357 return "/proc/mounts";
359 return _PATH_MOUNTED;
366 #ifndef HAVE_GETMNTENT_R
367 G_LOCK_DEFINE_STATIC(getmntent);
371 _g_get_unix_mounts (void)
373 #ifdef HAVE_GETMNTENT_R
377 struct mntent *mntent;
380 GUnixMountEntry *mount_entry;
381 GHashTable *mounts_hash;
384 read_file = get_mtab_read_file ();
386 file = setmntent (read_file, "r");
392 mounts_hash = g_hash_table_new (g_str_hash, g_str_equal);
394 #ifdef HAVE_GETMNTENT_R
395 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
398 while ((mntent = getmntent (file)) != NULL)
401 /* ignore any mnt_fsname that is repeated and begins with a '/'
403 * We do this to avoid being fooled by --bind mounts, since
404 * these have the same device as the location they bind to.
405 * It's not an ideal solution to the problem, but it's likely that
406 * the most important mountpoint is first and the --bind ones after
407 * that aren't as important. So it should work.
409 * The '/' is to handle procfs, tmpfs and other no device mounts.
411 if (mntent->mnt_fsname != NULL &&
412 mntent->mnt_fsname[0] == '/' &&
413 g_hash_table_lookup (mounts_hash, mntent->mnt_fsname))
416 mount_entry = g_new0 (GUnixMountEntry, 1);
417 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
418 if (g_strcmp0 (mntent->mnt_fsname, "/dev/root") == 0)
419 mount_entry->device_path = g_strdup (_resolve_dev_root ());
421 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
422 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
424 #if defined (HAVE_HASMNTOPT)
425 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
426 mount_entry->is_read_only = TRUE;
429 mount_entry->is_system_internal =
430 guess_system_internal (mount_entry->mount_path,
431 mount_entry->filesystem_type,
432 mount_entry->device_path);
434 g_hash_table_insert (mounts_hash,
435 mount_entry->device_path,
436 mount_entry->device_path);
438 return_list = g_list_prepend (return_list, mount_entry);
440 g_hash_table_destroy (mounts_hash);
444 #ifndef HAVE_GETMNTENT_R
445 G_UNLOCK (getmntent);
448 return g_list_reverse (return_list);
451 #elif defined (HAVE_SYS_MNTTAB_H)
453 G_LOCK_DEFINE_STATIC(getmntent);
456 get_mtab_read_file (void)
459 return _PATH_MOUNTED;
461 return "/etc/mnttab";
466 get_mtab_monitor_file (void)
468 return get_mtab_read_file ();
472 _g_get_unix_mounts (void)
474 struct mnttab mntent;
477 GUnixMountEntry *mount_entry;
480 read_file = get_mtab_read_file ();
482 file = setmntent (read_file, "r");
489 while (! getmntent (file, &mntent))
491 mount_entry = g_new0 (GUnixMountEntry, 1);
493 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
494 mount_entry->device_path = g_strdup (mntent.mnt_special);
495 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
497 #if defined (HAVE_HASMNTOPT)
498 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
499 mount_entry->is_read_only = TRUE;
502 mount_entry->is_system_internal =
503 guess_system_internal (mount_entry->mount_path,
504 mount_entry->filesystem_type,
505 mount_entry->device_path);
507 return_list = g_list_prepend (return_list, mount_entry);
512 G_UNLOCK (getmntent);
514 return g_list_reverse (return_list);
517 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
520 get_mtab_monitor_file (void)
526 _g_get_unix_mounts (void)
528 struct vfs_ent *fs_info;
529 struct vmount *vmount_info;
531 unsigned int vmount_size;
535 if (mntctl (MCTL_QUERY, sizeof (vmount_size), &vmount_size) != 0)
537 g_warning ("Unable to know the number of mounted volumes\n");
542 vmount_info = (struct vmount*)g_malloc (vmount_size);
544 vmount_number = mntctl (MCTL_QUERY, vmount_size, vmount_info);
546 if (vmount_info->vmt_revision != VMT_REVISION)
547 g_warning ("Bad vmount structure revision number, want %d, got %d\n", VMT_REVISION, vmount_info->vmt_revision);
549 if (vmount_number < 0)
551 g_warning ("Unable to recover mounted volumes information\n");
553 g_free (vmount_info);
558 while (vmount_number > 0)
560 mount_entry = g_new0 (GUnixMountEntry, 1);
562 mount_entry->device_path = g_strdup (vmt2dataptr (vmount_info, VMT_OBJECT));
563 mount_entry->mount_path = g_strdup (vmt2dataptr (vmount_info, VMT_STUB));
564 /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
565 mount_entry->is_read_only = (vmount_info->vmt_flags & MNT_READONLY) ? 1 : 0;
567 fs_info = getvfsbytype (vmount_info->vmt_gfstype);
570 mount_entry->filesystem_type = g_strdup ("unknown");
572 mount_entry->filesystem_type = g_strdup (fs_info->vfsent_name);
574 mount_entry->is_system_internal =
575 guess_system_internal (mount_entry->mount_path,
576 mount_entry->filesystem_type,
577 mount_entry->device_path);
579 return_list = g_list_prepend (return_list, mount_entry);
581 vmount_info = (struct vmount *)( (char*)vmount_info
582 + vmount_info->vmt_length);
586 g_free (vmount_info);
588 return g_list_reverse (return_list);
591 #elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
594 get_mtab_monitor_file (void)
600 _g_get_unix_mounts (void)
602 #if defined(HAVE_GETVFSSTAT)
603 struct statvfs *mntent = NULL;
604 #elif defined(HAVE_GETFSSTAT)
605 struct statfs *mntent = NULL;
607 #error statfs juggling failed
611 GUnixMountEntry *mount_entry;
614 /* Pass NOWAIT to avoid blocking trying to update NFS mounts. */
615 #if defined(HAVE_GETVFSSTAT)
616 num_mounts = getvfsstat (NULL, 0, ST_NOWAIT);
617 #elif defined(HAVE_GETFSSTAT)
618 num_mounts = getfsstat (NULL, 0, MNT_NOWAIT);
620 if (num_mounts == -1)
623 bufsize = num_mounts * sizeof (*mntent);
624 mntent = g_malloc (bufsize);
625 #if defined(HAVE_GETVFSSTAT)
626 num_mounts = getvfsstat (mntent, bufsize, ST_NOWAIT);
627 #elif defined(HAVE_GETFSSTAT)
628 num_mounts = getfsstat (mntent, bufsize, MNT_NOWAIT);
630 if (num_mounts == -1)
635 for (i = 0; i < num_mounts; i++)
637 mount_entry = g_new0 (GUnixMountEntry, 1);
639 mount_entry->mount_path = g_strdup (mntent[i].f_mntonname);
640 mount_entry->device_path = g_strdup (mntent[i].f_mntfromname);
641 mount_entry->filesystem_type = g_strdup (mntent[i].f_fstypename);
642 #if defined(HAVE_GETVFSSTAT)
643 if (mntent[i].f_flag & ST_RDONLY)
644 #elif defined(HAVE_GETFSSTAT)
645 if (mntent[i].f_flags & MNT_RDONLY)
647 mount_entry->is_read_only = TRUE;
649 mount_entry->is_system_internal =
650 guess_system_internal (mount_entry->mount_path,
651 mount_entry->filesystem_type,
652 mount_entry->device_path);
654 return_list = g_list_prepend (return_list, mount_entry);
659 return g_list_reverse (return_list);
661 #elif defined(__INTERIX)
664 get_mtab_monitor_file (void)
670 _g_get_unix_mounts (void)
673 GList* return_list = NULL;
674 char filename[9 + NAME_MAX];
676 dirp = opendir ("/dev/fs");
679 g_warning ("unable to read /dev/fs!");
685 struct statvfs statbuf;
687 struct dirent* result;
689 if (readdir_r (dirp, &entry, &result) || result == NULL)
692 strcpy (filename, "/dev/fs/");
693 strcat (filename, entry.d_name);
695 if (statvfs (filename, &statbuf) == 0)
697 GUnixMountEntry* mount_entry = g_new0(GUnixMountEntry, 1);
699 mount_entry->mount_path = g_strdup (statbuf.f_mntonname);
700 mount_entry->device_path = g_strdup (statbuf.f_mntfromname);
701 mount_entry->filesystem_type = g_strdup (statbuf.f_fstypename);
703 if (statbuf.f_flag & ST_RDONLY)
704 mount_entry->is_read_only = TRUE;
706 return_list = g_list_prepend(return_list, mount_entry);
710 return_list = g_list_reverse (return_list);
717 #error No _g_get_unix_mounts() implementation for system
720 /* _g_get_unix_mount_points():
722 * don't return swap and ignore mounts.
726 get_fstab_file (void)
728 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
730 return "/etc/filesystems";
731 #elif defined(_PATH_MNTTAB)
733 #elif defined(VFSTAB)
742 _g_get_unix_mount_points (void)
744 #ifdef HAVE_GETMNTENT_R
748 struct mntent *mntent;
751 GUnixMountPoint *mount_entry;
754 read_file = get_fstab_file ();
756 file = setmntent (read_file, "r");
762 #ifdef HAVE_GETMNTENT_R
763 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
766 while ((mntent = getmntent (file)) != NULL)
769 if ((strcmp (mntent->mnt_dir, "ignore") == 0) ||
770 (strcmp (mntent->mnt_dir, "swap") == 0) ||
771 (strcmp (mntent->mnt_dir, "none") == 0))
774 #ifdef HAVE_HASMNTOPT
775 /* We ignore bind fstab entries, as we ignore bind mounts anyway */
776 if (hasmntopt (mntent, "bind"))
780 mount_entry = g_new0 (GUnixMountPoint, 1);
781 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
782 if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
783 mount_entry->device_path = g_strdup (_resolve_dev_root ());
785 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
786 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
787 mount_entry->options = g_strdup (mntent->mnt_opts);
789 #ifdef HAVE_HASMNTOPT
790 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
791 mount_entry->is_read_only = TRUE;
793 if (hasmntopt (mntent, "loop") != NULL)
794 mount_entry->is_loopback = TRUE;
798 if ((mntent->mnt_type != NULL && strcmp ("supermount", mntent->mnt_type) == 0)
799 #ifdef HAVE_HASMNTOPT
800 || (hasmntopt (mntent, "user") != NULL
801 && hasmntopt (mntent, "user") != hasmntopt (mntent, "user_xattr"))
802 || hasmntopt (mntent, "pamconsole") != NULL
803 || hasmntopt (mntent, "users") != NULL
804 || hasmntopt (mntent, "owner") != NULL
807 mount_entry->is_user_mountable = TRUE;
809 return_list = g_list_prepend (return_list, mount_entry);
814 #ifndef HAVE_GETMNTENT_R
815 G_UNLOCK (getmntent);
818 return g_list_reverse (return_list);
821 #elif defined (HAVE_SYS_MNTTAB_H)
824 _g_get_unix_mount_points (void)
826 struct mnttab mntent;
829 GUnixMountPoint *mount_entry;
832 read_file = get_fstab_file ();
834 file = setmntent (read_file, "r");
841 while (! getmntent (file, &mntent))
843 if ((strcmp (mntent.mnt_mountp, "ignore") == 0) ||
844 (strcmp (mntent.mnt_mountp, "swap") == 0) ||
845 (strcmp (mntent.mnt_mountp, "none") == 0))
848 mount_entry = g_new0 (GUnixMountPoint, 1);
850 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
851 mount_entry->device_path = g_strdup (mntent.mnt_special);
852 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
853 mount_entry->options = g_strdup (mntent.mnt_mntopts);
855 #ifdef HAVE_HASMNTOPT
856 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
857 mount_entry->is_read_only = TRUE;
859 if (hasmntopt (&mntent, "lofs") != NULL)
860 mount_entry->is_loopback = TRUE;
863 if ((mntent.mnt_fstype != NULL)
864 #ifdef HAVE_HASMNTOPT
865 || (hasmntopt (&mntent, "user") != NULL
866 && hasmntopt (&mntent, "user") != hasmntopt (&mntent, "user_xattr"))
867 || hasmntopt (&mntent, "pamconsole") != NULL
868 || hasmntopt (&mntent, "users") != NULL
869 || hasmntopt (&mntent, "owner") != NULL
872 mount_entry->is_user_mountable = TRUE;
874 return_list = g_list_prepend (return_list, mount_entry);
878 G_UNLOCK (getmntent);
880 return g_list_reverse (return_list);
882 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
884 /* functions to parse /etc/filesystems on aix */
886 /* read character, ignoring comments (begin with '*', end with '\n' */
888 aix_fs_getc (FILE *fd)
892 while ((c = getc (fd)) == '*')
894 while (((c = getc (fd)) != '\n') && (c != EOF))
899 /* eat all continuous spaces in a file */
901 aix_fs_ignorespace (FILE *fd)
905 while ((c = aix_fs_getc (fd)) != EOF)
907 if (!g_ascii_isspace (c))
917 /* read one word from file */
919 aix_fs_getword (FILE *fd,
924 aix_fs_ignorespace (fd);
926 while (((c = aix_fs_getc (fd)) != EOF) && !g_ascii_isspace (c))
930 while (((c = aix_fs_getc (fd)) != EOF) && (c != '"'))
942 char mnt_mount[PATH_MAX];
943 char mnt_special[PATH_MAX];
945 char mnt_options[128];
946 } AixMountTableEntry;
948 /* read mount points properties */
950 aix_fs_get (FILE *fd,
951 AixMountTableEntry *prop)
953 static char word[PATH_MAX] = { 0 };
954 char value[PATH_MAX];
959 if (aix_fs_getword (fd, word) == EOF)
963 word[strlen(word) - 1] = 0;
964 strcpy (prop->mnt_mount, word);
966 /* read attributes and value */
968 while (aix_fs_getword (fd, word) != EOF)
970 /* test if is attribute or new stanza */
971 if (word[strlen(word) - 1] == ':')
975 aix_fs_getword (fd, value);
978 aix_fs_getword (fd, value);
980 if (strcmp (word, "dev") == 0)
981 strcpy (prop->mnt_special, value);
982 else if (strcmp (word, "vfs") == 0)
983 strcpy (prop->mnt_fstype, value);
984 else if (strcmp (word, "options") == 0)
985 strcpy(prop->mnt_options, value);
992 _g_get_unix_mount_points (void)
994 struct mntent *mntent;
997 GUnixMountPoint *mount_entry;
998 AixMountTableEntry mntent;
1001 read_file = get_fstab_file ();
1003 file = setmntent (read_file, "r");
1009 while (!aix_fs_get (file, &mntent))
1011 if (strcmp ("cdrfs", mntent.mnt_fstype) == 0)
1013 mount_entry = g_new0 (GUnixMountPoint, 1);
1015 mount_entry->mount_path = g_strdup (mntent.mnt_mount);
1016 mount_entry->device_path = g_strdup (mntent.mnt_special);
1017 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
1018 mount_entry->options = g_strdup (mntent.mnt_options);
1019 mount_entry->is_read_only = TRUE;
1020 mount_entry->is_user_mountable = TRUE;
1022 return_list = g_list_prepend (return_list, mount_entry);
1028 return g_list_reverse (return_list);
1031 #elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
1034 _g_get_unix_mount_points (void)
1036 struct fstab *fstab = NULL;
1037 GUnixMountPoint *mount_entry;
1039 #ifdef HAVE_SYS_SYSCTL_H
1041 size_t len = sizeof(usermnt);
1050 #ifdef HAVE_SYS_SYSCTL_H
1051 #if defined(HAVE_SYSCTLBYNAME)
1052 sysctlbyname ("vfs.usermount", &usermnt, &len, NULL, 0);
1053 #elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
1058 mib[1] = VFS_USERMOUNT;
1059 sysctl (mib, 2, &usermnt, &len, NULL, 0);
1061 #elif defined(CTL_KERN) && defined(KERN_USERMOUNT)
1066 mib[1] = KERN_USERMOUNT;
1067 sysctl (mib, 2, &usermnt, &len, NULL, 0);
1072 while ((fstab = getfsent ()) != NULL)
1074 if (strcmp (fstab->fs_vfstype, "swap") == 0)
1077 mount_entry = g_new0 (GUnixMountPoint, 1);
1079 mount_entry->mount_path = g_strdup (fstab->fs_file);
1080 mount_entry->device_path = g_strdup (fstab->fs_spec);
1081 mount_entry->filesystem_type = g_strdup (fstab->fs_vfstype);
1082 mount_entry->options = g_strdup (fstab->fs_mntops);
1084 if (strcmp (fstab->fs_type, "ro") == 0)
1085 mount_entry->is_read_only = TRUE;
1087 #ifdef HAVE_SYS_SYSCTL_H
1090 uid_t uid = getuid ();
1091 if (stat (fstab->fs_file, &sb) == 0)
1093 if (uid == 0 || sb.st_uid == uid)
1094 mount_entry->is_user_mountable = TRUE;
1099 return_list = g_list_prepend (return_list, mount_entry);
1104 return g_list_reverse (return_list);
1106 #elif defined(__INTERIX)
1108 _g_get_unix_mount_points (void)
1110 return _g_get_unix_mounts ();
1113 #error No g_get_mount_table() implementation for system
1117 get_mounts_timestamp (void)
1119 const char *monitor_file;
1122 monitor_file = get_mtab_monitor_file ();
1125 if (stat (monitor_file, &buf) == 0)
1126 return (guint64)buf.st_mtime;
1132 get_mount_points_timestamp (void)
1134 const char *monitor_file;
1137 monitor_file = get_fstab_file ();
1140 if (stat (monitor_file, &buf) == 0)
1141 return (guint64)buf.st_mtime;
1147 * g_unix_mounts_get: (skip)
1148 * @time_read: (out) (allow-none): guint64 to contain a timestamp, or %NULL
1150 * Gets a #GList of #GUnixMountEntry containing the unix mounts.
1151 * If @time_read is set, it will be filled with the mount
1152 * timestamp, allowing for checking if the mounts have changed
1153 * with g_unix_mounts_changed_since().
1155 * Returns: (element-type GUnixMountEntry) (transfer full):
1156 * a #GList of the UNIX mounts.
1159 g_unix_mounts_get (guint64 *time_read)
1162 *time_read = get_mounts_timestamp ();
1164 return _g_get_unix_mounts ();
1168 * g_unix_mount_at: (skip)
1169 * @mount_path: path for a possible unix mount.
1170 * @time_read: (out) (allow-none): guint64 to contain a timestamp.
1172 * Gets a #GUnixMountEntry for a given mount path. If @time_read
1173 * is set, it will be filled with a unix timestamp for checking
1174 * if the mounts have changed since with g_unix_mounts_changed_since().
1176 * Returns: (transfer full): a #GUnixMountEntry.
1179 g_unix_mount_at (const char *mount_path,
1183 GUnixMountEntry *mount_entry, *found;
1185 mounts = g_unix_mounts_get (time_read);
1188 for (l = mounts; l != NULL; l = l->next)
1190 mount_entry = l->data;
1192 if (!found && strcmp (mount_path, mount_entry->mount_path) == 0)
1193 found = mount_entry;
1195 g_unix_mount_free (mount_entry);
1197 g_list_free (mounts);
1203 * g_unix_mount_points_get: (skip)
1204 * @time_read: (out) (allow-none): guint64 to contain a timestamp.
1206 * Gets a #GList of #GUnixMountPoint containing the unix mount points.
1207 * If @time_read is set, it will be filled with the mount timestamp,
1208 * allowing for checking if the mounts have changed with
1209 * g_unix_mount_points_changed_since().
1211 * Returns: (element-type GUnixMountPoint) (transfer full):
1212 * a #GList of the UNIX mountpoints.
1215 g_unix_mount_points_get (guint64 *time_read)
1218 *time_read = get_mount_points_timestamp ();
1220 return _g_get_unix_mount_points ();
1224 * g_unix_mounts_changed_since:
1225 * @time: guint64 to contain a timestamp.
1227 * Checks if the unix mounts have changed since a given unix time.
1229 * Returns: %TRUE if the mounts have changed since @time.
1232 g_unix_mounts_changed_since (guint64 time)
1234 return get_mounts_timestamp () != time;
1238 * g_unix_mount_points_changed_since:
1239 * @time: guint64 to contain a timestamp.
1241 * Checks if the unix mount points have changed since a given unix time.
1243 * Returns: %TRUE if the mount points have changed since @time.
1246 g_unix_mount_points_changed_since (guint64 time)
1248 return get_mount_points_timestamp () != time;
1252 g_unix_mount_monitor_finalize (GObject *object)
1254 GUnixMountMonitor *monitor;
1256 monitor = G_UNIX_MOUNT_MONITOR (object);
1258 if (monitor->fstab_monitor)
1260 g_file_monitor_cancel (monitor->fstab_monitor);
1261 g_object_unref (monitor->fstab_monitor);
1264 if (monitor->proc_mounts_watch_source != NULL)
1265 g_source_destroy (monitor->proc_mounts_watch_source);
1267 if (monitor->mtab_monitor)
1269 g_file_monitor_cancel (monitor->mtab_monitor);
1270 g_object_unref (monitor->mtab_monitor);
1273 the_mount_monitor = NULL;
1275 G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize (object);
1280 g_unix_mount_monitor_class_init (GUnixMountMonitorClass *klass)
1282 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1284 gobject_class->finalize = g_unix_mount_monitor_finalize;
1287 * GUnixMountMonitor::mounts-changed:
1288 * @monitor: the object on which the signal is emitted
1290 * Emitted when the unix mounts have changed.
1292 signals[MOUNTS_CHANGED] =
1293 g_signal_new ("mounts-changed",
1294 G_TYPE_FROM_CLASS (klass),
1298 g_cclosure_marshal_VOID__VOID,
1302 * GUnixMountMonitor::mountpoints-changed:
1303 * @monitor: the object on which the signal is emitted
1305 * Emitted when the unix mount points have changed.
1307 signals[MOUNTPOINTS_CHANGED] =
1308 g_signal_new ("mountpoints-changed",
1309 G_TYPE_FROM_CLASS (klass),
1313 g_cclosure_marshal_VOID__VOID,
1318 fstab_file_changed (GFileMonitor *monitor,
1321 GFileMonitorEvent event_type,
1324 GUnixMountMonitor *mount_monitor;
1326 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1327 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1328 event_type != G_FILE_MONITOR_EVENT_DELETED)
1331 mount_monitor = user_data;
1332 g_signal_emit (mount_monitor, signals[MOUNTPOINTS_CHANGED], 0);
1336 mtab_file_changed (GFileMonitor *monitor,
1339 GFileMonitorEvent event_type,
1342 GUnixMountMonitor *mount_monitor;
1344 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1345 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1346 event_type != G_FILE_MONITOR_EVENT_DELETED)
1349 mount_monitor = user_data;
1350 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1354 proc_mounts_changed (GIOChannel *channel,
1358 GUnixMountMonitor *mount_monitor = G_UNIX_MOUNT_MONITOR (user_data);
1359 if (cond & G_IO_ERR)
1360 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1365 g_unix_mount_monitor_init (GUnixMountMonitor *monitor)
1369 if (get_fstab_file () != NULL)
1371 file = g_file_new_for_path (get_fstab_file ());
1372 monitor->fstab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1373 g_object_unref (file);
1375 g_signal_connect (monitor->fstab_monitor, "changed", (GCallback)fstab_file_changed, monitor);
1378 if (get_mtab_monitor_file () != NULL)
1380 const gchar *mtab_path;
1382 mtab_path = get_mtab_monitor_file ();
1383 /* /proc/mounts monitoring is special - can't just use GFileMonitor.
1384 * See 'man proc' for more details.
1386 if (g_strcmp0 (mtab_path, "/proc/mounts") == 0)
1388 GIOChannel *proc_mounts_channel;
1389 GError *error = NULL;
1390 proc_mounts_channel = g_io_channel_new_file ("/proc/mounts", "r", &error);
1391 if (proc_mounts_channel == NULL)
1393 g_warning ("Error creating IO channel for /proc/mounts: %s (%s, %d)",
1394 error->message, g_quark_to_string (error->domain), error->code);
1395 g_error_free (error);
1399 monitor->proc_mounts_watch_source = g_io_create_watch (proc_mounts_channel, G_IO_ERR);
1400 g_source_set_callback (monitor->proc_mounts_watch_source,
1401 (GSourceFunc) proc_mounts_changed,
1404 g_source_attach (monitor->proc_mounts_watch_source,
1405 g_main_context_get_thread_default ());
1406 g_source_unref (monitor->proc_mounts_watch_source);
1407 g_io_channel_unref (proc_mounts_channel);
1412 file = g_file_new_for_path (mtab_path);
1413 monitor->mtab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1414 g_object_unref (file);
1415 g_signal_connect (monitor->mtab_monitor, "changed", (GCallback)mtab_file_changed, monitor);
1421 * g_unix_mount_monitor_set_rate_limit:
1422 * @mount_monitor: a #GUnixMountMonitor
1423 * @limit_msec: a integer with the limit in milliseconds to
1426 * Sets the rate limit to which the @mount_monitor will report
1427 * consecutive change events to the mount and mount point entry files.
1432 g_unix_mount_monitor_set_rate_limit (GUnixMountMonitor *mount_monitor,
1435 g_return_if_fail (G_IS_UNIX_MOUNT_MONITOR (mount_monitor));
1437 if (mount_monitor->fstab_monitor != NULL)
1438 g_file_monitor_set_rate_limit (mount_monitor->fstab_monitor, limit_msec);
1440 if (mount_monitor->mtab_monitor != NULL)
1441 g_file_monitor_set_rate_limit (mount_monitor->mtab_monitor, limit_msec);
1445 * g_unix_mount_monitor_new:
1447 * Gets a new #GUnixMountMonitor. The default rate limit for which the
1448 * monitor will report consecutive changes for the mount and mount
1449 * point entry files is the default for a #GFileMonitor. Use
1450 * g_unix_mount_monitor_set_rate_limit() to change this.
1452 * Returns: a #GUnixMountMonitor.
1455 g_unix_mount_monitor_new (void)
1457 if (the_mount_monitor == NULL)
1459 the_mount_monitor = g_object_new (G_TYPE_UNIX_MOUNT_MONITOR, NULL);
1460 return the_mount_monitor;
1463 return g_object_ref (the_mount_monitor);
1467 * g_unix_mount_free:
1468 * @mount_entry: a #GUnixMountEntry.
1470 * Frees a unix mount.
1473 g_unix_mount_free (GUnixMountEntry *mount_entry)
1475 g_return_if_fail (mount_entry != NULL);
1477 g_free (mount_entry->mount_path);
1478 g_free (mount_entry->device_path);
1479 g_free (mount_entry->filesystem_type);
1480 g_free (mount_entry);
1484 * g_unix_mount_point_free:
1485 * @mount_point: unix mount point to free.
1487 * Frees a unix mount point.
1490 g_unix_mount_point_free (GUnixMountPoint *mount_point)
1492 g_return_if_fail (mount_point != NULL);
1494 g_free (mount_point->mount_path);
1495 g_free (mount_point->device_path);
1496 g_free (mount_point->filesystem_type);
1497 g_free (mount_point->options);
1498 g_free (mount_point);
1502 * g_unix_mount_compare:
1503 * @mount1: first #GUnixMountEntry to compare.
1504 * @mount2: second #GUnixMountEntry to compare.
1506 * Compares two unix mounts.
1508 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1509 * or less than @mount2, respectively.
1512 g_unix_mount_compare (GUnixMountEntry *mount1,
1513 GUnixMountEntry *mount2)
1517 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1519 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1523 res = g_strcmp0 (mount1->device_path, mount2->device_path);
1527 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1531 res = mount1->is_read_only - mount2->is_read_only;
1539 * g_unix_mount_get_mount_path:
1540 * @mount_entry: input #GUnixMountEntry to get the mount path for.
1542 * Gets the mount path for a unix mount.
1544 * Returns: the mount path for @mount_entry.
1547 g_unix_mount_get_mount_path (GUnixMountEntry *mount_entry)
1549 g_return_val_if_fail (mount_entry != NULL, NULL);
1551 return mount_entry->mount_path;
1555 * g_unix_mount_get_device_path:
1556 * @mount_entry: a #GUnixMount.
1558 * Gets the device path for a unix mount.
1560 * Returns: a string containing the device path.
1563 g_unix_mount_get_device_path (GUnixMountEntry *mount_entry)
1565 g_return_val_if_fail (mount_entry != NULL, NULL);
1567 return mount_entry->device_path;
1571 * g_unix_mount_get_fs_type:
1572 * @mount_entry: a #GUnixMount.
1574 * Gets the filesystem type for the unix mount.
1576 * Returns: a string containing the file system type.
1579 g_unix_mount_get_fs_type (GUnixMountEntry *mount_entry)
1581 g_return_val_if_fail (mount_entry != NULL, NULL);
1583 return mount_entry->filesystem_type;
1587 * g_unix_mount_is_readonly:
1588 * @mount_entry: a #GUnixMount.
1590 * Checks if a unix mount is mounted read only.
1592 * Returns: %TRUE if @mount_entry is read only.
1595 g_unix_mount_is_readonly (GUnixMountEntry *mount_entry)
1597 g_return_val_if_fail (mount_entry != NULL, FALSE);
1599 return mount_entry->is_read_only;
1603 * g_unix_mount_is_system_internal:
1604 * @mount_entry: a #GUnixMount.
1606 * Checks if a unix mount is a system path.
1608 * Returns: %TRUE if the unix mount is for a system path.
1611 g_unix_mount_is_system_internal (GUnixMountEntry *mount_entry)
1613 g_return_val_if_fail (mount_entry != NULL, FALSE);
1615 return mount_entry->is_system_internal;
1619 * g_unix_mount_point_compare:
1620 * @mount1: a #GUnixMount.
1621 * @mount2: a #GUnixMount.
1623 * Compares two unix mount points.
1625 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1626 * or less than @mount2, respectively.
1629 g_unix_mount_point_compare (GUnixMountPoint *mount1,
1630 GUnixMountPoint *mount2)
1634 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1636 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1640 res = g_strcmp0 (mount1->device_path, mount2->device_path);
1644 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1648 res = g_strcmp0 (mount1->options, mount2->options);
1652 res = mount1->is_read_only - mount2->is_read_only;
1656 res = mount1->is_user_mountable - mount2->is_user_mountable;
1660 res = mount1->is_loopback - mount2->is_loopback;
1668 * g_unix_mount_point_get_mount_path:
1669 * @mount_point: a #GUnixMountPoint.
1671 * Gets the mount path for a unix mount point.
1673 * Returns: a string containing the mount path.
1676 g_unix_mount_point_get_mount_path (GUnixMountPoint *mount_point)
1678 g_return_val_if_fail (mount_point != NULL, NULL);
1680 return mount_point->mount_path;
1684 * g_unix_mount_point_get_device_path:
1685 * @mount_point: a #GUnixMountPoint.
1687 * Gets the device path for a unix mount point.
1689 * Returns: a string containing the device path.
1692 g_unix_mount_point_get_device_path (GUnixMountPoint *mount_point)
1694 g_return_val_if_fail (mount_point != NULL, NULL);
1696 return mount_point->device_path;
1700 * g_unix_mount_point_get_fs_type:
1701 * @mount_point: a #GUnixMountPoint.
1703 * Gets the file system type for the mount point.
1705 * Returns: a string containing the file system type.
1708 g_unix_mount_point_get_fs_type (GUnixMountPoint *mount_point)
1710 g_return_val_if_fail (mount_point != NULL, NULL);
1712 return mount_point->filesystem_type;
1716 * g_unix_mount_point_get_options:
1717 * @mount_point: a #GUnixMountPoint.
1719 * Gets the options for the mount point.
1721 * Returns: a string containing the options.
1726 g_unix_mount_point_get_options (GUnixMountPoint *mount_point)
1728 g_return_val_if_fail (mount_point != NULL, NULL);
1730 return mount_point->options;
1734 * g_unix_mount_point_is_readonly:
1735 * @mount_point: a #GUnixMountPoint.
1737 * Checks if a unix mount point is read only.
1739 * Returns: %TRUE if a mount point is read only.
1742 g_unix_mount_point_is_readonly (GUnixMountPoint *mount_point)
1744 g_return_val_if_fail (mount_point != NULL, FALSE);
1746 return mount_point->is_read_only;
1750 * g_unix_mount_point_is_user_mountable:
1751 * @mount_point: a #GUnixMountPoint.
1753 * Checks if a unix mount point is mountable by the user.
1755 * Returns: %TRUE if the mount point is user mountable.
1758 g_unix_mount_point_is_user_mountable (GUnixMountPoint *mount_point)
1760 g_return_val_if_fail (mount_point != NULL, FALSE);
1762 return mount_point->is_user_mountable;
1766 * g_unix_mount_point_is_loopback:
1767 * @mount_point: a #GUnixMountPoint.
1769 * Checks if a unix mount point is a loopback device.
1771 * Returns: %TRUE if the mount point is a loopback. %FALSE otherwise.
1774 g_unix_mount_point_is_loopback (GUnixMountPoint *mount_point)
1776 g_return_val_if_fail (mount_point != NULL, FALSE);
1778 return mount_point->is_loopback;
1781 static GUnixMountType
1782 guess_mount_type (const char *mount_path,
1783 const char *device_path,
1784 const char *filesystem_type)
1786 GUnixMountType type;
1789 type = G_UNIX_MOUNT_TYPE_UNKNOWN;
1791 if ((strcmp (filesystem_type, "udf") == 0) ||
1792 (strcmp (filesystem_type, "iso9660") == 0) ||
1793 (strcmp (filesystem_type, "cd9660") == 0))
1794 type = G_UNIX_MOUNT_TYPE_CDROM;
1795 else if ((strcmp (filesystem_type, "nfs") == 0) ||
1796 (strcmp (filesystem_type, "nfs4") == 0))
1797 type = G_UNIX_MOUNT_TYPE_NFS;
1798 else if (g_str_has_prefix (device_path, "/vol/dev/diskette/") ||
1799 g_str_has_prefix (device_path, "/dev/fd") ||
1800 g_str_has_prefix (device_path, "/dev/floppy"))
1801 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1802 else if (g_str_has_prefix (device_path, "/dev/cdrom") ||
1803 g_str_has_prefix (device_path, "/dev/acd") ||
1804 g_str_has_prefix (device_path, "/dev/cd"))
1805 type = G_UNIX_MOUNT_TYPE_CDROM;
1806 else if (g_str_has_prefix (device_path, "/vol/"))
1808 const char *name = mount_path + strlen ("/");
1810 if (g_str_has_prefix (name, "cdrom"))
1811 type = G_UNIX_MOUNT_TYPE_CDROM;
1812 else if (g_str_has_prefix (name, "floppy") ||
1813 g_str_has_prefix (device_path, "/vol/dev/diskette/"))
1814 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1815 else if (g_str_has_prefix (name, "rmdisk"))
1816 type = G_UNIX_MOUNT_TYPE_ZIP;
1817 else if (g_str_has_prefix (name, "jaz"))
1818 type = G_UNIX_MOUNT_TYPE_JAZ;
1819 else if (g_str_has_prefix (name, "memstick"))
1820 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1824 basename = g_path_get_basename (mount_path);
1826 if (g_str_has_prefix (basename, "cdr") ||
1827 g_str_has_prefix (basename, "cdwriter") ||
1828 g_str_has_prefix (basename, "burn") ||
1829 g_str_has_prefix (basename, "dvdr"))
1830 type = G_UNIX_MOUNT_TYPE_CDROM;
1831 else if (g_str_has_prefix (basename, "floppy"))
1832 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1833 else if (g_str_has_prefix (basename, "zip"))
1834 type = G_UNIX_MOUNT_TYPE_ZIP;
1835 else if (g_str_has_prefix (basename, "jaz"))
1836 type = G_UNIX_MOUNT_TYPE_JAZ;
1837 else if (g_str_has_prefix (basename, "camera"))
1838 type = G_UNIX_MOUNT_TYPE_CAMERA;
1839 else if (g_str_has_prefix (basename, "memstick") ||
1840 g_str_has_prefix (basename, "memory_stick") ||
1841 g_str_has_prefix (basename, "ram"))
1842 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1843 else if (g_str_has_prefix (basename, "compact_flash"))
1844 type = G_UNIX_MOUNT_TYPE_CF;
1845 else if (g_str_has_prefix (basename, "smart_media"))
1846 type = G_UNIX_MOUNT_TYPE_SM;
1847 else if (g_str_has_prefix (basename, "sd_mmc"))
1848 type = G_UNIX_MOUNT_TYPE_SDMMC;
1849 else if (g_str_has_prefix (basename, "ipod"))
1850 type = G_UNIX_MOUNT_TYPE_IPOD;
1855 if (type == G_UNIX_MOUNT_TYPE_UNKNOWN)
1856 type = G_UNIX_MOUNT_TYPE_HD;
1862 * g_unix_mount_guess_type:
1863 * @mount_entry: a #GUnixMount.
1865 * Guesses the type of a unix mount. If the mount type cannot be
1866 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1868 * Returns: a #GUnixMountType.
1870 static GUnixMountType
1871 g_unix_mount_guess_type (GUnixMountEntry *mount_entry)
1873 g_return_val_if_fail (mount_entry != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1874 g_return_val_if_fail (mount_entry->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1875 g_return_val_if_fail (mount_entry->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1876 g_return_val_if_fail (mount_entry->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1878 return guess_mount_type (mount_entry->mount_path,
1879 mount_entry->device_path,
1880 mount_entry->filesystem_type);
1884 * g_unix_mount_point_guess_type:
1885 * @mount_point: a #GUnixMountPoint.
1887 * Guesses the type of a unix mount point.
1888 * If the mount type cannot be determined,
1889 * returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1891 * Returns: a #GUnixMountType.
1893 static GUnixMountType
1894 g_unix_mount_point_guess_type (GUnixMountPoint *mount_point)
1896 g_return_val_if_fail (mount_point != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1897 g_return_val_if_fail (mount_point->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1898 g_return_val_if_fail (mount_point->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1899 g_return_val_if_fail (mount_point->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1901 return guess_mount_type (mount_point->mount_path,
1902 mount_point->device_path,
1903 mount_point->filesystem_type);
1907 type_to_icon (GUnixMountType type, gboolean is_mount_point, gboolean use_symbolic)
1909 const char *icon_name;
1913 case G_UNIX_MOUNT_TYPE_HD:
1915 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
1917 icon_name = use_symbolic ? "drive-harddisk-symbolic" : "drive-harddisk";
1919 case G_UNIX_MOUNT_TYPE_FLOPPY:
1920 case G_UNIX_MOUNT_TYPE_ZIP:
1921 case G_UNIX_MOUNT_TYPE_JAZ:
1923 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
1925 icon_name = use_symbolic ? "media-removable-symbolic" : "media-floppy";
1927 case G_UNIX_MOUNT_TYPE_CDROM:
1929 icon_name = use_symbolic ? "drive-optical-symbolic" : "drive-optical";
1931 icon_name = use_symbolic ? "media-optical-symbolic" : "media-optical";
1933 case G_UNIX_MOUNT_TYPE_NFS:
1934 icon_name = use_symbolic ? "folder-remote-symbolic" : "folder-remote";
1936 case G_UNIX_MOUNT_TYPE_MEMSTICK:
1938 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
1940 icon_name = use_symbolic ? "media-removable-symbolic" : "media-flash";
1942 case G_UNIX_MOUNT_TYPE_CAMERA:
1944 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
1946 icon_name = use_symbolic ? "camera-photo-symbolic" : "camera-photo";
1948 case G_UNIX_MOUNT_TYPE_IPOD:
1950 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
1952 icon_name = use_symbolic ? "multimedia-player-symbolic" : "multimedia-player";
1954 case G_UNIX_MOUNT_TYPE_UNKNOWN:
1957 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
1959 icon_name = use_symbolic ? "drive-harddisk-symbolic" : "drive-harddisk";
1967 * g_unix_mount_guess_name:
1968 * @mount_entry: a #GUnixMountEntry
1970 * Guesses the name of a Unix mount.
1971 * The result is a translated string.
1973 * Returns: A newly allocated string that must
1974 * be freed with g_free()
1977 g_unix_mount_guess_name (GUnixMountEntry *mount_entry)
1981 if (strcmp (mount_entry->mount_path, "/") == 0)
1982 name = g_strdup (_("Filesystem root"));
1984 name = g_filename_display_basename (mount_entry->mount_path);
1990 * g_unix_mount_guess_icon:
1991 * @mount_entry: a #GUnixMountEntry
1993 * Guesses the icon of a Unix mount.
1995 * Returns: (transfer full): a #GIcon
1998 g_unix_mount_guess_icon (GUnixMountEntry *mount_entry)
2000 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE, FALSE));
2004 * g_unix_mount_guess_symbolic_icon:
2005 * @mount_entry: a #GUnixMountEntry
2007 * Guesses the symbolic icon of a Unix mount.
2009 * Returns: (transfer full): a #GIcon
2014 g_unix_mount_guess_symbolic_icon (GUnixMountEntry *mount_entry)
2016 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE, TRUE));
2020 * g_unix_mount_point_guess_name:
2021 * @mount_point: a #GUnixMountPoint
2023 * Guesses the name of a Unix mount point.
2024 * The result is a translated string.
2026 * Returns: A newly allocated string that must
2027 * be freed with g_free()
2030 g_unix_mount_point_guess_name (GUnixMountPoint *mount_point)
2034 if (strcmp (mount_point->mount_path, "/") == 0)
2035 name = g_strdup (_("Filesystem root"));
2037 name = g_filename_display_basename (mount_point->mount_path);
2043 * g_unix_mount_point_guess_icon:
2044 * @mount_point: a #GUnixMountPoint
2046 * Guesses the icon of a Unix mount point.
2048 * Returns: (transfer full): a #GIcon
2051 g_unix_mount_point_guess_icon (GUnixMountPoint *mount_point)
2053 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE, FALSE));
2057 * g_unix_mount_point_guess_symbolic_icon:
2058 * @mount_point: a #GUnixMountPoint
2060 * Guesses the symbolic icon of a Unix mount point.
2062 * Returns: (transfer full): a #GIcon
2067 g_unix_mount_point_guess_symbolic_icon (GUnixMountPoint *mount_point)
2069 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE, TRUE));
2073 * g_unix_mount_guess_can_eject:
2074 * @mount_entry: a #GUnixMountEntry
2076 * Guesses whether a Unix mount can be ejected.
2078 * Returns: %TRUE if @mount_entry is deemed to be ejectable.
2081 g_unix_mount_guess_can_eject (GUnixMountEntry *mount_entry)
2083 GUnixMountType guessed_type;
2085 guessed_type = g_unix_mount_guess_type (mount_entry);
2086 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
2087 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
2094 * g_unix_mount_guess_should_display:
2095 * @mount_entry: a #GUnixMountEntry
2097 * Guesses whether a Unix mount should be displayed in the UI.
2099 * Returns: %TRUE if @mount_entry is deemed to be displayable.
2102 g_unix_mount_guess_should_display (GUnixMountEntry *mount_entry)
2104 const char *mount_path;
2105 const gchar *user_name;
2106 gsize user_name_len;
2108 /* Never display internal mountpoints */
2109 if (g_unix_mount_is_system_internal (mount_entry))
2112 /* Only display things in /media (which are generally user mountable)
2113 and home dir (fuse stuff) and /run/media/$USER */
2114 mount_path = mount_entry->mount_path;
2115 if (mount_path != NULL)
2117 gboolean is_in_runtime_dir = FALSE;
2118 /* Hide mounts within a dot path, suppose it was a purpose to hide this mount */
2119 if (g_strstr_len (mount_path, -1, "/.") != NULL)
2122 /* Check /run/media/$USER/ */
2123 user_name = g_get_user_name ();
2124 user_name_len = strlen (user_name);
2125 if (strncmp (mount_path, "/run/media/", sizeof ("/run/media/") - 1) == 0 &&
2126 strncmp (mount_path + sizeof ("/run/media/") - 1, user_name, user_name_len) == 0 &&
2127 mount_path[sizeof ("/run/media/") - 1 + user_name_len] == '/')
2128 is_in_runtime_dir = TRUE;
2130 if (is_in_runtime_dir || g_str_has_prefix (mount_path, "/media/"))
2133 /* Avoid displaying mounts that are not accessible to the user.
2135 * See http://bugzilla.gnome.org/show_bug.cgi?id=526320 for why we
2136 * want to avoid g_access() for mount points which can potentially
2137 * block or fail stat()'ing, such as network mounts.
2139 path = g_path_get_dirname (mount_path);
2140 if (g_str_has_prefix (path, "/media/"))
2142 if (g_access (path, R_OK|X_OK) != 0)
2150 if (mount_entry->device_path && mount_entry->device_path[0] == '/')
2153 if (g_stat (mount_entry->device_path, &st) == 0 &&
2154 S_ISBLK(st.st_mode) &&
2155 g_access (mount_path, R_OK|X_OK) != 0)
2161 if (g_str_has_prefix (mount_path, g_get_home_dir ()) &&
2162 mount_path[strlen (g_get_home_dir())] == G_DIR_SEPARATOR)
2170 * g_unix_mount_point_guess_can_eject:
2171 * @mount_point: a #GUnixMountPoint
2173 * Guesses whether a Unix mount point can be ejected.
2175 * Returns: %TRUE if @mount_point is deemed to be ejectable.
2178 g_unix_mount_point_guess_can_eject (GUnixMountPoint *mount_point)
2180 GUnixMountType guessed_type;
2182 guessed_type = g_unix_mount_point_guess_type (mount_point);
2183 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
2184 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
2190 #ifdef HAVE_MNTENT_H
2191 /* borrowed from gtk/gtkfilesystemunix.c in GTK+ on 02/23/2006 */
2193 _canonicalize_filename (gchar *filename)
2196 gboolean last_was_slash = FALSE;
2203 if (*p == G_DIR_SEPARATOR)
2205 if (!last_was_slash)
2206 *q++ = G_DIR_SEPARATOR;
2208 last_was_slash = TRUE;
2212 if (last_was_slash && *p == '.')
2214 if (*(p + 1) == G_DIR_SEPARATOR ||
2217 if (*(p + 1) == '\0')
2222 else if (*(p + 1) == '.' &&
2223 (*(p + 2) == G_DIR_SEPARATOR ||
2226 if (q > filename + 1)
2229 while (q > filename + 1 &&
2230 *(q - 1) != G_DIR_SEPARATOR)
2234 if (*(p + 2) == '\0')
2242 last_was_slash = FALSE;
2248 last_was_slash = FALSE;
2255 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
2262 _resolve_symlink (const char *file)
2270 f = g_strdup (file);
2272 while (g_file_test (f, G_FILE_TEST_IS_SYMLINK))
2274 link = g_file_read_link (f, &error);
2277 g_error_free (error);
2283 dir = g_path_get_dirname (f);
2284 f1 = g_strdup_printf ("%s/%s", dir, link);
2293 _canonicalize_filename (f);
2298 _resolve_dev_root (void)
2300 static gboolean have_real_dev_root = FALSE;
2301 static char real_dev_root[256];
2302 struct stat statbuf;
2304 /* see if it's cached already */
2305 if (have_real_dev_root)
2308 /* otherwise we're going to find it right away.. */
2309 have_real_dev_root = TRUE;
2311 if (stat ("/dev/root", &statbuf) == 0)
2313 if (! S_ISLNK (statbuf.st_mode))
2315 dev_t root_dev = statbuf.st_dev;
2318 /* see if device with similar major:minor as /dev/root is mention
2319 * in /etc/mtab (it usually is)
2321 f = fopen ("/etc/mtab", "r");
2324 struct mntent *entp;
2325 #ifdef HAVE_GETMNTENT_R
2328 while ((entp = getmntent_r (f, &ent, buf, sizeof (buf))) != NULL)
2332 while ((entp = getmntent (f)) != NULL)
2335 if (stat (entp->mnt_fsname, &statbuf) == 0 &&
2336 statbuf.st_dev == root_dev)
2338 strncpy (real_dev_root, entp->mnt_fsname, sizeof (real_dev_root) - 1);
2339 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2347 #ifndef HAVE_GETMNTENT_R
2348 G_UNLOCK (getmntent);
2352 /* no, that didn't work.. next we could scan /dev ... but I digress.. */
2358 resolved = _resolve_symlink ("/dev/root");
2359 if (resolved != NULL)
2361 strncpy (real_dev_root, resolved, sizeof (real_dev_root) - 1);
2362 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2370 strcpy (real_dev_root, "/dev/root");
2373 return real_dev_root;