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 */
261 "/var/log/audit", /* https://bugzilla.redhat.com/show_bug.cgi?id=333041 */
264 "/var/tmp", /* https://bugzilla.redhat.com/show_bug.cgi?id=335241 */
272 if (is_in (mount_path, ignore_mountpoints))
275 if (g_str_has_prefix (mount_path, "/dev/") ||
276 g_str_has_prefix (mount_path, "/proc/") ||
277 g_str_has_prefix (mount_path, "/sys/"))
280 if (g_str_has_suffix (mount_path, "/.gvfs"))
287 guess_system_internal (const char *mountpoint,
291 const char *ignore_fs[] = {
312 const char *ignore_devices[] = {
322 if (is_in (fs, ignore_fs))
325 if (is_in (device, ignore_devices))
328 if (g_unix_is_mount_path_system_internal (mountpoint))
337 get_mtab_read_file (void)
341 return "/proc/mounts";
343 return _PATH_MOUNTED;
351 get_mtab_monitor_file (void)
354 return _PATH_MOUNTED;
360 #ifndef HAVE_GETMNTENT_R
361 G_LOCK_DEFINE_STATIC(getmntent);
365 _g_get_unix_mounts (void)
367 #ifdef HAVE_GETMNTENT_R
371 struct mntent *mntent;
374 GUnixMountEntry *mount_entry;
375 GHashTable *mounts_hash;
378 read_file = get_mtab_read_file ();
380 file = setmntent (read_file, "r");
386 mounts_hash = g_hash_table_new (g_str_hash, g_str_equal);
388 #ifdef HAVE_GETMNTENT_R
389 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
392 while ((mntent = getmntent (file)) != NULL)
395 /* ignore any mnt_fsname that is repeated and begins with a '/'
397 * We do this to avoid being fooled by --bind mounts, since
398 * these have the same device as the location they bind to.
399 * It's not an ideal solution to the problem, but it's likely that
400 * the most important mountpoint is first and the --bind ones after
401 * that aren't as important. So it should work.
403 * The '/' is to handle procfs, tmpfs and other no device mounts.
405 if (mntent->mnt_fsname != NULL &&
406 mntent->mnt_fsname[0] == '/' &&
407 g_hash_table_lookup (mounts_hash, mntent->mnt_fsname))
410 mount_entry = g_new0 (GUnixMountEntry, 1);
411 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
412 if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
413 mount_entry->device_path = g_strdup (_resolve_dev_root ());
415 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
416 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
418 #if defined (HAVE_HASMNTOPT)
419 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
420 mount_entry->is_read_only = TRUE;
423 mount_entry->is_system_internal =
424 guess_system_internal (mount_entry->mount_path,
425 mount_entry->filesystem_type,
426 mount_entry->device_path);
428 g_hash_table_insert (mounts_hash,
429 mount_entry->device_path,
430 mount_entry->device_path);
432 return_list = g_list_prepend (return_list, mount_entry);
434 g_hash_table_destroy (mounts_hash);
438 #ifndef HAVE_GETMNTENT_R
439 G_UNLOCK (getmntent);
442 return g_list_reverse (return_list);
445 #elif defined (HAVE_SYS_MNTTAB_H)
447 G_LOCK_DEFINE_STATIC(getmntent);
450 get_mtab_read_file (void)
453 return _PATH_MOUNTED;
455 return "/etc/mnttab";
460 get_mtab_monitor_file (void)
462 return get_mtab_read_file ();
466 _g_get_unix_mounts (void)
468 struct mnttab mntent;
471 GUnixMountEntry *mount_entry;
474 read_file = get_mtab_read_file ();
476 file = setmntent (read_file, "r");
483 while (! getmntent (file, &mntent))
485 mount_entry = g_new0 (GUnixMountEntry, 1);
487 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
488 mount_entry->device_path = g_strdup (mntent.mnt_special);
489 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
491 #if defined (HAVE_HASMNTOPT)
492 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
493 mount_entry->is_read_only = TRUE;
496 mount_entry->is_system_internal =
497 guess_system_internal (mount_entry->mount_path,
498 mount_entry->filesystem_type,
499 mount_entry->device_path);
501 return_list = g_list_prepend (return_list, mount_entry);
506 G_UNLOCK (getmntent);
508 return g_list_reverse (return_list);
511 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
514 get_mtab_monitor_file (void)
520 _g_get_unix_mounts (void)
522 struct vfs_ent *fs_info;
523 struct vmount *vmount_info;
525 unsigned int vmount_size;
529 if (mntctl (MCTL_QUERY, sizeof (vmount_size), &vmount_size) != 0)
531 g_warning ("Unable to know the number of mounted volumes\n");
536 vmount_info = (struct vmount*)g_malloc (vmount_size);
538 vmount_number = mntctl (MCTL_QUERY, vmount_size, vmount_info);
540 if (vmount_info->vmt_revision != VMT_REVISION)
541 g_warning ("Bad vmount structure revision number, want %d, got %d\n", VMT_REVISION, vmount_info->vmt_revision);
543 if (vmount_number < 0)
545 g_warning ("Unable to recover mounted volumes information\n");
547 g_free (vmount_info);
552 while (vmount_number > 0)
554 mount_entry = g_new0 (GUnixMountEntry, 1);
556 mount_entry->device_path = g_strdup (vmt2dataptr (vmount_info, VMT_OBJECT));
557 mount_entry->mount_path = g_strdup (vmt2dataptr (vmount_info, VMT_STUB));
558 /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
559 mount_entry->is_read_only = (vmount_info->vmt_flags & MNT_READONLY) ? 1 : 0;
561 fs_info = getvfsbytype (vmount_info->vmt_gfstype);
564 mount_entry->filesystem_type = g_strdup ("unknown");
566 mount_entry->filesystem_type = g_strdup (fs_info->vfsent_name);
568 mount_entry->is_system_internal =
569 guess_system_internal (mount_entry->mount_path,
570 mount_entry->filesystem_type,
571 mount_entry->device_path);
573 return_list = g_list_prepend (return_list, mount_entry);
575 vmount_info = (struct vmount *)( (char*)vmount_info
576 + vmount_info->vmt_length);
580 g_free (vmount_info);
582 return g_list_reverse (return_list);
585 #elif defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
588 get_mtab_monitor_file (void)
594 _g_get_unix_mounts (void)
596 #if defined(USE_STATFS)
597 struct statfs *mntent = NULL;
598 #elif defined(USE_STATVFS)
599 struct statvfs *mntent = NULL;
601 #error statfs juggling failed
604 GUnixMountEntry *mount_entry;
607 /* Pass MNT_NOWAIT to avoid blocking trying to update NFS mounts. */
608 if ((num_mounts = getmntinfo (&mntent, MNT_NOWAIT)) == 0)
613 for (i = 0; i < num_mounts; i++)
615 mount_entry = g_new0 (GUnixMountEntry, 1);
617 mount_entry->mount_path = g_strdup (mntent[i].f_mntonname);
618 mount_entry->device_path = g_strdup (mntent[i].f_mntfromname);
619 mount_entry->filesystem_type = g_strdup (mntent[i].f_fstypename);
620 #if defined(USE_STATFS)
621 if (mntent[i].f_flags & MNT_RDONLY)
622 #elif defined(USE_STATVFS)
623 if (mntent[i].f_flag & MNT_RDONLY)
625 mount_entry->is_read_only = TRUE;
627 mount_entry->is_system_internal =
628 guess_system_internal (mount_entry->mount_path,
629 mount_entry->filesystem_type,
630 mount_entry->device_path);
632 return_list = g_list_prepend (return_list, mount_entry);
635 return g_list_reverse (return_list);
637 #elif defined(__INTERIX)
640 get_mtab_monitor_file (void)
646 _g_get_unix_mounts (void)
649 GList* return_list = NULL;
650 char filename[9 + NAME_MAX];
652 dirp = opendir ("/dev/fs");
655 g_warning ("unable to read /dev/fs!");
661 struct statvfs statbuf;
663 struct dirent* result;
665 if (readdir_r (dirp, &entry, &result) || result == NULL)
668 strcpy (filename, "/dev/fs/");
669 strcat (filename, entry.d_name);
671 if (statvfs (filename, &statbuf) == 0)
673 GUnixMountEntry* mount_entry = g_new0(GUnixMountEntry, 1);
675 mount_entry->mount_path = g_strdup (statbuf.f_mntonname);
676 mount_entry->device_path = g_strdup (statbuf.f_mntfromname);
677 mount_entry->filesystem_type = g_strdup (statbuf.f_fstypename);
679 if (statbuf.f_flag & ST_RDONLY)
680 mount_entry->is_read_only = TRUE;
682 return_list = g_list_prepend(return_list, mount_entry);
686 return_list = g_list_reverse (return_list);
693 #error No _g_get_unix_mounts() implementation for system
696 /* _g_get_unix_mount_points():
698 * don't return swap and ignore mounts.
702 get_fstab_file (void)
704 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
706 return "/etc/filesystems";
707 #elif defined(_PATH_MNTTAB)
709 #elif defined(VFSTAB)
718 _g_get_unix_mount_points (void)
720 #ifdef HAVE_GETMNTENT_R
724 struct mntent *mntent;
727 GUnixMountPoint *mount_entry;
730 read_file = get_fstab_file ();
732 file = setmntent (read_file, "r");
738 #ifdef HAVE_GETMNTENT_R
739 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
742 while ((mntent = getmntent (file)) != NULL)
745 if ((strcmp (mntent->mnt_dir, "ignore") == 0) ||
746 (strcmp (mntent->mnt_dir, "swap") == 0))
749 mount_entry = g_new0 (GUnixMountPoint, 1);
750 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
751 if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
752 mount_entry->device_path = g_strdup (_resolve_dev_root ());
754 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
755 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
757 #ifdef HAVE_HASMNTOPT
758 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
759 mount_entry->is_read_only = TRUE;
761 if (hasmntopt (mntent, "loop") != NULL)
762 mount_entry->is_loopback = TRUE;
766 if ((mntent->mnt_type != NULL && strcmp ("supermount", mntent->mnt_type) == 0)
767 #ifdef HAVE_HASMNTOPT
768 || (hasmntopt (mntent, "user") != NULL
769 && hasmntopt (mntent, "user") != hasmntopt (mntent, "user_xattr"))
770 || hasmntopt (mntent, "pamconsole") != NULL
771 || hasmntopt (mntent, "users") != NULL
772 || hasmntopt (mntent, "owner") != NULL
775 mount_entry->is_user_mountable = TRUE;
777 return_list = g_list_prepend (return_list, mount_entry);
782 #ifndef HAVE_GETMNTENT_R
783 G_UNLOCK (getmntent);
786 return g_list_reverse (return_list);
789 #elif defined (HAVE_SYS_MNTTAB_H)
792 _g_get_unix_mount_points (void)
794 struct mnttab mntent;
797 GUnixMountPoint *mount_entry;
800 read_file = get_fstab_file ();
802 file = setmntent (read_file, "r");
809 while (! getmntent (file, &mntent))
811 if ((strcmp (mntent.mnt_mountp, "ignore") == 0) ||
812 (strcmp (mntent.mnt_mountp, "swap") == 0))
815 mount_entry = g_new0 (GUnixMountPoint, 1);
817 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
818 mount_entry->device_path = g_strdup (mntent.mnt_special);
819 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
821 #ifdef HAVE_HASMNTOPT
822 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
823 mount_entry->is_read_only = TRUE;
825 if (hasmntopt (&mntent, "lofs") != NULL)
826 mount_entry->is_loopback = TRUE;
829 if ((mntent.mnt_fstype != NULL)
830 #ifdef HAVE_HASMNTOPT
831 || (hasmntopt (&mntent, "user") != NULL
832 && hasmntopt (&mntent, "user") != hasmntopt (&mntent, "user_xattr"))
833 || hasmntopt (&mntent, "pamconsole") != NULL
834 || hasmntopt (&mntent, "users") != NULL
835 || hasmntopt (&mntent, "owner") != NULL
838 mount_entry->is_user_mountable = TRUE;
840 return_list = g_list_prepend (return_list, mount_entry);
844 G_UNLOCK (getmntent);
846 return g_list_reverse (return_list);
848 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
850 /* functions to parse /etc/filesystems on aix */
852 /* read character, ignoring comments (begin with '*', end with '\n' */
854 aix_fs_getc (FILE *fd)
858 while ((c = getc (fd)) == '*')
860 while (((c = getc (fd)) != '\n') && (c != EOF))
865 /* eat all continuous spaces in a file */
867 aix_fs_ignorespace (FILE *fd)
871 while ((c = aix_fs_getc (fd)) != EOF)
873 if (!g_ascii_isspace (c))
883 /* read one word from file */
885 aix_fs_getword (FILE *fd,
890 aix_fs_ignorespace (fd);
892 while (((c = aix_fs_getc (fd)) != EOF) && !g_ascii_isspace (c))
896 while (((c = aix_fs_getc (fd)) != EOF) && (c != '"'))
908 char mnt_mount[PATH_MAX];
909 char mnt_special[PATH_MAX];
911 char mnt_options[128];
912 } AixMountTableEntry;
914 /* read mount points properties */
916 aix_fs_get (FILE *fd,
917 AixMountTableEntry *prop)
919 static char word[PATH_MAX] = { 0 };
920 char value[PATH_MAX];
925 if (aix_fs_getword (fd, word) == EOF)
929 word[strlen(word) - 1] = 0;
930 strcpy (prop->mnt_mount, word);
932 /* read attributes and value */
934 while (aix_fs_getword (fd, word) != EOF)
936 /* test if is attribute or new stanza */
937 if (word[strlen(word) - 1] == ':')
941 aix_fs_getword (fd, value);
944 aix_fs_getword (fd, value);
946 if (strcmp (word, "dev") == 0)
947 strcpy (prop->mnt_special, value);
948 else if (strcmp (word, "vfs") == 0)
949 strcpy (prop->mnt_fstype, value);
950 else if (strcmp (word, "options") == 0)
951 strcpy(prop->mnt_options, value);
958 _g_get_unix_mount_points (void)
960 struct mntent *mntent;
963 GUnixMountPoint *mount_entry;
964 AixMountTableEntry mntent;
967 read_file = get_fstab_file ();
969 file = setmntent (read_file, "r");
975 while (!aix_fs_get (file, &mntent))
977 if (strcmp ("cdrfs", mntent.mnt_fstype) == 0)
979 mount_entry = g_new0 (GUnixMountPoint, 1);
981 mount_entry->mount_path = g_strdup (mntent.mnt_mount);
982 mount_entry->device_path = g_strdup (mntent.mnt_special);
983 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
984 mount_entry->is_read_only = TRUE;
985 mount_entry->is_user_mountable = TRUE;
987 return_list = g_list_prepend (return_list, mount_entry);
993 return g_list_reverse (return_list);
996 #elif defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
999 _g_get_unix_mount_points (void)
1001 struct fstab *fstab = NULL;
1002 GUnixMountPoint *mount_entry;
1004 #ifdef HAVE_SYS_SYSCTL_H
1006 size_t len = sizeof(usermnt);
1015 #ifdef HAVE_SYS_SYSCTL_H
1016 #if defined(HAVE_SYSCTLBYNAME)
1017 sysctlbyname ("vfs.usermount", &usermnt, &len, NULL, 0);
1018 #elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
1023 mib[1] = VFS_USERMOUNT;
1024 sysctl (mib, 2, &usermnt, &len, NULL, 0);
1026 #elif defined(CTL_KERN) && defined(KERN_USERMOUNT)
1031 mib[1] = KERN_USERMOUNT;
1032 sysctl (mib, 2, &usermnt, &len, NULL, 0);
1037 while ((fstab = getfsent ()) != NULL)
1039 if (strcmp (fstab->fs_vfstype, "swap") == 0)
1042 mount_entry = g_new0 (GUnixMountPoint, 1);
1044 mount_entry->mount_path = g_strdup (fstab->fs_file);
1045 mount_entry->device_path = g_strdup (fstab->fs_spec);
1046 mount_entry->filesystem_type = g_strdup (fstab->fs_vfstype);
1048 if (strcmp (fstab->fs_type, "ro") == 0)
1049 mount_entry->is_read_only = TRUE;
1051 #ifdef HAVE_SYS_SYSCTL_H
1054 uid_t uid = getuid ();
1055 if (stat (fstab->fs_file, &sb) == 0)
1057 if (uid == 0 || sb.st_uid == uid)
1058 mount_entry->is_user_mountable = TRUE;
1063 return_list = g_list_prepend (return_list, mount_entry);
1068 return g_list_reverse (return_list);
1070 #elif defined(__INTERIX)
1072 _g_get_unix_mount_points (void)
1074 return _g_get_unix_mounts ();
1077 #error No g_get_mount_table() implementation for system
1081 get_mounts_timestamp (void)
1083 const char *monitor_file;
1086 monitor_file = get_mtab_monitor_file ();
1089 if (stat (monitor_file, &buf) == 0)
1090 return (guint64)buf.st_mtime;
1096 get_mount_points_timestamp (void)
1098 const char *monitor_file;
1101 monitor_file = get_fstab_file ();
1104 if (stat (monitor_file, &buf) == 0)
1105 return (guint64)buf.st_mtime;
1111 * g_unix_mounts_get: (skip)
1112 * @time_read: (out) (allow-none): guint64 to contain a timestamp, or %NULL
1114 * Gets a #GList of #GUnixMountEntry containing the unix mounts.
1115 * If @time_read is set, it will be filled with the mount
1116 * timestamp, allowing for checking if the mounts have changed
1117 * with g_unix_mounts_changed_since().
1119 * Returns: (element-type GUnixMountEntry) (transfer full):
1120 * a #GList of the UNIX mounts.
1123 g_unix_mounts_get (guint64 *time_read)
1126 *time_read = get_mounts_timestamp ();
1128 return _g_get_unix_mounts ();
1132 * g_unix_mount_at: (skip)
1133 * @mount_path: path for a possible unix mount.
1134 * @time_read: (out) (allow-none): guint64 to contain a timestamp.
1136 * Gets a #GUnixMountEntry for a given mount path. If @time_read
1137 * is set, it will be filled with a unix timestamp for checking
1138 * if the mounts have changed since with g_unix_mounts_changed_since().
1140 * Returns: (transfer full): a #GUnixMountEntry.
1143 g_unix_mount_at (const char *mount_path,
1147 GUnixMountEntry *mount_entry, *found;
1149 mounts = g_unix_mounts_get (time_read);
1152 for (l = mounts; l != NULL; l = l->next)
1154 mount_entry = l->data;
1156 if (!found && strcmp (mount_path, mount_entry->mount_path) == 0)
1157 found = mount_entry;
1159 g_unix_mount_free (mount_entry);
1161 g_list_free (mounts);
1167 * g_unix_mount_points_get: (skip)
1168 * @time_read: (out) (allow-none): guint64 to contain a timestamp.
1170 * Gets a #GList of #GUnixMountPoint containing the unix mount points.
1171 * If @time_read is set, it will be filled with the mount timestamp,
1172 * allowing for checking if the mounts have changed with
1173 * g_unix_mount_points_changed_since().
1175 * Returns: (element-type GUnixMountPoint) (transfer full):
1176 * a #GList of the UNIX mountpoints.
1179 g_unix_mount_points_get (guint64 *time_read)
1182 *time_read = get_mount_points_timestamp ();
1184 return _g_get_unix_mount_points ();
1188 * g_unix_mounts_changed_since:
1189 * @time: guint64 to contain a timestamp.
1191 * Checks if the unix mounts have changed since a given unix time.
1193 * Returns: %TRUE if the mounts have changed since @time.
1196 g_unix_mounts_changed_since (guint64 time)
1198 return get_mounts_timestamp () != time;
1202 * g_unix_mount_points_changed_since:
1203 * @time: guint64 to contain a timestamp.
1205 * Checks if the unix mount points have changed since a given unix time.
1207 * Returns: %TRUE if the mount points have changed since @time.
1210 g_unix_mount_points_changed_since (guint64 time)
1212 return get_mount_points_timestamp () != time;
1216 g_unix_mount_monitor_finalize (GObject *object)
1218 GUnixMountMonitor *monitor;
1220 monitor = G_UNIX_MOUNT_MONITOR (object);
1222 if (monitor->fstab_monitor)
1224 g_file_monitor_cancel (monitor->fstab_monitor);
1225 g_object_unref (monitor->fstab_monitor);
1228 if (monitor->mtab_monitor)
1230 g_file_monitor_cancel (monitor->mtab_monitor);
1231 g_object_unref (monitor->mtab_monitor);
1234 the_mount_monitor = NULL;
1236 G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize (object);
1241 g_unix_mount_monitor_class_init (GUnixMountMonitorClass *klass)
1243 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1245 gobject_class->finalize = g_unix_mount_monitor_finalize;
1248 * GUnixMountMonitor::mounts-changed:
1249 * @monitor: the object on which the signal is emitted
1251 * Emitted when the unix mounts have changed.
1253 signals[MOUNTS_CHANGED] =
1254 g_signal_new ("mounts-changed",
1255 G_TYPE_FROM_CLASS (klass),
1259 g_cclosure_marshal_VOID__VOID,
1263 * GUnixMountMonitor::mountpoints-changed:
1264 * @monitor: the object on which the signal is emitted
1266 * Emitted when the unix mount points have changed.
1268 signals[MOUNTPOINTS_CHANGED] =
1269 g_signal_new ("mountpoints-changed",
1270 G_TYPE_FROM_CLASS (klass),
1274 g_cclosure_marshal_VOID__VOID,
1279 fstab_file_changed (GFileMonitor *monitor,
1282 GFileMonitorEvent event_type,
1285 GUnixMountMonitor *mount_monitor;
1287 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1288 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1289 event_type != G_FILE_MONITOR_EVENT_DELETED)
1292 mount_monitor = user_data;
1293 g_signal_emit (mount_monitor, signals[MOUNTPOINTS_CHANGED], 0);
1297 mtab_file_changed (GFileMonitor *monitor,
1300 GFileMonitorEvent event_type,
1303 GUnixMountMonitor *mount_monitor;
1305 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1306 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1307 event_type != G_FILE_MONITOR_EVENT_DELETED)
1310 mount_monitor = user_data;
1311 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1315 g_unix_mount_monitor_init (GUnixMountMonitor *monitor)
1319 if (get_fstab_file () != NULL)
1321 file = g_file_new_for_path (get_fstab_file ());
1322 monitor->fstab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1323 g_object_unref (file);
1325 g_signal_connect (monitor->fstab_monitor, "changed", (GCallback)fstab_file_changed, monitor);
1328 if (get_mtab_monitor_file () != NULL)
1330 file = g_file_new_for_path (get_mtab_monitor_file ());
1331 monitor->mtab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1332 g_object_unref (file);
1334 g_signal_connect (monitor->mtab_monitor, "changed", (GCallback)mtab_file_changed, monitor);
1339 * g_unix_mount_monitor_set_rate_limit:
1340 * @mount_monitor: a #GUnixMountMonitor
1341 * @limit_msec: a integer with the limit in milliseconds to
1344 * Sets the rate limit to which the @mount_monitor will report
1345 * consecutive change events to the mount and mount point entry files.
1350 g_unix_mount_monitor_set_rate_limit (GUnixMountMonitor *mount_monitor,
1353 g_return_if_fail (G_IS_UNIX_MOUNT_MONITOR (mount_monitor));
1355 if (mount_monitor->fstab_monitor != NULL)
1356 g_file_monitor_set_rate_limit (mount_monitor->fstab_monitor, limit_msec);
1358 if (mount_monitor->mtab_monitor != NULL)
1359 g_file_monitor_set_rate_limit (mount_monitor->mtab_monitor, limit_msec);
1363 * g_unix_mount_monitor_new:
1365 * Gets a new #GUnixMountMonitor. The default rate limit for which the
1366 * monitor will report consecutive changes for the mount and mount
1367 * point entry files is the default for a #GFileMonitor. Use
1368 * g_unix_mount_monitor_set_rate_limit() to change this.
1370 * Returns: a #GUnixMountMonitor.
1373 g_unix_mount_monitor_new (void)
1375 if (the_mount_monitor == NULL)
1377 the_mount_monitor = g_object_new (G_TYPE_UNIX_MOUNT_MONITOR, NULL);
1378 return the_mount_monitor;
1381 return g_object_ref (the_mount_monitor);
1385 * g_unix_mount_free:
1386 * @mount_entry: a #GUnixMount.
1388 * Frees a unix mount.
1391 g_unix_mount_free (GUnixMountEntry *mount_entry)
1393 g_return_if_fail (mount_entry != NULL);
1395 g_free (mount_entry->mount_path);
1396 g_free (mount_entry->device_path);
1397 g_free (mount_entry->filesystem_type);
1398 g_free (mount_entry);
1402 * g_unix_mount_point_free:
1403 * @mount_point: unix mount point to free.
1405 * Frees a unix mount point.
1408 g_unix_mount_point_free (GUnixMountPoint *mount_point)
1410 g_return_if_fail (mount_point != NULL);
1412 g_free (mount_point->mount_path);
1413 g_free (mount_point->device_path);
1414 g_free (mount_point->filesystem_type);
1415 g_free (mount_point);
1419 * g_unix_mount_compare:
1420 * @mount1: first #GUnixMountEntry to compare.
1421 * @mount2: second #GUnixMountEntry to compare.
1423 * Compares two unix mounts.
1425 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1426 * or less than @mount2, respectively.
1429 g_unix_mount_compare (GUnixMountEntry *mount1,
1430 GUnixMountEntry *mount2)
1434 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1436 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1440 res = g_strcmp0 (mount1->device_path, mount2->device_path);
1444 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1448 res = mount1->is_read_only - mount2->is_read_only;
1456 * g_unix_mount_get_mount_path:
1457 * @mount_entry: input #GUnixMountEntry to get the mount path for.
1459 * Gets the mount path for a unix mount.
1461 * Returns: the mount path for @mount_entry.
1464 g_unix_mount_get_mount_path (GUnixMountEntry *mount_entry)
1466 g_return_val_if_fail (mount_entry != NULL, NULL);
1468 return mount_entry->mount_path;
1472 * g_unix_mount_get_device_path:
1473 * @mount_entry: a #GUnixMount.
1475 * Gets the device path for a unix mount.
1477 * Returns: a string containing the device path.
1480 g_unix_mount_get_device_path (GUnixMountEntry *mount_entry)
1482 g_return_val_if_fail (mount_entry != NULL, NULL);
1484 return mount_entry->device_path;
1488 * g_unix_mount_get_fs_type:
1489 * @mount_entry: a #GUnixMount.
1491 * Gets the filesystem type for the unix mount.
1493 * Returns: a string containing the file system type.
1496 g_unix_mount_get_fs_type (GUnixMountEntry *mount_entry)
1498 g_return_val_if_fail (mount_entry != NULL, NULL);
1500 return mount_entry->filesystem_type;
1504 * g_unix_mount_is_readonly:
1505 * @mount_entry: a #GUnixMount.
1507 * Checks if a unix mount is mounted read only.
1509 * Returns: %TRUE if @mount_entry is read only.
1512 g_unix_mount_is_readonly (GUnixMountEntry *mount_entry)
1514 g_return_val_if_fail (mount_entry != NULL, FALSE);
1516 return mount_entry->is_read_only;
1520 * g_unix_mount_is_system_internal:
1521 * @mount_entry: a #GUnixMount.
1523 * Checks if a unix mount is a system path.
1525 * Returns: %TRUE if the unix mount is for a system path.
1528 g_unix_mount_is_system_internal (GUnixMountEntry *mount_entry)
1530 g_return_val_if_fail (mount_entry != NULL, FALSE);
1532 return mount_entry->is_system_internal;
1536 * g_unix_mount_point_compare:
1537 * @mount1: a #GUnixMount.
1538 * @mount2: a #GUnixMount.
1540 * Compares two unix mount points.
1542 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1543 * or less than @mount2, respectively.
1546 g_unix_mount_point_compare (GUnixMountPoint *mount1,
1547 GUnixMountPoint *mount2)
1551 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1553 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1557 res = g_strcmp0 (mount1->device_path, mount2->device_path);
1561 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1565 res = mount1->is_read_only - mount2->is_read_only;
1569 res = mount1->is_user_mountable - mount2->is_user_mountable;
1573 res = mount1->is_loopback - mount2->is_loopback;
1581 * g_unix_mount_point_get_mount_path:
1582 * @mount_point: a #GUnixMountPoint.
1584 * Gets the mount path for a unix mount point.
1586 * Returns: a string containing the mount path.
1589 g_unix_mount_point_get_mount_path (GUnixMountPoint *mount_point)
1591 g_return_val_if_fail (mount_point != NULL, NULL);
1593 return mount_point->mount_path;
1597 * g_unix_mount_point_get_device_path:
1598 * @mount_point: a #GUnixMountPoint.
1600 * Gets the device path for a unix mount point.
1602 * Returns: a string containing the device path.
1605 g_unix_mount_point_get_device_path (GUnixMountPoint *mount_point)
1607 g_return_val_if_fail (mount_point != NULL, NULL);
1609 return mount_point->device_path;
1613 * g_unix_mount_point_get_fs_type:
1614 * @mount_point: a #GUnixMountPoint.
1616 * Gets the file system type for the mount point.
1618 * Returns: a string containing the file system type.
1621 g_unix_mount_point_get_fs_type (GUnixMountPoint *mount_point)
1623 g_return_val_if_fail (mount_point != NULL, NULL);
1625 return mount_point->filesystem_type;
1629 * g_unix_mount_point_is_readonly:
1630 * @mount_point: a #GUnixMountPoint.
1632 * Checks if a unix mount point is read only.
1634 * Returns: %TRUE if a mount point is read only.
1637 g_unix_mount_point_is_readonly (GUnixMountPoint *mount_point)
1639 g_return_val_if_fail (mount_point != NULL, FALSE);
1641 return mount_point->is_read_only;
1645 * g_unix_mount_point_is_user_mountable:
1646 * @mount_point: a #GUnixMountPoint.
1648 * Checks if a unix mount point is mountable by the user.
1650 * Returns: %TRUE if the mount point is user mountable.
1653 g_unix_mount_point_is_user_mountable (GUnixMountPoint *mount_point)
1655 g_return_val_if_fail (mount_point != NULL, FALSE);
1657 return mount_point->is_user_mountable;
1661 * g_unix_mount_point_is_loopback:
1662 * @mount_point: a #GUnixMountPoint.
1664 * Checks if a unix mount point is a loopback device.
1666 * Returns: %TRUE if the mount point is a loopback. %FALSE otherwise.
1669 g_unix_mount_point_is_loopback (GUnixMountPoint *mount_point)
1671 g_return_val_if_fail (mount_point != NULL, FALSE);
1673 return mount_point->is_loopback;
1676 static GUnixMountType
1677 guess_mount_type (const char *mount_path,
1678 const char *device_path,
1679 const char *filesystem_type)
1681 GUnixMountType type;
1684 type = G_UNIX_MOUNT_TYPE_UNKNOWN;
1686 if ((strcmp (filesystem_type, "udf") == 0) ||
1687 (strcmp (filesystem_type, "iso9660") == 0) ||
1688 (strcmp (filesystem_type, "cd9660") == 0))
1689 type = G_UNIX_MOUNT_TYPE_CDROM;
1690 else if ((strcmp (filesystem_type, "nfs") == 0) ||
1691 (strcmp (filesystem_type, "nfs4") == 0))
1692 type = G_UNIX_MOUNT_TYPE_NFS;
1693 else if (g_str_has_prefix (device_path, "/vol/dev/diskette/") ||
1694 g_str_has_prefix (device_path, "/dev/fd") ||
1695 g_str_has_prefix (device_path, "/dev/floppy"))
1696 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1697 else if (g_str_has_prefix (device_path, "/dev/cdrom") ||
1698 g_str_has_prefix (device_path, "/dev/acd") ||
1699 g_str_has_prefix (device_path, "/dev/cd"))
1700 type = G_UNIX_MOUNT_TYPE_CDROM;
1701 else if (g_str_has_prefix (device_path, "/vol/"))
1703 const char *name = mount_path + strlen ("/");
1705 if (g_str_has_prefix (name, "cdrom"))
1706 type = G_UNIX_MOUNT_TYPE_CDROM;
1707 else if (g_str_has_prefix (name, "floppy") ||
1708 g_str_has_prefix (device_path, "/vol/dev/diskette/"))
1709 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1710 else if (g_str_has_prefix (name, "rmdisk"))
1711 type = G_UNIX_MOUNT_TYPE_ZIP;
1712 else if (g_str_has_prefix (name, "jaz"))
1713 type = G_UNIX_MOUNT_TYPE_JAZ;
1714 else if (g_str_has_prefix (name, "memstick"))
1715 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1719 basename = g_path_get_basename (mount_path);
1721 if (g_str_has_prefix (basename, "cdr") ||
1722 g_str_has_prefix (basename, "cdwriter") ||
1723 g_str_has_prefix (basename, "burn") ||
1724 g_str_has_prefix (basename, "dvdr"))
1725 type = G_UNIX_MOUNT_TYPE_CDROM;
1726 else if (g_str_has_prefix (basename, "floppy"))
1727 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1728 else if (g_str_has_prefix (basename, "zip"))
1729 type = G_UNIX_MOUNT_TYPE_ZIP;
1730 else if (g_str_has_prefix (basename, "jaz"))
1731 type = G_UNIX_MOUNT_TYPE_JAZ;
1732 else if (g_str_has_prefix (basename, "camera"))
1733 type = G_UNIX_MOUNT_TYPE_CAMERA;
1734 else if (g_str_has_prefix (basename, "memstick") ||
1735 g_str_has_prefix (basename, "memory_stick") ||
1736 g_str_has_prefix (basename, "ram"))
1737 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1738 else if (g_str_has_prefix (basename, "compact_flash"))
1739 type = G_UNIX_MOUNT_TYPE_CF;
1740 else if (g_str_has_prefix (basename, "smart_media"))
1741 type = G_UNIX_MOUNT_TYPE_SM;
1742 else if (g_str_has_prefix (basename, "sd_mmc"))
1743 type = G_UNIX_MOUNT_TYPE_SDMMC;
1744 else if (g_str_has_prefix (basename, "ipod"))
1745 type = G_UNIX_MOUNT_TYPE_IPOD;
1750 if (type == G_UNIX_MOUNT_TYPE_UNKNOWN)
1751 type = G_UNIX_MOUNT_TYPE_HD;
1757 * g_unix_mount_guess_type:
1758 * @mount_entry: a #GUnixMount.
1760 * Guesses the type of a unix mount. If the mount type cannot be
1761 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1763 * Returns: a #GUnixMountType.
1765 static GUnixMountType
1766 g_unix_mount_guess_type (GUnixMountEntry *mount_entry)
1768 g_return_val_if_fail (mount_entry != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1769 g_return_val_if_fail (mount_entry->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1770 g_return_val_if_fail (mount_entry->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1771 g_return_val_if_fail (mount_entry->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1773 return guess_mount_type (mount_entry->mount_path,
1774 mount_entry->device_path,
1775 mount_entry->filesystem_type);
1779 * g_unix_mount_point_guess_type:
1780 * @mount_point: a #GUnixMountPoint.
1782 * Guesses the type of a unix mount point.
1783 * If the mount type cannot be determined,
1784 * returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1786 * Returns: a #GUnixMountType.
1788 static GUnixMountType
1789 g_unix_mount_point_guess_type (GUnixMountPoint *mount_point)
1791 g_return_val_if_fail (mount_point != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1792 g_return_val_if_fail (mount_point->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1793 g_return_val_if_fail (mount_point->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1794 g_return_val_if_fail (mount_point->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1796 return guess_mount_type (mount_point->mount_path,
1797 mount_point->device_path,
1798 mount_point->filesystem_type);
1802 type_to_icon (GUnixMountType type, gboolean is_mount_point)
1804 const char *icon_name;
1808 case G_UNIX_MOUNT_TYPE_HD:
1810 icon_name = "drive-removable-media";
1812 icon_name = "drive-harddisk";
1814 case G_UNIX_MOUNT_TYPE_FLOPPY:
1815 case G_UNIX_MOUNT_TYPE_ZIP:
1816 case G_UNIX_MOUNT_TYPE_JAZ:
1818 icon_name = "drive-removable-media";
1820 icon_name = "media-floppy";
1822 case G_UNIX_MOUNT_TYPE_CDROM:
1824 icon_name = "drive-optical";
1826 icon_name = "media-optical";
1828 case G_UNIX_MOUNT_TYPE_NFS:
1829 /* TODO: Would like a better icon here... */
1831 icon_name = "drive-removable-media";
1833 icon_name = "drive-harddisk";
1835 case G_UNIX_MOUNT_TYPE_MEMSTICK:
1837 icon_name = "drive-removable-media";
1839 icon_name = "media-flash";
1841 case G_UNIX_MOUNT_TYPE_CAMERA:
1843 icon_name = "drive-removable-media";
1845 icon_name = "camera-photo";
1847 case G_UNIX_MOUNT_TYPE_IPOD:
1849 icon_name = "drive-removable-media";
1851 icon_name = "multimedia-player";
1853 case G_UNIX_MOUNT_TYPE_UNKNOWN:
1856 icon_name = "drive-removable-media";
1858 icon_name = "drive-harddisk";
1866 * g_unix_mount_guess_name:
1867 * @mount_entry: a #GUnixMountEntry
1869 * Guesses the name of a Unix mount.
1870 * The result is a translated string.
1872 * Returns: A newly allocated string that must
1873 * be freed with g_free()
1876 g_unix_mount_guess_name (GUnixMountEntry *mount_entry)
1880 if (strcmp (mount_entry->mount_path, "/") == 0)
1881 name = g_strdup (_("Filesystem root"));
1883 name = g_filename_display_basename (mount_entry->mount_path);
1889 * g_unix_mount_guess_icon:
1890 * @mount_entry: a #GUnixMountEntry
1892 * Guesses the icon of a Unix mount.
1894 * Returns: (transfer full): a #GIcon
1897 g_unix_mount_guess_icon (GUnixMountEntry *mount_entry)
1899 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE));
1903 * g_unix_mount_point_guess_name:
1904 * @mount_point: a #GUnixMountPoint
1906 * Guesses the name of a Unix mount point.
1907 * The result is a translated string.
1909 * Returns: A newly allocated string that must
1910 * be freed with g_free()
1913 g_unix_mount_point_guess_name (GUnixMountPoint *mount_point)
1917 if (strcmp (mount_point->mount_path, "/") == 0)
1918 name = g_strdup (_("Filesystem root"));
1920 name = g_filename_display_basename (mount_point->mount_path);
1926 * g_unix_mount_point_guess_icon:
1927 * @mount_point: a #GUnixMountPoint
1929 * Guesses the icon of a Unix mount point.
1931 * Returns: (transfer full): a #GIcon
1934 g_unix_mount_point_guess_icon (GUnixMountPoint *mount_point)
1936 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE));
1940 * g_unix_mount_guess_can_eject:
1941 * @mount_entry: a #GUnixMountEntry
1943 * Guesses whether a Unix mount can be ejected.
1945 * Returns: %TRUE if @mount_entry is deemed to be ejectable.
1948 g_unix_mount_guess_can_eject (GUnixMountEntry *mount_entry)
1950 GUnixMountType guessed_type;
1952 guessed_type = g_unix_mount_guess_type (mount_entry);
1953 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
1954 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
1961 * g_unix_mount_guess_should_display:
1962 * @mount_entry: a #GUnixMountEntry
1964 * Guesses whether a Unix mount should be displayed in the UI.
1966 * Returns: %TRUE if @mount_entry is deemed to be displayable.
1969 g_unix_mount_guess_should_display (GUnixMountEntry *mount_entry)
1971 const char *mount_path;
1973 /* Never display internal mountpoints */
1974 if (g_unix_mount_is_system_internal (mount_entry))
1977 /* Only display things in /media (which are generally user mountable)
1978 and home dir (fuse stuff) */
1979 mount_path = mount_entry->mount_path;
1980 if (mount_path != NULL)
1982 /* Hide mounts within a dot path, suppose it was a purpose to hide this mount */
1983 if (g_strstr_len (mount_path, -1, "/.") != NULL)
1986 if (g_str_has_prefix (mount_path, "/media/"))
1989 /* Avoid displaying mounts that are not accessible to the user.
1991 * See http://bugzilla.gnome.org/show_bug.cgi?id=526320 for why we
1992 * want to avoid g_access() for mount points which can potentially
1993 * block or fail stat()'ing, such as network mounts.
1995 path = g_path_get_dirname (mount_path);
1996 if (g_str_has_prefix (path, "/media/"))
1998 if (g_access (path, R_OK|X_OK) != 0)
2006 if (mount_entry->device_path && mount_entry->device_path[0] == '/')
2009 if (g_stat (mount_entry->device_path, &st) == 0 &&
2010 S_ISBLK(st.st_mode) &&
2011 g_access (mount_path, R_OK|X_OK) != 0)
2017 if (g_str_has_prefix (mount_path, g_get_home_dir ()) &&
2018 mount_path[strlen (g_get_home_dir())] == G_DIR_SEPARATOR)
2026 * g_unix_mount_point_guess_can_eject:
2027 * @mount_point: a #GUnixMountPoint
2029 * Guesses whether a Unix mount point can be ejected.
2031 * Returns: %TRUE if @mount_point is deemed to be ejectable.
2034 g_unix_mount_point_guess_can_eject (GUnixMountPoint *mount_point)
2036 GUnixMountType guessed_type;
2038 guessed_type = g_unix_mount_point_guess_type (mount_point);
2039 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
2040 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
2047 /* borrowed from gtk/gtkfilesystemunix.c in GTK+ on 02/23/2006 */
2049 _canonicalize_filename (gchar *filename)
2052 gboolean last_was_slash = FALSE;
2059 if (*p == G_DIR_SEPARATOR)
2061 if (!last_was_slash)
2062 *q++ = G_DIR_SEPARATOR;
2064 last_was_slash = TRUE;
2068 if (last_was_slash && *p == '.')
2070 if (*(p + 1) == G_DIR_SEPARATOR ||
2073 if (*(p + 1) == '\0')
2078 else if (*(p + 1) == '.' &&
2079 (*(p + 2) == G_DIR_SEPARATOR ||
2082 if (q > filename + 1)
2085 while (q > filename + 1 &&
2086 *(q - 1) != G_DIR_SEPARATOR)
2090 if (*(p + 2) == '\0')
2098 last_was_slash = FALSE;
2104 last_was_slash = FALSE;
2111 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
2118 _resolve_symlink (const char *file)
2126 f = g_strdup (file);
2128 while (g_file_test (f, G_FILE_TEST_IS_SYMLINK))
2130 link = g_file_read_link (f, &error);
2133 g_error_free (error);
2139 dir = g_path_get_dirname (f);
2140 f1 = g_strdup_printf ("%s/%s", dir, link);
2149 _canonicalize_filename (f);
2153 #ifdef HAVE_MNTENT_H
2155 _resolve_dev_root (void)
2157 static gboolean have_real_dev_root = FALSE;
2158 static char real_dev_root[256];
2159 struct stat statbuf;
2161 /* see if it's cached already */
2162 if (have_real_dev_root)
2165 /* otherwise we're going to find it right away.. */
2166 have_real_dev_root = TRUE;
2168 if (stat ("/dev/root", &statbuf) == 0)
2170 if (! S_ISLNK (statbuf.st_mode))
2172 dev_t root_dev = statbuf.st_dev;
2176 /* see if device with similar major:minor as /dev/root is mention
2177 * in /etc/mtab (it usually is)
2179 f = fopen ("/etc/mtab", "r");
2182 struct mntent *entp;
2183 #ifdef HAVE_GETMNTENT_R
2185 while ((entp = getmntent_r (f, &ent, buf, sizeof (buf))) != NULL)
2189 while ((entp = getmntent (f)) != NULL)
2192 if (stat (entp->mnt_fsname, &statbuf) == 0 &&
2193 statbuf.st_dev == root_dev)
2195 strncpy (real_dev_root, entp->mnt_fsname, sizeof (real_dev_root) - 1);
2196 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2204 #ifndef HAVE_GETMNTENT_R
2205 G_UNLOCK (getmntent);
2209 /* no, that didn't work.. next we could scan /dev ... but I digress.. */
2215 resolved = _resolve_symlink ("/dev/root");
2216 if (resolved != NULL)
2218 strncpy (real_dev_root, resolved, sizeof (real_dev_root) - 1);
2219 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2227 strcpy (real_dev_root, "/dev/root");
2230 return real_dev_root;