r70: Removed some old code. Dragging now quotes the correct MIME type for files.
[rox-filer.git] / ROX-Filer / src / mount.c
blob1ab3c8a02cf82bb2d4e7dc8de79a1f3793db4e35
1 /* vi: set cindent:
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * By Thomas Leonard, <tal197@ecs.soton.ac.uk>.
6 */
8 /* mount.c - code for handling mount points */
10 #include <stdio.h>
11 #include <mntent.h>
12 #include <sys/stat.h>
13 #include <sys/time.h>
14 #include <unistd.h>
16 #include <glib.h>
17 #include <gtk/gtk.h>
19 #include "mount.h"
21 /* Map mount points to mntent structures */
22 GHashTable *fstab_mounts = NULL;
23 GHashTable *mtab_mounts = NULL;
24 time_t fstab_time;
25 time_t mtab_time;
27 /* Static prototypes */
28 static GHashTable *read_table(GHashTable *mount_points, char *path);
29 static time_t read_time(char *path);
30 static gboolean free_mp(gpointer key, gpointer value, gpointer data);
31 static void clear_table(GHashTable *table);
32 static time_t read_time(char *path);
34 void mount_init()
36 fstab_mounts = g_hash_table_new(g_str_hash, g_str_equal);
37 mtab_mounts = g_hash_table_new(g_str_hash, g_str_equal);
39 fstab_time = read_time("/etc/fstab");
40 read_table(fstab_mounts, "/etc/fstab");
41 mtab_time = read_time("/etc/mtab");
42 read_table(mtab_mounts, "/etc/mtab");
45 void mount_update()
47 time_t time;
49 /* Ensure everything is uptodate */
51 time = read_time("/etc/fstab");
52 if (time != fstab_time)
54 fstab_time = time;
55 read_table(fstab_mounts, "/etc/fstab");
58 time = read_time("/etc/mtab");
59 if (time != mtab_time)
61 mtab_time = time;
62 read_table(mtab_mounts, "/etc/mtab");
66 static gboolean free_mp(gpointer key, gpointer value, gpointer data)
68 MountPoint *mp = (MountPoint *) value;
70 g_free(mp->name);
71 g_free(mp->dir);
72 g_free(mp);
74 return TRUE;
77 /* Remove all entries from a hash table, freeing them as we go */
78 static void clear_table(GHashTable *table)
80 g_hash_table_foreach_remove(table, free_mp, NULL);
83 /* Return the mtime of a file */
84 static time_t read_time(char *path)
86 struct stat info;
88 g_return_val_if_fail(stat(path, &info) == 0, 0);
90 return info.st_mtime;
93 static GHashTable *read_table(GHashTable *mount_points, char *path)
95 FILE *tab;
96 struct mntent *ent;
97 MountPoint *mp;
99 clear_table(mount_points);
101 tab = setmntent(path, "r");
102 g_return_val_if_fail(tab != NULL, NULL);
104 while ((ent = getmntent(tab)))
106 if (strcmp(ent->mnt_dir, "swap") == 0)
107 continue;
109 mp = g_malloc(sizeof(MountPoint));
110 mp->name = g_strdup(ent->mnt_fsname);
111 mp->dir = g_strdup(ent->mnt_dir);
113 g_hash_table_insert(mount_points, mp->dir, mp);
116 endmntent(tab);
118 return mount_points;