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. <literal>/media/disk</literal> or <literal>/usr</literal>
198 * Determines if @mount_path is considered an implementation of the
199 * OS. This is primarily used for hiding mountable and mounted volumes
200 * that only are used in the OS and has little to no relevance to the
203 * Returns; %TRUE if @mount_path is considered an implementation detail of the OS.
206 g_unix_is_mount_path_system_internal (const char *mount_path)
208 const char *ignore_mountpoints[] = {
209 /* Includes all FHS 2.3 toplevel dirs and other specilized
210 * directories that we want to hide from the user.
212 "/", /* we already have "Filesystem root" in Nautilus */
229 "/var/log/audit", /* https://bugzilla.redhat.com/show_bug.cgi?id=333041 */
230 "/var/tmp", /* https://bugzilla.redhat.com/show_bug.cgi?id=335241 */
237 if (is_in (mount_path, ignore_mountpoints))
240 if (g_str_has_prefix (mount_path, "/dev") ||
241 g_str_has_prefix (mount_path, "/proc") ||
242 g_str_has_prefix (mount_path, "/sys"))
245 if (strstr (mount_path, "/.gvfs") != NULL)
252 guess_system_internal (const char *mountpoint,
256 const char *ignore_fs[] = {
274 const char *ignore_devices[] = {
284 if (is_in (fs, ignore_fs))
287 if (is_in (device, ignore_devices))
290 if (g_unix_is_mount_path_system_internal (mountpoint))
299 get_mtab_read_file (void)
303 return "/proc/mounts";
305 return _PATH_MOUNTED;
313 get_mtab_monitor_file (void)
316 return _PATH_MOUNTED;
322 G_LOCK_DEFINE_STATIC(getmntent);
325 _g_get_unix_mounts ()
327 struct mntent *mntent;
330 GUnixMountEntry *mount_entry;
331 GHashTable *mounts_hash;
334 read_file = get_mtab_read_file ();
336 file = setmntent (read_file, "r");
342 mounts_hash = g_hash_table_new (g_str_hash, g_str_equal);
345 while ((mntent = getmntent (file)) != NULL)
347 /* ignore any mnt_fsname that is repeated and begins with a '/'
349 * We do this to avoid being fooled by --bind mounts, since
350 * these have the same device as the location they bind to.
351 * Its not an ideal solution to the problem, but its likely that
352 * the most important mountpoint is first and the --bind ones after
353 * that aren't as important. So it should work.
355 * The '/' is to handle procfs, tmpfs and other no device mounts.
357 if (mntent->mnt_fsname != NULL &&
358 mntent->mnt_fsname[0] == '/' &&
359 g_hash_table_lookup (mounts_hash, mntent->mnt_fsname))
362 mount_entry = g_new0 (GUnixMountEntry, 1);
363 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
364 if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
365 mount_entry->device_path = g_strdup (_resolve_dev_root ());
367 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
368 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
370 #if defined (HAVE_HASMNTOPT)
371 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
372 mount_entry->is_read_only = TRUE;
375 mount_entry->is_system_internal =
376 guess_system_internal (mount_entry->mount_path,
377 mount_entry->filesystem_type,
378 mount_entry->device_path);
380 g_hash_table_insert (mounts_hash,
381 mount_entry->device_path,
382 mount_entry->device_path);
384 return_list = g_list_prepend (return_list, mount_entry);
386 g_hash_table_destroy (mounts_hash);
390 G_UNLOCK (getmntent);
392 return g_list_reverse (return_list);
395 #elif defined (HAVE_SYS_MNTTAB_H)
397 G_LOCK_DEFINE_STATIC(getmntent);
400 get_mtab_read_file (void)
403 return _PATH_MOUNTED;
405 return "/etc/mnttab";
410 get_mtab_monitor_file (void)
412 return get_mtab_read_file ();
416 _g_get_unix_mounts (void)
418 struct mnttab mntent;
421 GUnixMountEntry *mount_entry;
424 read_file = get_mtab_read_file ();
426 file = setmntent (read_file, "r");
433 while (! getmntent (file, &mntent))
435 mount_entry = g_new0 (GUnixMountEntry, 1);
437 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
438 mount_entry->device_path = g_strdup (mntent.mnt_special);
439 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
441 #if defined (HAVE_HASMNTOPT)
442 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
443 mount_entry->is_read_only = TRUE;
446 mount_entry->is_system_internal =
447 guess_system_internal (mount_entry->mount_path,
448 mount_entry->filesystem_type,
449 mount_entry->device_path);
451 return_list = g_list_prepend (return_list, mount_entry);
456 G_UNLOCK (getmntent);
458 return g_list_reverse (return_list);
461 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
464 get_mtab_monitor_file (void)
470 _g_get_unix_mounts (void)
472 struct vfs_ent *fs_info;
473 struct vmount *vmount_info;
475 unsigned int vmount_size;
479 if (mntctl (MCTL_QUERY, sizeof (vmount_size), &vmount_size) != 0)
481 g_warning ("Unable to know the number of mounted volumes\n");
486 vmount_info = (struct vmount*)g_malloc (vmount_size);
488 vmount_number = mntctl (MCTL_QUERY, vmount_size, vmount_info);
490 if (vmount_info->vmt_revision != VMT_REVISION)
491 g_warning ("Bad vmount structure revision number, want %d, got %d\n", VMT_REVISION, vmount_info->vmt_revision);
493 if (vmount_number < 0)
495 g_warning ("Unable to recover mounted volumes information\n");
497 g_free (vmount_info);
502 while (vmount_number > 0)
504 mount_entry = g_new0 (GUnixMountEntry, 1);
506 mount_entry->device_path = g_strdup (vmt2dataptr (vmount_info, VMT_OBJECT));
507 mount_entry->mount_path = g_strdup (vmt2dataptr (vmount_info, VMT_STUB));
508 /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
509 mount_entry->is_read_only = (vmount_info->vmt_flags & MNT_READONLY) ? 1 : 0;
511 fs_info = getvfsbytype (vmount_info->vmt_gfstype);
514 mount_entry->filesystem_type = g_strdup ("unknown");
516 mount_entry->filesystem_type = g_strdup (fs_info->vfsent_name);
518 mount_entry->is_system_internal =
519 guess_system_internal (mount_entry->mount_path,
520 mount_entry->filesystem_type,
521 mount_entry->device_path);
523 return_list = g_list_prepend (return_list, mount_entry);
525 vmount_info = (struct vmount *)( (char*)vmount_info
526 + vmount_info->vmt_length);
531 g_free (vmount_info);
533 return g_list_reverse (return_list);
536 #elif defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
539 get_mtab_monitor_file (void)
545 _g_get_unix_mounts (void)
547 struct statfs *mntent = NULL;
549 GUnixMountEntry *mount_entry;
552 /* Pass MNT_NOWAIT to avoid blocking trying to update NFS mounts. */
553 if ((num_mounts = getmntinfo (&mntent, MNT_NOWAIT)) == 0)
558 for (i = 0; i < num_mounts; i++)
560 mount_entry = g_new0 (GUnixMountEntry, 1);
562 mount_entry->mount_path = g_strdup (mntent[i].f_mntonname);
563 mount_entry->device_path = g_strdup (mntent[i].f_mntfromname);
564 mount_entry->filesystem_type = g_strdup (mntent[i].f_fstypename);
565 if (mntent[i].f_flags & MNT_RDONLY)
566 mount_entry->is_read_only = TRUE;
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);
576 return g_list_reverse (return_list);
579 #error No _g_get_unix_mounts() implementation for system
582 /* _g_get_unix_mount_points():
584 * don't return swap and ignore mounts.
588 get_fstab_file (void)
590 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
592 return "/etc/filesystems";
593 #elif defined(_PATH_MNTTAB)
595 #elif defined(VFSTAB)
604 _g_get_unix_mount_points (void)
606 struct mntent *mntent;
609 GUnixMountPoint *mount_entry;
612 read_file = get_fstab_file ();
614 file = setmntent (read_file, "r");
621 while ((mntent = getmntent (file)) != NULL)
623 if ((strcmp (mntent->mnt_dir, "ignore") == 0) ||
624 (strcmp (mntent->mnt_dir, "swap") == 0))
627 mount_entry = g_new0 (GUnixMountPoint, 1);
628 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
629 if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
630 mount_entry->device_path = g_strdup (_resolve_dev_root ());
632 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
633 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
635 #ifdef HAVE_HASMNTOPT
636 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
637 mount_entry->is_read_only = TRUE;
639 if (hasmntopt (mntent, "loop") != NULL)
640 mount_entry->is_loopback = TRUE;
644 if ((mntent->mnt_type != NULL && strcmp ("supermount", mntent->mnt_type) == 0)
645 #ifdef HAVE_HASMNTOPT
646 || (hasmntopt (mntent, "user") != NULL
647 && hasmntopt (mntent, "user") != hasmntopt (mntent, "user_xattr"))
648 || hasmntopt (mntent, "pamconsole") != NULL
649 || hasmntopt (mntent, "users") != NULL
650 || hasmntopt (mntent, "owner") != NULL
653 mount_entry->is_user_mountable = TRUE;
655 return_list = g_list_prepend (return_list, mount_entry);
659 G_UNLOCK (getmntent);
661 return g_list_reverse (return_list);
664 #elif defined (HAVE_SYS_MNTTAB_H)
667 _g_get_unix_mount_points (void)
669 struct mnttab mntent;
672 GUnixMountPoint *mount_entry;
675 read_file = get_fstab_file ();
677 file = setmntent (read_file, "r");
684 while (! getmntent (file, &mntent))
686 if ((strcmp (mntent.mnt_mountp, "ignore") == 0) ||
687 (strcmp (mntent.mnt_mountp, "swap") == 0))
690 mount_entry = g_new0 (GUnixMountPoint, 1);
692 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
693 mount_entry->device_path = g_strdup (mntent.mnt_special);
694 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
696 #ifdef HAVE_HASMNTOPT
697 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
698 mount_entry->is_read_only = TRUE;
700 if (hasmntopt (&mntent, "lofs") != NULL)
701 mount_entry->is_loopback = TRUE;
704 if ((mntent.mnt_fstype != NULL)
705 #ifdef HAVE_HASMNTOPT
706 || (hasmntopt (&mntent, "user") != NULL
707 && hasmntopt (&mntent, "user") != hasmntopt (&mntent, "user_xattr"))
708 || hasmntopt (&mntent, "pamconsole") != NULL
709 || hasmntopt (&mntent, "users") != NULL
710 || hasmntopt (&mntent, "owner") != NULL
713 mount_entry->is_user_mountable = TRUE;
716 return_list = g_list_prepend (return_list, mount_entry);
720 G_UNLOCK (getmntent);
722 return g_list_reverse (return_list);
724 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
726 /* functions to parse /etc/filesystems on aix */
728 /* read character, ignoring comments (begin with '*', end with '\n' */
730 aix_fs_getc (FILE *fd)
734 while ((c = getc (fd)) == '*')
736 while (((c = getc (fd)) != '\n') && (c != EOF))
741 /* eat all continuous spaces in a file */
743 aix_fs_ignorespace (FILE *fd)
747 while ((c = aix_fs_getc (fd)) != EOF)
749 if (!g_ascii_isspace (c))
759 /* read one word from file */
761 aix_fs_getword (FILE *fd,
766 aix_fs_ignorespace (fd);
768 while (((c = aix_fs_getc (fd)) != EOF) && !g_ascii_isspace (c))
772 while (((c = aix_fs_getc (fd)) != EOF) && (c != '"'))
784 char mnt_mount[PATH_MAX];
785 char mnt_special[PATH_MAX];
787 char mnt_options[128];
788 } AixMountTableEntry;
790 /* read mount points properties */
792 aix_fs_get (FILE *fd,
793 AixMountTableEntry *prop)
795 static char word[PATH_MAX] = { 0 };
796 char value[PATH_MAX];
801 if (aix_fs_getword (fd, word) == EOF)
805 word[strlen(word) - 1] = 0;
806 strcpy (prop->mnt_mount, word);
808 /* read attributes and value */
810 while (aix_fs_getword (fd, word) != EOF)
812 /* test if is attribute or new stanza */
813 if (word[strlen(word) - 1] == ':')
817 aix_fs_getword (fd, value);
820 aix_fs_getword (fd, value);
822 if (strcmp (word, "dev") == 0)
823 strcpy (prop->mnt_special, value);
824 else if (strcmp (word, "vfs") == 0)
825 strcpy (prop->mnt_fstype, value);
826 else if (strcmp (word, "options") == 0)
827 strcpy(prop->mnt_options, value);
834 _g_get_unix_mount_points (void)
836 struct mntent *mntent;
839 GUnixMountPoint *mount_entry;
840 AixMountTableEntry mntent;
843 read_file = get_fstab_file ();
845 file = setmntent (read_file, "r");
851 while (!aix_fs_get (file, &mntent))
853 if (strcmp ("cdrfs", mntent.mnt_fstype) == 0)
855 mount_entry = g_new0 (GUnixMountPoint, 1);
858 mount_entry->mount_path = g_strdup (mntent.mnt_mount);
859 mount_entry->device_path = g_strdup (mntent.mnt_special);
860 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
861 mount_entry->is_read_only = TRUE;
862 mount_entry->is_user_mountable = TRUE;
864 return_list = g_list_prepend (return_list, mount_entry);
870 return g_list_reverse (return_list);
873 #elif defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
876 _g_get_unix_mount_points (void)
878 struct fstab *fstab = NULL;
879 GUnixMountPoint *mount_entry;
881 #ifdef HAVE_SYS_SYSCTL_H
883 size_t len = sizeof(usermnt);
892 #ifdef HAVE_SYS_SYSCTL_H
893 #if defined(HAVE_SYSCTLBYNAME)
894 sysctlbyname ("vfs.usermount", &usermnt, &len, NULL, 0);
895 #elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
900 mib[1] = VFS_USERMOUNT;
901 sysctl (mib, 2, &usermnt, &len, NULL, 0);
903 #elif defined(CTL_KERN) && defined(KERN_USERMOUNT)
908 mib[1] = KERN_USERMOUNT;
909 sysctl (mib, 2, &usermnt, &len, NULL, 0);
914 while ((fstab = getfsent ()) != NULL)
916 if (strcmp (fstab->fs_vfstype, "swap") == 0)
919 mount_entry = g_new0 (GUnixMountPoint, 1);
921 mount_entry->mount_path = g_strdup (fstab->fs_file);
922 mount_entry->device_path = g_strdup (fstab->fs_spec);
923 mount_entry->filesystem_type = g_strdup (fstab->fs_vfstype);
925 if (strcmp (fstab->fs_type, "ro") == 0)
926 mount_entry->is_read_only = TRUE;
928 #ifdef HAVE_SYS_SYSCTL_H
931 uid_t uid = getuid ();
932 if (stat (fstab->fs_file, &sb) == 0)
934 if (uid == 0 || sb.st_uid == uid)
935 mount_entry->is_user_mountable = TRUE;
940 return_list = g_list_prepend (return_list, mount_entry);
945 return g_list_reverse (return_list);
948 #error No g_get_mount_table() implementation for system
952 get_mounts_timestamp (void)
954 const char *monitor_file;
957 monitor_file = get_mtab_monitor_file ();
960 if (stat (monitor_file, &buf) == 0)
961 return (guint64)buf.st_mtime;
967 get_mount_points_timestamp (void)
969 const char *monitor_file;
972 monitor_file = get_fstab_file ();
975 if (stat (monitor_file, &buf) == 0)
976 return (guint64)buf.st_mtime;
983 * @time_read: guint64 to contain a timestamp.
985 * Gets a #GList of strings containing the unix mounts.
986 * If @time_read is set, it will be filled with the mount
987 * timestamp, allowing for checking if the mounts have changed
988 * with g_unix_mounts_changed_since().
990 * Returns: a #GList of the UNIX mounts.
993 g_get_unix_mounts (guint64 *time_read)
996 *time_read = get_mounts_timestamp ();
998 return _g_get_unix_mounts ();
1002 * g_get_unix_mount_at:
1003 * @mount_path: path for a possible unix mount.
1004 * @time_read: guint64 to contain a timestamp.
1006 * Gets a #GUnixMountEntry for a given mount path. If @time_read
1007 * is set, it will be filled with a unix timestamp for checking
1008 * if the mounts have changed since with g_unix_mounts_changed_since().
1010 * Returns: a #GUnixMount.
1013 g_get_unix_mount_at (const char *mount_path,
1017 GUnixMountEntry *mount_entry, *found;
1019 mounts = g_get_unix_mounts (time_read);
1022 for (l = mounts; l != NULL; l = l->next)
1024 mount_entry = l->data;
1026 if (strcmp (mount_path, mount_entry->mount_path) == 0)
1027 found = mount_entry;
1029 g_unix_mount_free (mount_entry);
1032 g_list_free (mounts);
1038 * g_get_unix_mount_points:
1039 * @time_read: guint64 to contain a timestamp.
1041 * Gets a #GList of strings containing the unix mount points.
1042 * If @time_read is set, it will be filled with the mount timestamp,
1043 * allowing for checking if the mounts have changed with
1044 * g_unix_mounts_points_changed_since().
1046 * Returns: a #GList of the UNIX mountpoints.
1049 g_get_unix_mount_points (guint64 *time_read)
1052 *time_read = get_mount_points_timestamp ();
1054 return _g_get_unix_mount_points ();
1058 * g_unix_mounts_changed_since:
1059 * @time: guint64 to contain a timestamp.
1061 * Checks if the unix mounts have changed since a given unix time.
1063 * Returns: %TRUE if the mounts have changed since @time.
1066 g_unix_mounts_changed_since (guint64 time)
1068 return get_mounts_timestamp () != time;
1072 * g_unix_mount_points_changed_since:
1073 * @time: guint64 to contain a timestamp.
1075 * Checks if the unix mount points have changed since a given unix time.
1077 * Returns: %TRUE if the mount points have changed since @time.
1080 g_unix_mount_points_changed_since (guint64 time)
1082 return get_mount_points_timestamp () != time;
1086 g_unix_mount_monitor_finalize (GObject *object)
1088 GUnixMountMonitor *monitor;
1090 monitor = G_UNIX_MOUNT_MONITOR (object);
1092 if (monitor->fstab_monitor)
1094 g_file_monitor_cancel (monitor->fstab_monitor);
1095 g_object_unref (monitor->fstab_monitor);
1098 if (monitor->mtab_monitor)
1100 g_file_monitor_cancel (monitor->mtab_monitor);
1101 g_object_unref (monitor->mtab_monitor);
1104 the_mount_monitor = NULL;
1106 if (G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize)
1107 (*G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize) (object);
1112 g_unix_mount_monitor_class_init (GUnixMountMonitorClass *klass)
1114 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1116 gobject_class->finalize = g_unix_mount_monitor_finalize;
1118 * GUnixMountMonitor::mounts-changed:
1120 * Emitted when the unix mounts have changed.
1122 signals[MOUNTS_CHANGED] =
1123 g_signal_new ("mounts_changed",
1124 G_TYPE_FROM_CLASS (klass),
1128 g_cclosure_marshal_VOID__VOID,
1131 * GUnixMountMonitor::mountpoints-changed:
1133 * Emitted when the unix mount points have changed.
1135 signals[MOUNTPOINTS_CHANGED] =
1136 g_signal_new ("mountpoints_changed",
1137 G_TYPE_FROM_CLASS (klass),
1141 g_cclosure_marshal_VOID__VOID,
1146 fstab_file_changed (GFileMonitor *monitor,
1149 GFileMonitorEvent event_type,
1152 GUnixMountMonitor *mount_monitor;
1154 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1155 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1156 event_type != G_FILE_MONITOR_EVENT_DELETED)
1159 mount_monitor = user_data;
1160 g_signal_emit (mount_monitor, signals[MOUNTPOINTS_CHANGED], 0);
1164 mtab_file_changed (GFileMonitor *monitor,
1167 GFileMonitorEvent event_type,
1170 GUnixMountMonitor *mount_monitor;
1172 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1173 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1174 event_type != G_FILE_MONITOR_EVENT_DELETED)
1177 mount_monitor = user_data;
1178 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1182 g_unix_mount_monitor_init (GUnixMountMonitor *monitor)
1186 if (get_fstab_file () != NULL)
1188 file = g_file_new_for_path (get_fstab_file ());
1189 monitor->fstab_monitor = g_file_monitor_file (file, 0, NULL);
1190 g_object_unref (file);
1192 g_signal_connect (monitor->fstab_monitor, "changed", (GCallback)fstab_file_changed, monitor);
1195 if (get_mtab_monitor_file () != NULL)
1197 file = g_file_new_for_path (get_mtab_monitor_file ());
1198 monitor->mtab_monitor = g_file_monitor_file (file, 0, NULL);
1199 g_object_unref (file);
1201 g_signal_connect (monitor->mtab_monitor, "changed", (GCallback)mtab_file_changed, monitor);
1206 * g_unix_mount_monitor_new:
1208 * Gets a new #GUnixMountMonitor.
1210 * Returns: a #GUnixMountMonitor.
1213 g_unix_mount_monitor_new (void)
1215 if (the_mount_monitor == NULL)
1217 the_mount_monitor = g_object_new (G_TYPE_UNIX_MOUNT_MONITOR, NULL);
1218 return the_mount_monitor;
1221 return g_object_ref (the_mount_monitor);
1225 * g_unix_mount_free:
1226 * @mount_entry: a #GUnixMount.
1228 * Frees a unix mount.
1231 g_unix_mount_free (GUnixMountEntry *mount_entry)
1233 g_return_if_fail (mount_entry != NULL);
1235 g_free (mount_entry->mount_path);
1236 g_free (mount_entry->device_path);
1237 g_free (mount_entry->filesystem_type);
1238 g_free (mount_entry);
1242 * g_unix_mount_point_free:
1243 * @mount_point: unix mount point to free.
1245 * Frees a unix mount point.
1248 g_unix_mount_point_free (GUnixMountPoint *mount_point)
1250 g_return_if_fail (mount_point != NULL);
1252 g_free (mount_point->mount_path);
1253 g_free (mount_point->device_path);
1254 g_free (mount_point->filesystem_type);
1255 g_free (mount_point);
1259 strcmp_null (const char *str1,
1264 if (str1 == NULL && str2 != NULL)
1266 if (str1 != NULL && str2 == NULL)
1268 return strcmp (str1, str2);
1272 * g_unix_mount_compare:
1273 * @mount1: first #GUnixMountEntry to compare.
1274 * @mount2: second #GUnixMountEntry to compare.
1276 * Compares two unix mounts.
1278 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1279 * or less than @mount2, respectively.
1282 g_unix_mount_compare (GUnixMountEntry *mount1,
1283 GUnixMountEntry *mount2)
1287 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1289 res = strcmp_null (mount1->mount_path, mount2->mount_path);
1293 res = strcmp_null (mount1->device_path, mount2->device_path);
1297 res = strcmp_null (mount1->filesystem_type, mount2->filesystem_type);
1301 res = mount1->is_read_only - mount2->is_read_only;
1309 * g_unix_mount_get_mount_path:
1310 * @mount_entry: input #GUnixMountEntry to get the mount path for.
1312 * Gets the mount path for a unix mount.
1314 * Returns: the mount path for @mount_entry.
1317 g_unix_mount_get_mount_path (GUnixMountEntry *mount_entry)
1319 g_return_val_if_fail (mount_entry != NULL, NULL);
1321 return mount_entry->mount_path;
1325 * g_unix_mount_get_device_path:
1326 * @mount_entry: a #GUnixMount.
1328 * Gets the device path for a unix mount.
1330 * Returns: a string containing the device path.
1333 g_unix_mount_get_device_path (GUnixMountEntry *mount_entry)
1335 g_return_val_if_fail (mount_entry != NULL, NULL);
1337 return mount_entry->device_path;
1341 * g_unix_mount_get_fs_type:
1342 * @mount_entry: a #GUnixMount.
1344 * Gets the filesystem type for the unix mount.
1346 * Returns: a string containing the file system type.
1349 g_unix_mount_get_fs_type (GUnixMountEntry *mount_entry)
1351 g_return_val_if_fail (mount_entry != NULL, NULL);
1353 return mount_entry->filesystem_type;
1357 * g_unix_mount_is_readonly:
1358 * @mount_entry: a #GUnixMount.
1360 * Checks if a unix mount is mounted read only.
1362 * Returns: %TRUE if @mount_entry is read only.
1365 g_unix_mount_is_readonly (GUnixMountEntry *mount_entry)
1367 g_return_val_if_fail (mount_entry != NULL, FALSE);
1369 return mount_entry->is_read_only;
1373 * g_unix_mount_is_system_internal:
1374 * @mount_entry: a #GUnixMount.
1376 * Checks if a unix mount is a system path.
1378 * Returns: %TRUE if the unix mount is for a system path.
1381 g_unix_mount_is_system_internal (GUnixMountEntry *mount_entry)
1383 g_return_val_if_fail (mount_entry != NULL, FALSE);
1385 return mount_entry->is_system_internal;
1389 * g_unix_mount_point_compare:
1390 * @mount1: a #GUnixMount.
1391 * @mount2: a #GUnixMount.
1393 * Compares two unix mount points.
1395 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1396 * or less than @mount2, respectively.
1399 g_unix_mount_point_compare (GUnixMountPoint *mount1,
1400 GUnixMountPoint *mount2)
1404 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1406 res = strcmp_null (mount1->mount_path, mount2->mount_path);
1410 res = strcmp_null (mount1->device_path, mount2->device_path);
1414 res = strcmp_null (mount1->filesystem_type, mount2->filesystem_type);
1418 res = mount1->is_read_only - mount2->is_read_only;
1422 res = mount1->is_user_mountable - mount2->is_user_mountable;
1426 res = mount1->is_loopback - mount2->is_loopback;
1434 * g_unix_mount_point_get_mount_path:
1435 * @mount_point: a #GUnixMountPoint.
1437 * Gets the mount path for a unix mount point.
1439 * Returns: a string containing the mount path.
1442 g_unix_mount_point_get_mount_path (GUnixMountPoint *mount_point)
1444 g_return_val_if_fail (mount_point != NULL, NULL);
1446 return mount_point->mount_path;
1450 * g_unix_mount_point_get_device_path:
1451 * @mount_point: a #GUnixMountPoint.
1453 * Gets the device path for a unix mount point.
1455 * Returns: a string containing the device path.
1458 g_unix_mount_point_get_device_path (GUnixMountPoint *mount_point)
1460 g_return_val_if_fail (mount_point != NULL, NULL);
1462 return mount_point->device_path;
1466 * g_unix_mount_point_get_fs_type:
1467 * @mount_point: a #GUnixMountPoint.
1469 * Gets the file system type for the mount point.
1471 * Returns: a string containing the file system type.
1474 g_unix_mount_point_get_fs_type (GUnixMountPoint *mount_point)
1476 g_return_val_if_fail (mount_point != NULL, NULL);
1478 return mount_point->filesystem_type;
1482 * g_unix_mount_point_is_readonly:
1483 * @mount_point: a #GUnixMountPoint.
1485 * Checks if a unix mount point is read only.
1487 * Returns: %TRUE if a mount point is read only.
1490 g_unix_mount_point_is_readonly (GUnixMountPoint *mount_point)
1492 g_return_val_if_fail (mount_point != NULL, FALSE);
1494 return mount_point->is_read_only;
1498 * g_unix_mount_point_is_user_mountable:
1499 * @mount_point: a #GUnixMountPoint.
1501 * Checks if a unix mount point is mountable by the user.
1503 * Returns: %TRUE if the mount point is user mountable.
1506 g_unix_mount_point_is_user_mountable (GUnixMountPoint *mount_point)
1508 g_return_val_if_fail (mount_point != NULL, FALSE);
1510 return mount_point->is_user_mountable;
1514 * g_unix_mount_point_is_loopback:
1515 * @mount_point: a #GUnixMountPoint.
1517 * Checks if a unix mount point is a loopback device.
1519 * Returns: %TRUE if the mount point is a loopback. %FALSE otherwise.
1522 g_unix_mount_point_is_loopback (GUnixMountPoint *mount_point)
1524 g_return_val_if_fail (mount_point != NULL, FALSE);
1526 return mount_point->is_loopback;
1529 static GUnixMountType
1530 guess_mount_type (const char *mount_path,
1531 const char *device_path,
1532 const char *filesystem_type)
1534 GUnixMountType type;
1537 type = G_UNIX_MOUNT_TYPE_UNKNOWN;
1539 if ((strcmp (filesystem_type, "udf") == 0) ||
1540 (strcmp (filesystem_type, "iso9660") == 0) ||
1541 (strcmp (filesystem_type, "cd9660") == 0))
1542 type = G_UNIX_MOUNT_TYPE_CDROM;
1543 else if (strcmp (filesystem_type, "nfs") == 0)
1544 type = G_UNIX_MOUNT_TYPE_NFS;
1545 else if (g_str_has_prefix (device_path, "/vol/dev/diskette/") ||
1546 g_str_has_prefix (device_path, "/dev/fd") ||
1547 g_str_has_prefix (device_path, "/dev/floppy"))
1548 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1549 else if (g_str_has_prefix (device_path, "/dev/cdrom") ||
1550 g_str_has_prefix (device_path, "/dev/acd") ||
1551 g_str_has_prefix (device_path, "/dev/cd"))
1552 type = G_UNIX_MOUNT_TYPE_CDROM;
1553 else if (g_str_has_prefix (device_path, "/vol/"))
1555 const char *name = mount_path + strlen ("/");
1557 if (g_str_has_prefix (name, "cdrom"))
1558 type = G_UNIX_MOUNT_TYPE_CDROM;
1559 else if (g_str_has_prefix (name, "floppy") ||
1560 g_str_has_prefix (device_path, "/vol/dev/diskette/"))
1561 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1562 else if (g_str_has_prefix (name, "rmdisk"))
1563 type = G_UNIX_MOUNT_TYPE_ZIP;
1564 else if (g_str_has_prefix (name, "jaz"))
1565 type = G_UNIX_MOUNT_TYPE_JAZ;
1566 else if (g_str_has_prefix (name, "memstick"))
1567 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1571 basename = g_path_get_basename (mount_path);
1573 if (g_str_has_prefix (basename, "cdrom") ||
1574 g_str_has_prefix (basename, "cdwriter") ||
1575 g_str_has_prefix (basename, "burn") ||
1576 g_str_has_prefix (basename, "cdr") ||
1577 g_str_has_prefix (basename, "cdrw") ||
1578 g_str_has_prefix (basename, "dvdrom") ||
1579 g_str_has_prefix (basename, "dvdram") ||
1580 g_str_has_prefix (basename, "dvdr") ||
1581 g_str_has_prefix (basename, "dvdrw") ||
1582 g_str_has_prefix (basename, "cdrom_dvdrom") ||
1583 g_str_has_prefix (basename, "cdrom_dvdram") ||
1584 g_str_has_prefix (basename, "cdrom_dvdr") ||
1585 g_str_has_prefix (basename, "cdrom_dvdrw") ||
1586 g_str_has_prefix (basename, "cdr_dvdrom") ||
1587 g_str_has_prefix (basename, "cdr_dvdram") ||
1588 g_str_has_prefix (basename, "cdr_dvdr") ||
1589 g_str_has_prefix (basename, "cdr_dvdrw") ||
1590 g_str_has_prefix (basename, "cdrw_dvdrom") ||
1591 g_str_has_prefix (basename, "cdrw_dvdram") ||
1592 g_str_has_prefix (basename, "cdrw_dvdr") ||
1593 g_str_has_prefix (basename, "cdrw_dvdrw"))
1594 type = G_UNIX_MOUNT_TYPE_CDROM;
1595 else if (g_str_has_prefix (basename, "floppy"))
1596 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1597 else if (g_str_has_prefix (basename, "zip"))
1598 type = G_UNIX_MOUNT_TYPE_ZIP;
1599 else if (g_str_has_prefix (basename, "jaz"))
1600 type = G_UNIX_MOUNT_TYPE_JAZ;
1601 else if (g_str_has_prefix (basename, "camera"))
1602 type = G_UNIX_MOUNT_TYPE_CAMERA;
1603 else if (g_str_has_prefix (basename, "memstick") ||
1604 g_str_has_prefix (basename, "memory_stick") ||
1605 g_str_has_prefix (basename, "ram"))
1606 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1607 else if (g_str_has_prefix (basename, "compact_flash"))
1608 type = G_UNIX_MOUNT_TYPE_CF;
1609 else if (g_str_has_prefix (basename, "smart_media"))
1610 type = G_UNIX_MOUNT_TYPE_SM;
1611 else if (g_str_has_prefix (basename, "sd_mmc"))
1612 type = G_UNIX_MOUNT_TYPE_SDMMC;
1613 else if (g_str_has_prefix (basename, "ipod"))
1614 type = G_UNIX_MOUNT_TYPE_IPOD;
1619 if (type == G_UNIX_MOUNT_TYPE_UNKNOWN)
1620 type = G_UNIX_MOUNT_TYPE_HD;
1626 * g_unix_mount_guess_type:
1627 * @mount_entry: a #GUnixMount.
1629 * Guesses the type of a unix mount. If the mount type cannot be
1630 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1632 * Returns: a #GUnixMountType.
1635 g_unix_mount_guess_type (GUnixMountEntry *mount_entry)
1637 g_return_val_if_fail (mount_entry != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1638 g_return_val_if_fail (mount_entry->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1639 g_return_val_if_fail (mount_entry->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1640 g_return_val_if_fail (mount_entry->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1642 return guess_mount_type (mount_entry->mount_path,
1643 mount_entry->device_path,
1644 mount_entry->filesystem_type);
1648 * g_unix_mount_point_guess_type:
1649 * @mount_point: a #GUnixMountPoint.
1651 * Guesses the type of a unix mount point. If the mount type cannot be
1652 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1654 * Returns: a #GUnixMountType.
1657 g_unix_mount_point_guess_type (GUnixMountPoint *mount_point)
1659 g_return_val_if_fail (mount_point != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1660 g_return_val_if_fail (mount_point->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1661 g_return_val_if_fail (mount_point->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1662 g_return_val_if_fail (mount_point->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1664 return guess_mount_type (mount_point->mount_path,
1665 mount_point->device_path,
1666 mount_point->filesystem_type);
1670 type_to_icon (GUnixMountType type, gboolean is_mount_point)
1672 const char *icon_name;
1676 case G_UNIX_MOUNT_TYPE_HD:
1678 icon_name = "drive-removable-media";
1680 icon_name = "drive-harddisk";
1682 case G_UNIX_MOUNT_TYPE_FLOPPY:
1683 case G_UNIX_MOUNT_TYPE_ZIP:
1684 case G_UNIX_MOUNT_TYPE_JAZ:
1686 icon_name = "drive-removable-media";
1688 icon_name = "media-floppy";
1690 case G_UNIX_MOUNT_TYPE_CDROM:
1692 icon_name = "drive-optical";
1694 icon_name = "media-optical";
1696 case G_UNIX_MOUNT_TYPE_NFS:
1697 /* TODO: Would like a better icon here... */
1699 icon_name = "drive-removable-media";
1701 icon_name = "drive-harddisk";
1703 case G_UNIX_MOUNT_TYPE_MEMSTICK:
1705 icon_name = "drive-removable-media";
1707 icon_name = "media-flash";
1709 case G_UNIX_MOUNT_TYPE_CAMERA:
1711 icon_name = "drive-removable-media";
1713 icon_name = "camera-photo";
1715 case G_UNIX_MOUNT_TYPE_IPOD:
1717 icon_name = "drive-removable-media";
1719 icon_name = "multimedia-player";
1721 case G_UNIX_MOUNT_TYPE_UNKNOWN:
1724 icon_name = "drive-removable-media";
1726 icon_name = "drive-harddisk";
1734 g_unix_mount_guess_name (GUnixMountEntry *mount_entry)
1738 if (strcmp (mount_entry->mount_path, "/") == 0)
1739 name = g_strdup (_("Filesystem root"));
1741 name = g_filename_display_basename (mount_entry->mount_path);
1747 g_unix_mount_guess_icon (GUnixMountEntry *mount_entry)
1749 return g_themed_icon_new (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE));
1753 g_unix_mount_point_guess_name (GUnixMountPoint *mount_point)
1757 if (strcmp (mount_point->mount_path, "/") == 0)
1758 name = g_strdup (_("Filesystem root"));
1760 name = g_filename_display_basename (mount_point->mount_path);
1766 g_unix_mount_point_guess_icon (GUnixMountPoint *mount_point)
1768 return g_themed_icon_new (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE));
1772 /* borrowed from gtk/gtkfilesystemunix.c in GTK+ on 02/23/2006 */
1774 _canonicalize_filename (gchar *filename)
1777 gboolean last_was_slash = FALSE;
1784 if (*p == G_DIR_SEPARATOR)
1786 if (!last_was_slash)
1787 *q++ = G_DIR_SEPARATOR;
1789 last_was_slash = TRUE;
1793 if (last_was_slash && *p == '.')
1795 if (*(p + 1) == G_DIR_SEPARATOR ||
1798 if (*(p + 1) == '\0')
1803 else if (*(p + 1) == '.' &&
1804 (*(p + 2) == G_DIR_SEPARATOR ||
1807 if (q > filename + 1)
1810 while (q > filename + 1 &&
1811 *(q - 1) != G_DIR_SEPARATOR)
1815 if (*(p + 2) == '\0')
1823 last_was_slash = FALSE;
1829 last_was_slash = FALSE;
1836 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
1843 _resolve_symlink (const char *file)
1851 f = g_strdup (file);
1853 while (g_file_test (f, G_FILE_TEST_IS_SYMLINK)) {
1854 link = g_file_read_link (f, &error);
1856 g_error_free (error);
1862 dir = g_path_get_dirname (f);
1863 f1 = g_strdup_printf ("%s/%s", dir, link);
1872 _canonicalize_filename (f);
1877 _resolve_dev_root (void)
1879 static gboolean have_real_dev_root = FALSE;
1880 static char real_dev_root[256];
1881 struct stat statbuf;
1883 /* see if it's cached already */
1884 if (have_real_dev_root)
1887 /* otherwise we're going to find it right away.. */
1888 have_real_dev_root = TRUE;
1890 if (stat ("/dev/root", &statbuf) == 0) {
1891 if (! S_ISLNK (statbuf.st_mode)) {
1892 dev_t root_dev = statbuf.st_dev;
1896 /* see if device with similar major:minor as /dev/root is mention
1897 * in /etc/mtab (it usually is)
1899 f = fopen ("/etc/mtab", "r");
1903 while (getmntent_r (f, &ent, buf, sizeof (buf)) != NULL) {
1905 if (stat (ent.mnt_fsname, &statbuf) == 0 &&
1906 statbuf.st_dev == root_dev) {
1907 strncpy (real_dev_root, ent.mnt_fsname, sizeof (real_dev_root) - 1);
1908 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
1916 /* no, that didn't work.. next we could scan /dev ... but I digress.. */
1920 resolved = _resolve_symlink ("/dev/root");
1921 if (resolved != NULL) {
1922 strncpy (real_dev_root, resolved, sizeof (real_dev_root) - 1);
1923 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
1931 strcpy (real_dev_root, "/dev/root");
1934 return real_dev_root;
1937 #define __G_UNIX_MOUNTS_C__
1938 #include "gioaliasdef.c"