1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
27 #include <sys/types.h>
30 #ifndef HAVE_SYSCTLBYNAME
31 #ifdef HAVE_SYS_PARAM_H
32 #include <sys/param.h>
34 #ifdef HAVE_SYS_POLL_H
48 #include "gunixmounts.h"
50 #include "gfilemonitor.h"
52 #include "gthemedicon.h"
56 static const char *_resolve_dev_root (void);
60 * @short_description: Unix Mounts
62 * Routines for managing mounted UNIX mount points and paths.
68 * @G_UNIX_MOUNT_TYPE_UNKNOWN: Unknown UNIX mount type.
69 * @G_UNIX_MOUNT_TYPE_FLOPPY: Floppy disk UNIX mount type.
70 * @G_UNIX_MOUNT_TYPE_CDROM: CDROM UNIX mount type.
71 * @G_UNIX_MOUNT_TYPE_NFS: Network File System (NFS) UNIX mount type.
72 * @G_UNIX_MOUNT_TYPE_ZIP: ZIP UNIX mount type.
73 * @G_UNIX_MOUNT_TYPE_JAZ: JAZZ UNIX mount type.
74 * @G_UNIX_MOUNT_TYPE_MEMSTICK: Memory Stick UNIX mount type.
75 * @G_UNIX_MOUNT_TYPE_CF: Compact Flash UNIX mount type.
76 * @G_UNIX_MOUNT_TYPE_SM: Smart Media UNIX mount type.
77 * @G_UNIX_MOUNT_TYPE_SDMMC: SD/MMC UNIX mount type.
78 * @G_UNIX_MOUNT_TYPE_IPOD: iPod UNIX mount type.
79 * @G_UNIX_MOUNT_TYPE_CAMERA: Digital camera UNIX mount type.
80 * @G_UNIX_MOUNT_TYPE_HD: Hard drive UNIX mount type.
82 * Types of UNIX mounts.
85 G_UNIX_MOUNT_TYPE_UNKNOWN,
86 G_UNIX_MOUNT_TYPE_FLOPPY,
87 G_UNIX_MOUNT_TYPE_CDROM,
88 G_UNIX_MOUNT_TYPE_NFS,
89 G_UNIX_MOUNT_TYPE_ZIP,
90 G_UNIX_MOUNT_TYPE_JAZ,
91 G_UNIX_MOUNT_TYPE_MEMSTICK,
94 G_UNIX_MOUNT_TYPE_SDMMC,
95 G_UNIX_MOUNT_TYPE_IPOD,
96 G_UNIX_MOUNT_TYPE_CAMERA,
100 struct _GUnixMountEntry {
103 char *filesystem_type;
104 gboolean is_read_only;
105 gboolean is_system_internal;
108 struct _GUnixMountPoint {
111 char *filesystem_type;
112 gboolean is_read_only;
113 gboolean is_user_mountable;
114 gboolean is_loopback;
123 static guint signals[LAST_SIGNAL];
125 struct _GUnixMountMonitor {
128 GFileMonitor *fstab_monitor;
129 GFileMonitor *mtab_monitor;
132 struct _GUnixMountMonitorClass {
133 GObjectClass parent_class;
136 static GUnixMountMonitor *the_mount_monitor = NULL;
138 static GList *_g_get_unix_mounts (void);
139 static GList *_g_get_unix_mount_points (void);
141 G_DEFINE_TYPE (GUnixMountMonitor, g_unix_mount_monitor, G_TYPE_OBJECT);
143 #define MOUNT_POLL_INTERVAL 4000
145 #ifdef HAVE_SYS_MNTTAB_H
146 #define MNTOPT_RO "ro"
151 #elif defined (HAVE_SYS_MNTTAB_H)
152 #include <sys/mnttab.h>
155 #ifdef HAVE_SYS_VFSTAB_H
156 #include <sys/vfstab.h>
159 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
160 #include <sys/mntctl.h>
162 #include <sys/vmount.h>
166 #if defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
167 #include <sys/ucred.h>
168 #include <sys/mount.h>
170 #ifdef HAVE_SYS_SYSCTL_H
171 #include <sys/sysctl.h>
175 #ifndef HAVE_SETMNTENT
176 #define setmntent(f,m) fopen(f,m)
178 #ifndef HAVE_ENDMNTENT
179 #define endmntent(f) fclose(f)
183 is_in (const char *value, const char *set[])
186 for (i = 0; set[i] != NULL; i++)
188 if (strcmp (set[i], value) == 0)
195 * g_unix_is_mount_path_system_internal:
196 * @mount_path: a mount path, e.g. <filename>/media/disk</filename>
197 * or <filename>/usr</filename>
199 * Determines if @mount_path is considered an implementation of the
200 * OS. This is primarily used for hiding mountable and mounted volumes
201 * that only are used in the OS and has little to no relevance to the
204 * Returns: %TRUE if @mount_path is considered an implementation detail
208 g_unix_is_mount_path_system_internal (const char *mount_path)
210 const char *ignore_mountpoints[] = {
211 /* Includes all FHS 2.3 toplevel dirs and other specilized
212 * directories that we want to hide from the user.
214 "/", /* we already have "Filesystem root" in Nautilus */
231 "/var/log/audit", /* https://bugzilla.redhat.com/show_bug.cgi?id=333041 */
232 "/var/tmp", /* https://bugzilla.redhat.com/show_bug.cgi?id=335241 */
239 if (is_in (mount_path, ignore_mountpoints))
242 if (g_str_has_prefix (mount_path, "/dev") ||
243 g_str_has_prefix (mount_path, "/proc") ||
244 g_str_has_prefix (mount_path, "/sys"))
247 if (strstr (mount_path, "/.gvfs") != NULL)
254 guess_system_internal (const char *mountpoint,
258 const char *ignore_fs[] = {
276 const char *ignore_devices[] = {
286 if (is_in (fs, ignore_fs))
289 if (is_in (device, ignore_devices))
292 if (g_unix_is_mount_path_system_internal (mountpoint))
301 get_mtab_read_file (void)
305 return "/proc/mounts";
307 return _PATH_MOUNTED;
315 get_mtab_monitor_file (void)
318 return _PATH_MOUNTED;
324 G_LOCK_DEFINE_STATIC(getmntent);
327 _g_get_unix_mounts ()
329 struct mntent *mntent;
332 GUnixMountEntry *mount_entry;
333 GHashTable *mounts_hash;
336 read_file = get_mtab_read_file ();
338 file = setmntent (read_file, "r");
344 mounts_hash = g_hash_table_new (g_str_hash, g_str_equal);
347 while ((mntent = getmntent (file)) != NULL)
349 /* ignore any mnt_fsname that is repeated and begins with a '/'
351 * We do this to avoid being fooled by --bind mounts, since
352 * these have the same device as the location they bind to.
353 * Its not an ideal solution to the problem, but its likely that
354 * the most important mountpoint is first and the --bind ones after
355 * that aren't as important. So it should work.
357 * The '/' is to handle procfs, tmpfs and other no device mounts.
359 if (mntent->mnt_fsname != NULL &&
360 mntent->mnt_fsname[0] == '/' &&
361 g_hash_table_lookup (mounts_hash, mntent->mnt_fsname))
364 mount_entry = g_new0 (GUnixMountEntry, 1);
365 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
366 if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
367 mount_entry->device_path = g_strdup (_resolve_dev_root ());
369 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
370 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
372 #if defined (HAVE_HASMNTOPT)
373 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
374 mount_entry->is_read_only = TRUE;
377 mount_entry->is_system_internal =
378 guess_system_internal (mount_entry->mount_path,
379 mount_entry->filesystem_type,
380 mount_entry->device_path);
382 g_hash_table_insert (mounts_hash,
383 mount_entry->device_path,
384 mount_entry->device_path);
386 return_list = g_list_prepend (return_list, mount_entry);
388 g_hash_table_destroy (mounts_hash);
392 G_UNLOCK (getmntent);
394 return g_list_reverse (return_list);
397 #elif defined (HAVE_SYS_MNTTAB_H)
399 G_LOCK_DEFINE_STATIC(getmntent);
402 get_mtab_read_file (void)
405 return _PATH_MOUNTED;
407 return "/etc/mnttab";
412 get_mtab_monitor_file (void)
414 return get_mtab_read_file ();
418 _g_get_unix_mounts (void)
420 struct mnttab mntent;
423 GUnixMountEntry *mount_entry;
426 read_file = get_mtab_read_file ();
428 file = setmntent (read_file, "r");
435 while (! getmntent (file, &mntent))
437 mount_entry = g_new0 (GUnixMountEntry, 1);
439 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
440 mount_entry->device_path = g_strdup (mntent.mnt_special);
441 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
443 #if defined (HAVE_HASMNTOPT)
444 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
445 mount_entry->is_read_only = TRUE;
448 mount_entry->is_system_internal =
449 guess_system_internal (mount_entry->mount_path,
450 mount_entry->filesystem_type,
451 mount_entry->device_path);
453 return_list = g_list_prepend (return_list, mount_entry);
458 G_UNLOCK (getmntent);
460 return g_list_reverse (return_list);
463 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
466 get_mtab_monitor_file (void)
472 _g_get_unix_mounts (void)
474 struct vfs_ent *fs_info;
475 struct vmount *vmount_info;
477 unsigned int vmount_size;
481 if (mntctl (MCTL_QUERY, sizeof (vmount_size), &vmount_size) != 0)
483 g_warning ("Unable to know the number of mounted volumes\n");
488 vmount_info = (struct vmount*)g_malloc (vmount_size);
490 vmount_number = mntctl (MCTL_QUERY, vmount_size, vmount_info);
492 if (vmount_info->vmt_revision != VMT_REVISION)
493 g_warning ("Bad vmount structure revision number, want %d, got %d\n", VMT_REVISION, vmount_info->vmt_revision);
495 if (vmount_number < 0)
497 g_warning ("Unable to recover mounted volumes information\n");
499 g_free (vmount_info);
504 while (vmount_number > 0)
506 mount_entry = g_new0 (GUnixMountEntry, 1);
508 mount_entry->device_path = g_strdup (vmt2dataptr (vmount_info, VMT_OBJECT));
509 mount_entry->mount_path = g_strdup (vmt2dataptr (vmount_info, VMT_STUB));
510 /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
511 mount_entry->is_read_only = (vmount_info->vmt_flags & MNT_READONLY) ? 1 : 0;
513 fs_info = getvfsbytype (vmount_info->vmt_gfstype);
516 mount_entry->filesystem_type = g_strdup ("unknown");
518 mount_entry->filesystem_type = g_strdup (fs_info->vfsent_name);
520 mount_entry->is_system_internal =
521 guess_system_internal (mount_entry->mount_path,
522 mount_entry->filesystem_type,
523 mount_entry->device_path);
525 return_list = g_list_prepend (return_list, mount_entry);
527 vmount_info = (struct vmount *)( (char*)vmount_info
528 + vmount_info->vmt_length);
533 g_free (vmount_info);
535 return g_list_reverse (return_list);
538 #elif defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
541 get_mtab_monitor_file (void)
547 _g_get_unix_mounts (void)
549 struct statfs *mntent = NULL;
551 GUnixMountEntry *mount_entry;
554 /* Pass MNT_NOWAIT to avoid blocking trying to update NFS mounts. */
555 if ((num_mounts = getmntinfo (&mntent, MNT_NOWAIT)) == 0)
560 for (i = 0; i < num_mounts; i++)
562 mount_entry = g_new0 (GUnixMountEntry, 1);
564 mount_entry->mount_path = g_strdup (mntent[i].f_mntonname);
565 mount_entry->device_path = g_strdup (mntent[i].f_mntfromname);
566 mount_entry->filesystem_type = g_strdup (mntent[i].f_fstypename);
567 if (mntent[i].f_flags & MNT_RDONLY)
568 mount_entry->is_read_only = TRUE;
570 mount_entry->is_system_internal =
571 guess_system_internal (mount_entry->mount_path,
572 mount_entry->filesystem_type,
573 mount_entry->device_path);
575 return_list = g_list_prepend (return_list, mount_entry);
578 return g_list_reverse (return_list);
581 #error No _g_get_unix_mounts() implementation for system
584 /* _g_get_unix_mount_points():
586 * don't return swap and ignore mounts.
590 get_fstab_file (void)
592 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
594 return "/etc/filesystems";
595 #elif defined(_PATH_MNTTAB)
597 #elif defined(VFSTAB)
606 _g_get_unix_mount_points (void)
608 struct mntent *mntent;
611 GUnixMountPoint *mount_entry;
614 read_file = get_fstab_file ();
616 file = setmntent (read_file, "r");
623 while ((mntent = getmntent (file)) != NULL)
625 if ((strcmp (mntent->mnt_dir, "ignore") == 0) ||
626 (strcmp (mntent->mnt_dir, "swap") == 0))
629 mount_entry = g_new0 (GUnixMountPoint, 1);
630 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
631 if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
632 mount_entry->device_path = g_strdup (_resolve_dev_root ());
634 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
635 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
637 #ifdef HAVE_HASMNTOPT
638 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
639 mount_entry->is_read_only = TRUE;
641 if (hasmntopt (mntent, "loop") != NULL)
642 mount_entry->is_loopback = TRUE;
646 if ((mntent->mnt_type != NULL && strcmp ("supermount", mntent->mnt_type) == 0)
647 #ifdef HAVE_HASMNTOPT
648 || (hasmntopt (mntent, "user") != NULL
649 && hasmntopt (mntent, "user") != hasmntopt (mntent, "user_xattr"))
650 || hasmntopt (mntent, "pamconsole") != NULL
651 || hasmntopt (mntent, "users") != NULL
652 || hasmntopt (mntent, "owner") != NULL
655 mount_entry->is_user_mountable = TRUE;
657 return_list = g_list_prepend (return_list, mount_entry);
661 G_UNLOCK (getmntent);
663 return g_list_reverse (return_list);
666 #elif defined (HAVE_SYS_MNTTAB_H)
669 _g_get_unix_mount_points (void)
671 struct mnttab mntent;
674 GUnixMountPoint *mount_entry;
677 read_file = get_fstab_file ();
679 file = setmntent (read_file, "r");
686 while (! getmntent (file, &mntent))
688 if ((strcmp (mntent.mnt_mountp, "ignore") == 0) ||
689 (strcmp (mntent.mnt_mountp, "swap") == 0))
692 mount_entry = g_new0 (GUnixMountPoint, 1);
694 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
695 mount_entry->device_path = g_strdup (mntent.mnt_special);
696 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
698 #ifdef HAVE_HASMNTOPT
699 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
700 mount_entry->is_read_only = TRUE;
702 if (hasmntopt (&mntent, "lofs") != NULL)
703 mount_entry->is_loopback = TRUE;
706 if ((mntent.mnt_fstype != NULL)
707 #ifdef HAVE_HASMNTOPT
708 || (hasmntopt (&mntent, "user") != NULL
709 && hasmntopt (&mntent, "user") != hasmntopt (&mntent, "user_xattr"))
710 || hasmntopt (&mntent, "pamconsole") != NULL
711 || hasmntopt (&mntent, "users") != NULL
712 || hasmntopt (&mntent, "owner") != NULL
715 mount_entry->is_user_mountable = TRUE;
718 return_list = g_list_prepend (return_list, mount_entry);
722 G_UNLOCK (getmntent);
724 return g_list_reverse (return_list);
726 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
728 /* functions to parse /etc/filesystems on aix */
730 /* read character, ignoring comments (begin with '*', end with '\n' */
732 aix_fs_getc (FILE *fd)
736 while ((c = getc (fd)) == '*')
738 while (((c = getc (fd)) != '\n') && (c != EOF))
743 /* eat all continuous spaces in a file */
745 aix_fs_ignorespace (FILE *fd)
749 while ((c = aix_fs_getc (fd)) != EOF)
751 if (!g_ascii_isspace (c))
761 /* read one word from file */
763 aix_fs_getword (FILE *fd,
768 aix_fs_ignorespace (fd);
770 while (((c = aix_fs_getc (fd)) != EOF) && !g_ascii_isspace (c))
774 while (((c = aix_fs_getc (fd)) != EOF) && (c != '"'))
786 char mnt_mount[PATH_MAX];
787 char mnt_special[PATH_MAX];
789 char mnt_options[128];
790 } AixMountTableEntry;
792 /* read mount points properties */
794 aix_fs_get (FILE *fd,
795 AixMountTableEntry *prop)
797 static char word[PATH_MAX] = { 0 };
798 char value[PATH_MAX];
803 if (aix_fs_getword (fd, word) == EOF)
807 word[strlen(word) - 1] = 0;
808 strcpy (prop->mnt_mount, word);
810 /* read attributes and value */
812 while (aix_fs_getword (fd, word) != EOF)
814 /* test if is attribute or new stanza */
815 if (word[strlen(word) - 1] == ':')
819 aix_fs_getword (fd, value);
822 aix_fs_getword (fd, value);
824 if (strcmp (word, "dev") == 0)
825 strcpy (prop->mnt_special, value);
826 else if (strcmp (word, "vfs") == 0)
827 strcpy (prop->mnt_fstype, value);
828 else if (strcmp (word, "options") == 0)
829 strcpy(prop->mnt_options, value);
836 _g_get_unix_mount_points (void)
838 struct mntent *mntent;
841 GUnixMountPoint *mount_entry;
842 AixMountTableEntry mntent;
845 read_file = get_fstab_file ();
847 file = setmntent (read_file, "r");
853 while (!aix_fs_get (file, &mntent))
855 if (strcmp ("cdrfs", mntent.mnt_fstype) == 0)
857 mount_entry = g_new0 (GUnixMountPoint, 1);
860 mount_entry->mount_path = g_strdup (mntent.mnt_mount);
861 mount_entry->device_path = g_strdup (mntent.mnt_special);
862 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
863 mount_entry->is_read_only = TRUE;
864 mount_entry->is_user_mountable = TRUE;
866 return_list = g_list_prepend (return_list, mount_entry);
872 return g_list_reverse (return_list);
875 #elif defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
878 _g_get_unix_mount_points (void)
880 struct fstab *fstab = NULL;
881 GUnixMountPoint *mount_entry;
883 #ifdef HAVE_SYS_SYSCTL_H
885 size_t len = sizeof(usermnt);
894 #ifdef HAVE_SYS_SYSCTL_H
895 #if defined(HAVE_SYSCTLBYNAME)
896 sysctlbyname ("vfs.usermount", &usermnt, &len, NULL, 0);
897 #elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
902 mib[1] = VFS_USERMOUNT;
903 sysctl (mib, 2, &usermnt, &len, NULL, 0);
905 #elif defined(CTL_KERN) && defined(KERN_USERMOUNT)
910 mib[1] = KERN_USERMOUNT;
911 sysctl (mib, 2, &usermnt, &len, NULL, 0);
916 while ((fstab = getfsent ()) != NULL)
918 if (strcmp (fstab->fs_vfstype, "swap") == 0)
921 mount_entry = g_new0 (GUnixMountPoint, 1);
923 mount_entry->mount_path = g_strdup (fstab->fs_file);
924 mount_entry->device_path = g_strdup (fstab->fs_spec);
925 mount_entry->filesystem_type = g_strdup (fstab->fs_vfstype);
927 if (strcmp (fstab->fs_type, "ro") == 0)
928 mount_entry->is_read_only = TRUE;
930 #ifdef HAVE_SYS_SYSCTL_H
933 uid_t uid = getuid ();
934 if (stat (fstab->fs_file, &sb) == 0)
936 if (uid == 0 || sb.st_uid == uid)
937 mount_entry->is_user_mountable = TRUE;
942 return_list = g_list_prepend (return_list, mount_entry);
947 return g_list_reverse (return_list);
950 #error No g_get_mount_table() implementation for system
954 get_mounts_timestamp (void)
956 const char *monitor_file;
959 monitor_file = get_mtab_monitor_file ();
962 if (stat (monitor_file, &buf) == 0)
963 return (guint64)buf.st_mtime;
969 get_mount_points_timestamp (void)
971 const char *monitor_file;
974 monitor_file = get_fstab_file ();
977 if (stat (monitor_file, &buf) == 0)
978 return (guint64)buf.st_mtime;
985 * @time_read: guint64 to contain a timestamp.
987 * Gets a #GList of strings containing the unix mounts.
988 * If @time_read is set, it will be filled with the mount
989 * timestamp, allowing for checking if the mounts have changed
990 * with g_unix_mounts_changed_since().
992 * Returns: a #GList of the UNIX mounts.
995 g_get_unix_mounts (guint64 *time_read)
998 *time_read = get_mounts_timestamp ();
1000 return _g_get_unix_mounts ();
1004 * g_get_unix_mount_at:
1005 * @mount_path: path for a possible unix mount.
1006 * @time_read: guint64 to contain a timestamp.
1008 * Gets a #GUnixMountEntry for a given mount path. If @time_read
1009 * is set, it will be filled with a unix timestamp for checking
1010 * if the mounts have changed since with g_unix_mounts_changed_since().
1012 * Returns: a #GUnixMount.
1015 g_get_unix_mount_at (const char *mount_path,
1019 GUnixMountEntry *mount_entry, *found;
1021 mounts = g_get_unix_mounts (time_read);
1024 for (l = mounts; l != NULL; l = l->next)
1026 mount_entry = l->data;
1028 if (strcmp (mount_path, mount_entry->mount_path) == 0)
1029 found = mount_entry;
1031 g_unix_mount_free (mount_entry);
1034 g_list_free (mounts);
1040 * g_get_unix_mount_points:
1041 * @time_read: guint64 to contain a timestamp.
1043 * Gets a #GList of strings containing the unix mount points.
1044 * If @time_read is set, it will be filled with the mount timestamp,
1045 * allowing for checking if the mounts have changed with
1046 * g_unix_mounts_points_changed_since().
1048 * Returns: a #GList of the UNIX mountpoints.
1051 g_get_unix_mount_points (guint64 *time_read)
1054 *time_read = get_mount_points_timestamp ();
1056 return _g_get_unix_mount_points ();
1060 * g_unix_mounts_changed_since:
1061 * @time: guint64 to contain a timestamp.
1063 * Checks if the unix mounts have changed since a given unix time.
1065 * Returns: %TRUE if the mounts have changed since @time.
1068 g_unix_mounts_changed_since (guint64 time)
1070 return get_mounts_timestamp () != time;
1074 * g_unix_mount_points_changed_since:
1075 * @time: guint64 to contain a timestamp.
1077 * Checks if the unix mount points have changed since a given unix time.
1079 * Returns: %TRUE if the mount points have changed since @time.
1082 g_unix_mount_points_changed_since (guint64 time)
1084 return get_mount_points_timestamp () != time;
1088 g_unix_mount_monitor_finalize (GObject *object)
1090 GUnixMountMonitor *monitor;
1092 monitor = G_UNIX_MOUNT_MONITOR (object);
1094 if (monitor->fstab_monitor)
1096 g_file_monitor_cancel (monitor->fstab_monitor);
1097 g_object_unref (monitor->fstab_monitor);
1100 if (monitor->mtab_monitor)
1102 g_file_monitor_cancel (monitor->mtab_monitor);
1103 g_object_unref (monitor->mtab_monitor);
1106 the_mount_monitor = NULL;
1108 if (G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize)
1109 (*G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize) (object);
1114 g_unix_mount_monitor_class_init (GUnixMountMonitorClass *klass)
1116 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1118 gobject_class->finalize = g_unix_mount_monitor_finalize;
1120 * GUnixMountMonitor::mounts-changed:
1122 * Emitted when the unix mounts have changed.
1124 signals[MOUNTS_CHANGED] =
1125 g_signal_new ("mounts_changed",
1126 G_TYPE_FROM_CLASS (klass),
1130 g_cclosure_marshal_VOID__VOID,
1133 * GUnixMountMonitor::mountpoints-changed:
1135 * Emitted when the unix mount points have changed.
1137 signals[MOUNTPOINTS_CHANGED] =
1138 g_signal_new ("mountpoints_changed",
1139 G_TYPE_FROM_CLASS (klass),
1143 g_cclosure_marshal_VOID__VOID,
1148 fstab_file_changed (GFileMonitor *monitor,
1151 GFileMonitorEvent event_type,
1154 GUnixMountMonitor *mount_monitor;
1156 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1157 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1158 event_type != G_FILE_MONITOR_EVENT_DELETED)
1161 mount_monitor = user_data;
1162 g_signal_emit (mount_monitor, signals[MOUNTPOINTS_CHANGED], 0);
1166 mtab_file_changed (GFileMonitor *monitor,
1169 GFileMonitorEvent event_type,
1172 GUnixMountMonitor *mount_monitor;
1174 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1175 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1176 event_type != G_FILE_MONITOR_EVENT_DELETED)
1179 mount_monitor = user_data;
1180 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1184 g_unix_mount_monitor_init (GUnixMountMonitor *monitor)
1188 if (get_fstab_file () != NULL)
1190 file = g_file_new_for_path (get_fstab_file ());
1191 monitor->fstab_monitor = g_file_monitor_file (file, 0, NULL);
1192 g_object_unref (file);
1194 g_signal_connect (monitor->fstab_monitor, "changed", (GCallback)fstab_file_changed, monitor);
1197 if (get_mtab_monitor_file () != NULL)
1199 file = g_file_new_for_path (get_mtab_monitor_file ());
1200 monitor->mtab_monitor = g_file_monitor_file (file, 0, NULL);
1201 g_object_unref (file);
1203 g_signal_connect (monitor->mtab_monitor, "changed", (GCallback)mtab_file_changed, monitor);
1208 * g_unix_mount_monitor_new:
1210 * Gets a new #GUnixMountMonitor.
1212 * Returns: a #GUnixMountMonitor.
1215 g_unix_mount_monitor_new (void)
1217 if (the_mount_monitor == NULL)
1219 the_mount_monitor = g_object_new (G_TYPE_UNIX_MOUNT_MONITOR, NULL);
1220 return the_mount_monitor;
1223 return g_object_ref (the_mount_monitor);
1227 * g_unix_mount_free:
1228 * @mount_entry: a #GUnixMount.
1230 * Frees a unix mount.
1233 g_unix_mount_free (GUnixMountEntry *mount_entry)
1235 g_return_if_fail (mount_entry != NULL);
1237 g_free (mount_entry->mount_path);
1238 g_free (mount_entry->device_path);
1239 g_free (mount_entry->filesystem_type);
1240 g_free (mount_entry);
1244 * g_unix_mount_point_free:
1245 * @mount_point: unix mount point to free.
1247 * Frees a unix mount point.
1250 g_unix_mount_point_free (GUnixMountPoint *mount_point)
1252 g_return_if_fail (mount_point != NULL);
1254 g_free (mount_point->mount_path);
1255 g_free (mount_point->device_path);
1256 g_free (mount_point->filesystem_type);
1257 g_free (mount_point);
1261 strcmp_null (const char *str1,
1266 if (str1 == NULL && str2 != NULL)
1268 if (str1 != NULL && str2 == NULL)
1270 return strcmp (str1, str2);
1274 * g_unix_mount_compare:
1275 * @mount1: first #GUnixMountEntry to compare.
1276 * @mount2: second #GUnixMountEntry to compare.
1278 * Compares two unix mounts.
1280 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1281 * or less than @mount2, respectively.
1284 g_unix_mount_compare (GUnixMountEntry *mount1,
1285 GUnixMountEntry *mount2)
1289 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1291 res = strcmp_null (mount1->mount_path, mount2->mount_path);
1295 res = strcmp_null (mount1->device_path, mount2->device_path);
1299 res = strcmp_null (mount1->filesystem_type, mount2->filesystem_type);
1303 res = mount1->is_read_only - mount2->is_read_only;
1311 * g_unix_mount_get_mount_path:
1312 * @mount_entry: input #GUnixMountEntry to get the mount path for.
1314 * Gets the mount path for a unix mount.
1316 * Returns: the mount path for @mount_entry.
1319 g_unix_mount_get_mount_path (GUnixMountEntry *mount_entry)
1321 g_return_val_if_fail (mount_entry != NULL, NULL);
1323 return mount_entry->mount_path;
1327 * g_unix_mount_get_device_path:
1328 * @mount_entry: a #GUnixMount.
1330 * Gets the device path for a unix mount.
1332 * Returns: a string containing the device path.
1335 g_unix_mount_get_device_path (GUnixMountEntry *mount_entry)
1337 g_return_val_if_fail (mount_entry != NULL, NULL);
1339 return mount_entry->device_path;
1343 * g_unix_mount_get_fs_type:
1344 * @mount_entry: a #GUnixMount.
1346 * Gets the filesystem type for the unix mount.
1348 * Returns: a string containing the file system type.
1351 g_unix_mount_get_fs_type (GUnixMountEntry *mount_entry)
1353 g_return_val_if_fail (mount_entry != NULL, NULL);
1355 return mount_entry->filesystem_type;
1359 * g_unix_mount_is_readonly:
1360 * @mount_entry: a #GUnixMount.
1362 * Checks if a unix mount is mounted read only.
1364 * Returns: %TRUE if @mount_entry is read only.
1367 g_unix_mount_is_readonly (GUnixMountEntry *mount_entry)
1369 g_return_val_if_fail (mount_entry != NULL, FALSE);
1371 return mount_entry->is_read_only;
1375 * g_unix_mount_is_system_internal:
1376 * @mount_entry: a #GUnixMount.
1378 * Checks if a unix mount is a system path.
1380 * Returns: %TRUE if the unix mount is for a system path.
1383 g_unix_mount_is_system_internal (GUnixMountEntry *mount_entry)
1385 g_return_val_if_fail (mount_entry != NULL, FALSE);
1387 return mount_entry->is_system_internal;
1391 * g_unix_mount_point_compare:
1392 * @mount1: a #GUnixMount.
1393 * @mount2: a #GUnixMount.
1395 * Compares two unix mount points.
1397 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1398 * or less than @mount2, respectively.
1401 g_unix_mount_point_compare (GUnixMountPoint *mount1,
1402 GUnixMountPoint *mount2)
1406 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1408 res = strcmp_null (mount1->mount_path, mount2->mount_path);
1412 res = strcmp_null (mount1->device_path, mount2->device_path);
1416 res = strcmp_null (mount1->filesystem_type, mount2->filesystem_type);
1420 res = mount1->is_read_only - mount2->is_read_only;
1424 res = mount1->is_user_mountable - mount2->is_user_mountable;
1428 res = mount1->is_loopback - mount2->is_loopback;
1436 * g_unix_mount_point_get_mount_path:
1437 * @mount_point: a #GUnixMountPoint.
1439 * Gets the mount path for a unix mount point.
1441 * Returns: a string containing the mount path.
1444 g_unix_mount_point_get_mount_path (GUnixMountPoint *mount_point)
1446 g_return_val_if_fail (mount_point != NULL, NULL);
1448 return mount_point->mount_path;
1452 * g_unix_mount_point_get_device_path:
1453 * @mount_point: a #GUnixMountPoint.
1455 * Gets the device path for a unix mount point.
1457 * Returns: a string containing the device path.
1460 g_unix_mount_point_get_device_path (GUnixMountPoint *mount_point)
1462 g_return_val_if_fail (mount_point != NULL, NULL);
1464 return mount_point->device_path;
1468 * g_unix_mount_point_get_fs_type:
1469 * @mount_point: a #GUnixMountPoint.
1471 * Gets the file system type for the mount point.
1473 * Returns: a string containing the file system type.
1476 g_unix_mount_point_get_fs_type (GUnixMountPoint *mount_point)
1478 g_return_val_if_fail (mount_point != NULL, NULL);
1480 return mount_point->filesystem_type;
1484 * g_unix_mount_point_is_readonly:
1485 * @mount_point: a #GUnixMountPoint.
1487 * Checks if a unix mount point is read only.
1489 * Returns: %TRUE if a mount point is read only.
1492 g_unix_mount_point_is_readonly (GUnixMountPoint *mount_point)
1494 g_return_val_if_fail (mount_point != NULL, FALSE);
1496 return mount_point->is_read_only;
1500 * g_unix_mount_point_is_user_mountable:
1501 * @mount_point: a #GUnixMountPoint.
1503 * Checks if a unix mount point is mountable by the user.
1505 * Returns: %TRUE if the mount point is user mountable.
1508 g_unix_mount_point_is_user_mountable (GUnixMountPoint *mount_point)
1510 g_return_val_if_fail (mount_point != NULL, FALSE);
1512 return mount_point->is_user_mountable;
1516 * g_unix_mount_point_is_loopback:
1517 * @mount_point: a #GUnixMountPoint.
1519 * Checks if a unix mount point is a loopback device.
1521 * Returns: %TRUE if the mount point is a loopback. %FALSE otherwise.
1524 g_unix_mount_point_is_loopback (GUnixMountPoint *mount_point)
1526 g_return_val_if_fail (mount_point != NULL, FALSE);
1528 return mount_point->is_loopback;
1531 static GUnixMountType
1532 guess_mount_type (const char *mount_path,
1533 const char *device_path,
1534 const char *filesystem_type)
1536 GUnixMountType type;
1539 type = G_UNIX_MOUNT_TYPE_UNKNOWN;
1541 if ((strcmp (filesystem_type, "udf") == 0) ||
1542 (strcmp (filesystem_type, "iso9660") == 0) ||
1543 (strcmp (filesystem_type, "cd9660") == 0))
1544 type = G_UNIX_MOUNT_TYPE_CDROM;
1545 else if (strcmp (filesystem_type, "nfs") == 0)
1546 type = G_UNIX_MOUNT_TYPE_NFS;
1547 else if (g_str_has_prefix (device_path, "/vol/dev/diskette/") ||
1548 g_str_has_prefix (device_path, "/dev/fd") ||
1549 g_str_has_prefix (device_path, "/dev/floppy"))
1550 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1551 else if (g_str_has_prefix (device_path, "/dev/cdrom") ||
1552 g_str_has_prefix (device_path, "/dev/acd") ||
1553 g_str_has_prefix (device_path, "/dev/cd"))
1554 type = G_UNIX_MOUNT_TYPE_CDROM;
1555 else if (g_str_has_prefix (device_path, "/vol/"))
1557 const char *name = mount_path + strlen ("/");
1559 if (g_str_has_prefix (name, "cdrom"))
1560 type = G_UNIX_MOUNT_TYPE_CDROM;
1561 else if (g_str_has_prefix (name, "floppy") ||
1562 g_str_has_prefix (device_path, "/vol/dev/diskette/"))
1563 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1564 else if (g_str_has_prefix (name, "rmdisk"))
1565 type = G_UNIX_MOUNT_TYPE_ZIP;
1566 else if (g_str_has_prefix (name, "jaz"))
1567 type = G_UNIX_MOUNT_TYPE_JAZ;
1568 else if (g_str_has_prefix (name, "memstick"))
1569 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1573 basename = g_path_get_basename (mount_path);
1575 if (g_str_has_prefix (basename, "cdrom") ||
1576 g_str_has_prefix (basename, "cdwriter") ||
1577 g_str_has_prefix (basename, "burn") ||
1578 g_str_has_prefix (basename, "cdr") ||
1579 g_str_has_prefix (basename, "cdrw") ||
1580 g_str_has_prefix (basename, "dvdrom") ||
1581 g_str_has_prefix (basename, "dvdram") ||
1582 g_str_has_prefix (basename, "dvdr") ||
1583 g_str_has_prefix (basename, "dvdrw") ||
1584 g_str_has_prefix (basename, "cdrom_dvdrom") ||
1585 g_str_has_prefix (basename, "cdrom_dvdram") ||
1586 g_str_has_prefix (basename, "cdrom_dvdr") ||
1587 g_str_has_prefix (basename, "cdrom_dvdrw") ||
1588 g_str_has_prefix (basename, "cdr_dvdrom") ||
1589 g_str_has_prefix (basename, "cdr_dvdram") ||
1590 g_str_has_prefix (basename, "cdr_dvdr") ||
1591 g_str_has_prefix (basename, "cdr_dvdrw") ||
1592 g_str_has_prefix (basename, "cdrw_dvdrom") ||
1593 g_str_has_prefix (basename, "cdrw_dvdram") ||
1594 g_str_has_prefix (basename, "cdrw_dvdr") ||
1595 g_str_has_prefix (basename, "cdrw_dvdrw"))
1596 type = G_UNIX_MOUNT_TYPE_CDROM;
1597 else if (g_str_has_prefix (basename, "floppy"))
1598 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1599 else if (g_str_has_prefix (basename, "zip"))
1600 type = G_UNIX_MOUNT_TYPE_ZIP;
1601 else if (g_str_has_prefix (basename, "jaz"))
1602 type = G_UNIX_MOUNT_TYPE_JAZ;
1603 else if (g_str_has_prefix (basename, "camera"))
1604 type = G_UNIX_MOUNT_TYPE_CAMERA;
1605 else if (g_str_has_prefix (basename, "memstick") ||
1606 g_str_has_prefix (basename, "memory_stick") ||
1607 g_str_has_prefix (basename, "ram"))
1608 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1609 else if (g_str_has_prefix (basename, "compact_flash"))
1610 type = G_UNIX_MOUNT_TYPE_CF;
1611 else if (g_str_has_prefix (basename, "smart_media"))
1612 type = G_UNIX_MOUNT_TYPE_SM;
1613 else if (g_str_has_prefix (basename, "sd_mmc"))
1614 type = G_UNIX_MOUNT_TYPE_SDMMC;
1615 else if (g_str_has_prefix (basename, "ipod"))
1616 type = G_UNIX_MOUNT_TYPE_IPOD;
1621 if (type == G_UNIX_MOUNT_TYPE_UNKNOWN)
1622 type = G_UNIX_MOUNT_TYPE_HD;
1628 * g_unix_mount_guess_type:
1629 * @mount_entry: a #GUnixMount.
1631 * Guesses the type of a unix mount. If the mount type cannot be
1632 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1634 * Returns: a #GUnixMountType.
1636 static GUnixMountType
1637 g_unix_mount_guess_type (GUnixMountEntry *mount_entry)
1639 g_return_val_if_fail (mount_entry != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1640 g_return_val_if_fail (mount_entry->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1641 g_return_val_if_fail (mount_entry->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1642 g_return_val_if_fail (mount_entry->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1644 return guess_mount_type (mount_entry->mount_path,
1645 mount_entry->device_path,
1646 mount_entry->filesystem_type);
1650 * g_unix_mount_point_guess_type:
1651 * @mount_point: a #GUnixMountPoint.
1653 * Guesses the type of a unix mount point. If the mount type cannot be
1654 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1656 * Returns: a #GUnixMountType.
1658 static GUnixMountType
1659 g_unix_mount_point_guess_type (GUnixMountPoint *mount_point)
1661 g_return_val_if_fail (mount_point != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1662 g_return_val_if_fail (mount_point->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1663 g_return_val_if_fail (mount_point->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1664 g_return_val_if_fail (mount_point->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1666 return guess_mount_type (mount_point->mount_path,
1667 mount_point->device_path,
1668 mount_point->filesystem_type);
1672 type_to_icon (GUnixMountType type, gboolean is_mount_point)
1674 const char *icon_name;
1678 case G_UNIX_MOUNT_TYPE_HD:
1680 icon_name = "drive-removable-media";
1682 icon_name = "drive-harddisk";
1684 case G_UNIX_MOUNT_TYPE_FLOPPY:
1685 case G_UNIX_MOUNT_TYPE_ZIP:
1686 case G_UNIX_MOUNT_TYPE_JAZ:
1688 icon_name = "drive-removable-media";
1690 icon_name = "media-floppy";
1692 case G_UNIX_MOUNT_TYPE_CDROM:
1694 icon_name = "drive-optical";
1696 icon_name = "media-optical";
1698 case G_UNIX_MOUNT_TYPE_NFS:
1699 /* TODO: Would like a better icon here... */
1701 icon_name = "drive-removable-media";
1703 icon_name = "drive-harddisk";
1705 case G_UNIX_MOUNT_TYPE_MEMSTICK:
1707 icon_name = "drive-removable-media";
1709 icon_name = "media-flash";
1711 case G_UNIX_MOUNT_TYPE_CAMERA:
1713 icon_name = "drive-removable-media";
1715 icon_name = "camera-photo";
1717 case G_UNIX_MOUNT_TYPE_IPOD:
1719 icon_name = "drive-removable-media";
1721 icon_name = "multimedia-player";
1723 case G_UNIX_MOUNT_TYPE_UNKNOWN:
1726 icon_name = "drive-removable-media";
1728 icon_name = "drive-harddisk";
1736 g_unix_mount_guess_name (GUnixMountEntry *mount_entry)
1740 if (strcmp (mount_entry->mount_path, "/") == 0)
1741 name = g_strdup (_("Filesystem root"));
1743 name = g_filename_display_basename (mount_entry->mount_path);
1749 g_unix_mount_guess_icon (GUnixMountEntry *mount_entry)
1751 return g_themed_icon_new (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE));
1755 g_unix_mount_point_guess_name (GUnixMountPoint *mount_point)
1759 if (strcmp (mount_point->mount_path, "/") == 0)
1760 name = g_strdup (_("Filesystem root"));
1762 name = g_filename_display_basename (mount_point->mount_path);
1768 g_unix_mount_point_guess_icon (GUnixMountPoint *mount_point)
1770 return g_themed_icon_new (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE));
1774 /* borrowed from gtk/gtkfilesystemunix.c in GTK+ on 02/23/2006 */
1776 _canonicalize_filename (gchar *filename)
1779 gboolean last_was_slash = FALSE;
1786 if (*p == G_DIR_SEPARATOR)
1788 if (!last_was_slash)
1789 *q++ = G_DIR_SEPARATOR;
1791 last_was_slash = TRUE;
1795 if (last_was_slash && *p == '.')
1797 if (*(p + 1) == G_DIR_SEPARATOR ||
1800 if (*(p + 1) == '\0')
1805 else if (*(p + 1) == '.' &&
1806 (*(p + 2) == G_DIR_SEPARATOR ||
1809 if (q > filename + 1)
1812 while (q > filename + 1 &&
1813 *(q - 1) != G_DIR_SEPARATOR)
1817 if (*(p + 2) == '\0')
1825 last_was_slash = FALSE;
1831 last_was_slash = FALSE;
1838 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
1845 _resolve_symlink (const char *file)
1853 f = g_strdup (file);
1855 while (g_file_test (f, G_FILE_TEST_IS_SYMLINK)) {
1856 link = g_file_read_link (f, &error);
1858 g_error_free (error);
1864 dir = g_path_get_dirname (f);
1865 f1 = g_strdup_printf ("%s/%s", dir, link);
1874 _canonicalize_filename (f);
1879 _resolve_dev_root (void)
1881 static gboolean have_real_dev_root = FALSE;
1882 static char real_dev_root[256];
1883 struct stat statbuf;
1885 /* see if it's cached already */
1886 if (have_real_dev_root)
1889 /* otherwise we're going to find it right away.. */
1890 have_real_dev_root = TRUE;
1892 if (stat ("/dev/root", &statbuf) == 0) {
1893 if (! S_ISLNK (statbuf.st_mode)) {
1894 dev_t root_dev = statbuf.st_dev;
1898 /* see if device with similar major:minor as /dev/root is mention
1899 * in /etc/mtab (it usually is)
1901 f = fopen ("/etc/mtab", "r");
1905 while (getmntent_r (f, &ent, buf, sizeof (buf)) != NULL) {
1907 if (stat (ent.mnt_fsname, &statbuf) == 0 &&
1908 statbuf.st_dev == root_dev) {
1909 strncpy (real_dev_root, ent.mnt_fsname, sizeof (real_dev_root) - 1);
1910 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
1918 /* no, that didn't work.. next we could scan /dev ... but I digress.. */
1922 resolved = _resolve_symlink ("/dev/root");
1923 if (resolved != NULL) {
1924 strncpy (real_dev_root, resolved, sizeof (real_dev_root) - 1);
1925 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
1933 strcpy (real_dev_root, "/dev/root");
1936 return real_dev_root;
1939 #define __G_UNIX_MOUNTS_C__
1940 #include "gioaliasdef.c"