1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /*
4 * Engrampa
5 *
6 * Copyright (C) 2001 The Free Software Foundation, Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Street #330, Boston, MA 02110-1301, USA.
21 */
22
23 #include <config.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27
28 #include <glib.h>
29
30 #include "file-data.h"
31 #include "file-utils.h"
32 #include "glib-utils.h"
33 #include "fr-command.h"
34 #include "fr-command-dpkg.h"
35
36 static void fr_command_dpkg_class_init (FrCommandDpkgClass *class);
37 static void fr_command_dpkg_init (FrCommand *afile);
38 static void fr_command_dpkg_finalize (GObject *object);
39
40 /* Parent Class */
41
42 static FrCommandClass *parent_class = NULL;
43
44 static void
process_metadata_line(char * line,FrCommand * comm)45 process_metadata_line (char *line,
46 FrCommand *comm)
47 {
48 FileData *fdata;
49 char **fields;
50 char *name;
51
52 g_return_if_fail (line != NULL);
53
54 fields = split_line (line, 6);
55 if (!fields[1] || !g_str_equal (fields[1], "bytes,")) {
56 g_strfreev (fields);
57 return;
58 }
59
60 fdata = file_data_new ();
61 fdata->size = g_ascii_strtoull (fields[0], NULL, 10);
62
63 if (fields[5] && g_str_equal (fields[4],"*")) {
64 name = g_strdup (fields[5]);
65 } else {
66 name = g_strdup (get_last_field (line, 5));
67 }
68 g_strstrip (name);
69
70 fdata->full_path = g_strconcat ("/DEBIAN/", name, NULL);
71 fdata->original_path = fdata->full_path + 1;
72
73 g_strfreev (fields);
74
75 fdata->name = g_strdup (name);
76 fdata->path = g_strdup ("DEBIAN");
77
78 g_free (name);
79
80 fr_command_add_file (comm, fdata);
81 }
82
83 static void
process_data_line(char * line,gpointer data)84 process_data_line (char *line,
85 gpointer data)
86 {
87 FileData *fdata;
88 FrCommand *comm = FR_COMMAND (data);
89 char **fields;
90 char **tmfields;
91 struct tm tm = {0, };
92 const char *name;
93
94 g_return_if_fail (line != NULL);
95
96 if (line[0] == ' ') {
97 /* This is the output of dpkg-deb -I */
98 process_metadata_line (line, comm);
99 return;
100 }
101
102 fdata = file_data_new ();
103
104 fields = split_line (line, 5);
105 fdata->size = g_ascii_strtoull (fields[2], NULL, 10);
106 tmfields = g_strsplit(fields[3], "-", 3);
107 if (tmfields[2]) {
108 tm.tm_year = atoi (tmfields[0]) - 1900;
109 tm.tm_mon = atoi (tmfields[1]);
110 tm.tm_mday = atoi (tmfields[2]);
111 }
112 g_strfreev (tmfields);
113 tmfields = g_strsplit (fields[4], ":", 2);
114 if (tmfields[1]) {
115 tm.tm_hour = atoi (tmfields[0]);
116 tm.tm_min = atoi (tmfields[1]);
117 }
118 g_strfreev (tmfields);
119 fdata->modified = mktime (&tm);
120 g_strfreev (fields);
121
122 name = get_last_field (line, 6);
123 fields = g_strsplit (name, " -> ", 2);
124
125 fdata->dir = line[0] == 'd';
126 name = fields[0];
127 if (g_str_has_prefix (name, "./")) { /* Should generally be the case */
128 fdata->full_path = g_strdup (name + 1);
129 fdata->original_path = fdata->full_path + 1;
130 } else if (name[0] == '/') {
131 fdata->full_path = g_strdup (name);
132 fdata->original_path = fdata->full_path;
133 } else {
134 fdata->full_path = g_strconcat ("/", name, NULL);
135 fdata->original_path = fdata->full_path + 1;
136 }
137 if (fdata->dir && (name[strlen (name) - 1] != '/')) {
138 char *old_full_path = fdata->full_path;
139 fdata->full_path = g_strconcat (old_full_path, "/", NULL);
140 g_free (old_full_path);
141 fdata->original_path = g_strdup (name);
142 fdata->free_original_path = TRUE;
143 }
144
145 if (fields[1] != NULL)
146 fdata->link = g_strdup (fields[1]);
147 g_strfreev (fields);
148
149 if (fdata->dir)
150 fdata->name = dir_name_from_path (fdata->full_path);
151 else
152 fdata->name = g_strdup (file_name_from_path (fdata->full_path));
153 fdata->path = remove_level_from_path (fdata->full_path);
154
155 if (*fdata->name == 0)
156 file_data_free (fdata);
157 else
158 fr_command_add_file (comm, fdata);
159 }
160
161
162 static void
fr_command_dpkg_list(FrCommand * comm)163 fr_command_dpkg_list (FrCommand *comm)
164 {
165 fr_process_set_out_line_func (comm->process, process_data_line, comm);
166
167 fr_process_begin_command (comm->process, "dpkg-deb");
168 fr_process_add_arg (comm->process, "-I");
169 fr_process_add_arg (comm->process, comm->filename);
170 fr_process_end_command (comm->process);
171 fr_process_start (comm->process);
172
173 fr_process_begin_command (comm->process, "dpkg-deb");
174 fr_process_add_arg (comm->process, "-c");
175 fr_process_add_arg (comm->process, comm->filename);
176 fr_process_end_command (comm->process);
177 fr_process_start (comm->process);
178 }
179
180
181 static void
fr_command_dpkg_extract(FrCommand * comm,const char * from_file,GList * file_list,const char * dest_dir,gboolean overwrite,gboolean skip_older,gboolean junk_paths)182 fr_command_dpkg_extract (FrCommand *comm,
183 const char *from_file,
184 GList *file_list,
185 const char *dest_dir,
186 gboolean overwrite,
187 gboolean skip_older,
188 gboolean junk_paths)
189 {
190 fr_process_begin_command (comm->process, "dpkg-deb");
191 fr_process_add_arg (comm->process, "-x");
192 fr_process_add_arg (comm->process, comm->filename);
193 if (dest_dir != NULL) {
194 fr_process_add_arg (comm->process, dest_dir);
195 } else {
196 fr_process_add_arg (comm->process, ".");
197 }
198 /* FIXME it is not possible to unpack only some files */
199 fr_process_end_command (comm->process);
200
201 /* Also extract metadata in DEBIAN/ */
202 fr_process_begin_command (comm->process, "dpkg-deb");
203 if (dest_dir != NULL) {
204 fr_process_set_working_dir (comm->process, dest_dir);
205 }
206 fr_process_add_arg (comm->process, "-e");
207 fr_process_add_arg (comm->process, comm->filename);
208 fr_process_end_command (comm->process);
209 }
210
211
212 const char *dpkg_mime_type[] = { "application/x-deb", NULL };
213
214
215 static const char **
fr_command_dpkg_get_mime_types(FrCommand * comm)216 fr_command_dpkg_get_mime_types (FrCommand *comm)
217 {
218 return dpkg_mime_type;
219 }
220
221
222 static FrCommandCap
fr_command_dpkg_get_capabilities(FrCommand * comm,const char * mime_type,gboolean check_command)223 fr_command_dpkg_get_capabilities (FrCommand *comm,
224 const char *mime_type,
225 gboolean check_command)
226 {
227 FrCommandCap capabilities;
228
229 capabilities = FR_COMMAND_CAN_ARCHIVE_MANY_FILES;
230 if (is_program_available ("dpkg-deb", check_command))
231 capabilities |= FR_COMMAND_CAN_READ;
232
233 return capabilities;
234 }
235
236
237 static const char *
fr_command_dpkg_get_packages(FrCommand * comm,const char * mime_type)238 fr_command_dpkg_get_packages (FrCommand *comm,
239 const char *mime_type)
240 {
241 return PACKAGES ("dpkg");
242 }
243
244
245 static void
fr_command_dpkg_class_init(FrCommandDpkgClass * class)246 fr_command_dpkg_class_init (FrCommandDpkgClass *class)
247 {
248 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
249 FrCommandClass *afc;
250
251 parent_class = g_type_class_peek_parent (class);
252 afc = (FrCommandClass*) class;
253
254 gobject_class->finalize = fr_command_dpkg_finalize;
255
256 afc->list = fr_command_dpkg_list;
257 afc->extract = fr_command_dpkg_extract;
258 afc->get_mime_types = fr_command_dpkg_get_mime_types;
259 afc->get_capabilities = fr_command_dpkg_get_capabilities;
260 afc->get_packages = fr_command_dpkg_get_packages;
261 }
262
263
264 static void
fr_command_dpkg_init(FrCommand * comm)265 fr_command_dpkg_init (FrCommand *comm)
266 {
267 comm->propAddCanUpdate = FALSE;
268 comm->propAddCanReplace = FALSE;
269 comm->propExtractCanAvoidOverwrite = FALSE;
270 comm->propExtractCanSkipOlder = FALSE;
271 comm->propExtractCanJunkPaths = FALSE;
272 comm->propPassword = FALSE;
273 comm->propTest = FALSE;
274 }
275
276
277 static void
fr_command_dpkg_finalize(GObject * object)278 fr_command_dpkg_finalize (GObject *object)
279 {
280 g_return_if_fail (object != NULL);
281 g_return_if_fail (FR_IS_COMMAND_DPKG (object));
282
283 /* Chain up */
284 if (G_OBJECT_CLASS (parent_class)->finalize)
285 G_OBJECT_CLASS (parent_class)->finalize (object);
286 }
287
288
289 GType
fr_command_dpkg_get_type()290 fr_command_dpkg_get_type ()
291 {
292 static GType type = 0;
293
294 if (! type) {
295 GTypeInfo type_info = {
296 sizeof (FrCommandDpkgClass),
297 NULL,
298 NULL,
299 (GClassInitFunc) fr_command_dpkg_class_init,
300 NULL,
301 NULL,
302 sizeof (FrCommandDpkg),
303 0,
304 (GInstanceInitFunc) fr_command_dpkg_init
305 };
306
307 type = g_type_register_static (FR_TYPE_COMMAND,
308 "FRCommandDpkg",
309 &type_info,
310 0);
311 }
312
313 return type;
314 }
315