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
51 #include <sys/statfs.h>
53 #if HAVE_SYS_STATVFS_H
54 #include <sys/statvfs.h>
58 #elif HAVE_SYS_MOUNT_H
60 #include <sys/param.h>
62 #include <sys/mount.h>
69 #include "gunixmounts.h"
71 #include "gfilemonitor.h"
73 #include "gthemedicon.h"
77 static const char *_resolve_dev_root (void);
82 * @include: gio/gunixmounts.h
83 * @short_description: UNIX mounts
85 * Routines for managing mounted UNIX mount points and paths.
87 * Note that <filename><gio/gunixmounts.h></filename> belongs to the
88 * UNIX-specific GIO interfaces, thus you have to use the
89 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
94 * @G_UNIX_MOUNT_TYPE_UNKNOWN: Unknown UNIX mount type.
95 * @G_UNIX_MOUNT_TYPE_FLOPPY: Floppy disk UNIX mount type.
96 * @G_UNIX_MOUNT_TYPE_CDROM: CDROM UNIX mount type.
97 * @G_UNIX_MOUNT_TYPE_NFS: Network File System (NFS) UNIX mount type.
98 * @G_UNIX_MOUNT_TYPE_ZIP: ZIP UNIX mount type.
99 * @G_UNIX_MOUNT_TYPE_JAZ: JAZZ UNIX mount type.
100 * @G_UNIX_MOUNT_TYPE_MEMSTICK: Memory Stick UNIX mount type.
101 * @G_UNIX_MOUNT_TYPE_CF: Compact Flash UNIX mount type.
102 * @G_UNIX_MOUNT_TYPE_SM: Smart Media UNIX mount type.
103 * @G_UNIX_MOUNT_TYPE_SDMMC: SD/MMC UNIX mount type.
104 * @G_UNIX_MOUNT_TYPE_IPOD: iPod UNIX mount type.
105 * @G_UNIX_MOUNT_TYPE_CAMERA: Digital camera UNIX mount type.
106 * @G_UNIX_MOUNT_TYPE_HD: Hard drive UNIX mount type.
108 * Types of UNIX mounts.
111 G_UNIX_MOUNT_TYPE_UNKNOWN,
112 G_UNIX_MOUNT_TYPE_FLOPPY,
113 G_UNIX_MOUNT_TYPE_CDROM,
114 G_UNIX_MOUNT_TYPE_NFS,
115 G_UNIX_MOUNT_TYPE_ZIP,
116 G_UNIX_MOUNT_TYPE_JAZ,
117 G_UNIX_MOUNT_TYPE_MEMSTICK,
118 G_UNIX_MOUNT_TYPE_CF,
119 G_UNIX_MOUNT_TYPE_SM,
120 G_UNIX_MOUNT_TYPE_SDMMC,
121 G_UNIX_MOUNT_TYPE_IPOD,
122 G_UNIX_MOUNT_TYPE_CAMERA,
126 struct _GUnixMountEntry {
129 char *filesystem_type;
130 gboolean is_read_only;
131 gboolean is_system_internal;
134 struct _GUnixMountPoint {
137 char *filesystem_type;
138 gboolean is_read_only;
139 gboolean is_user_mountable;
140 gboolean is_loopback;
149 static guint signals[LAST_SIGNAL];
151 struct _GUnixMountMonitor {
154 GFileMonitor *fstab_monitor;
155 GFileMonitor *mtab_monitor;
157 GSource *proc_mounts_watch_source;
160 struct _GUnixMountMonitorClass {
161 GObjectClass parent_class;
164 static GUnixMountMonitor *the_mount_monitor = NULL;
166 static GList *_g_get_unix_mounts (void);
167 static GList *_g_get_unix_mount_points (void);
169 G_DEFINE_TYPE (GUnixMountMonitor, g_unix_mount_monitor, G_TYPE_OBJECT);
171 #define MOUNT_POLL_INTERVAL 4000
173 #ifdef HAVE_SYS_MNTTAB_H
174 #define MNTOPT_RO "ro"
179 #elif defined (HAVE_SYS_MNTTAB_H)
180 #include <sys/mnttab.h>
183 #ifdef HAVE_SYS_VFSTAB_H
184 #include <sys/vfstab.h>
187 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
188 #include <sys/mntctl.h>
190 #include <sys/vmount.h>
194 #if (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
195 #include <sys/ucred.h>
196 #include <sys/mount.h>
198 #ifdef HAVE_SYS_SYSCTL_H
199 #include <sys/sysctl.h>
203 #ifndef HAVE_SETMNTENT
204 #define setmntent(f,m) fopen(f,m)
206 #ifndef HAVE_ENDMNTENT
207 #define endmntent(f) fclose(f)
211 is_in (const char *value, const char *set[])
214 for (i = 0; set[i] != NULL; i++)
216 if (strcmp (set[i], value) == 0)
223 * g_unix_is_mount_path_system_internal:
224 * @mount_path: a mount path, e.g. <filename>/media/disk</filename>
225 * or <filename>/usr</filename>
227 * Determines if @mount_path is considered an implementation of the
228 * OS. This is primarily used for hiding mountable and mounted volumes
229 * that only are used in the OS and has little to no relevance to the
232 * Returns: %TRUE if @mount_path is considered an implementation detail
236 g_unix_is_mount_path_system_internal (const char *mount_path)
238 const char *ignore_mountpoints[] = {
239 /* Includes all FHS 2.3 toplevel dirs and other specilized
240 * directories that we want to hide from the user.
242 "/", /* we already have "Filesystem root" in Nautilus */
265 "/var/log/audit", /* https://bugzilla.redhat.com/show_bug.cgi?id=333041 */
268 "/var/tmp", /* https://bugzilla.redhat.com/show_bug.cgi?id=335241 */
276 if (is_in (mount_path, ignore_mountpoints))
279 if (g_str_has_prefix (mount_path, "/dev/") ||
280 g_str_has_prefix (mount_path, "/proc/") ||
281 g_str_has_prefix (mount_path, "/sys/"))
284 if (g_str_has_suffix (mount_path, "/.gvfs"))
291 guess_system_internal (const char *mountpoint,
295 const char *ignore_fs[] = {
316 const char *ignore_devices[] = {
326 if (is_in (fs, ignore_fs))
329 if (is_in (device, ignore_devices))
332 if (g_unix_is_mount_path_system_internal (mountpoint))
341 get_mtab_read_file (void)
345 return "/proc/mounts";
347 return _PATH_MOUNTED;
355 get_mtab_monitor_file (void)
359 return "/proc/mounts";
361 return _PATH_MOUNTED;
368 #ifndef HAVE_GETMNTENT_R
369 G_LOCK_DEFINE_STATIC(getmntent);
373 _g_get_unix_mounts (void)
375 #ifdef HAVE_GETMNTENT_R
379 struct mntent *mntent;
382 GUnixMountEntry *mount_entry;
383 GHashTable *mounts_hash;
386 read_file = get_mtab_read_file ();
388 file = setmntent (read_file, "r");
394 mounts_hash = g_hash_table_new (g_str_hash, g_str_equal);
396 #ifdef HAVE_GETMNTENT_R
397 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
400 while ((mntent = getmntent (file)) != NULL)
403 /* ignore any mnt_fsname that is repeated and begins with a '/'
405 * We do this to avoid being fooled by --bind mounts, since
406 * these have the same device as the location they bind to.
407 * It's not an ideal solution to the problem, but it's likely that
408 * the most important mountpoint is first and the --bind ones after
409 * that aren't as important. So it should work.
411 * The '/' is to handle procfs, tmpfs and other no device mounts.
413 if (mntent->mnt_fsname != NULL &&
414 mntent->mnt_fsname[0] == '/' &&
415 g_hash_table_lookup (mounts_hash, mntent->mnt_fsname))
418 mount_entry = g_new0 (GUnixMountEntry, 1);
419 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
420 if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
421 mount_entry->device_path = g_strdup (_resolve_dev_root ());
423 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
424 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
426 #if defined (HAVE_HASMNTOPT)
427 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
428 mount_entry->is_read_only = TRUE;
431 mount_entry->is_system_internal =
432 guess_system_internal (mount_entry->mount_path,
433 mount_entry->filesystem_type,
434 mount_entry->device_path);
436 g_hash_table_insert (mounts_hash,
437 mount_entry->device_path,
438 mount_entry->device_path);
440 return_list = g_list_prepend (return_list, mount_entry);
442 g_hash_table_destroy (mounts_hash);
446 #ifndef HAVE_GETMNTENT_R
447 G_UNLOCK (getmntent);
450 return g_list_reverse (return_list);
453 #elif defined (HAVE_SYS_MNTTAB_H)
455 G_LOCK_DEFINE_STATIC(getmntent);
458 get_mtab_read_file (void)
461 return _PATH_MOUNTED;
463 return "/etc/mnttab";
468 get_mtab_monitor_file (void)
470 return get_mtab_read_file ();
474 _g_get_unix_mounts (void)
476 struct mnttab mntent;
479 GUnixMountEntry *mount_entry;
482 read_file = get_mtab_read_file ();
484 file = setmntent (read_file, "r");
491 while (! getmntent (file, &mntent))
493 mount_entry = g_new0 (GUnixMountEntry, 1);
495 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
496 mount_entry->device_path = g_strdup (mntent.mnt_special);
497 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
499 #if defined (HAVE_HASMNTOPT)
500 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
501 mount_entry->is_read_only = TRUE;
504 mount_entry->is_system_internal =
505 guess_system_internal (mount_entry->mount_path,
506 mount_entry->filesystem_type,
507 mount_entry->device_path);
509 return_list = g_list_prepend (return_list, mount_entry);
514 G_UNLOCK (getmntent);
516 return g_list_reverse (return_list);
519 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
522 get_mtab_monitor_file (void)
528 _g_get_unix_mounts (void)
530 struct vfs_ent *fs_info;
531 struct vmount *vmount_info;
533 unsigned int vmount_size;
537 if (mntctl (MCTL_QUERY, sizeof (vmount_size), &vmount_size) != 0)
539 g_warning ("Unable to know the number of mounted volumes\n");
544 vmount_info = (struct vmount*)g_malloc (vmount_size);
546 vmount_number = mntctl (MCTL_QUERY, vmount_size, vmount_info);
548 if (vmount_info->vmt_revision != VMT_REVISION)
549 g_warning ("Bad vmount structure revision number, want %d, got %d\n", VMT_REVISION, vmount_info->vmt_revision);
551 if (vmount_number < 0)
553 g_warning ("Unable to recover mounted volumes information\n");
555 g_free (vmount_info);
560 while (vmount_number > 0)
562 mount_entry = g_new0 (GUnixMountEntry, 1);
564 mount_entry->device_path = g_strdup (vmt2dataptr (vmount_info, VMT_OBJECT));
565 mount_entry->mount_path = g_strdup (vmt2dataptr (vmount_info, VMT_STUB));
566 /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
567 mount_entry->is_read_only = (vmount_info->vmt_flags & MNT_READONLY) ? 1 : 0;
569 fs_info = getvfsbytype (vmount_info->vmt_gfstype);
572 mount_entry->filesystem_type = g_strdup ("unknown");
574 mount_entry->filesystem_type = g_strdup (fs_info->vfsent_name);
576 mount_entry->is_system_internal =
577 guess_system_internal (mount_entry->mount_path,
578 mount_entry->filesystem_type,
579 mount_entry->device_path);
581 return_list = g_list_prepend (return_list, mount_entry);
583 vmount_info = (struct vmount *)( (char*)vmount_info
584 + vmount_info->vmt_length);
588 g_free (vmount_info);
590 return g_list_reverse (return_list);
593 #elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
596 get_mtab_monitor_file (void)
602 _g_get_unix_mounts (void)
604 #if defined(HAVE_GETVFSSTAT)
605 struct statvfs *mntent = NULL;
606 #elif defined(HAVE_GETFSSTAT)
607 struct statfs *mntent = NULL;
609 #error statfs juggling failed
613 GUnixMountEntry *mount_entry;
616 /* Pass NOWAIT to avoid blocking trying to update NFS mounts. */
617 #if defined(HAVE_GETVFSSTAT)
618 num_mounts = getvfsstat (NULL, 0, ST_NOWAIT);
619 #elif defined(HAVE_GETFSSTAT)
620 num_mounts = getfsstat (NULL, 0, MNT_NOWAIT);
622 if (num_mounts == -1)
625 bufsize = num_mounts * sizeof (*mntent);
626 mntent = g_malloc (bufsize);
627 #if defined(HAVE_GETVFSSTAT)
628 num_mounts = getvfsstat (mntent, bufsize, ST_NOWAIT);
629 #elif defined(HAVE_GETFSSTAT)
630 num_mounts = getfsstat (mntent, bufsize, MNT_NOWAIT);
632 if (num_mounts == -1)
637 for (i = 0; i < num_mounts; i++)
639 mount_entry = g_new0 (GUnixMountEntry, 1);
641 mount_entry->mount_path = g_strdup (mntent[i].f_mntonname);
642 mount_entry->device_path = g_strdup (mntent[i].f_mntfromname);
643 mount_entry->filesystem_type = g_strdup (mntent[i].f_fstypename);
644 #if defined(HAVE_GETVFSSTAT)
645 if (mntent[i].f_flag & ST_RDONLY)
646 #elif defined(HAVE_GETFSSTAT)
647 if (mntent[i].f_flags & MNT_RDONLY)
649 mount_entry->is_read_only = TRUE;
651 mount_entry->is_system_internal =
652 guess_system_internal (mount_entry->mount_path,
653 mount_entry->filesystem_type,
654 mount_entry->device_path);
656 return_list = g_list_prepend (return_list, mount_entry);
661 return g_list_reverse (return_list);
663 #elif defined(__INTERIX)
666 get_mtab_monitor_file (void)
672 _g_get_unix_mounts (void)
675 GList* return_list = NULL;
676 char filename[9 + NAME_MAX];
678 dirp = opendir ("/dev/fs");
681 g_warning ("unable to read /dev/fs!");
687 struct statvfs statbuf;
689 struct dirent* result;
691 if (readdir_r (dirp, &entry, &result) || result == NULL)
694 strcpy (filename, "/dev/fs/");
695 strcat (filename, entry.d_name);
697 if (statvfs (filename, &statbuf) == 0)
699 GUnixMountEntry* mount_entry = g_new0(GUnixMountEntry, 1);
701 mount_entry->mount_path = g_strdup (statbuf.f_mntonname);
702 mount_entry->device_path = g_strdup (statbuf.f_mntfromname);
703 mount_entry->filesystem_type = g_strdup (statbuf.f_fstypename);
705 if (statbuf.f_flag & ST_RDONLY)
706 mount_entry->is_read_only = TRUE;
708 return_list = g_list_prepend(return_list, mount_entry);
712 return_list = g_list_reverse (return_list);
719 #error No _g_get_unix_mounts() implementation for system
722 /* _g_get_unix_mount_points():
724 * don't return swap and ignore mounts.
728 get_fstab_file (void)
730 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
732 return "/etc/filesystems";
733 #elif defined(_PATH_MNTTAB)
735 #elif defined(VFSTAB)
744 _g_get_unix_mount_points (void)
746 #ifdef HAVE_GETMNTENT_R
750 struct mntent *mntent;
753 GUnixMountPoint *mount_entry;
756 read_file = get_fstab_file ();
758 file = setmntent (read_file, "r");
764 #ifdef HAVE_GETMNTENT_R
765 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
768 while ((mntent = getmntent (file)) != NULL)
771 if ((strcmp (mntent->mnt_dir, "ignore") == 0) ||
772 (strcmp (mntent->mnt_dir, "swap") == 0) ||
773 (strcmp (mntent->mnt_dir, "none") == 0))
776 mount_entry = g_new0 (GUnixMountPoint, 1);
777 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
778 if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
779 mount_entry->device_path = g_strdup (_resolve_dev_root ());
781 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
782 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
784 #ifdef HAVE_HASMNTOPT
785 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
786 mount_entry->is_read_only = TRUE;
788 if (hasmntopt (mntent, "loop") != NULL)
789 mount_entry->is_loopback = TRUE;
793 if ((mntent->mnt_type != NULL && strcmp ("supermount", mntent->mnt_type) == 0)
794 #ifdef HAVE_HASMNTOPT
795 || (hasmntopt (mntent, "user") != NULL
796 && hasmntopt (mntent, "user") != hasmntopt (mntent, "user_xattr"))
797 || hasmntopt (mntent, "pamconsole") != NULL
798 || hasmntopt (mntent, "users") != NULL
799 || hasmntopt (mntent, "owner") != NULL
802 mount_entry->is_user_mountable = TRUE;
804 return_list = g_list_prepend (return_list, mount_entry);
809 #ifndef HAVE_GETMNTENT_R
810 G_UNLOCK (getmntent);
813 return g_list_reverse (return_list);
816 #elif defined (HAVE_SYS_MNTTAB_H)
819 _g_get_unix_mount_points (void)
821 struct mnttab mntent;
824 GUnixMountPoint *mount_entry;
827 read_file = get_fstab_file ();
829 file = setmntent (read_file, "r");
836 while (! getmntent (file, &mntent))
838 if ((strcmp (mntent.mnt_mountp, "ignore") == 0) ||
839 (strcmp (mntent.mnt_mountp, "swap") == 0) ||
840 (strcmp (mntent.mnt_mountp, "none") == 0))
843 mount_entry = g_new0 (GUnixMountPoint, 1);
845 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
846 mount_entry->device_path = g_strdup (mntent.mnt_special);
847 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
849 #ifdef HAVE_HASMNTOPT
850 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
851 mount_entry->is_read_only = TRUE;
853 if (hasmntopt (&mntent, "lofs") != NULL)
854 mount_entry->is_loopback = TRUE;
857 if ((mntent.mnt_fstype != NULL)
858 #ifdef HAVE_HASMNTOPT
859 || (hasmntopt (&mntent, "user") != NULL
860 && hasmntopt (&mntent, "user") != hasmntopt (&mntent, "user_xattr"))
861 || hasmntopt (&mntent, "pamconsole") != NULL
862 || hasmntopt (&mntent, "users") != NULL
863 || hasmntopt (&mntent, "owner") != NULL
866 mount_entry->is_user_mountable = TRUE;
868 return_list = g_list_prepend (return_list, mount_entry);
872 G_UNLOCK (getmntent);
874 return g_list_reverse (return_list);
876 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
878 /* functions to parse /etc/filesystems on aix */
880 /* read character, ignoring comments (begin with '*', end with '\n' */
882 aix_fs_getc (FILE *fd)
886 while ((c = getc (fd)) == '*')
888 while (((c = getc (fd)) != '\n') && (c != EOF))
893 /* eat all continuous spaces in a file */
895 aix_fs_ignorespace (FILE *fd)
899 while ((c = aix_fs_getc (fd)) != EOF)
901 if (!g_ascii_isspace (c))
911 /* read one word from file */
913 aix_fs_getword (FILE *fd,
918 aix_fs_ignorespace (fd);
920 while (((c = aix_fs_getc (fd)) != EOF) && !g_ascii_isspace (c))
924 while (((c = aix_fs_getc (fd)) != EOF) && (c != '"'))
936 char mnt_mount[PATH_MAX];
937 char mnt_special[PATH_MAX];
939 char mnt_options[128];
940 } AixMountTableEntry;
942 /* read mount points properties */
944 aix_fs_get (FILE *fd,
945 AixMountTableEntry *prop)
947 static char word[PATH_MAX] = { 0 };
948 char value[PATH_MAX];
953 if (aix_fs_getword (fd, word) == EOF)
957 word[strlen(word) - 1] = 0;
958 strcpy (prop->mnt_mount, word);
960 /* read attributes and value */
962 while (aix_fs_getword (fd, word) != EOF)
964 /* test if is attribute or new stanza */
965 if (word[strlen(word) - 1] == ':')
969 aix_fs_getword (fd, value);
972 aix_fs_getword (fd, value);
974 if (strcmp (word, "dev") == 0)
975 strcpy (prop->mnt_special, value);
976 else if (strcmp (word, "vfs") == 0)
977 strcpy (prop->mnt_fstype, value);
978 else if (strcmp (word, "options") == 0)
979 strcpy(prop->mnt_options, value);
986 _g_get_unix_mount_points (void)
988 struct mntent *mntent;
991 GUnixMountPoint *mount_entry;
992 AixMountTableEntry mntent;
995 read_file = get_fstab_file ();
997 file = setmntent (read_file, "r");
1003 while (!aix_fs_get (file, &mntent))
1005 if (strcmp ("cdrfs", mntent.mnt_fstype) == 0)
1007 mount_entry = g_new0 (GUnixMountPoint, 1);
1009 mount_entry->mount_path = g_strdup (mntent.mnt_mount);
1010 mount_entry->device_path = g_strdup (mntent.mnt_special);
1011 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
1012 mount_entry->is_read_only = TRUE;
1013 mount_entry->is_user_mountable = TRUE;
1015 return_list = g_list_prepend (return_list, mount_entry);
1021 return g_list_reverse (return_list);
1024 #elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
1027 _g_get_unix_mount_points (void)
1029 struct fstab *fstab = NULL;
1030 GUnixMountPoint *mount_entry;
1032 #ifdef HAVE_SYS_SYSCTL_H
1034 size_t len = sizeof(usermnt);
1043 #ifdef HAVE_SYS_SYSCTL_H
1044 #if defined(HAVE_SYSCTLBYNAME)
1045 sysctlbyname ("vfs.usermount", &usermnt, &len, NULL, 0);
1046 #elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
1051 mib[1] = VFS_USERMOUNT;
1052 sysctl (mib, 2, &usermnt, &len, NULL, 0);
1054 #elif defined(CTL_KERN) && defined(KERN_USERMOUNT)
1059 mib[1] = KERN_USERMOUNT;
1060 sysctl (mib, 2, &usermnt, &len, NULL, 0);
1065 while ((fstab = getfsent ()) != NULL)
1067 if (strcmp (fstab->fs_vfstype, "swap") == 0)
1070 mount_entry = g_new0 (GUnixMountPoint, 1);
1072 mount_entry->mount_path = g_strdup (fstab->fs_file);
1073 mount_entry->device_path = g_strdup (fstab->fs_spec);
1074 mount_entry->filesystem_type = g_strdup (fstab->fs_vfstype);
1076 if (strcmp (fstab->fs_type, "ro") == 0)
1077 mount_entry->is_read_only = TRUE;
1079 #ifdef HAVE_SYS_SYSCTL_H
1082 uid_t uid = getuid ();
1083 if (stat (fstab->fs_file, &sb) == 0)
1085 if (uid == 0 || sb.st_uid == uid)
1086 mount_entry->is_user_mountable = TRUE;
1091 return_list = g_list_prepend (return_list, mount_entry);
1096 return g_list_reverse (return_list);
1098 #elif defined(__INTERIX)
1100 _g_get_unix_mount_points (void)
1102 return _g_get_unix_mounts ();
1105 #error No g_get_mount_table() implementation for system
1109 get_mounts_timestamp (void)
1111 const char *monitor_file;
1114 monitor_file = get_mtab_monitor_file ();
1117 if (stat (monitor_file, &buf) == 0)
1118 return (guint64)buf.st_mtime;
1124 get_mount_points_timestamp (void)
1126 const char *monitor_file;
1129 monitor_file = get_fstab_file ();
1132 if (stat (monitor_file, &buf) == 0)
1133 return (guint64)buf.st_mtime;
1139 * g_unix_mounts_get: (skip)
1140 * @time_read: (out) (allow-none): guint64 to contain a timestamp, or %NULL
1142 * Gets a #GList of #GUnixMountEntry containing the unix mounts.
1143 * If @time_read is set, it will be filled with the mount
1144 * timestamp, allowing for checking if the mounts have changed
1145 * with g_unix_mounts_changed_since().
1147 * Returns: (element-type GUnixMountEntry) (transfer full):
1148 * a #GList of the UNIX mounts.
1151 g_unix_mounts_get (guint64 *time_read)
1154 *time_read = get_mounts_timestamp ();
1156 return _g_get_unix_mounts ();
1160 * g_unix_mount_at: (skip)
1161 * @mount_path: path for a possible unix mount.
1162 * @time_read: (out) (allow-none): guint64 to contain a timestamp.
1164 * Gets a #GUnixMountEntry for a given mount path. If @time_read
1165 * is set, it will be filled with a unix timestamp for checking
1166 * if the mounts have changed since with g_unix_mounts_changed_since().
1168 * Returns: (transfer full): a #GUnixMountEntry.
1171 g_unix_mount_at (const char *mount_path,
1175 GUnixMountEntry *mount_entry, *found;
1177 mounts = g_unix_mounts_get (time_read);
1180 for (l = mounts; l != NULL; l = l->next)
1182 mount_entry = l->data;
1184 if (!found && strcmp (mount_path, mount_entry->mount_path) == 0)
1185 found = mount_entry;
1187 g_unix_mount_free (mount_entry);
1189 g_list_free (mounts);
1195 * g_unix_mount_points_get: (skip)
1196 * @time_read: (out) (allow-none): guint64 to contain a timestamp.
1198 * Gets a #GList of #GUnixMountPoint containing the unix mount points.
1199 * If @time_read is set, it will be filled with the mount timestamp,
1200 * allowing for checking if the mounts have changed with
1201 * g_unix_mount_points_changed_since().
1203 * Returns: (element-type GUnixMountPoint) (transfer full):
1204 * a #GList of the UNIX mountpoints.
1207 g_unix_mount_points_get (guint64 *time_read)
1210 *time_read = get_mount_points_timestamp ();
1212 return _g_get_unix_mount_points ();
1216 * g_unix_mounts_changed_since:
1217 * @time: guint64 to contain a timestamp.
1219 * Checks if the unix mounts have changed since a given unix time.
1221 * Returns: %TRUE if the mounts have changed since @time.
1224 g_unix_mounts_changed_since (guint64 time)
1226 return get_mounts_timestamp () != time;
1230 * g_unix_mount_points_changed_since:
1231 * @time: guint64 to contain a timestamp.
1233 * Checks if the unix mount points have changed since a given unix time.
1235 * Returns: %TRUE if the mount points have changed since @time.
1238 g_unix_mount_points_changed_since (guint64 time)
1240 return get_mount_points_timestamp () != time;
1244 g_unix_mount_monitor_finalize (GObject *object)
1246 GUnixMountMonitor *monitor;
1248 monitor = G_UNIX_MOUNT_MONITOR (object);
1250 if (monitor->fstab_monitor)
1252 g_file_monitor_cancel (monitor->fstab_monitor);
1253 g_object_unref (monitor->fstab_monitor);
1256 if (monitor->proc_mounts_watch_source != NULL)
1257 g_source_destroy (monitor->proc_mounts_watch_source);
1259 if (monitor->mtab_monitor)
1261 g_file_monitor_cancel (monitor->mtab_monitor);
1262 g_object_unref (monitor->mtab_monitor);
1265 the_mount_monitor = NULL;
1267 G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize (object);
1272 g_unix_mount_monitor_class_init (GUnixMountMonitorClass *klass)
1274 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1276 gobject_class->finalize = g_unix_mount_monitor_finalize;
1279 * GUnixMountMonitor::mounts-changed:
1280 * @monitor: the object on which the signal is emitted
1282 * Emitted when the unix mounts have changed.
1284 signals[MOUNTS_CHANGED] =
1285 g_signal_new ("mounts-changed",
1286 G_TYPE_FROM_CLASS (klass),
1290 g_cclosure_marshal_VOID__VOID,
1294 * GUnixMountMonitor::mountpoints-changed:
1295 * @monitor: the object on which the signal is emitted
1297 * Emitted when the unix mount points have changed.
1299 signals[MOUNTPOINTS_CHANGED] =
1300 g_signal_new ("mountpoints-changed",
1301 G_TYPE_FROM_CLASS (klass),
1305 g_cclosure_marshal_VOID__VOID,
1310 fstab_file_changed (GFileMonitor *monitor,
1313 GFileMonitorEvent event_type,
1316 GUnixMountMonitor *mount_monitor;
1318 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1319 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1320 event_type != G_FILE_MONITOR_EVENT_DELETED)
1323 mount_monitor = user_data;
1324 g_signal_emit (mount_monitor, signals[MOUNTPOINTS_CHANGED], 0);
1328 mtab_file_changed (GFileMonitor *monitor,
1331 GFileMonitorEvent event_type,
1334 GUnixMountMonitor *mount_monitor;
1336 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1337 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1338 event_type != G_FILE_MONITOR_EVENT_DELETED)
1341 mount_monitor = user_data;
1342 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1346 proc_mounts_changed (GIOChannel *channel,
1350 GUnixMountMonitor *mount_monitor = G_UNIX_MOUNT_MONITOR (user_data);
1351 if (cond & G_IO_ERR)
1352 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1357 g_unix_mount_monitor_init (GUnixMountMonitor *monitor)
1361 if (get_fstab_file () != NULL)
1363 file = g_file_new_for_path (get_fstab_file ());
1364 monitor->fstab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1365 g_object_unref (file);
1367 g_signal_connect (monitor->fstab_monitor, "changed", (GCallback)fstab_file_changed, monitor);
1370 if (get_mtab_monitor_file () != NULL)
1372 const gchar *mtab_path;
1374 mtab_path = get_mtab_monitor_file ();
1375 /* /proc/mounts monitoring is special - can't just use GFileMonitor.
1376 * See 'man proc' for more details.
1378 if (g_strcmp0 (mtab_path, "/proc/mounts") == 0)
1380 GIOChannel *proc_mounts_channel;
1381 GError *error = NULL;
1382 proc_mounts_channel = g_io_channel_new_file ("/proc/mounts", "r", &error);
1383 if (proc_mounts_channel == NULL)
1385 g_warning ("Error creating IO channel for /proc/mounts: %s (%s, %d)",
1386 error->message, g_quark_to_string (error->domain), error->code);
1387 g_error_free (error);
1391 monitor->proc_mounts_watch_source = g_io_create_watch (proc_mounts_channel, G_IO_ERR);
1392 g_source_set_callback (monitor->proc_mounts_watch_source,
1393 (GSourceFunc) proc_mounts_changed,
1396 g_source_attach (monitor->proc_mounts_watch_source,
1397 g_main_context_get_thread_default ());
1398 g_source_unref (monitor->proc_mounts_watch_source);
1399 g_io_channel_unref (proc_mounts_channel);
1404 file = g_file_new_for_path (mtab_path);
1405 monitor->mtab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1406 g_object_unref (file);
1407 g_signal_connect (monitor->mtab_monitor, "changed", (GCallback)mtab_file_changed, monitor);
1413 * g_unix_mount_monitor_set_rate_limit:
1414 * @mount_monitor: a #GUnixMountMonitor
1415 * @limit_msec: a integer with the limit in milliseconds to
1418 * Sets the rate limit to which the @mount_monitor will report
1419 * consecutive change events to the mount and mount point entry files.
1424 g_unix_mount_monitor_set_rate_limit (GUnixMountMonitor *mount_monitor,
1427 g_return_if_fail (G_IS_UNIX_MOUNT_MONITOR (mount_monitor));
1429 if (mount_monitor->fstab_monitor != NULL)
1430 g_file_monitor_set_rate_limit (mount_monitor->fstab_monitor, limit_msec);
1432 if (mount_monitor->mtab_monitor != NULL)
1433 g_file_monitor_set_rate_limit (mount_monitor->mtab_monitor, limit_msec);
1437 * g_unix_mount_monitor_new:
1439 * Gets a new #GUnixMountMonitor. The default rate limit for which the
1440 * monitor will report consecutive changes for the mount and mount
1441 * point entry files is the default for a #GFileMonitor. Use
1442 * g_unix_mount_monitor_set_rate_limit() to change this.
1444 * Returns: a #GUnixMountMonitor.
1447 g_unix_mount_monitor_new (void)
1449 if (the_mount_monitor == NULL)
1451 the_mount_monitor = g_object_new (G_TYPE_UNIX_MOUNT_MONITOR, NULL);
1452 return the_mount_monitor;
1455 return g_object_ref (the_mount_monitor);
1459 * g_unix_mount_free:
1460 * @mount_entry: a #GUnixMount.
1462 * Frees a unix mount.
1465 g_unix_mount_free (GUnixMountEntry *mount_entry)
1467 g_return_if_fail (mount_entry != NULL);
1469 g_free (mount_entry->mount_path);
1470 g_free (mount_entry->device_path);
1471 g_free (mount_entry->filesystem_type);
1472 g_free (mount_entry);
1476 * g_unix_mount_point_free:
1477 * @mount_point: unix mount point to free.
1479 * Frees a unix mount point.
1482 g_unix_mount_point_free (GUnixMountPoint *mount_point)
1484 g_return_if_fail (mount_point != NULL);
1486 g_free (mount_point->mount_path);
1487 g_free (mount_point->device_path);
1488 g_free (mount_point->filesystem_type);
1489 g_free (mount_point);
1493 * g_unix_mount_compare:
1494 * @mount1: first #GUnixMountEntry to compare.
1495 * @mount2: second #GUnixMountEntry to compare.
1497 * Compares two unix mounts.
1499 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1500 * or less than @mount2, respectively.
1503 g_unix_mount_compare (GUnixMountEntry *mount1,
1504 GUnixMountEntry *mount2)
1508 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1510 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1514 res = g_strcmp0 (mount1->device_path, mount2->device_path);
1518 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1522 res = mount1->is_read_only - mount2->is_read_only;
1530 * g_unix_mount_get_mount_path:
1531 * @mount_entry: input #GUnixMountEntry to get the mount path for.
1533 * Gets the mount path for a unix mount.
1535 * Returns: the mount path for @mount_entry.
1538 g_unix_mount_get_mount_path (GUnixMountEntry *mount_entry)
1540 g_return_val_if_fail (mount_entry != NULL, NULL);
1542 return mount_entry->mount_path;
1546 * g_unix_mount_get_device_path:
1547 * @mount_entry: a #GUnixMount.
1549 * Gets the device path for a unix mount.
1551 * Returns: a string containing the device path.
1554 g_unix_mount_get_device_path (GUnixMountEntry *mount_entry)
1556 g_return_val_if_fail (mount_entry != NULL, NULL);
1558 return mount_entry->device_path;
1562 * g_unix_mount_get_fs_type:
1563 * @mount_entry: a #GUnixMount.
1565 * Gets the filesystem type for the unix mount.
1567 * Returns: a string containing the file system type.
1570 g_unix_mount_get_fs_type (GUnixMountEntry *mount_entry)
1572 g_return_val_if_fail (mount_entry != NULL, NULL);
1574 return mount_entry->filesystem_type;
1578 * g_unix_mount_is_readonly:
1579 * @mount_entry: a #GUnixMount.
1581 * Checks if a unix mount is mounted read only.
1583 * Returns: %TRUE if @mount_entry is read only.
1586 g_unix_mount_is_readonly (GUnixMountEntry *mount_entry)
1588 g_return_val_if_fail (mount_entry != NULL, FALSE);
1590 return mount_entry->is_read_only;
1594 * g_unix_mount_is_system_internal:
1595 * @mount_entry: a #GUnixMount.
1597 * Checks if a unix mount is a system path.
1599 * Returns: %TRUE if the unix mount is for a system path.
1602 g_unix_mount_is_system_internal (GUnixMountEntry *mount_entry)
1604 g_return_val_if_fail (mount_entry != NULL, FALSE);
1606 return mount_entry->is_system_internal;
1610 * g_unix_mount_point_compare:
1611 * @mount1: a #GUnixMount.
1612 * @mount2: a #GUnixMount.
1614 * Compares two unix mount points.
1616 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1617 * or less than @mount2, respectively.
1620 g_unix_mount_point_compare (GUnixMountPoint *mount1,
1621 GUnixMountPoint *mount2)
1625 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1627 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1631 res = g_strcmp0 (mount1->device_path, mount2->device_path);
1635 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1639 res = mount1->is_read_only - mount2->is_read_only;
1643 res = mount1->is_user_mountable - mount2->is_user_mountable;
1647 res = mount1->is_loopback - mount2->is_loopback;
1655 * g_unix_mount_point_get_mount_path:
1656 * @mount_point: a #GUnixMountPoint.
1658 * Gets the mount path for a unix mount point.
1660 * Returns: a string containing the mount path.
1663 g_unix_mount_point_get_mount_path (GUnixMountPoint *mount_point)
1665 g_return_val_if_fail (mount_point != NULL, NULL);
1667 return mount_point->mount_path;
1671 * g_unix_mount_point_get_device_path:
1672 * @mount_point: a #GUnixMountPoint.
1674 * Gets the device path for a unix mount point.
1676 * Returns: a string containing the device path.
1679 g_unix_mount_point_get_device_path (GUnixMountPoint *mount_point)
1681 g_return_val_if_fail (mount_point != NULL, NULL);
1683 return mount_point->device_path;
1687 * g_unix_mount_point_get_fs_type:
1688 * @mount_point: a #GUnixMountPoint.
1690 * Gets the file system type for the mount point.
1692 * Returns: a string containing the file system type.
1695 g_unix_mount_point_get_fs_type (GUnixMountPoint *mount_point)
1697 g_return_val_if_fail (mount_point != NULL, NULL);
1699 return mount_point->filesystem_type;
1703 * g_unix_mount_point_is_readonly:
1704 * @mount_point: a #GUnixMountPoint.
1706 * Checks if a unix mount point is read only.
1708 * Returns: %TRUE if a mount point is read only.
1711 g_unix_mount_point_is_readonly (GUnixMountPoint *mount_point)
1713 g_return_val_if_fail (mount_point != NULL, FALSE);
1715 return mount_point->is_read_only;
1719 * g_unix_mount_point_is_user_mountable:
1720 * @mount_point: a #GUnixMountPoint.
1722 * Checks if a unix mount point is mountable by the user.
1724 * Returns: %TRUE if the mount point is user mountable.
1727 g_unix_mount_point_is_user_mountable (GUnixMountPoint *mount_point)
1729 g_return_val_if_fail (mount_point != NULL, FALSE);
1731 return mount_point->is_user_mountable;
1735 * g_unix_mount_point_is_loopback:
1736 * @mount_point: a #GUnixMountPoint.
1738 * Checks if a unix mount point is a loopback device.
1740 * Returns: %TRUE if the mount point is a loopback. %FALSE otherwise.
1743 g_unix_mount_point_is_loopback (GUnixMountPoint *mount_point)
1745 g_return_val_if_fail (mount_point != NULL, FALSE);
1747 return mount_point->is_loopback;
1750 static GUnixMountType
1751 guess_mount_type (const char *mount_path,
1752 const char *device_path,
1753 const char *filesystem_type)
1755 GUnixMountType type;
1758 type = G_UNIX_MOUNT_TYPE_UNKNOWN;
1760 if ((strcmp (filesystem_type, "udf") == 0) ||
1761 (strcmp (filesystem_type, "iso9660") == 0) ||
1762 (strcmp (filesystem_type, "cd9660") == 0))
1763 type = G_UNIX_MOUNT_TYPE_CDROM;
1764 else if ((strcmp (filesystem_type, "nfs") == 0) ||
1765 (strcmp (filesystem_type, "nfs4") == 0))
1766 type = G_UNIX_MOUNT_TYPE_NFS;
1767 else if (g_str_has_prefix (device_path, "/vol/dev/diskette/") ||
1768 g_str_has_prefix (device_path, "/dev/fd") ||
1769 g_str_has_prefix (device_path, "/dev/floppy"))
1770 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1771 else if (g_str_has_prefix (device_path, "/dev/cdrom") ||
1772 g_str_has_prefix (device_path, "/dev/acd") ||
1773 g_str_has_prefix (device_path, "/dev/cd"))
1774 type = G_UNIX_MOUNT_TYPE_CDROM;
1775 else if (g_str_has_prefix (device_path, "/vol/"))
1777 const char *name = mount_path + strlen ("/");
1779 if (g_str_has_prefix (name, "cdrom"))
1780 type = G_UNIX_MOUNT_TYPE_CDROM;
1781 else if (g_str_has_prefix (name, "floppy") ||
1782 g_str_has_prefix (device_path, "/vol/dev/diskette/"))
1783 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1784 else if (g_str_has_prefix (name, "rmdisk"))
1785 type = G_UNIX_MOUNT_TYPE_ZIP;
1786 else if (g_str_has_prefix (name, "jaz"))
1787 type = G_UNIX_MOUNT_TYPE_JAZ;
1788 else if (g_str_has_prefix (name, "memstick"))
1789 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1793 basename = g_path_get_basename (mount_path);
1795 if (g_str_has_prefix (basename, "cdr") ||
1796 g_str_has_prefix (basename, "cdwriter") ||
1797 g_str_has_prefix (basename, "burn") ||
1798 g_str_has_prefix (basename, "dvdr"))
1799 type = G_UNIX_MOUNT_TYPE_CDROM;
1800 else if (g_str_has_prefix (basename, "floppy"))
1801 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1802 else if (g_str_has_prefix (basename, "zip"))
1803 type = G_UNIX_MOUNT_TYPE_ZIP;
1804 else if (g_str_has_prefix (basename, "jaz"))
1805 type = G_UNIX_MOUNT_TYPE_JAZ;
1806 else if (g_str_has_prefix (basename, "camera"))
1807 type = G_UNIX_MOUNT_TYPE_CAMERA;
1808 else if (g_str_has_prefix (basename, "memstick") ||
1809 g_str_has_prefix (basename, "memory_stick") ||
1810 g_str_has_prefix (basename, "ram"))
1811 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1812 else if (g_str_has_prefix (basename, "compact_flash"))
1813 type = G_UNIX_MOUNT_TYPE_CF;
1814 else if (g_str_has_prefix (basename, "smart_media"))
1815 type = G_UNIX_MOUNT_TYPE_SM;
1816 else if (g_str_has_prefix (basename, "sd_mmc"))
1817 type = G_UNIX_MOUNT_TYPE_SDMMC;
1818 else if (g_str_has_prefix (basename, "ipod"))
1819 type = G_UNIX_MOUNT_TYPE_IPOD;
1824 if (type == G_UNIX_MOUNT_TYPE_UNKNOWN)
1825 type = G_UNIX_MOUNT_TYPE_HD;
1831 * g_unix_mount_guess_type:
1832 * @mount_entry: a #GUnixMount.
1834 * Guesses the type of a unix mount. If the mount type cannot be
1835 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1837 * Returns: a #GUnixMountType.
1839 static GUnixMountType
1840 g_unix_mount_guess_type (GUnixMountEntry *mount_entry)
1842 g_return_val_if_fail (mount_entry != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1843 g_return_val_if_fail (mount_entry->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1844 g_return_val_if_fail (mount_entry->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1845 g_return_val_if_fail (mount_entry->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1847 return guess_mount_type (mount_entry->mount_path,
1848 mount_entry->device_path,
1849 mount_entry->filesystem_type);
1853 * g_unix_mount_point_guess_type:
1854 * @mount_point: a #GUnixMountPoint.
1856 * Guesses the type of a unix mount point.
1857 * If the mount type cannot be determined,
1858 * returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1860 * Returns: a #GUnixMountType.
1862 static GUnixMountType
1863 g_unix_mount_point_guess_type (GUnixMountPoint *mount_point)
1865 g_return_val_if_fail (mount_point != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1866 g_return_val_if_fail (mount_point->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1867 g_return_val_if_fail (mount_point->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1868 g_return_val_if_fail (mount_point->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1870 return guess_mount_type (mount_point->mount_path,
1871 mount_point->device_path,
1872 mount_point->filesystem_type);
1876 type_to_icon (GUnixMountType type, gboolean is_mount_point)
1878 const char *icon_name;
1882 case G_UNIX_MOUNT_TYPE_HD:
1884 icon_name = "drive-removable-media";
1886 icon_name = "drive-harddisk";
1888 case G_UNIX_MOUNT_TYPE_FLOPPY:
1889 case G_UNIX_MOUNT_TYPE_ZIP:
1890 case G_UNIX_MOUNT_TYPE_JAZ:
1892 icon_name = "drive-removable-media";
1894 icon_name = "media-floppy";
1896 case G_UNIX_MOUNT_TYPE_CDROM:
1898 icon_name = "drive-optical";
1900 icon_name = "media-optical";
1902 case G_UNIX_MOUNT_TYPE_NFS:
1903 /* TODO: Would like a better icon here... */
1905 icon_name = "drive-removable-media";
1907 icon_name = "drive-harddisk";
1909 case G_UNIX_MOUNT_TYPE_MEMSTICK:
1911 icon_name = "drive-removable-media";
1913 icon_name = "media-flash";
1915 case G_UNIX_MOUNT_TYPE_CAMERA:
1917 icon_name = "drive-removable-media";
1919 icon_name = "camera-photo";
1921 case G_UNIX_MOUNT_TYPE_IPOD:
1923 icon_name = "drive-removable-media";
1925 icon_name = "multimedia-player";
1927 case G_UNIX_MOUNT_TYPE_UNKNOWN:
1930 icon_name = "drive-removable-media";
1932 icon_name = "drive-harddisk";
1940 * g_unix_mount_guess_name:
1941 * @mount_entry: a #GUnixMountEntry
1943 * Guesses the name of a Unix mount.
1944 * The result is a translated string.
1946 * Returns: A newly allocated string that must
1947 * be freed with g_free()
1950 g_unix_mount_guess_name (GUnixMountEntry *mount_entry)
1954 if (strcmp (mount_entry->mount_path, "/") == 0)
1955 name = g_strdup (_("Filesystem root"));
1957 name = g_filename_display_basename (mount_entry->mount_path);
1963 * g_unix_mount_guess_icon:
1964 * @mount_entry: a #GUnixMountEntry
1966 * Guesses the icon of a Unix mount.
1968 * Returns: (transfer full): a #GIcon
1971 g_unix_mount_guess_icon (GUnixMountEntry *mount_entry)
1973 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE));
1977 * g_unix_mount_point_guess_name:
1978 * @mount_point: a #GUnixMountPoint
1980 * Guesses the name of a Unix mount point.
1981 * The result is a translated string.
1983 * Returns: A newly allocated string that must
1984 * be freed with g_free()
1987 g_unix_mount_point_guess_name (GUnixMountPoint *mount_point)
1991 if (strcmp (mount_point->mount_path, "/") == 0)
1992 name = g_strdup (_("Filesystem root"));
1994 name = g_filename_display_basename (mount_point->mount_path);
2000 * g_unix_mount_point_guess_icon:
2001 * @mount_point: a #GUnixMountPoint
2003 * Guesses the icon of a Unix mount point.
2005 * Returns: (transfer full): a #GIcon
2008 g_unix_mount_point_guess_icon (GUnixMountPoint *mount_point)
2010 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE));
2014 * g_unix_mount_guess_can_eject:
2015 * @mount_entry: a #GUnixMountEntry
2017 * Guesses whether a Unix mount can be ejected.
2019 * Returns: %TRUE if @mount_entry is deemed to be ejectable.
2022 g_unix_mount_guess_can_eject (GUnixMountEntry *mount_entry)
2024 GUnixMountType guessed_type;
2026 guessed_type = g_unix_mount_guess_type (mount_entry);
2027 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
2028 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
2035 * g_unix_mount_guess_should_display:
2036 * @mount_entry: a #GUnixMountEntry
2038 * Guesses whether a Unix mount should be displayed in the UI.
2040 * Returns: %TRUE if @mount_entry is deemed to be displayable.
2043 g_unix_mount_guess_should_display (GUnixMountEntry *mount_entry)
2045 const char *mount_path;
2047 /* Never display internal mountpoints */
2048 if (g_unix_mount_is_system_internal (mount_entry))
2051 /* Only display things in /media (which are generally user mountable)
2052 and home dir (fuse stuff) and $XDG_RUNTIME_DIR */
2053 mount_path = mount_entry->mount_path;
2054 if (mount_path != NULL)
2056 gboolean is_in_runtime_dir = FALSE;
2057 /* Hide mounts within a dot path, suppose it was a purpose to hide this mount */
2058 if (g_strstr_len (mount_path, -1, "/.") != NULL)
2061 if (g_getenv ("XDG_RUNTIME_DIR") != NULL && g_str_has_prefix (mount_path, g_get_user_runtime_dir ()))
2062 is_in_runtime_dir = TRUE;
2064 if (is_in_runtime_dir || g_str_has_prefix (mount_path, "/media/"))
2067 /* Avoid displaying mounts that are not accessible to the user.
2069 * See http://bugzilla.gnome.org/show_bug.cgi?id=526320 for why we
2070 * want to avoid g_access() for mount points which can potentially
2071 * block or fail stat()'ing, such as network mounts.
2073 path = g_path_get_dirname (mount_path);
2074 if (g_str_has_prefix (path, "/media/"))
2076 if (g_access (path, R_OK|X_OK) != 0)
2084 if (mount_entry->device_path && mount_entry->device_path[0] == '/')
2087 if (g_stat (mount_entry->device_path, &st) == 0 &&
2088 S_ISBLK(st.st_mode) &&
2089 g_access (mount_path, R_OK|X_OK) != 0)
2095 if (g_str_has_prefix (mount_path, g_get_home_dir ()) &&
2096 mount_path[strlen (g_get_home_dir())] == G_DIR_SEPARATOR)
2104 * g_unix_mount_point_guess_can_eject:
2105 * @mount_point: a #GUnixMountPoint
2107 * Guesses whether a Unix mount point can be ejected.
2109 * Returns: %TRUE if @mount_point is deemed to be ejectable.
2112 g_unix_mount_point_guess_can_eject (GUnixMountPoint *mount_point)
2114 GUnixMountType guessed_type;
2116 guessed_type = g_unix_mount_point_guess_type (mount_point);
2117 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
2118 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
2124 #ifdef HAVE_MNTENT_H
2125 /* borrowed from gtk/gtkfilesystemunix.c in GTK+ on 02/23/2006 */
2127 _canonicalize_filename (gchar *filename)
2130 gboolean last_was_slash = FALSE;
2137 if (*p == G_DIR_SEPARATOR)
2139 if (!last_was_slash)
2140 *q++ = G_DIR_SEPARATOR;
2142 last_was_slash = TRUE;
2146 if (last_was_slash && *p == '.')
2148 if (*(p + 1) == G_DIR_SEPARATOR ||
2151 if (*(p + 1) == '\0')
2156 else if (*(p + 1) == '.' &&
2157 (*(p + 2) == G_DIR_SEPARATOR ||
2160 if (q > filename + 1)
2163 while (q > filename + 1 &&
2164 *(q - 1) != G_DIR_SEPARATOR)
2168 if (*(p + 2) == '\0')
2176 last_was_slash = FALSE;
2182 last_was_slash = FALSE;
2189 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
2196 _resolve_symlink (const char *file)
2204 f = g_strdup (file);
2206 while (g_file_test (f, G_FILE_TEST_IS_SYMLINK))
2208 link = g_file_read_link (f, &error);
2211 g_error_free (error);
2217 dir = g_path_get_dirname (f);
2218 f1 = g_strdup_printf ("%s/%s", dir, link);
2227 _canonicalize_filename (f);
2232 _resolve_dev_root (void)
2234 static gboolean have_real_dev_root = FALSE;
2235 static char real_dev_root[256];
2236 struct stat statbuf;
2238 /* see if it's cached already */
2239 if (have_real_dev_root)
2242 /* otherwise we're going to find it right away.. */
2243 have_real_dev_root = TRUE;
2245 if (stat ("/dev/root", &statbuf) == 0)
2247 if (! S_ISLNK (statbuf.st_mode))
2249 dev_t root_dev = statbuf.st_dev;
2253 /* see if device with similar major:minor as /dev/root is mention
2254 * in /etc/mtab (it usually is)
2256 f = fopen ("/etc/mtab", "r");
2259 struct mntent *entp;
2260 #ifdef HAVE_GETMNTENT_R
2262 while ((entp = getmntent_r (f, &ent, buf, sizeof (buf))) != NULL)
2266 while ((entp = getmntent (f)) != NULL)
2269 if (stat (entp->mnt_fsname, &statbuf) == 0 &&
2270 statbuf.st_dev == root_dev)
2272 strncpy (real_dev_root, entp->mnt_fsname, sizeof (real_dev_root) - 1);
2273 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2281 #ifndef HAVE_GETMNTENT_R
2282 G_UNLOCK (getmntent);
2286 /* no, that didn't work.. next we could scan /dev ... but I digress.. */
2292 resolved = _resolve_symlink ("/dev/root");
2293 if (resolved != NULL)
2295 strncpy (real_dev_root, resolved, sizeof (real_dev_root) - 1);
2296 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2304 strcpy (real_dev_root, "/dev/root");
2307 return real_dev_root;