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
41 #if HAVE_SYS_STATVFS_H
42 #include <sys/statvfs.h>
53 #include "gunixmounts.h"
55 #include "gfilemonitor.h"
57 #include "gthemedicon.h"
60 static const char *_resolve_dev_root (void);
64 * @include: gio/gunixmounts.h
65 * @short_description: UNIX mounts
67 * Routines for managing mounted UNIX mount points and paths.
69 * Note that <filename><gio/gunixmounts.h></filename> belongs to the
70 * UNIX-specific GIO interfaces, thus you have to use the
71 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
76 * @G_UNIX_MOUNT_TYPE_UNKNOWN: Unknown UNIX mount type.
77 * @G_UNIX_MOUNT_TYPE_FLOPPY: Floppy disk UNIX mount type.
78 * @G_UNIX_MOUNT_TYPE_CDROM: CDROM UNIX mount type.
79 * @G_UNIX_MOUNT_TYPE_NFS: Network File System (NFS) UNIX mount type.
80 * @G_UNIX_MOUNT_TYPE_ZIP: ZIP UNIX mount type.
81 * @G_UNIX_MOUNT_TYPE_JAZ: JAZZ UNIX mount type.
82 * @G_UNIX_MOUNT_TYPE_MEMSTICK: Memory Stick UNIX mount type.
83 * @G_UNIX_MOUNT_TYPE_CF: Compact Flash UNIX mount type.
84 * @G_UNIX_MOUNT_TYPE_SM: Smart Media UNIX mount type.
85 * @G_UNIX_MOUNT_TYPE_SDMMC: SD/MMC UNIX mount type.
86 * @G_UNIX_MOUNT_TYPE_IPOD: iPod UNIX mount type.
87 * @G_UNIX_MOUNT_TYPE_CAMERA: Digital camera UNIX mount type.
88 * @G_UNIX_MOUNT_TYPE_HD: Hard drive UNIX mount type.
90 * Types of UNIX mounts.
93 G_UNIX_MOUNT_TYPE_UNKNOWN
,
94 G_UNIX_MOUNT_TYPE_FLOPPY
,
95 G_UNIX_MOUNT_TYPE_CDROM
,
96 G_UNIX_MOUNT_TYPE_NFS
,
97 G_UNIX_MOUNT_TYPE_ZIP
,
98 G_UNIX_MOUNT_TYPE_JAZ
,
99 G_UNIX_MOUNT_TYPE_MEMSTICK
,
100 G_UNIX_MOUNT_TYPE_CF
,
101 G_UNIX_MOUNT_TYPE_SM
,
102 G_UNIX_MOUNT_TYPE_SDMMC
,
103 G_UNIX_MOUNT_TYPE_IPOD
,
104 G_UNIX_MOUNT_TYPE_CAMERA
,
108 struct _GUnixMountEntry
{
111 char *filesystem_type
;
112 gboolean is_read_only
;
113 gboolean is_system_internal
;
116 struct _GUnixMountPoint
{
119 char *filesystem_type
;
120 gboolean is_read_only
;
121 gboolean is_user_mountable
;
122 gboolean is_loopback
;
131 static guint signals
[LAST_SIGNAL
];
133 struct _GUnixMountMonitor
{
136 GFileMonitor
*fstab_monitor
;
137 GFileMonitor
*mtab_monitor
;
140 struct _GUnixMountMonitorClass
{
141 GObjectClass parent_class
;
144 static GUnixMountMonitor
*the_mount_monitor
= NULL
;
146 static GList
*_g_get_unix_mounts (void);
147 static GList
*_g_get_unix_mount_points (void);
149 G_DEFINE_TYPE (GUnixMountMonitor
, g_unix_mount_monitor
, G_TYPE_OBJECT
);
151 #define MOUNT_POLL_INTERVAL 4000
153 #ifdef HAVE_SYS_MNTTAB_H
154 #define MNTOPT_RO "ro"
159 #elif defined (HAVE_SYS_MNTTAB_H)
160 #include <sys/mnttab.h>
163 #ifdef HAVE_SYS_VFSTAB_H
164 #include <sys/vfstab.h>
167 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
168 #include <sys/mntctl.h>
170 #include <sys/vmount.h>
174 #if defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
175 #include <sys/ucred.h>
176 #include <sys/mount.h>
178 #ifdef HAVE_SYS_SYSCTL_H
179 #include <sys/sysctl.h>
183 #ifndef HAVE_SETMNTENT
184 #define setmntent(f,m) fopen(f,m)
186 #ifndef HAVE_ENDMNTENT
187 #define endmntent(f) fclose(f)
191 is_in (const char *value
, const char *set
[])
194 for (i
= 0; set
[i
] != NULL
; i
++)
196 if (strcmp (set
[i
], value
) == 0)
203 * g_unix_is_mount_path_system_internal:
204 * @mount_path: a mount path, e.g. <filename>/media/disk</filename>
205 * or <filename>/usr</filename>
207 * Determines if @mount_path is considered an implementation of the
208 * OS. This is primarily used for hiding mountable and mounted volumes
209 * that only are used in the OS and has little to no relevance to the
212 * Returns: %TRUE if @mount_path is considered an implementation detail
216 g_unix_is_mount_path_system_internal (const char *mount_path
)
218 const char *ignore_mountpoints
[] = {
219 /* Includes all FHS 2.3 toplevel dirs and other specilized
220 * directories that we want to hide from the user.
222 "/", /* we already have "Filesystem root" in Nautilus */
240 "/var/log/audit", /* https://bugzilla.redhat.com/show_bug.cgi?id=333041 */
241 "/var/tmp", /* https://bugzilla.redhat.com/show_bug.cgi?id=335241 */
249 if (is_in (mount_path
, ignore_mountpoints
))
252 if (g_str_has_prefix (mount_path
, "/dev/") ||
253 g_str_has_prefix (mount_path
, "/proc/") ||
254 g_str_has_prefix (mount_path
, "/sys/"))
257 if (g_str_has_suffix (mount_path
, "/.gvfs"))
264 guess_system_internal (const char *mountpoint
,
268 const char *ignore_fs
[] = {
289 const char *ignore_devices
[] = {
299 if (is_in (fs
, ignore_fs
))
302 if (is_in (device
, ignore_devices
))
305 if (g_unix_is_mount_path_system_internal (mountpoint
))
314 get_mtab_read_file (void)
318 return "/proc/mounts";
320 return _PATH_MOUNTED
;
328 get_mtab_monitor_file (void)
331 return _PATH_MOUNTED
;
337 #ifndef HAVE_GETMNTENT_R
338 G_LOCK_DEFINE_STATIC(getmntent
);
342 _g_get_unix_mounts (void)
344 #ifdef HAVE_GETMNTENT_R
348 struct mntent
*mntent
;
351 GUnixMountEntry
*mount_entry
;
352 GHashTable
*mounts_hash
;
355 read_file
= get_mtab_read_file ();
357 file
= setmntent (read_file
, "r");
363 mounts_hash
= g_hash_table_new (g_str_hash
, g_str_equal
);
365 #ifdef HAVE_GETMNTENT_R
366 while ((mntent
= getmntent_r (file
, &ent
, buf
, sizeof (buf
))) != NULL
)
369 while ((mntent
= getmntent (file
)) != NULL
)
372 /* ignore any mnt_fsname that is repeated and begins with a '/'
374 * We do this to avoid being fooled by --bind mounts, since
375 * these have the same device as the location they bind to.
376 * It's not an ideal solution to the problem, but it's likely that
377 * the most important mountpoint is first and the --bind ones after
378 * that aren't as important. So it should work.
380 * The '/' is to handle procfs, tmpfs and other no device mounts.
382 if (mntent
->mnt_fsname
!= NULL
&&
383 mntent
->mnt_fsname
[0] == '/' &&
384 g_hash_table_lookup (mounts_hash
, mntent
->mnt_fsname
))
387 mount_entry
= g_new0 (GUnixMountEntry
, 1);
388 mount_entry
->mount_path
= g_strdup (mntent
->mnt_dir
);
389 if (strcmp (mntent
->mnt_fsname
, "/dev/root") == 0)
390 mount_entry
->device_path
= g_strdup (_resolve_dev_root ());
392 mount_entry
->device_path
= g_strdup (mntent
->mnt_fsname
);
393 mount_entry
->filesystem_type
= g_strdup (mntent
->mnt_type
);
395 #if defined (HAVE_HASMNTOPT)
396 if (hasmntopt (mntent
, MNTOPT_RO
) != NULL
)
397 mount_entry
->is_read_only
= TRUE
;
400 mount_entry
->is_system_internal
=
401 guess_system_internal (mount_entry
->mount_path
,
402 mount_entry
->filesystem_type
,
403 mount_entry
->device_path
);
405 g_hash_table_insert (mounts_hash
,
406 mount_entry
->device_path
,
407 mount_entry
->device_path
);
409 return_list
= g_list_prepend (return_list
, mount_entry
);
411 g_hash_table_destroy (mounts_hash
);
415 #ifndef HAVE_GETMNTENT_R
416 G_UNLOCK (getmntent
);
419 return g_list_reverse (return_list
);
422 #elif defined (HAVE_SYS_MNTTAB_H)
424 G_LOCK_DEFINE_STATIC(getmntent
);
427 get_mtab_read_file (void)
430 return _PATH_MOUNTED
;
432 return "/etc/mnttab";
437 get_mtab_monitor_file (void)
439 return get_mtab_read_file ();
443 _g_get_unix_mounts (void)
445 struct mnttab mntent
;
448 GUnixMountEntry
*mount_entry
;
451 read_file
= get_mtab_read_file ();
453 file
= setmntent (read_file
, "r");
460 while (! getmntent (file
, &mntent
))
462 mount_entry
= g_new0 (GUnixMountEntry
, 1);
464 mount_entry
->mount_path
= g_strdup (mntent
.mnt_mountp
);
465 mount_entry
->device_path
= g_strdup (mntent
.mnt_special
);
466 mount_entry
->filesystem_type
= g_strdup (mntent
.mnt_fstype
);
468 #if defined (HAVE_HASMNTOPT)
469 if (hasmntopt (&mntent
, MNTOPT_RO
) != NULL
)
470 mount_entry
->is_read_only
= TRUE
;
473 mount_entry
->is_system_internal
=
474 guess_system_internal (mount_entry
->mount_path
,
475 mount_entry
->filesystem_type
,
476 mount_entry
->device_path
);
478 return_list
= g_list_prepend (return_list
, mount_entry
);
483 G_UNLOCK (getmntent
);
485 return g_list_reverse (return_list
);
488 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
491 get_mtab_monitor_file (void)
497 _g_get_unix_mounts (void)
499 struct vfs_ent
*fs_info
;
500 struct vmount
*vmount_info
;
502 unsigned int vmount_size
;
506 if (mntctl (MCTL_QUERY
, sizeof (vmount_size
), &vmount_size
) != 0)
508 g_warning ("Unable to know the number of mounted volumes\n");
513 vmount_info
= (struct vmount
*)g_malloc (vmount_size
);
515 vmount_number
= mntctl (MCTL_QUERY
, vmount_size
, vmount_info
);
517 if (vmount_info
->vmt_revision
!= VMT_REVISION
)
518 g_warning ("Bad vmount structure revision number, want %d, got %d\n", VMT_REVISION
, vmount_info
->vmt_revision
);
520 if (vmount_number
< 0)
522 g_warning ("Unable to recover mounted volumes information\n");
524 g_free (vmount_info
);
529 while (vmount_number
> 0)
531 mount_entry
= g_new0 (GUnixMountEntry
, 1);
533 mount_entry
->device_path
= g_strdup (vmt2dataptr (vmount_info
, VMT_OBJECT
));
534 mount_entry
->mount_path
= g_strdup (vmt2dataptr (vmount_info
, VMT_STUB
));
535 /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
536 mount_entry
->is_read_only
= (vmount_info
->vmt_flags
& MNT_READONLY
) ? 1 : 0;
538 fs_info
= getvfsbytype (vmount_info
->vmt_gfstype
);
541 mount_entry
->filesystem_type
= g_strdup ("unknown");
543 mount_entry
->filesystem_type
= g_strdup (fs_info
->vfsent_name
);
545 mount_entry
->is_system_internal
=
546 guess_system_internal (mount_entry
->mount_path
,
547 mount_entry
->filesystem_type
,
548 mount_entry
->device_path
);
550 return_list
= g_list_prepend (return_list
, mount_entry
);
552 vmount_info
= (struct vmount
*)( (char*)vmount_info
553 + vmount_info
->vmt_length
);
557 g_free (vmount_info
);
559 return g_list_reverse (return_list
);
562 #elif defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
565 get_mtab_monitor_file (void)
571 _g_get_unix_mounts (void)
573 struct statfs
*mntent
= NULL
;
575 GUnixMountEntry
*mount_entry
;
578 /* Pass MNT_NOWAIT to avoid blocking trying to update NFS mounts. */
579 if ((num_mounts
= getmntinfo (&mntent
, MNT_NOWAIT
)) == 0)
584 for (i
= 0; i
< num_mounts
; i
++)
586 mount_entry
= g_new0 (GUnixMountEntry
, 1);
588 mount_entry
->mount_path
= g_strdup (mntent
[i
].f_mntonname
);
589 mount_entry
->device_path
= g_strdup (mntent
[i
].f_mntfromname
);
590 mount_entry
->filesystem_type
= g_strdup (mntent
[i
].f_fstypename
);
591 if (mntent
[i
].f_flags
& MNT_RDONLY
)
592 mount_entry
->is_read_only
= TRUE
;
594 mount_entry
->is_system_internal
=
595 guess_system_internal (mount_entry
->mount_path
,
596 mount_entry
->filesystem_type
,
597 mount_entry
->device_path
);
599 return_list
= g_list_prepend (return_list
, mount_entry
);
602 return g_list_reverse (return_list
);
604 #elif defined(__INTERIX)
607 get_mtab_monitor_file (void)
613 _g_get_unix_mounts (void)
616 GList
* return_list
= NULL
;
617 char filename
[9 + NAME_MAX
];
619 dirp
= opendir ("/dev/fs");
622 g_warning ("unable to read /dev/fs!");
628 struct statvfs statbuf
;
630 struct dirent
* result
;
632 if (readdir_r (dirp
, &entry
, &result
) || result
== NULL
)
635 strcpy (filename
, "/dev/fs/");
636 strcat (filename
, entry
.d_name
);
638 if (statvfs (filename
, &statbuf
) == 0)
640 GUnixMountEntry
* mount_entry
= g_new0(GUnixMountEntry
, 1);
642 mount_entry
->mount_path
= g_strdup (statbuf
.f_mntonname
);
643 mount_entry
->device_path
= g_strdup (statbuf
.f_mntfromname
);
644 mount_entry
->filesystem_type
= g_strdup (statbuf
.f_fstypename
);
646 if (statbuf
.f_flag
& ST_RDONLY
)
647 mount_entry
->is_read_only
= TRUE
;
649 return_list
= g_list_prepend(return_list
, mount_entry
);
653 return_list
= g_list_reverse (return_list
);
660 #error No _g_get_unix_mounts() implementation for system
663 /* _g_get_unix_mount_points():
665 * don't return swap and ignore mounts.
669 get_fstab_file (void)
671 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
673 return "/etc/filesystems";
674 #elif defined(_PATH_MNTTAB)
676 #elif defined(VFSTAB)
685 _g_get_unix_mount_points (void)
687 #ifdef HAVE_GETMNTENT_R
691 struct mntent
*mntent
;
694 GUnixMountPoint
*mount_entry
;
697 read_file
= get_fstab_file ();
699 file
= setmntent (read_file
, "r");
705 #ifdef HAVE_GETMNTENT_R
706 while ((mntent
= getmntent_r (file
, &ent
, buf
, sizeof (buf
))) != NULL
)
709 while ((mntent
= getmntent (file
)) != NULL
)
712 if ((strcmp (mntent
->mnt_dir
, "ignore") == 0) ||
713 (strcmp (mntent
->mnt_dir
, "swap") == 0))
716 mount_entry
= g_new0 (GUnixMountPoint
, 1);
717 mount_entry
->mount_path
= g_strdup (mntent
->mnt_dir
);
718 if (strcmp (mntent
->mnt_fsname
, "/dev/root") == 0)
719 mount_entry
->device_path
= g_strdup (_resolve_dev_root ());
721 mount_entry
->device_path
= g_strdup (mntent
->mnt_fsname
);
722 mount_entry
->filesystem_type
= g_strdup (mntent
->mnt_type
);
724 #ifdef HAVE_HASMNTOPT
725 if (hasmntopt (mntent
, MNTOPT_RO
) != NULL
)
726 mount_entry
->is_read_only
= TRUE
;
728 if (hasmntopt (mntent
, "loop") != NULL
)
729 mount_entry
->is_loopback
= TRUE
;
733 if ((mntent
->mnt_type
!= NULL
&& strcmp ("supermount", mntent
->mnt_type
) == 0)
734 #ifdef HAVE_HASMNTOPT
735 || (hasmntopt (mntent
, "user") != NULL
736 && hasmntopt (mntent
, "user") != hasmntopt (mntent
, "user_xattr"))
737 || hasmntopt (mntent
, "pamconsole") != NULL
738 || hasmntopt (mntent
, "users") != NULL
739 || hasmntopt (mntent
, "owner") != NULL
742 mount_entry
->is_user_mountable
= TRUE
;
744 return_list
= g_list_prepend (return_list
, mount_entry
);
749 #ifndef HAVE_GETMNTENT_R
750 G_UNLOCK (getmntent
);
753 return g_list_reverse (return_list
);
756 #elif defined (HAVE_SYS_MNTTAB_H)
759 _g_get_unix_mount_points (void)
761 struct mnttab mntent
;
764 GUnixMountPoint
*mount_entry
;
767 read_file
= get_fstab_file ();
769 file
= setmntent (read_file
, "r");
776 while (! getmntent (file
, &mntent
))
778 if ((strcmp (mntent
.mnt_mountp
, "ignore") == 0) ||
779 (strcmp (mntent
.mnt_mountp
, "swap") == 0))
782 mount_entry
= g_new0 (GUnixMountPoint
, 1);
784 mount_entry
->mount_path
= g_strdup (mntent
.mnt_mountp
);
785 mount_entry
->device_path
= g_strdup (mntent
.mnt_special
);
786 mount_entry
->filesystem_type
= g_strdup (mntent
.mnt_fstype
);
788 #ifdef HAVE_HASMNTOPT
789 if (hasmntopt (&mntent
, MNTOPT_RO
) != NULL
)
790 mount_entry
->is_read_only
= TRUE
;
792 if (hasmntopt (&mntent
, "lofs") != NULL
)
793 mount_entry
->is_loopback
= TRUE
;
796 if ((mntent
.mnt_fstype
!= NULL
)
797 #ifdef HAVE_HASMNTOPT
798 || (hasmntopt (&mntent
, "user") != NULL
799 && hasmntopt (&mntent
, "user") != hasmntopt (&mntent
, "user_xattr"))
800 || hasmntopt (&mntent
, "pamconsole") != NULL
801 || hasmntopt (&mntent
, "users") != NULL
802 || hasmntopt (&mntent
, "owner") != NULL
805 mount_entry
->is_user_mountable
= TRUE
;
807 return_list
= g_list_prepend (return_list
, mount_entry
);
811 G_UNLOCK (getmntent
);
813 return g_list_reverse (return_list
);
815 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
817 /* functions to parse /etc/filesystems on aix */
819 /* read character, ignoring comments (begin with '*', end with '\n' */
821 aix_fs_getc (FILE *fd
)
825 while ((c
= getc (fd
)) == '*')
827 while (((c
= getc (fd
)) != '\n') && (c
!= EOF
))
832 /* eat all continuous spaces in a file */
834 aix_fs_ignorespace (FILE *fd
)
838 while ((c
= aix_fs_getc (fd
)) != EOF
)
840 if (!g_ascii_isspace (c
))
850 /* read one word from file */
852 aix_fs_getword (FILE *fd
,
857 aix_fs_ignorespace (fd
);
859 while (((c
= aix_fs_getc (fd
)) != EOF
) && !g_ascii_isspace (c
))
863 while (((c
= aix_fs_getc (fd
)) != EOF
) && (c
!= '"'))
875 char mnt_mount
[PATH_MAX
];
876 char mnt_special
[PATH_MAX
];
878 char mnt_options
[128];
879 } AixMountTableEntry
;
881 /* read mount points properties */
883 aix_fs_get (FILE *fd
,
884 AixMountTableEntry
*prop
)
886 static char word
[PATH_MAX
] = { 0 };
887 char value
[PATH_MAX
];
892 if (aix_fs_getword (fd
, word
) == EOF
)
896 word
[strlen(word
) - 1] = 0;
897 strcpy (prop
->mnt_mount
, word
);
899 /* read attributes and value */
901 while (aix_fs_getword (fd
, word
) != EOF
)
903 /* test if is attribute or new stanza */
904 if (word
[strlen(word
) - 1] == ':')
908 aix_fs_getword (fd
, value
);
911 aix_fs_getword (fd
, value
);
913 if (strcmp (word
, "dev") == 0)
914 strcpy (prop
->mnt_special
, value
);
915 else if (strcmp (word
, "vfs") == 0)
916 strcpy (prop
->mnt_fstype
, value
);
917 else if (strcmp (word
, "options") == 0)
918 strcpy(prop
->mnt_options
, value
);
925 _g_get_unix_mount_points (void)
927 struct mntent
*mntent
;
930 GUnixMountPoint
*mount_entry
;
931 AixMountTableEntry mntent
;
934 read_file
= get_fstab_file ();
936 file
= setmntent (read_file
, "r");
942 while (!aix_fs_get (file
, &mntent
))
944 if (strcmp ("cdrfs", mntent
.mnt_fstype
) == 0)
946 mount_entry
= g_new0 (GUnixMountPoint
, 1);
948 mount_entry
->mount_path
= g_strdup (mntent
.mnt_mount
);
949 mount_entry
->device_path
= g_strdup (mntent
.mnt_special
);
950 mount_entry
->filesystem_type
= g_strdup (mntent
.mnt_fstype
);
951 mount_entry
->is_read_only
= TRUE
;
952 mount_entry
->is_user_mountable
= TRUE
;
954 return_list
= g_list_prepend (return_list
, mount_entry
);
960 return g_list_reverse (return_list
);
963 #elif defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
966 _g_get_unix_mount_points (void)
968 struct fstab
*fstab
= NULL
;
969 GUnixMountPoint
*mount_entry
;
971 #ifdef HAVE_SYS_SYSCTL_H
973 size_t len
= sizeof(usermnt
);
982 #ifdef HAVE_SYS_SYSCTL_H
983 #if defined(HAVE_SYSCTLBYNAME)
984 sysctlbyname ("vfs.usermount", &usermnt
, &len
, NULL
, 0);
985 #elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
990 mib
[1] = VFS_USERMOUNT
;
991 sysctl (mib
, 2, &usermnt
, &len
, NULL
, 0);
993 #elif defined(CTL_KERN) && defined(KERN_USERMOUNT)
998 mib
[1] = KERN_USERMOUNT
;
999 sysctl (mib
, 2, &usermnt
, &len
, NULL
, 0);
1004 while ((fstab
= getfsent ()) != NULL
)
1006 if (strcmp (fstab
->fs_vfstype
, "swap") == 0)
1009 mount_entry
= g_new0 (GUnixMountPoint
, 1);
1011 mount_entry
->mount_path
= g_strdup (fstab
->fs_file
);
1012 mount_entry
->device_path
= g_strdup (fstab
->fs_spec
);
1013 mount_entry
->filesystem_type
= g_strdup (fstab
->fs_vfstype
);
1015 if (strcmp (fstab
->fs_type
, "ro") == 0)
1016 mount_entry
->is_read_only
= TRUE
;
1018 #ifdef HAVE_SYS_SYSCTL_H
1021 uid_t uid
= getuid ();
1022 if (stat (fstab
->fs_file
, &sb
) == 0)
1024 if (uid
== 0 || sb
.st_uid
== uid
)
1025 mount_entry
->is_user_mountable
= TRUE
;
1030 return_list
= g_list_prepend (return_list
, mount_entry
);
1035 return g_list_reverse (return_list
);
1037 #elif defined(__INTERIX)
1039 _g_get_unix_mount_points (void)
1041 return _g_get_unix_mounts ();
1044 #error No g_get_mount_table() implementation for system
1048 get_mounts_timestamp (void)
1050 const char *monitor_file
;
1053 monitor_file
= get_mtab_monitor_file ();
1056 if (stat (monitor_file
, &buf
) == 0)
1057 return (guint64
)buf
.st_mtime
;
1063 get_mount_points_timestamp (void)
1065 const char *monitor_file
;
1068 monitor_file
= get_fstab_file ();
1071 if (stat (monitor_file
, &buf
) == 0)
1072 return (guint64
)buf
.st_mtime
;
1078 * g_unix_mounts_get:
1079 * @time_read: (allow-none): guint64 to contain a timestamp, or %NULL
1081 * Gets a #GList of #GUnixMountEntry containing the unix mounts.
1082 * If @time_read is set, it will be filled with the mount
1083 * timestamp, allowing for checking if the mounts have changed
1084 * with g_unix_mounts_changed_since().
1086 * Returns: a #GList of the UNIX mounts.
1089 g_unix_mounts_get (guint64
*time_read
)
1092 *time_read
= get_mounts_timestamp ();
1094 return _g_get_unix_mounts ();
1099 * @mount_path: path for a possible unix mount.
1100 * @time_read: guint64 to contain a timestamp.
1102 * Gets a #GUnixMountEntry for a given mount path. If @time_read
1103 * is set, it will be filled with a unix timestamp for checking
1104 * if the mounts have changed since with g_unix_mounts_changed_since().
1106 * Returns: a #GUnixMount.
1109 g_unix_mount_at (const char *mount_path
,
1113 GUnixMountEntry
*mount_entry
, *found
;
1115 mounts
= g_unix_mounts_get (time_read
);
1118 for (l
= mounts
; l
!= NULL
; l
= l
->next
)
1120 mount_entry
= l
->data
;
1122 if (!found
&& strcmp (mount_path
, mount_entry
->mount_path
) == 0)
1123 found
= mount_entry
;
1125 g_unix_mount_free (mount_entry
);
1127 g_list_free (mounts
);
1133 * g_unix_mount_points_get:
1134 * @time_read: (allow-none): guint64 to contain a timestamp.
1136 * Gets a #GList of #GUnixMountPoint containing the unix mount points.
1137 * If @time_read is set, it will be filled with the mount timestamp,
1138 * allowing for checking if the mounts have changed with
1139 * g_unix_mounts_points_changed_since().
1141 * Returns: a #GList of the UNIX mountpoints.
1144 g_unix_mount_points_get (guint64
*time_read
)
1147 *time_read
= get_mount_points_timestamp ();
1149 return _g_get_unix_mount_points ();
1153 * g_unix_mounts_changed_since:
1154 * @time: guint64 to contain a timestamp.
1156 * Checks if the unix mounts have changed since a given unix time.
1158 * Returns: %TRUE if the mounts have changed since @time.
1161 g_unix_mounts_changed_since (guint64 time
)
1163 return get_mounts_timestamp () != time
;
1167 * g_unix_mount_points_changed_since:
1168 * @time: guint64 to contain a timestamp.
1170 * Checks if the unix mount points have changed since a given unix time.
1172 * Returns: %TRUE if the mount points have changed since @time.
1175 g_unix_mount_points_changed_since (guint64 time
)
1177 return get_mount_points_timestamp () != time
;
1181 g_unix_mount_monitor_finalize (GObject
*object
)
1183 GUnixMountMonitor
*monitor
;
1185 monitor
= G_UNIX_MOUNT_MONITOR (object
);
1187 if (monitor
->fstab_monitor
)
1189 g_file_monitor_cancel (monitor
->fstab_monitor
);
1190 g_object_unref (monitor
->fstab_monitor
);
1193 if (monitor
->mtab_monitor
)
1195 g_file_monitor_cancel (monitor
->mtab_monitor
);
1196 g_object_unref (monitor
->mtab_monitor
);
1199 the_mount_monitor
= NULL
;
1201 G_OBJECT_CLASS (g_unix_mount_monitor_parent_class
)->finalize (object
);
1206 g_unix_mount_monitor_class_init (GUnixMountMonitorClass
*klass
)
1208 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
1210 gobject_class
->finalize
= g_unix_mount_monitor_finalize
;
1213 * GUnixMountMonitor::mounts-changed:
1214 * @monitor: the object on which the signal is emitted
1216 * Emitted when the unix mounts have changed.
1218 signals
[MOUNTS_CHANGED
] =
1219 g_signal_new ("mounts-changed",
1220 G_TYPE_FROM_CLASS (klass
),
1224 g_cclosure_marshal_VOID__VOID
,
1228 * GUnixMountMonitor::mountpoints-changed:
1229 * @monitor: the object on which the signal is emitted
1231 * Emitted when the unix mount points have changed.
1233 signals
[MOUNTPOINTS_CHANGED
] =
1234 g_signal_new ("mountpoints-changed",
1235 G_TYPE_FROM_CLASS (klass
),
1239 g_cclosure_marshal_VOID__VOID
,
1244 fstab_file_changed (GFileMonitor
*monitor
,
1247 GFileMonitorEvent event_type
,
1250 GUnixMountMonitor
*mount_monitor
;
1252 if (event_type
!= G_FILE_MONITOR_EVENT_CHANGED
&&
1253 event_type
!= G_FILE_MONITOR_EVENT_CREATED
&&
1254 event_type
!= G_FILE_MONITOR_EVENT_DELETED
)
1257 mount_monitor
= user_data
;
1258 g_signal_emit (mount_monitor
, signals
[MOUNTPOINTS_CHANGED
], 0);
1262 mtab_file_changed (GFileMonitor
*monitor
,
1265 GFileMonitorEvent event_type
,
1268 GUnixMountMonitor
*mount_monitor
;
1270 if (event_type
!= G_FILE_MONITOR_EVENT_CHANGED
&&
1271 event_type
!= G_FILE_MONITOR_EVENT_CREATED
&&
1272 event_type
!= G_FILE_MONITOR_EVENT_DELETED
)
1275 mount_monitor
= user_data
;
1276 g_signal_emit (mount_monitor
, signals
[MOUNTS_CHANGED
], 0);
1280 g_unix_mount_monitor_init (GUnixMountMonitor
*monitor
)
1284 if (get_fstab_file () != NULL
)
1286 file
= g_file_new_for_path (get_fstab_file ());
1287 monitor
->fstab_monitor
= g_file_monitor_file (file
, 0, NULL
, NULL
);
1288 g_object_unref (file
);
1290 g_signal_connect (monitor
->fstab_monitor
, "changed", (GCallback
)fstab_file_changed
, monitor
);
1293 if (get_mtab_monitor_file () != NULL
)
1295 file
= g_file_new_for_path (get_mtab_monitor_file ());
1296 monitor
->mtab_monitor
= g_file_monitor_file (file
, 0, NULL
, NULL
);
1297 g_object_unref (file
);
1299 g_signal_connect (monitor
->mtab_monitor
, "changed", (GCallback
)mtab_file_changed
, monitor
);
1304 * g_unix_mount_monitor_set_rate_limit:
1305 * @mount_monitor: a #GUnixMountMonitor
1306 * @limit_msec: a integer with the limit in milliseconds to
1309 * Sets the rate limit to which the @mount_monitor will report
1310 * consecutive change events to the mount and mount point entry files.
1315 g_unix_mount_monitor_set_rate_limit (GUnixMountMonitor
*mount_monitor
,
1318 g_return_if_fail (G_IS_UNIX_MOUNT_MONITOR (mount_monitor
));
1320 if (mount_monitor
->fstab_monitor
!= NULL
)
1321 g_file_monitor_set_rate_limit (mount_monitor
->fstab_monitor
, limit_msec
);
1323 if (mount_monitor
->mtab_monitor
!= NULL
)
1324 g_file_monitor_set_rate_limit (mount_monitor
->mtab_monitor
, limit_msec
);
1328 * g_unix_mount_monitor_new:
1330 * Gets a new #GUnixMountMonitor. The default rate limit for which the
1331 * monitor will report consecutive changes for the mount and mount
1332 * point entry files is the default for a #GFileMonitor. Use
1333 * g_unix_mount_monitor_set_rate_limit() to change this.
1335 * Returns: a #GUnixMountMonitor.
1338 g_unix_mount_monitor_new (void)
1340 if (the_mount_monitor
== NULL
)
1342 the_mount_monitor
= g_object_new (G_TYPE_UNIX_MOUNT_MONITOR
, NULL
);
1343 return the_mount_monitor
;
1346 return g_object_ref (the_mount_monitor
);
1350 * g_unix_mount_free:
1351 * @mount_entry: a #GUnixMount.
1353 * Frees a unix mount.
1356 g_unix_mount_free (GUnixMountEntry
*mount_entry
)
1358 g_return_if_fail (mount_entry
!= NULL
);
1360 g_free (mount_entry
->mount_path
);
1361 g_free (mount_entry
->device_path
);
1362 g_free (mount_entry
->filesystem_type
);
1363 g_free (mount_entry
);
1367 * g_unix_mount_point_free:
1368 * @mount_point: unix mount point to free.
1370 * Frees a unix mount point.
1373 g_unix_mount_point_free (GUnixMountPoint
*mount_point
)
1375 g_return_if_fail (mount_point
!= NULL
);
1377 g_free (mount_point
->mount_path
);
1378 g_free (mount_point
->device_path
);
1379 g_free (mount_point
->filesystem_type
);
1380 g_free (mount_point
);
1384 * g_unix_mount_compare:
1385 * @mount1: first #GUnixMountEntry to compare.
1386 * @mount2: second #GUnixMountEntry to compare.
1388 * Compares two unix mounts.
1390 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1391 * or less than @mount2, respectively.
1394 g_unix_mount_compare (GUnixMountEntry
*mount1
,
1395 GUnixMountEntry
*mount2
)
1399 g_return_val_if_fail (mount1
!= NULL
&& mount2
!= NULL
, 0);
1401 res
= g_strcmp0 (mount1
->mount_path
, mount2
->mount_path
);
1405 res
= g_strcmp0 (mount1
->device_path
, mount2
->device_path
);
1409 res
= g_strcmp0 (mount1
->filesystem_type
, mount2
->filesystem_type
);
1413 res
= mount1
->is_read_only
- mount2
->is_read_only
;
1421 * g_unix_mount_get_mount_path:
1422 * @mount_entry: input #GUnixMountEntry to get the mount path for.
1424 * Gets the mount path for a unix mount.
1426 * Returns: the mount path for @mount_entry.
1429 g_unix_mount_get_mount_path (GUnixMountEntry
*mount_entry
)
1431 g_return_val_if_fail (mount_entry
!= NULL
, NULL
);
1433 return mount_entry
->mount_path
;
1437 * g_unix_mount_get_device_path:
1438 * @mount_entry: a #GUnixMount.
1440 * Gets the device path for a unix mount.
1442 * Returns: a string containing the device path.
1445 g_unix_mount_get_device_path (GUnixMountEntry
*mount_entry
)
1447 g_return_val_if_fail (mount_entry
!= NULL
, NULL
);
1449 return mount_entry
->device_path
;
1453 * g_unix_mount_get_fs_type:
1454 * @mount_entry: a #GUnixMount.
1456 * Gets the filesystem type for the unix mount.
1458 * Returns: a string containing the file system type.
1461 g_unix_mount_get_fs_type (GUnixMountEntry
*mount_entry
)
1463 g_return_val_if_fail (mount_entry
!= NULL
, NULL
);
1465 return mount_entry
->filesystem_type
;
1469 * g_unix_mount_is_readonly:
1470 * @mount_entry: a #GUnixMount.
1472 * Checks if a unix mount is mounted read only.
1474 * Returns: %TRUE if @mount_entry is read only.
1477 g_unix_mount_is_readonly (GUnixMountEntry
*mount_entry
)
1479 g_return_val_if_fail (mount_entry
!= NULL
, FALSE
);
1481 return mount_entry
->is_read_only
;
1485 * g_unix_mount_is_system_internal:
1486 * @mount_entry: a #GUnixMount.
1488 * Checks if a unix mount is a system path.
1490 * Returns: %TRUE if the unix mount is for a system path.
1493 g_unix_mount_is_system_internal (GUnixMountEntry
*mount_entry
)
1495 g_return_val_if_fail (mount_entry
!= NULL
, FALSE
);
1497 return mount_entry
->is_system_internal
;
1501 * g_unix_mount_point_compare:
1502 * @mount1: a #GUnixMount.
1503 * @mount2: a #GUnixMount.
1505 * Compares two unix mount points.
1507 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1508 * or less than @mount2, respectively.
1511 g_unix_mount_point_compare (GUnixMountPoint
*mount1
,
1512 GUnixMountPoint
*mount2
)
1516 g_return_val_if_fail (mount1
!= NULL
&& mount2
!= NULL
, 0);
1518 res
= g_strcmp0 (mount1
->mount_path
, mount2
->mount_path
);
1522 res
= g_strcmp0 (mount1
->device_path
, mount2
->device_path
);
1526 res
= g_strcmp0 (mount1
->filesystem_type
, mount2
->filesystem_type
);
1530 res
= mount1
->is_read_only
- mount2
->is_read_only
;
1534 res
= mount1
->is_user_mountable
- mount2
->is_user_mountable
;
1538 res
= mount1
->is_loopback
- mount2
->is_loopback
;
1546 * g_unix_mount_point_get_mount_path:
1547 * @mount_point: a #GUnixMountPoint.
1549 * Gets the mount path for a unix mount point.
1551 * Returns: a string containing the mount path.
1554 g_unix_mount_point_get_mount_path (GUnixMountPoint
*mount_point
)
1556 g_return_val_if_fail (mount_point
!= NULL
, NULL
);
1558 return mount_point
->mount_path
;
1562 * g_unix_mount_point_get_device_path:
1563 * @mount_point: a #GUnixMountPoint.
1565 * Gets the device path for a unix mount point.
1567 * Returns: a string containing the device path.
1570 g_unix_mount_point_get_device_path (GUnixMountPoint
*mount_point
)
1572 g_return_val_if_fail (mount_point
!= NULL
, NULL
);
1574 return mount_point
->device_path
;
1578 * g_unix_mount_point_get_fs_type:
1579 * @mount_point: a #GUnixMountPoint.
1581 * Gets the file system type for the mount point.
1583 * Returns: a string containing the file system type.
1586 g_unix_mount_point_get_fs_type (GUnixMountPoint
*mount_point
)
1588 g_return_val_if_fail (mount_point
!= NULL
, NULL
);
1590 return mount_point
->filesystem_type
;
1594 * g_unix_mount_point_is_readonly:
1595 * @mount_point: a #GUnixMountPoint.
1597 * Checks if a unix mount point is read only.
1599 * Returns: %TRUE if a mount point is read only.
1602 g_unix_mount_point_is_readonly (GUnixMountPoint
*mount_point
)
1604 g_return_val_if_fail (mount_point
!= NULL
, FALSE
);
1606 return mount_point
->is_read_only
;
1610 * g_unix_mount_point_is_user_mountable:
1611 * @mount_point: a #GUnixMountPoint.
1613 * Checks if a unix mount point is mountable by the user.
1615 * Returns: %TRUE if the mount point is user mountable.
1618 g_unix_mount_point_is_user_mountable (GUnixMountPoint
*mount_point
)
1620 g_return_val_if_fail (mount_point
!= NULL
, FALSE
);
1622 return mount_point
->is_user_mountable
;
1626 * g_unix_mount_point_is_loopback:
1627 * @mount_point: a #GUnixMountPoint.
1629 * Checks if a unix mount point is a loopback device.
1631 * Returns: %TRUE if the mount point is a loopback. %FALSE otherwise.
1634 g_unix_mount_point_is_loopback (GUnixMountPoint
*mount_point
)
1636 g_return_val_if_fail (mount_point
!= NULL
, FALSE
);
1638 return mount_point
->is_loopback
;
1641 static GUnixMountType
1642 guess_mount_type (const char *mount_path
,
1643 const char *device_path
,
1644 const char *filesystem_type
)
1646 GUnixMountType type
;
1649 type
= G_UNIX_MOUNT_TYPE_UNKNOWN
;
1651 if ((strcmp (filesystem_type
, "udf") == 0) ||
1652 (strcmp (filesystem_type
, "iso9660") == 0) ||
1653 (strcmp (filesystem_type
, "cd9660") == 0))
1654 type
= G_UNIX_MOUNT_TYPE_CDROM
;
1655 else if ((strcmp (filesystem_type
, "nfs") == 0) ||
1656 (strcmp (filesystem_type
, "nfs4") == 0))
1657 type
= G_UNIX_MOUNT_TYPE_NFS
;
1658 else if (g_str_has_prefix (device_path
, "/vol/dev/diskette/") ||
1659 g_str_has_prefix (device_path
, "/dev/fd") ||
1660 g_str_has_prefix (device_path
, "/dev/floppy"))
1661 type
= G_UNIX_MOUNT_TYPE_FLOPPY
;
1662 else if (g_str_has_prefix (device_path
, "/dev/cdrom") ||
1663 g_str_has_prefix (device_path
, "/dev/acd") ||
1664 g_str_has_prefix (device_path
, "/dev/cd"))
1665 type
= G_UNIX_MOUNT_TYPE_CDROM
;
1666 else if (g_str_has_prefix (device_path
, "/vol/"))
1668 const char *name
= mount_path
+ strlen ("/");
1670 if (g_str_has_prefix (name
, "cdrom"))
1671 type
= G_UNIX_MOUNT_TYPE_CDROM
;
1672 else if (g_str_has_prefix (name
, "floppy") ||
1673 g_str_has_prefix (device_path
, "/vol/dev/diskette/"))
1674 type
= G_UNIX_MOUNT_TYPE_FLOPPY
;
1675 else if (g_str_has_prefix (name
, "rmdisk"))
1676 type
= G_UNIX_MOUNT_TYPE_ZIP
;
1677 else if (g_str_has_prefix (name
, "jaz"))
1678 type
= G_UNIX_MOUNT_TYPE_JAZ
;
1679 else if (g_str_has_prefix (name
, "memstick"))
1680 type
= G_UNIX_MOUNT_TYPE_MEMSTICK
;
1684 basename
= g_path_get_basename (mount_path
);
1686 if (g_str_has_prefix (basename
, "cdr") ||
1687 g_str_has_prefix (basename
, "cdwriter") ||
1688 g_str_has_prefix (basename
, "burn") ||
1689 g_str_has_prefix (basename
, "dvdr"))
1690 type
= G_UNIX_MOUNT_TYPE_CDROM
;
1691 else if (g_str_has_prefix (basename
, "floppy"))
1692 type
= G_UNIX_MOUNT_TYPE_FLOPPY
;
1693 else if (g_str_has_prefix (basename
, "zip"))
1694 type
= G_UNIX_MOUNT_TYPE_ZIP
;
1695 else if (g_str_has_prefix (basename
, "jaz"))
1696 type
= G_UNIX_MOUNT_TYPE_JAZ
;
1697 else if (g_str_has_prefix (basename
, "camera"))
1698 type
= G_UNIX_MOUNT_TYPE_CAMERA
;
1699 else if (g_str_has_prefix (basename
, "memstick") ||
1700 g_str_has_prefix (basename
, "memory_stick") ||
1701 g_str_has_prefix (basename
, "ram"))
1702 type
= G_UNIX_MOUNT_TYPE_MEMSTICK
;
1703 else if (g_str_has_prefix (basename
, "compact_flash"))
1704 type
= G_UNIX_MOUNT_TYPE_CF
;
1705 else if (g_str_has_prefix (basename
, "smart_media"))
1706 type
= G_UNIX_MOUNT_TYPE_SM
;
1707 else if (g_str_has_prefix (basename
, "sd_mmc"))
1708 type
= G_UNIX_MOUNT_TYPE_SDMMC
;
1709 else if (g_str_has_prefix (basename
, "ipod"))
1710 type
= G_UNIX_MOUNT_TYPE_IPOD
;
1715 if (type
== G_UNIX_MOUNT_TYPE_UNKNOWN
)
1716 type
= G_UNIX_MOUNT_TYPE_HD
;
1722 * g_unix_mount_guess_type:
1723 * @mount_entry: a #GUnixMount.
1725 * Guesses the type of a unix mount. If the mount type cannot be
1726 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1728 * Returns: a #GUnixMountType.
1730 static GUnixMountType
1731 g_unix_mount_guess_type (GUnixMountEntry
*mount_entry
)
1733 g_return_val_if_fail (mount_entry
!= NULL
, G_UNIX_MOUNT_TYPE_UNKNOWN
);
1734 g_return_val_if_fail (mount_entry
->mount_path
!= NULL
, G_UNIX_MOUNT_TYPE_UNKNOWN
);
1735 g_return_val_if_fail (mount_entry
->device_path
!= NULL
, G_UNIX_MOUNT_TYPE_UNKNOWN
);
1736 g_return_val_if_fail (mount_entry
->filesystem_type
!= NULL
, G_UNIX_MOUNT_TYPE_UNKNOWN
);
1738 return guess_mount_type (mount_entry
->mount_path
,
1739 mount_entry
->device_path
,
1740 mount_entry
->filesystem_type
);
1744 * g_unix_mount_point_guess_type:
1745 * @mount_point: a #GUnixMountPoint.
1747 * Guesses the type of a unix mount point.
1748 * If the mount type cannot be determined,
1749 * returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1751 * Returns: a #GUnixMountType.
1753 static GUnixMountType
1754 g_unix_mount_point_guess_type (GUnixMountPoint
*mount_point
)
1756 g_return_val_if_fail (mount_point
!= NULL
, G_UNIX_MOUNT_TYPE_UNKNOWN
);
1757 g_return_val_if_fail (mount_point
->mount_path
!= NULL
, G_UNIX_MOUNT_TYPE_UNKNOWN
);
1758 g_return_val_if_fail (mount_point
->device_path
!= NULL
, G_UNIX_MOUNT_TYPE_UNKNOWN
);
1759 g_return_val_if_fail (mount_point
->filesystem_type
!= NULL
, G_UNIX_MOUNT_TYPE_UNKNOWN
);
1761 return guess_mount_type (mount_point
->mount_path
,
1762 mount_point
->device_path
,
1763 mount_point
->filesystem_type
);
1767 type_to_icon (GUnixMountType type
, gboolean is_mount_point
)
1769 const char *icon_name
;
1773 case G_UNIX_MOUNT_TYPE_HD
:
1775 icon_name
= "drive-removable-media";
1777 icon_name
= "drive-harddisk";
1779 case G_UNIX_MOUNT_TYPE_FLOPPY
:
1780 case G_UNIX_MOUNT_TYPE_ZIP
:
1781 case G_UNIX_MOUNT_TYPE_JAZ
:
1783 icon_name
= "drive-removable-media";
1785 icon_name
= "media-floppy";
1787 case G_UNIX_MOUNT_TYPE_CDROM
:
1789 icon_name
= "drive-optical";
1791 icon_name
= "media-optical";
1793 case G_UNIX_MOUNT_TYPE_NFS
:
1794 /* TODO: Would like a better icon here... */
1796 icon_name
= "drive-removable-media";
1798 icon_name
= "drive-harddisk";
1800 case G_UNIX_MOUNT_TYPE_MEMSTICK
:
1802 icon_name
= "drive-removable-media";
1804 icon_name
= "media-flash";
1806 case G_UNIX_MOUNT_TYPE_CAMERA
:
1808 icon_name
= "drive-removable-media";
1810 icon_name
= "camera-photo";
1812 case G_UNIX_MOUNT_TYPE_IPOD
:
1814 icon_name
= "drive-removable-media";
1816 icon_name
= "multimedia-player";
1818 case G_UNIX_MOUNT_TYPE_UNKNOWN
:
1821 icon_name
= "drive-removable-media";
1823 icon_name
= "drive-harddisk";
1831 * g_unix_mount_guess_name:
1832 * @mount_entry: a #GUnixMountEntry
1834 * Guesses the name of a Unix mount.
1835 * The result is a translated string.
1837 * Returns: A newly allocated string that must
1838 * be freed with g_free()
1841 g_unix_mount_guess_name (GUnixMountEntry
*mount_entry
)
1845 if (strcmp (mount_entry
->mount_path
, "/") == 0)
1846 name
= g_strdup (_("Filesystem root"));
1848 name
= g_filename_display_basename (mount_entry
->mount_path
);
1854 * g_unix_mount_guess_icon:
1855 * @mount_entry: a #GUnixMountEntry
1857 * Guesses the icon of a Unix mount.
1862 g_unix_mount_guess_icon (GUnixMountEntry
*mount_entry
)
1864 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry
), FALSE
));
1868 * g_unix_mount_point_guess_name:
1869 * @mount_point: a #GUnixMountPoint
1871 * Guesses the name of a Unix mount point.
1872 * The result is a translated string.
1874 * Returns: A newly allocated string that must
1875 * be freed with g_free()
1878 g_unix_mount_point_guess_name (GUnixMountPoint
*mount_point
)
1882 if (strcmp (mount_point
->mount_path
, "/") == 0)
1883 name
= g_strdup (_("Filesystem root"));
1885 name
= g_filename_display_basename (mount_point
->mount_path
);
1891 * g_unix_mount_point_guess_icon:
1892 * @mount_point: a #GUnixMountPoint
1894 * Guesses the icon of a Unix mount point.
1899 g_unix_mount_point_guess_icon (GUnixMountPoint
*mount_point
)
1901 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point
), TRUE
));
1905 * g_unix_mount_guess_can_eject:
1906 * @mount_entry: a #GUnixMountEntry
1908 * Guesses whether a Unix mount can be ejected.
1910 * Returns: %TRUE if @mount_entry is deemed to be ejectable.
1913 g_unix_mount_guess_can_eject (GUnixMountEntry
*mount_entry
)
1915 GUnixMountType guessed_type
;
1917 guessed_type
= g_unix_mount_guess_type (mount_entry
);
1918 if (guessed_type
== G_UNIX_MOUNT_TYPE_IPOD
||
1919 guessed_type
== G_UNIX_MOUNT_TYPE_CDROM
)
1926 * g_unix_mount_guess_should_display:
1927 * @mount_entry: a #GUnixMountEntry
1929 * Guesses whether a Unix mount should be displayed in the UI.
1931 * Returns: %TRUE if @mount_entry is deemed to be displayable.
1934 g_unix_mount_guess_should_display (GUnixMountEntry
*mount_entry
)
1936 const char *mount_path
;
1938 /* Never display internal mountpoints */
1939 if (g_unix_mount_is_system_internal (mount_entry
))
1942 /* Only display things in /media (which are generally user mountable)
1943 and home dir (fuse stuff) */
1944 mount_path
= mount_entry
->mount_path
;
1945 if (mount_path
!= NULL
)
1947 if (g_str_has_prefix (mount_path
, "/media/"))
1950 /* Avoid displaying mounts that are not accessible to the user.
1952 * See http://bugzilla.gnome.org/show_bug.cgi?id=526320 for why we
1953 * want to avoid g_access() for mount points which can potentially
1954 * block or fail stat()'ing, such as network mounts.
1956 path
= g_path_get_dirname (mount_path
);
1957 if (g_str_has_prefix (path
, "/media/"))
1959 if (g_access (path
, R_OK
|X_OK
) != 0)
1967 if (mount_entry
->device_path
&& mount_entry
->device_path
[0] == '/')
1970 if (g_stat (mount_entry
->device_path
, &st
) == 0 &&
1971 S_ISBLK(st
.st_mode
) &&
1972 g_access (mount_path
, R_OK
|X_OK
) != 0)
1978 if (g_str_has_prefix (mount_path
, g_get_home_dir ()) &&
1979 mount_path
[strlen (g_get_home_dir())] == G_DIR_SEPARATOR
)
1987 * g_unix_mount_point_guess_can_eject:
1988 * @mount_point: a #GUnixMountPoint
1990 * Guesses whether a Unix mount point can be ejected.
1992 * Returns: %TRUE if @mount_point is deemed to be ejectable.
1995 g_unix_mount_point_guess_can_eject (GUnixMountPoint
*mount_point
)
1997 GUnixMountType guessed_type
;
1999 guessed_type
= g_unix_mount_point_guess_type (mount_point
);
2000 if (guessed_type
== G_UNIX_MOUNT_TYPE_IPOD
||
2001 guessed_type
== G_UNIX_MOUNT_TYPE_CDROM
)
2008 /* borrowed from gtk/gtkfilesystemunix.c in GTK+ on 02/23/2006 */
2010 _canonicalize_filename (gchar
*filename
)
2013 gboolean last_was_slash
= FALSE
;
2020 if (*p
== G_DIR_SEPARATOR
)
2022 if (!last_was_slash
)
2023 *q
++ = G_DIR_SEPARATOR
;
2025 last_was_slash
= TRUE
;
2029 if (last_was_slash
&& *p
== '.')
2031 if (*(p
+ 1) == G_DIR_SEPARATOR
||
2034 if (*(p
+ 1) == '\0')
2039 else if (*(p
+ 1) == '.' &&
2040 (*(p
+ 2) == G_DIR_SEPARATOR
||
2043 if (q
> filename
+ 1)
2046 while (q
> filename
+ 1 &&
2047 *(q
- 1) != G_DIR_SEPARATOR
)
2051 if (*(p
+ 2) == '\0')
2059 last_was_slash
= FALSE
;
2065 last_was_slash
= FALSE
;
2072 if (q
> filename
+ 1 && *(q
- 1) == G_DIR_SEPARATOR
)
2079 _resolve_symlink (const char *file
)
2087 f
= g_strdup (file
);
2089 while (g_file_test (f
, G_FILE_TEST_IS_SYMLINK
))
2091 link
= g_file_read_link (f
, &error
);
2094 g_error_free (error
);
2100 dir
= g_path_get_dirname (f
);
2101 f1
= g_strdup_printf ("%s/%s", dir
, link
);
2110 _canonicalize_filename (f
);
2114 #ifdef HAVE_MNTENT_H
2116 _resolve_dev_root (void)
2118 static gboolean have_real_dev_root
= FALSE
;
2119 static char real_dev_root
[256];
2120 struct stat statbuf
;
2122 /* see if it's cached already */
2123 if (have_real_dev_root
)
2126 /* otherwise we're going to find it right away.. */
2127 have_real_dev_root
= TRUE
;
2129 if (stat ("/dev/root", &statbuf
) == 0)
2131 if (! S_ISLNK (statbuf
.st_mode
))
2133 dev_t root_dev
= statbuf
.st_dev
;
2137 /* see if device with similar major:minor as /dev/root is mention
2138 * in /etc/mtab (it usually is)
2140 f
= fopen ("/etc/mtab", "r");
2143 struct mntent
*entp
;
2144 #ifdef HAVE_GETMNTENT_R
2146 while ((entp
= getmntent_r (f
, &ent
, buf
, sizeof (buf
))) != NULL
)
2150 while ((entp
= getmntent (f
)) != NULL
)
2153 if (stat (entp
->mnt_fsname
, &statbuf
) == 0 &&
2154 statbuf
.st_dev
== root_dev
)
2156 strncpy (real_dev_root
, entp
->mnt_fsname
, sizeof (real_dev_root
) - 1);
2157 real_dev_root
[sizeof (real_dev_root
) - 1] = '\0';
2165 #ifndef HAVE_GETMNTENT_R
2166 G_UNLOCK (getmntent
);
2170 /* no, that didn't work.. next we could scan /dev ... but I digress.. */
2176 resolved
= _resolve_symlink ("/dev/root");
2177 if (resolved
!= NULL
)
2179 strncpy (real_dev_root
, resolved
, sizeof (real_dev_root
) - 1);
2180 real_dev_root
[sizeof (real_dev_root
) - 1] = '\0';
2188 strcpy (real_dev_root
, "/dev/root");
2191 return real_dev_root
;