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"
76 static const char *_resolve_dev_root (void);
80 * @include: gio/gunixmounts.h
81 * @short_description: UNIX mounts
83 * Routines for managing mounted UNIX mount points and paths.
85 * Note that <filename><gio/gunixmounts.h></filename> belongs to the
86 * UNIX-specific GIO interfaces, thus you have to use the
87 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
92 * @G_UNIX_MOUNT_TYPE_UNKNOWN: Unknown UNIX mount type.
93 * @G_UNIX_MOUNT_TYPE_FLOPPY: Floppy disk UNIX mount type.
94 * @G_UNIX_MOUNT_TYPE_CDROM: CDROM UNIX mount type.
95 * @G_UNIX_MOUNT_TYPE_NFS: Network File System (NFS) UNIX mount type.
96 * @G_UNIX_MOUNT_TYPE_ZIP: ZIP UNIX mount type.
97 * @G_UNIX_MOUNT_TYPE_JAZ: JAZZ UNIX mount type.
98 * @G_UNIX_MOUNT_TYPE_MEMSTICK: Memory Stick UNIX mount type.
99 * @G_UNIX_MOUNT_TYPE_CF: Compact Flash UNIX mount type.
100 * @G_UNIX_MOUNT_TYPE_SM: Smart Media UNIX mount type.
101 * @G_UNIX_MOUNT_TYPE_SDMMC: SD/MMC UNIX mount type.
102 * @G_UNIX_MOUNT_TYPE_IPOD: iPod UNIX mount type.
103 * @G_UNIX_MOUNT_TYPE_CAMERA: Digital camera UNIX mount type.
104 * @G_UNIX_MOUNT_TYPE_HD: Hard drive UNIX mount type.
106 * Types of UNIX mounts.
109 G_UNIX_MOUNT_TYPE_UNKNOWN,
110 G_UNIX_MOUNT_TYPE_FLOPPY,
111 G_UNIX_MOUNT_TYPE_CDROM,
112 G_UNIX_MOUNT_TYPE_NFS,
113 G_UNIX_MOUNT_TYPE_ZIP,
114 G_UNIX_MOUNT_TYPE_JAZ,
115 G_UNIX_MOUNT_TYPE_MEMSTICK,
116 G_UNIX_MOUNT_TYPE_CF,
117 G_UNIX_MOUNT_TYPE_SM,
118 G_UNIX_MOUNT_TYPE_SDMMC,
119 G_UNIX_MOUNT_TYPE_IPOD,
120 G_UNIX_MOUNT_TYPE_CAMERA,
124 struct _GUnixMountEntry {
127 char *filesystem_type;
128 gboolean is_read_only;
129 gboolean is_system_internal;
132 struct _GUnixMountPoint {
135 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;
156 struct _GUnixMountMonitorClass {
157 GObjectClass parent_class;
160 static GUnixMountMonitor *the_mount_monitor = NULL;
162 static GList *_g_get_unix_mounts (void);
163 static GList *_g_get_unix_mount_points (void);
165 G_DEFINE_TYPE (GUnixMountMonitor, g_unix_mount_monitor, G_TYPE_OBJECT);
167 #define MOUNT_POLL_INTERVAL 4000
169 #ifdef HAVE_SYS_MNTTAB_H
170 #define MNTOPT_RO "ro"
175 #elif defined (HAVE_SYS_MNTTAB_H)
176 #include <sys/mnttab.h>
179 #ifdef HAVE_SYS_VFSTAB_H
180 #include <sys/vfstab.h>
183 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
184 #include <sys/mntctl.h>
186 #include <sys/vmount.h>
190 #if defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
191 #include <sys/ucred.h>
192 #include <sys/mount.h>
194 #ifdef HAVE_SYS_SYSCTL_H
195 #include <sys/sysctl.h>
199 #ifndef HAVE_SETMNTENT
200 #define setmntent(f,m) fopen(f,m)
202 #ifndef HAVE_ENDMNTENT
203 #define endmntent(f) fclose(f)
207 is_in (const char *value, const char *set[])
210 for (i = 0; set[i] != NULL; i++)
212 if (strcmp (set[i], value) == 0)
219 * g_unix_is_mount_path_system_internal:
220 * @mount_path: a mount path, e.g. <filename>/media/disk</filename>
221 * or <filename>/usr</filename>
223 * Determines if @mount_path is considered an implementation of the
224 * OS. This is primarily used for hiding mountable and mounted volumes
225 * that only are used in the OS and has little to no relevance to the
228 * Returns: %TRUE if @mount_path is considered an implementation detail
232 g_unix_is_mount_path_system_internal (const char *mount_path)
234 const char *ignore_mountpoints[] = {
235 /* Includes all FHS 2.3 toplevel dirs and other specilized
236 * directories that we want to hide from the user.
238 "/", /* we already have "Filesystem root" in Nautilus */
256 "/var/log/audit", /* https://bugzilla.redhat.com/show_bug.cgi?id=333041 */
257 "/var/tmp", /* https://bugzilla.redhat.com/show_bug.cgi?id=335241 */
265 if (is_in (mount_path, ignore_mountpoints))
268 if (g_str_has_prefix (mount_path, "/dev/") ||
269 g_str_has_prefix (mount_path, "/proc/") ||
270 g_str_has_prefix (mount_path, "/sys/"))
273 if (g_str_has_suffix (mount_path, "/.gvfs"))
280 guess_system_internal (const char *mountpoint,
284 const char *ignore_fs[] = {
305 const char *ignore_devices[] = {
315 if (is_in (fs, ignore_fs))
318 if (is_in (device, ignore_devices))
321 if (g_unix_is_mount_path_system_internal (mountpoint))
330 get_mtab_read_file (void)
334 return "/proc/mounts";
336 return _PATH_MOUNTED;
344 get_mtab_monitor_file (void)
347 return _PATH_MOUNTED;
353 #ifndef HAVE_GETMNTENT_R
354 G_LOCK_DEFINE_STATIC(getmntent);
358 _g_get_unix_mounts (void)
360 #ifdef HAVE_GETMNTENT_R
364 struct mntent *mntent;
367 GUnixMountEntry *mount_entry;
368 GHashTable *mounts_hash;
371 read_file = get_mtab_read_file ();
373 file = setmntent (read_file, "r");
379 mounts_hash = g_hash_table_new (g_str_hash, g_str_equal);
381 #ifdef HAVE_GETMNTENT_R
382 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
385 while ((mntent = getmntent (file)) != NULL)
388 /* ignore any mnt_fsname that is repeated and begins with a '/'
390 * We do this to avoid being fooled by --bind mounts, since
391 * these have the same device as the location they bind to.
392 * It's not an ideal solution to the problem, but it's likely that
393 * the most important mountpoint is first and the --bind ones after
394 * that aren't as important. So it should work.
396 * The '/' is to handle procfs, tmpfs and other no device mounts.
398 if (mntent->mnt_fsname != NULL &&
399 mntent->mnt_fsname[0] == '/' &&
400 g_hash_table_lookup (mounts_hash, mntent->mnt_fsname))
403 mount_entry = g_new0 (GUnixMountEntry, 1);
404 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
405 if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
406 mount_entry->device_path = g_strdup (_resolve_dev_root ());
408 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
409 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
411 #if defined (HAVE_HASMNTOPT)
412 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
413 mount_entry->is_read_only = TRUE;
416 mount_entry->is_system_internal =
417 guess_system_internal (mount_entry->mount_path,
418 mount_entry->filesystem_type,
419 mount_entry->device_path);
421 g_hash_table_insert (mounts_hash,
422 mount_entry->device_path,
423 mount_entry->device_path);
425 return_list = g_list_prepend (return_list, mount_entry);
427 g_hash_table_destroy (mounts_hash);
431 #ifndef HAVE_GETMNTENT_R
432 G_UNLOCK (getmntent);
435 return g_list_reverse (return_list);
438 #elif defined (HAVE_SYS_MNTTAB_H)
440 G_LOCK_DEFINE_STATIC(getmntent);
443 get_mtab_read_file (void)
446 return _PATH_MOUNTED;
448 return "/etc/mnttab";
453 get_mtab_monitor_file (void)
455 return get_mtab_read_file ();
459 _g_get_unix_mounts (void)
461 struct mnttab mntent;
464 GUnixMountEntry *mount_entry;
467 read_file = get_mtab_read_file ();
469 file = setmntent (read_file, "r");
476 while (! getmntent (file, &mntent))
478 mount_entry = g_new0 (GUnixMountEntry, 1);
480 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
481 mount_entry->device_path = g_strdup (mntent.mnt_special);
482 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
484 #if defined (HAVE_HASMNTOPT)
485 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
486 mount_entry->is_read_only = TRUE;
489 mount_entry->is_system_internal =
490 guess_system_internal (mount_entry->mount_path,
491 mount_entry->filesystem_type,
492 mount_entry->device_path);
494 return_list = g_list_prepend (return_list, mount_entry);
499 G_UNLOCK (getmntent);
501 return g_list_reverse (return_list);
504 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
507 get_mtab_monitor_file (void)
513 _g_get_unix_mounts (void)
515 struct vfs_ent *fs_info;
516 struct vmount *vmount_info;
518 unsigned int vmount_size;
522 if (mntctl (MCTL_QUERY, sizeof (vmount_size), &vmount_size) != 0)
524 g_warning ("Unable to know the number of mounted volumes\n");
529 vmount_info = (struct vmount*)g_malloc (vmount_size);
531 vmount_number = mntctl (MCTL_QUERY, vmount_size, vmount_info);
533 if (vmount_info->vmt_revision != VMT_REVISION)
534 g_warning ("Bad vmount structure revision number, want %d, got %d\n", VMT_REVISION, vmount_info->vmt_revision);
536 if (vmount_number < 0)
538 g_warning ("Unable to recover mounted volumes information\n");
540 g_free (vmount_info);
545 while (vmount_number > 0)
547 mount_entry = g_new0 (GUnixMountEntry, 1);
549 mount_entry->device_path = g_strdup (vmt2dataptr (vmount_info, VMT_OBJECT));
550 mount_entry->mount_path = g_strdup (vmt2dataptr (vmount_info, VMT_STUB));
551 /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
552 mount_entry->is_read_only = (vmount_info->vmt_flags & MNT_READONLY) ? 1 : 0;
554 fs_info = getvfsbytype (vmount_info->vmt_gfstype);
557 mount_entry->filesystem_type = g_strdup ("unknown");
559 mount_entry->filesystem_type = g_strdup (fs_info->vfsent_name);
561 mount_entry->is_system_internal =
562 guess_system_internal (mount_entry->mount_path,
563 mount_entry->filesystem_type,
564 mount_entry->device_path);
566 return_list = g_list_prepend (return_list, mount_entry);
568 vmount_info = (struct vmount *)( (char*)vmount_info
569 + vmount_info->vmt_length);
573 g_free (vmount_info);
575 return g_list_reverse (return_list);
578 #elif defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
581 get_mtab_monitor_file (void)
587 _g_get_unix_mounts (void)
589 #if defined(USE_STATFS)
590 struct statfs *mntent = NULL;
591 #elif defined(USE_STATVFS)
592 struct statvfs *mntent = NULL;
594 #error statfs juggling failed
597 GUnixMountEntry *mount_entry;
600 /* Pass MNT_NOWAIT to avoid blocking trying to update NFS mounts. */
601 if ((num_mounts = getmntinfo (&mntent, MNT_NOWAIT)) == 0)
606 for (i = 0; i < num_mounts; i++)
608 mount_entry = g_new0 (GUnixMountEntry, 1);
610 mount_entry->mount_path = g_strdup (mntent[i].f_mntonname);
611 mount_entry->device_path = g_strdup (mntent[i].f_mntfromname);
612 mount_entry->filesystem_type = g_strdup (mntent[i].f_fstypename);
613 #if defined(USE_STATFS)
614 if (mntent[i].f_flags & MNT_RDONLY)
615 #elif defined(USE_STATVFS)
616 if (mntent[i].f_flag & MNT_RDONLY)
618 mount_entry->is_read_only = TRUE;
620 mount_entry->is_system_internal =
621 guess_system_internal (mount_entry->mount_path,
622 mount_entry->filesystem_type,
623 mount_entry->device_path);
625 return_list = g_list_prepend (return_list, mount_entry);
628 return g_list_reverse (return_list);
630 #elif defined(__INTERIX)
633 get_mtab_monitor_file (void)
639 _g_get_unix_mounts (void)
642 GList* return_list = NULL;
643 char filename[9 + NAME_MAX];
645 dirp = opendir ("/dev/fs");
648 g_warning ("unable to read /dev/fs!");
654 struct statvfs statbuf;
656 struct dirent* result;
658 if (readdir_r (dirp, &entry, &result) || result == NULL)
661 strcpy (filename, "/dev/fs/");
662 strcat (filename, entry.d_name);
664 if (statvfs (filename, &statbuf) == 0)
666 GUnixMountEntry* mount_entry = g_new0(GUnixMountEntry, 1);
668 mount_entry->mount_path = g_strdup (statbuf.f_mntonname);
669 mount_entry->device_path = g_strdup (statbuf.f_mntfromname);
670 mount_entry->filesystem_type = g_strdup (statbuf.f_fstypename);
672 if (statbuf.f_flag & ST_RDONLY)
673 mount_entry->is_read_only = TRUE;
675 return_list = g_list_prepend(return_list, mount_entry);
679 return_list = g_list_reverse (return_list);
686 #error No _g_get_unix_mounts() implementation for system
689 /* _g_get_unix_mount_points():
691 * don't return swap and ignore mounts.
695 get_fstab_file (void)
697 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
699 return "/etc/filesystems";
700 #elif defined(_PATH_MNTTAB)
702 #elif defined(VFSTAB)
711 _g_get_unix_mount_points (void)
713 #ifdef HAVE_GETMNTENT_R
717 struct mntent *mntent;
720 GUnixMountPoint *mount_entry;
723 read_file = get_fstab_file ();
725 file = setmntent (read_file, "r");
731 #ifdef HAVE_GETMNTENT_R
732 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
735 while ((mntent = getmntent (file)) != NULL)
738 if ((strcmp (mntent->mnt_dir, "ignore") == 0) ||
739 (strcmp (mntent->mnt_dir, "swap") == 0))
742 mount_entry = g_new0 (GUnixMountPoint, 1);
743 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
744 if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
745 mount_entry->device_path = g_strdup (_resolve_dev_root ());
747 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
748 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
750 #ifdef HAVE_HASMNTOPT
751 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
752 mount_entry->is_read_only = TRUE;
754 if (hasmntopt (mntent, "loop") != NULL)
755 mount_entry->is_loopback = TRUE;
759 if ((mntent->mnt_type != NULL && strcmp ("supermount", mntent->mnt_type) == 0)
760 #ifdef HAVE_HASMNTOPT
761 || (hasmntopt (mntent, "user") != NULL
762 && hasmntopt (mntent, "user") != hasmntopt (mntent, "user_xattr"))
763 || hasmntopt (mntent, "pamconsole") != NULL
764 || hasmntopt (mntent, "users") != NULL
765 || hasmntopt (mntent, "owner") != NULL
768 mount_entry->is_user_mountable = TRUE;
770 return_list = g_list_prepend (return_list, mount_entry);
775 #ifndef HAVE_GETMNTENT_R
776 G_UNLOCK (getmntent);
779 return g_list_reverse (return_list);
782 #elif defined (HAVE_SYS_MNTTAB_H)
785 _g_get_unix_mount_points (void)
787 struct mnttab mntent;
790 GUnixMountPoint *mount_entry;
793 read_file = get_fstab_file ();
795 file = setmntent (read_file, "r");
802 while (! getmntent (file, &mntent))
804 if ((strcmp (mntent.mnt_mountp, "ignore") == 0) ||
805 (strcmp (mntent.mnt_mountp, "swap") == 0))
808 mount_entry = g_new0 (GUnixMountPoint, 1);
810 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
811 mount_entry->device_path = g_strdup (mntent.mnt_special);
812 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
814 #ifdef HAVE_HASMNTOPT
815 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
816 mount_entry->is_read_only = TRUE;
818 if (hasmntopt (&mntent, "lofs") != NULL)
819 mount_entry->is_loopback = TRUE;
822 if ((mntent.mnt_fstype != NULL)
823 #ifdef HAVE_HASMNTOPT
824 || (hasmntopt (&mntent, "user") != NULL
825 && hasmntopt (&mntent, "user") != hasmntopt (&mntent, "user_xattr"))
826 || hasmntopt (&mntent, "pamconsole") != NULL
827 || hasmntopt (&mntent, "users") != NULL
828 || hasmntopt (&mntent, "owner") != NULL
831 mount_entry->is_user_mountable = TRUE;
833 return_list = g_list_prepend (return_list, mount_entry);
837 G_UNLOCK (getmntent);
839 return g_list_reverse (return_list);
841 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
843 /* functions to parse /etc/filesystems on aix */
845 /* read character, ignoring comments (begin with '*', end with '\n' */
847 aix_fs_getc (FILE *fd)
851 while ((c = getc (fd)) == '*')
853 while (((c = getc (fd)) != '\n') && (c != EOF))
858 /* eat all continuous spaces in a file */
860 aix_fs_ignorespace (FILE *fd)
864 while ((c = aix_fs_getc (fd)) != EOF)
866 if (!g_ascii_isspace (c))
876 /* read one word from file */
878 aix_fs_getword (FILE *fd,
883 aix_fs_ignorespace (fd);
885 while (((c = aix_fs_getc (fd)) != EOF) && !g_ascii_isspace (c))
889 while (((c = aix_fs_getc (fd)) != EOF) && (c != '"'))
901 char mnt_mount[PATH_MAX];
902 char mnt_special[PATH_MAX];
904 char mnt_options[128];
905 } AixMountTableEntry;
907 /* read mount points properties */
909 aix_fs_get (FILE *fd,
910 AixMountTableEntry *prop)
912 static char word[PATH_MAX] = { 0 };
913 char value[PATH_MAX];
918 if (aix_fs_getword (fd, word) == EOF)
922 word[strlen(word) - 1] = 0;
923 strcpy (prop->mnt_mount, word);
925 /* read attributes and value */
927 while (aix_fs_getword (fd, word) != EOF)
929 /* test if is attribute or new stanza */
930 if (word[strlen(word) - 1] == ':')
934 aix_fs_getword (fd, value);
937 aix_fs_getword (fd, value);
939 if (strcmp (word, "dev") == 0)
940 strcpy (prop->mnt_special, value);
941 else if (strcmp (word, "vfs") == 0)
942 strcpy (prop->mnt_fstype, value);
943 else if (strcmp (word, "options") == 0)
944 strcpy(prop->mnt_options, value);
951 _g_get_unix_mount_points (void)
953 struct mntent *mntent;
956 GUnixMountPoint *mount_entry;
957 AixMountTableEntry mntent;
960 read_file = get_fstab_file ();
962 file = setmntent (read_file, "r");
968 while (!aix_fs_get (file, &mntent))
970 if (strcmp ("cdrfs", mntent.mnt_fstype) == 0)
972 mount_entry = g_new0 (GUnixMountPoint, 1);
974 mount_entry->mount_path = g_strdup (mntent.mnt_mount);
975 mount_entry->device_path = g_strdup (mntent.mnt_special);
976 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
977 mount_entry->is_read_only = TRUE;
978 mount_entry->is_user_mountable = TRUE;
980 return_list = g_list_prepend (return_list, mount_entry);
986 return g_list_reverse (return_list);
989 #elif defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
992 _g_get_unix_mount_points (void)
994 struct fstab *fstab = NULL;
995 GUnixMountPoint *mount_entry;
997 #ifdef HAVE_SYS_SYSCTL_H
999 size_t len = sizeof(usermnt);
1008 #ifdef HAVE_SYS_SYSCTL_H
1009 #if defined(HAVE_SYSCTLBYNAME)
1010 sysctlbyname ("vfs.usermount", &usermnt, &len, NULL, 0);
1011 #elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
1016 mib[1] = VFS_USERMOUNT;
1017 sysctl (mib, 2, &usermnt, &len, NULL, 0);
1019 #elif defined(CTL_KERN) && defined(KERN_USERMOUNT)
1024 mib[1] = KERN_USERMOUNT;
1025 sysctl (mib, 2, &usermnt, &len, NULL, 0);
1030 while ((fstab = getfsent ()) != NULL)
1032 if (strcmp (fstab->fs_vfstype, "swap") == 0)
1035 mount_entry = g_new0 (GUnixMountPoint, 1);
1037 mount_entry->mount_path = g_strdup (fstab->fs_file);
1038 mount_entry->device_path = g_strdup (fstab->fs_spec);
1039 mount_entry->filesystem_type = g_strdup (fstab->fs_vfstype);
1041 if (strcmp (fstab->fs_type, "ro") == 0)
1042 mount_entry->is_read_only = TRUE;
1044 #ifdef HAVE_SYS_SYSCTL_H
1047 uid_t uid = getuid ();
1048 if (stat (fstab->fs_file, &sb) == 0)
1050 if (uid == 0 || sb.st_uid == uid)
1051 mount_entry->is_user_mountable = TRUE;
1056 return_list = g_list_prepend (return_list, mount_entry);
1061 return g_list_reverse (return_list);
1063 #elif defined(__INTERIX)
1065 _g_get_unix_mount_points (void)
1067 return _g_get_unix_mounts ();
1070 #error No g_get_mount_table() implementation for system
1074 get_mounts_timestamp (void)
1076 const char *monitor_file;
1079 monitor_file = get_mtab_monitor_file ();
1082 if (stat (monitor_file, &buf) == 0)
1083 return (guint64)buf.st_mtime;
1089 get_mount_points_timestamp (void)
1091 const char *monitor_file;
1094 monitor_file = get_fstab_file ();
1097 if (stat (monitor_file, &buf) == 0)
1098 return (guint64)buf.st_mtime;
1104 * g_unix_mounts_get: (skip)
1105 * @time_read: (out) (allow-none): guint64 to contain a timestamp, or %NULL
1107 * Gets a #GList of #GUnixMountEntry containing the unix mounts.
1108 * If @time_read is set, it will be filled with the mount
1109 * timestamp, allowing for checking if the mounts have changed
1110 * with g_unix_mounts_changed_since().
1112 * Returns: (element-type GUnixMountEntry) (transfer full):
1113 * a #GList of the UNIX mounts.
1116 g_unix_mounts_get (guint64 *time_read)
1119 *time_read = get_mounts_timestamp ();
1121 return _g_get_unix_mounts ();
1125 * g_unix_mount_at: (skip)
1126 * @mount_path: path for a possible unix mount.
1127 * @time_read: (out) (allow-none): guint64 to contain a timestamp.
1129 * Gets a #GUnixMountEntry for a given mount path. If @time_read
1130 * is set, it will be filled with a unix timestamp for checking
1131 * if the mounts have changed since with g_unix_mounts_changed_since().
1133 * Returns: (transfer full): a #GUnixMountEntry.
1136 g_unix_mount_at (const char *mount_path,
1140 GUnixMountEntry *mount_entry, *found;
1142 mounts = g_unix_mounts_get (time_read);
1145 for (l = mounts; l != NULL; l = l->next)
1147 mount_entry = l->data;
1149 if (!found && strcmp (mount_path, mount_entry->mount_path) == 0)
1150 found = mount_entry;
1152 g_unix_mount_free (mount_entry);
1154 g_list_free (mounts);
1160 * g_unix_mount_points_get: (skip)
1161 * @time_read: (out) (allow-none): guint64 to contain a timestamp.
1163 * Gets a #GList of #GUnixMountPoint containing the unix mount points.
1164 * If @time_read is set, it will be filled with the mount timestamp,
1165 * allowing for checking if the mounts have changed with
1166 * g_unix_mount_points_changed_since().
1168 * Returns: (element-type GUnixMountPoint) (transfer full):
1169 * a #GList of the UNIX mountpoints.
1172 g_unix_mount_points_get (guint64 *time_read)
1175 *time_read = get_mount_points_timestamp ();
1177 return _g_get_unix_mount_points ();
1181 * g_unix_mounts_changed_since:
1182 * @time: guint64 to contain a timestamp.
1184 * Checks if the unix mounts have changed since a given unix time.
1186 * Returns: %TRUE if the mounts have changed since @time.
1189 g_unix_mounts_changed_since (guint64 time)
1191 return get_mounts_timestamp () != time;
1195 * g_unix_mount_points_changed_since:
1196 * @time: guint64 to contain a timestamp.
1198 * Checks if the unix mount points have changed since a given unix time.
1200 * Returns: %TRUE if the mount points have changed since @time.
1203 g_unix_mount_points_changed_since (guint64 time)
1205 return get_mount_points_timestamp () != time;
1209 g_unix_mount_monitor_finalize (GObject *object)
1211 GUnixMountMonitor *monitor;
1213 monitor = G_UNIX_MOUNT_MONITOR (object);
1215 if (monitor->fstab_monitor)
1217 g_file_monitor_cancel (monitor->fstab_monitor);
1218 g_object_unref (monitor->fstab_monitor);
1221 if (monitor->mtab_monitor)
1223 g_file_monitor_cancel (monitor->mtab_monitor);
1224 g_object_unref (monitor->mtab_monitor);
1227 the_mount_monitor = NULL;
1229 G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize (object);
1234 g_unix_mount_monitor_class_init (GUnixMountMonitorClass *klass)
1236 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1238 gobject_class->finalize = g_unix_mount_monitor_finalize;
1241 * GUnixMountMonitor::mounts-changed:
1242 * @monitor: the object on which the signal is emitted
1244 * Emitted when the unix mounts have changed.
1246 signals[MOUNTS_CHANGED] =
1247 g_signal_new ("mounts-changed",
1248 G_TYPE_FROM_CLASS (klass),
1252 g_cclosure_marshal_VOID__VOID,
1256 * GUnixMountMonitor::mountpoints-changed:
1257 * @monitor: the object on which the signal is emitted
1259 * Emitted when the unix mount points have changed.
1261 signals[MOUNTPOINTS_CHANGED] =
1262 g_signal_new ("mountpoints-changed",
1263 G_TYPE_FROM_CLASS (klass),
1267 g_cclosure_marshal_VOID__VOID,
1272 fstab_file_changed (GFileMonitor *monitor,
1275 GFileMonitorEvent event_type,
1278 GUnixMountMonitor *mount_monitor;
1280 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1281 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1282 event_type != G_FILE_MONITOR_EVENT_DELETED)
1285 mount_monitor = user_data;
1286 g_signal_emit (mount_monitor, signals[MOUNTPOINTS_CHANGED], 0);
1290 mtab_file_changed (GFileMonitor *monitor,
1293 GFileMonitorEvent event_type,
1296 GUnixMountMonitor *mount_monitor;
1298 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1299 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1300 event_type != G_FILE_MONITOR_EVENT_DELETED)
1303 mount_monitor = user_data;
1304 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1308 g_unix_mount_monitor_init (GUnixMountMonitor *monitor)
1312 if (get_fstab_file () != NULL)
1314 file = g_file_new_for_path (get_fstab_file ());
1315 monitor->fstab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1316 g_object_unref (file);
1318 g_signal_connect (monitor->fstab_monitor, "changed", (GCallback)fstab_file_changed, monitor);
1321 if (get_mtab_monitor_file () != NULL)
1323 file = g_file_new_for_path (get_mtab_monitor_file ());
1324 monitor->mtab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1325 g_object_unref (file);
1327 g_signal_connect (monitor->mtab_monitor, "changed", (GCallback)mtab_file_changed, monitor);
1332 * g_unix_mount_monitor_set_rate_limit:
1333 * @mount_monitor: a #GUnixMountMonitor
1334 * @limit_msec: a integer with the limit in milliseconds to
1337 * Sets the rate limit to which the @mount_monitor will report
1338 * consecutive change events to the mount and mount point entry files.
1343 g_unix_mount_monitor_set_rate_limit (GUnixMountMonitor *mount_monitor,
1346 g_return_if_fail (G_IS_UNIX_MOUNT_MONITOR (mount_monitor));
1348 if (mount_monitor->fstab_monitor != NULL)
1349 g_file_monitor_set_rate_limit (mount_monitor->fstab_monitor, limit_msec);
1351 if (mount_monitor->mtab_monitor != NULL)
1352 g_file_monitor_set_rate_limit (mount_monitor->mtab_monitor, limit_msec);
1356 * g_unix_mount_monitor_new:
1358 * Gets a new #GUnixMountMonitor. The default rate limit for which the
1359 * monitor will report consecutive changes for the mount and mount
1360 * point entry files is the default for a #GFileMonitor. Use
1361 * g_unix_mount_monitor_set_rate_limit() to change this.
1363 * Returns: a #GUnixMountMonitor.
1366 g_unix_mount_monitor_new (void)
1368 if (the_mount_monitor == NULL)
1370 the_mount_monitor = g_object_new (G_TYPE_UNIX_MOUNT_MONITOR, NULL);
1371 return the_mount_monitor;
1374 return g_object_ref (the_mount_monitor);
1378 * g_unix_mount_free:
1379 * @mount_entry: a #GUnixMount.
1381 * Frees a unix mount.
1384 g_unix_mount_free (GUnixMountEntry *mount_entry)
1386 g_return_if_fail (mount_entry != NULL);
1388 g_free (mount_entry->mount_path);
1389 g_free (mount_entry->device_path);
1390 g_free (mount_entry->filesystem_type);
1391 g_free (mount_entry);
1395 * g_unix_mount_point_free:
1396 * @mount_point: unix mount point to free.
1398 * Frees a unix mount point.
1401 g_unix_mount_point_free (GUnixMountPoint *mount_point)
1403 g_return_if_fail (mount_point != NULL);
1405 g_free (mount_point->mount_path);
1406 g_free (mount_point->device_path);
1407 g_free (mount_point->filesystem_type);
1408 g_free (mount_point);
1412 * g_unix_mount_compare:
1413 * @mount1: first #GUnixMountEntry to compare.
1414 * @mount2: second #GUnixMountEntry to compare.
1416 * Compares two unix mounts.
1418 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1419 * or less than @mount2, respectively.
1422 g_unix_mount_compare (GUnixMountEntry *mount1,
1423 GUnixMountEntry *mount2)
1427 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1429 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1433 res = g_strcmp0 (mount1->device_path, mount2->device_path);
1437 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1441 res = mount1->is_read_only - mount2->is_read_only;
1449 * g_unix_mount_get_mount_path:
1450 * @mount_entry: input #GUnixMountEntry to get the mount path for.
1452 * Gets the mount path for a unix mount.
1454 * Returns: the mount path for @mount_entry.
1457 g_unix_mount_get_mount_path (GUnixMountEntry *mount_entry)
1459 g_return_val_if_fail (mount_entry != NULL, NULL);
1461 return mount_entry->mount_path;
1465 * g_unix_mount_get_device_path:
1466 * @mount_entry: a #GUnixMount.
1468 * Gets the device path for a unix mount.
1470 * Returns: a string containing the device path.
1473 g_unix_mount_get_device_path (GUnixMountEntry *mount_entry)
1475 g_return_val_if_fail (mount_entry != NULL, NULL);
1477 return mount_entry->device_path;
1481 * g_unix_mount_get_fs_type:
1482 * @mount_entry: a #GUnixMount.
1484 * Gets the filesystem type for the unix mount.
1486 * Returns: a string containing the file system type.
1489 g_unix_mount_get_fs_type (GUnixMountEntry *mount_entry)
1491 g_return_val_if_fail (mount_entry != NULL, NULL);
1493 return mount_entry->filesystem_type;
1497 * g_unix_mount_is_readonly:
1498 * @mount_entry: a #GUnixMount.
1500 * Checks if a unix mount is mounted read only.
1502 * Returns: %TRUE if @mount_entry is read only.
1505 g_unix_mount_is_readonly (GUnixMountEntry *mount_entry)
1507 g_return_val_if_fail (mount_entry != NULL, FALSE);
1509 return mount_entry->is_read_only;
1513 * g_unix_mount_is_system_internal:
1514 * @mount_entry: a #GUnixMount.
1516 * Checks if a unix mount is a system path.
1518 * Returns: %TRUE if the unix mount is for a system path.
1521 g_unix_mount_is_system_internal (GUnixMountEntry *mount_entry)
1523 g_return_val_if_fail (mount_entry != NULL, FALSE);
1525 return mount_entry->is_system_internal;
1529 * g_unix_mount_point_compare:
1530 * @mount1: a #GUnixMount.
1531 * @mount2: a #GUnixMount.
1533 * Compares two unix mount points.
1535 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1536 * or less than @mount2, respectively.
1539 g_unix_mount_point_compare (GUnixMountPoint *mount1,
1540 GUnixMountPoint *mount2)
1544 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1546 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1550 res = g_strcmp0 (mount1->device_path, mount2->device_path);
1554 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1558 res = mount1->is_read_only - mount2->is_read_only;
1562 res = mount1->is_user_mountable - mount2->is_user_mountable;
1566 res = mount1->is_loopback - mount2->is_loopback;
1574 * g_unix_mount_point_get_mount_path:
1575 * @mount_point: a #GUnixMountPoint.
1577 * Gets the mount path for a unix mount point.
1579 * Returns: a string containing the mount path.
1582 g_unix_mount_point_get_mount_path (GUnixMountPoint *mount_point)
1584 g_return_val_if_fail (mount_point != NULL, NULL);
1586 return mount_point->mount_path;
1590 * g_unix_mount_point_get_device_path:
1591 * @mount_point: a #GUnixMountPoint.
1593 * Gets the device path for a unix mount point.
1595 * Returns: a string containing the device path.
1598 g_unix_mount_point_get_device_path (GUnixMountPoint *mount_point)
1600 g_return_val_if_fail (mount_point != NULL, NULL);
1602 return mount_point->device_path;
1606 * g_unix_mount_point_get_fs_type:
1607 * @mount_point: a #GUnixMountPoint.
1609 * Gets the file system type for the mount point.
1611 * Returns: a string containing the file system type.
1614 g_unix_mount_point_get_fs_type (GUnixMountPoint *mount_point)
1616 g_return_val_if_fail (mount_point != NULL, NULL);
1618 return mount_point->filesystem_type;
1622 * g_unix_mount_point_is_readonly:
1623 * @mount_point: a #GUnixMountPoint.
1625 * Checks if a unix mount point is read only.
1627 * Returns: %TRUE if a mount point is read only.
1630 g_unix_mount_point_is_readonly (GUnixMountPoint *mount_point)
1632 g_return_val_if_fail (mount_point != NULL, FALSE);
1634 return mount_point->is_read_only;
1638 * g_unix_mount_point_is_user_mountable:
1639 * @mount_point: a #GUnixMountPoint.
1641 * Checks if a unix mount point is mountable by the user.
1643 * Returns: %TRUE if the mount point is user mountable.
1646 g_unix_mount_point_is_user_mountable (GUnixMountPoint *mount_point)
1648 g_return_val_if_fail (mount_point != NULL, FALSE);
1650 return mount_point->is_user_mountable;
1654 * g_unix_mount_point_is_loopback:
1655 * @mount_point: a #GUnixMountPoint.
1657 * Checks if a unix mount point is a loopback device.
1659 * Returns: %TRUE if the mount point is a loopback. %FALSE otherwise.
1662 g_unix_mount_point_is_loopback (GUnixMountPoint *mount_point)
1664 g_return_val_if_fail (mount_point != NULL, FALSE);
1666 return mount_point->is_loopback;
1669 static GUnixMountType
1670 guess_mount_type (const char *mount_path,
1671 const char *device_path,
1672 const char *filesystem_type)
1674 GUnixMountType type;
1677 type = G_UNIX_MOUNT_TYPE_UNKNOWN;
1679 if ((strcmp (filesystem_type, "udf") == 0) ||
1680 (strcmp (filesystem_type, "iso9660") == 0) ||
1681 (strcmp (filesystem_type, "cd9660") == 0))
1682 type = G_UNIX_MOUNT_TYPE_CDROM;
1683 else if ((strcmp (filesystem_type, "nfs") == 0) ||
1684 (strcmp (filesystem_type, "nfs4") == 0))
1685 type = G_UNIX_MOUNT_TYPE_NFS;
1686 else if (g_str_has_prefix (device_path, "/vol/dev/diskette/") ||
1687 g_str_has_prefix (device_path, "/dev/fd") ||
1688 g_str_has_prefix (device_path, "/dev/floppy"))
1689 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1690 else if (g_str_has_prefix (device_path, "/dev/cdrom") ||
1691 g_str_has_prefix (device_path, "/dev/acd") ||
1692 g_str_has_prefix (device_path, "/dev/cd"))
1693 type = G_UNIX_MOUNT_TYPE_CDROM;
1694 else if (g_str_has_prefix (device_path, "/vol/"))
1696 const char *name = mount_path + strlen ("/");
1698 if (g_str_has_prefix (name, "cdrom"))
1699 type = G_UNIX_MOUNT_TYPE_CDROM;
1700 else if (g_str_has_prefix (name, "floppy") ||
1701 g_str_has_prefix (device_path, "/vol/dev/diskette/"))
1702 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1703 else if (g_str_has_prefix (name, "rmdisk"))
1704 type = G_UNIX_MOUNT_TYPE_ZIP;
1705 else if (g_str_has_prefix (name, "jaz"))
1706 type = G_UNIX_MOUNT_TYPE_JAZ;
1707 else if (g_str_has_prefix (name, "memstick"))
1708 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1712 basename = g_path_get_basename (mount_path);
1714 if (g_str_has_prefix (basename, "cdr") ||
1715 g_str_has_prefix (basename, "cdwriter") ||
1716 g_str_has_prefix (basename, "burn") ||
1717 g_str_has_prefix (basename, "dvdr"))
1718 type = G_UNIX_MOUNT_TYPE_CDROM;
1719 else if (g_str_has_prefix (basename, "floppy"))
1720 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1721 else if (g_str_has_prefix (basename, "zip"))
1722 type = G_UNIX_MOUNT_TYPE_ZIP;
1723 else if (g_str_has_prefix (basename, "jaz"))
1724 type = G_UNIX_MOUNT_TYPE_JAZ;
1725 else if (g_str_has_prefix (basename, "camera"))
1726 type = G_UNIX_MOUNT_TYPE_CAMERA;
1727 else if (g_str_has_prefix (basename, "memstick") ||
1728 g_str_has_prefix (basename, "memory_stick") ||
1729 g_str_has_prefix (basename, "ram"))
1730 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1731 else if (g_str_has_prefix (basename, "compact_flash"))
1732 type = G_UNIX_MOUNT_TYPE_CF;
1733 else if (g_str_has_prefix (basename, "smart_media"))
1734 type = G_UNIX_MOUNT_TYPE_SM;
1735 else if (g_str_has_prefix (basename, "sd_mmc"))
1736 type = G_UNIX_MOUNT_TYPE_SDMMC;
1737 else if (g_str_has_prefix (basename, "ipod"))
1738 type = G_UNIX_MOUNT_TYPE_IPOD;
1743 if (type == G_UNIX_MOUNT_TYPE_UNKNOWN)
1744 type = G_UNIX_MOUNT_TYPE_HD;
1750 * g_unix_mount_guess_type:
1751 * @mount_entry: a #GUnixMount.
1753 * Guesses the type of a unix mount. If the mount type cannot be
1754 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1756 * Returns: a #GUnixMountType.
1758 static GUnixMountType
1759 g_unix_mount_guess_type (GUnixMountEntry *mount_entry)
1761 g_return_val_if_fail (mount_entry != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1762 g_return_val_if_fail (mount_entry->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1763 g_return_val_if_fail (mount_entry->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1764 g_return_val_if_fail (mount_entry->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1766 return guess_mount_type (mount_entry->mount_path,
1767 mount_entry->device_path,
1768 mount_entry->filesystem_type);
1772 * g_unix_mount_point_guess_type:
1773 * @mount_point: a #GUnixMountPoint.
1775 * Guesses the type of a unix mount point.
1776 * If the mount type cannot be determined,
1777 * returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1779 * Returns: a #GUnixMountType.
1781 static GUnixMountType
1782 g_unix_mount_point_guess_type (GUnixMountPoint *mount_point)
1784 g_return_val_if_fail (mount_point != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1785 g_return_val_if_fail (mount_point->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1786 g_return_val_if_fail (mount_point->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1787 g_return_val_if_fail (mount_point->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1789 return guess_mount_type (mount_point->mount_path,
1790 mount_point->device_path,
1791 mount_point->filesystem_type);
1795 type_to_icon (GUnixMountType type, gboolean is_mount_point)
1797 const char *icon_name;
1801 case G_UNIX_MOUNT_TYPE_HD:
1803 icon_name = "drive-removable-media";
1805 icon_name = "drive-harddisk";
1807 case G_UNIX_MOUNT_TYPE_FLOPPY:
1808 case G_UNIX_MOUNT_TYPE_ZIP:
1809 case G_UNIX_MOUNT_TYPE_JAZ:
1811 icon_name = "drive-removable-media";
1813 icon_name = "media-floppy";
1815 case G_UNIX_MOUNT_TYPE_CDROM:
1817 icon_name = "drive-optical";
1819 icon_name = "media-optical";
1821 case G_UNIX_MOUNT_TYPE_NFS:
1822 /* TODO: Would like a better icon here... */
1824 icon_name = "drive-removable-media";
1826 icon_name = "drive-harddisk";
1828 case G_UNIX_MOUNT_TYPE_MEMSTICK:
1830 icon_name = "drive-removable-media";
1832 icon_name = "media-flash";
1834 case G_UNIX_MOUNT_TYPE_CAMERA:
1836 icon_name = "drive-removable-media";
1838 icon_name = "camera-photo";
1840 case G_UNIX_MOUNT_TYPE_IPOD:
1842 icon_name = "drive-removable-media";
1844 icon_name = "multimedia-player";
1846 case G_UNIX_MOUNT_TYPE_UNKNOWN:
1849 icon_name = "drive-removable-media";
1851 icon_name = "drive-harddisk";
1859 * g_unix_mount_guess_name:
1860 * @mount_entry: a #GUnixMountEntry
1862 * Guesses the name of a Unix mount.
1863 * The result is a translated string.
1865 * Returns: A newly allocated string that must
1866 * be freed with g_free()
1869 g_unix_mount_guess_name (GUnixMountEntry *mount_entry)
1873 if (strcmp (mount_entry->mount_path, "/") == 0)
1874 name = g_strdup (_("Filesystem root"));
1876 name = g_filename_display_basename (mount_entry->mount_path);
1882 * g_unix_mount_guess_icon:
1883 * @mount_entry: a #GUnixMountEntry
1885 * Guesses the icon of a Unix mount.
1887 * Returns: (transfer full): a #GIcon
1890 g_unix_mount_guess_icon (GUnixMountEntry *mount_entry)
1892 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE));
1896 * g_unix_mount_point_guess_name:
1897 * @mount_point: a #GUnixMountPoint
1899 * Guesses the name of a Unix mount point.
1900 * The result is a translated string.
1902 * Returns: A newly allocated string that must
1903 * be freed with g_free()
1906 g_unix_mount_point_guess_name (GUnixMountPoint *mount_point)
1910 if (strcmp (mount_point->mount_path, "/") == 0)
1911 name = g_strdup (_("Filesystem root"));
1913 name = g_filename_display_basename (mount_point->mount_path);
1919 * g_unix_mount_point_guess_icon:
1920 * @mount_point: a #GUnixMountPoint
1922 * Guesses the icon of a Unix mount point.
1924 * Returns: (transfer full): a #GIcon
1927 g_unix_mount_point_guess_icon (GUnixMountPoint *mount_point)
1929 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE));
1933 * g_unix_mount_guess_can_eject:
1934 * @mount_entry: a #GUnixMountEntry
1936 * Guesses whether a Unix mount can be ejected.
1938 * Returns: %TRUE if @mount_entry is deemed to be ejectable.
1941 g_unix_mount_guess_can_eject (GUnixMountEntry *mount_entry)
1943 GUnixMountType guessed_type;
1945 guessed_type = g_unix_mount_guess_type (mount_entry);
1946 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
1947 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
1954 * g_unix_mount_guess_should_display:
1955 * @mount_entry: a #GUnixMountEntry
1957 * Guesses whether a Unix mount should be displayed in the UI.
1959 * Returns: %TRUE if @mount_entry is deemed to be displayable.
1962 g_unix_mount_guess_should_display (GUnixMountEntry *mount_entry)
1964 const char *mount_path;
1966 /* Never display internal mountpoints */
1967 if (g_unix_mount_is_system_internal (mount_entry))
1970 /* Only display things in /media (which are generally user mountable)
1971 and home dir (fuse stuff) */
1972 mount_path = mount_entry->mount_path;
1973 if (mount_path != NULL)
1975 /* Hide mounts within a dot path, suppose it was a purpose to hide this mount */
1976 if (g_strstr_len (mount_path, -1, "/.") != NULL)
1979 if (g_str_has_prefix (mount_path, "/media/"))
1982 /* Avoid displaying mounts that are not accessible to the user.
1984 * See http://bugzilla.gnome.org/show_bug.cgi?id=526320 for why we
1985 * want to avoid g_access() for mount points which can potentially
1986 * block or fail stat()'ing, such as network mounts.
1988 path = g_path_get_dirname (mount_path);
1989 if (g_str_has_prefix (path, "/media/"))
1991 if (g_access (path, R_OK|X_OK) != 0)
1999 if (mount_entry->device_path && mount_entry->device_path[0] == '/')
2002 if (g_stat (mount_entry->device_path, &st) == 0 &&
2003 S_ISBLK(st.st_mode) &&
2004 g_access (mount_path, R_OK|X_OK) != 0)
2010 if (g_str_has_prefix (mount_path, g_get_home_dir ()) &&
2011 mount_path[strlen (g_get_home_dir())] == G_DIR_SEPARATOR)
2019 * g_unix_mount_point_guess_can_eject:
2020 * @mount_point: a #GUnixMountPoint
2022 * Guesses whether a Unix mount point can be ejected.
2024 * Returns: %TRUE if @mount_point is deemed to be ejectable.
2027 g_unix_mount_point_guess_can_eject (GUnixMountPoint *mount_point)
2029 GUnixMountType guessed_type;
2031 guessed_type = g_unix_mount_point_guess_type (mount_point);
2032 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
2033 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
2040 /* borrowed from gtk/gtkfilesystemunix.c in GTK+ on 02/23/2006 */
2042 _canonicalize_filename (gchar *filename)
2045 gboolean last_was_slash = FALSE;
2052 if (*p == G_DIR_SEPARATOR)
2054 if (!last_was_slash)
2055 *q++ = G_DIR_SEPARATOR;
2057 last_was_slash = TRUE;
2061 if (last_was_slash && *p == '.')
2063 if (*(p + 1) == G_DIR_SEPARATOR ||
2066 if (*(p + 1) == '\0')
2071 else if (*(p + 1) == '.' &&
2072 (*(p + 2) == G_DIR_SEPARATOR ||
2075 if (q > filename + 1)
2078 while (q > filename + 1 &&
2079 *(q - 1) != G_DIR_SEPARATOR)
2083 if (*(p + 2) == '\0')
2091 last_was_slash = FALSE;
2097 last_was_slash = FALSE;
2104 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
2111 _resolve_symlink (const char *file)
2119 f = g_strdup (file);
2121 while (g_file_test (f, G_FILE_TEST_IS_SYMLINK))
2123 link = g_file_read_link (f, &error);
2126 g_error_free (error);
2132 dir = g_path_get_dirname (f);
2133 f1 = g_strdup_printf ("%s/%s", dir, link);
2142 _canonicalize_filename (f);
2146 #ifdef HAVE_MNTENT_H
2148 _resolve_dev_root (void)
2150 static gboolean have_real_dev_root = FALSE;
2151 static char real_dev_root[256];
2152 struct stat statbuf;
2154 /* see if it's cached already */
2155 if (have_real_dev_root)
2158 /* otherwise we're going to find it right away.. */
2159 have_real_dev_root = TRUE;
2161 if (stat ("/dev/root", &statbuf) == 0)
2163 if (! S_ISLNK (statbuf.st_mode))
2165 dev_t root_dev = statbuf.st_dev;
2169 /* see if device with similar major:minor as /dev/root is mention
2170 * in /etc/mtab (it usually is)
2172 f = fopen ("/etc/mtab", "r");
2175 struct mntent *entp;
2176 #ifdef HAVE_GETMNTENT_R
2178 while ((entp = getmntent_r (f, &ent, buf, sizeof (buf))) != NULL)
2182 while ((entp = getmntent (f)) != NULL)
2185 if (stat (entp->mnt_fsname, &statbuf) == 0 &&
2186 statbuf.st_dev == root_dev)
2188 strncpy (real_dev_root, entp->mnt_fsname, sizeof (real_dev_root) - 1);
2189 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2197 #ifndef HAVE_GETMNTENT_R
2198 G_UNLOCK (getmntent);
2202 /* no, that didn't work.. next we could scan /dev ... but I digress.. */
2208 resolved = _resolve_symlink ("/dev/root");
2209 if (resolved != NULL)
2211 strncpy (real_dev_root, resolved, sizeof (real_dev_root) - 1);
2212 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2220 strcpy (real_dev_root, "/dev/root");
2223 return real_dev_root;