Bootstrap of 'captive-install-acquire' for W32 modules acquiration process.
[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
46         g_return_if_fail(device!=NULL);
47         g_return_if_fail(vol_name!=NULL);
48         g_return_if_fail(foreach_param!=NULL);
49
50         uri_device=gnome_vfs_uri_new("file:///");
51         uri_device=gnome_vfs_uri_append_path(uri_device,device);
52         g_assert(uri_device->parent==NULL);
53         uri_libntfs=gnome_vfs_uri_new("libntfs:///");
54         g_assert(uri_libntfs->parent==NULL);
55
56         uri_libntfs->parent=uri_device;
57
58         foreach_param->uri_list=g_list_prepend(foreach_param->uri_list,uri_libntfs);
59 }
60
61 static GList * /* of (GnomeVFSURI *) */ mod_uri_libntfs_proc_partitions(void)
62 {
63 /* map: (gchar *)device -> (gchar *)vol_name */
64 GHashTable *proc_partitions_ntfs_hash;
65 struct mod_uri_libntfs_proc_partitions_foreach_param foreach_param;
66
67         proc_partitions_ntfs_hash=proc_partitions_ntfs_hash_get(optarg_verbose);
68
69         foreach_param.proc_partitions_ntfs_hash=proc_partitions_ntfs_hash;
70         foreach_param.uri_list=NULL;
71         g_hash_table_foreach(proc_partitions_ntfs_hash,
72                         (GHFunc)mod_uri_libntfs_proc_partitions_foreach,        /* func */
73                         &foreach_param);        /* user_data */
74         g_hash_table_destroy(proc_partitions_ntfs_hash);
75
76         return foreach_param.uri_list;
77 }
78
79
80 static GList * /* of (GnomeVFSURI *) */ mod_uri_mtab(void)
81 {
82 struct mntent *mntent;
83 FILE *mntentfiler;
84 GList *uri_list;        /* of (GnomeVFSURI *) */
85 GnomeVFSURI *uri;
86
87         if (!(mntentfiler=setmntent(FILENAME_ETC_MTAB,"r"))) {
88                 g_warning(_("Cannot open \"%s\" for reading: %m"),FILENAME_ETC_MTAB);
89                 return NULL;
90                 }
91         uri_list=NULL;
92         while ((mntent=getmntent(mntentfiler))) {
93                 if (!strcmp(mntent->mnt_type,"proc"))   /* optimize... */
94                         continue;
95                 uri=gnome_vfs_uri_new("file:///");
96                 uri=gnome_vfs_uri_append_path(uri,mntent->mnt_dir);
97                 uri_list=g_list_prepend(uri_list,uri);
98                 }
99         if (1!=endmntent(mntentfiler))
100                 g_warning(_("Cannot close \"%s\" after reading: %m"),FILENAME_ETC_MTAB);
101
102         return uri_list;
103 }
104
105 static GList * /* of (GnomeVFSURI *) */ filter_non_local_uri(GList * /* of (GnomeVFSURI *) */ uri_list)
106 {
107 GList *uril;
108
109         /* Filter-out non-local URIs */
110         for (uril=uri_list;uril;) {
111 GnomeVFSURI *uri=uril->data;
112
113                 if (!gnome_vfs_uri_is_local(uri)) {
114                         if (optarg_verbose) {
115 gchar *uri_string;
116
117                                 uri_string=gnome_vfs_uri_to_string(uri,GNOME_VFS_URI_HIDE_PASSWORD);
118                                 g_message(_("Ignoring URI in auto-search as it is not local: %s"),uri_string);
119                                 g_free(uri_string);
120                                 }
121                         gnome_vfs_uri_unref(uri);
122                         /* It is safe to be used during traversal this way: */
123                         uril=uril->next;
124                         uri_list=g_list_remove_link(uri_list,uril);
125                         /* Prevent auto-traversal to the next list item: */
126                         continue;
127                         }
128                 uril=uril->next;
129                 }
130         return uri_list;
131 }
132
133 GList * /* of (GnomeVFSURI *) */ mod_uri_list(void)
134 {
135 GList *uri_list;
136
137         uri_list=NULL;
138
139         uri_list=g_list_concat(uri_list,mod_uri_mtab());
140         uri_list=g_list_concat(uri_list,mod_uri_libntfs_proc_partitions());
141
142         uri_list=filter_non_local_uri(uri_list);
143
144         return uri_list;
145 }