Initial original import from: fuse-2.4.2-2.fc4
[captive.git] / src / install / acquire / diskscan.c
1 /* $Id$
2  * W32 disk modules finder for acquiration installation utility
3  * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include <glib/gmessages.h>
23 #include <mntent.h>
24 #include <glib/ghash.h>
25 #include <glib/glist.h>
26 #include <libgnomevfs/gnome-vfs-uri.h>
27 #include "../libcaptive-install/proc_partitions.h"
28 #include "main.h"
29 #include <string.h>
30
31
32 /* Config: */
33 #define FILENAME_ETC_MTAB "/etc/mtab"
34
35
36 struct mod_uri_libntfs_proc_partitions_foreach_param {
37         GHashTable *proc_partitions_ntfs_hash;
38         GList *uri_list;        /* of (GnomeVFSURI *) */
39         };
40
41 static void mod_uri_libntfs_proc_partitions_foreach(const gchar *device /* key */,const gchar *vol_name /* value */,
42                 struct mod_uri_libntfs_proc_partitions_foreach_param *foreach_param /* user_data */)
43 {
44 GnomeVFSURI *uri_device,*uri_libntfs;
45 static gboolean libntfs_warned=FALSE;
46
47         g_return_if_fail(device!=NULL);
48         g_return_if_fail(vol_name!=NULL);
49         g_return_if_fail(foreach_param!=NULL);
50
51         uri_libntfs=gnome_vfs_uri_new("libntfs:///");
52         if (!uri_libntfs) {
53                 if (!libntfs_warned) {
54                         libntfs_warned=TRUE;
55                         g_warning(_("'libntfs' GnomeVFS method not supported; install package 'ntfsprogs-gnomevfs' >=%s"),"1.8.0beta2");
56                         }
57                 return;
58                 }
59         g_assert(uri_libntfs->parent==NULL);
60
61         uri_device=gnome_vfs_uri_new("file:///");
62         g_assert(uri_device!=NULL);
63         uri_device=gnome_vfs_uri_append_path(uri_device,device);
64         g_assert(uri_device->parent==NULL);
65
66         uri_libntfs->parent=uri_device;
67
68         foreach_param->uri_list=g_list_prepend(foreach_param->uri_list,uri_libntfs);
69 }
70
71 static GList * /* of (GnomeVFSURI *) */ mod_uri_libntfs_proc_partitions(void)
72 {
73 /* map: (gchar *)device -> (gchar *)vol_name */
74 GHashTable *proc_partitions_ntfs_hash;
75 struct mod_uri_libntfs_proc_partitions_foreach_param foreach_param;
76
77         proc_partitions_ntfs_hash=proc_partitions_ntfs_hash_get(optarg_verbose);
78
79         foreach_param.proc_partitions_ntfs_hash=proc_partitions_ntfs_hash;
80         foreach_param.uri_list=NULL;
81         g_hash_table_foreach(proc_partitions_ntfs_hash,
82                         (GHFunc)mod_uri_libntfs_proc_partitions_foreach,        /* func */
83                         &foreach_param);        /* user_data */
84         g_hash_table_destroy(proc_partitions_ntfs_hash);
85
86         return foreach_param.uri_list;
87 }
88
89
90 static GList * /* of (GnomeVFSURI *) */ mod_uri_mtab(void)
91 {
92 struct mntent *mntent;
93 FILE *mntentfiler;
94 GList *uri_list;        /* of (GnomeVFSURI *) */
95 GnomeVFSURI *uri;
96
97         if (!(mntentfiler=setmntent(FILENAME_ETC_MTAB,"r"))) {
98                 g_warning(_("Cannot open \"%s\" for reading: %m"),FILENAME_ETC_MTAB);
99                 return NULL;
100                 }
101         uri_list=NULL;
102         while ((mntent=getmntent(mntentfiler))) {
103                 if (!strcmp(mntent->mnt_type,"proc"))   /* optimize... */
104                         continue;
105                 uri=gnome_vfs_uri_new("file:///");
106                 g_assert(uri!=NULL);
107                 uri=gnome_vfs_uri_append_path(uri,mntent->mnt_dir);
108                 uri_list=g_list_prepend(uri_list,uri);
109                 }
110         if (1!=endmntent(mntentfiler))
111                 g_warning(_("Cannot close \"%s\" after reading: %m"),FILENAME_ETC_MTAB);
112
113         return uri_list;
114 }
115
116 static GList * /* of (GnomeVFSURI *) */ filter_non_local_uri(GList * /* of (GnomeVFSURI *) */ uri_list)
117 {
118 GList *uril;
119
120         /* Filter-out non-local URIs */
121         for (uril=uri_list;uril;) {
122 GnomeVFSURI *uri=uril->data;
123 GList *uril_remove;
124
125                 if (!gnome_vfs_uri_is_local(uri)) {
126                         if (optarg_verbose) {
127 gchar *uri_string;
128
129                                 uri_string=gnome_vfs_uri_to_string(uri,GNOME_VFS_URI_HIDE_PASSWORD);
130                                 g_message(_("Ignoring URI in auto-search as it is not local: %s"),uri_string);
131                                 g_free(uri_string);
132                                 }
133                         uril_remove=uril;
134                         uril=uril->next;
135                         gnome_vfs_uri_unref(uri);
136                         /* It is safe to be used during traversal this way: */
137                         uri_list=g_list_remove_link(uri_list,uril_remove);
138                         /* Prevent auto-traversal to the next list item: */
139                         continue;
140                         }
141                 uril=uril->next;
142                 }
143         return uri_list;
144 }
145
146 GList * /* of (GnomeVFSURI *) */ mod_uri_list(void)
147 {
148 GList *uri_list;
149
150         uri_list=NULL;
151
152         uri_list=g_list_concat(uri_list,mod_uri_mtab());
153         uri_list=g_list_concat(uri_list,mod_uri_libntfs_proc_partitions());
154
155         uri_list=filter_non_local_uri(uri_list);
156
157         return uri_list;
158 }