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 <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26 #include <unistd.h>
27
28 #include <glib.h>
29
30 #include "file-data.h"
31 #include "file-utils.h"
32 #include "fr-command.h"
33 #include "fr-command-unstuff.h"
34
35 static void fr_command_unstuff_class_init (FrCommandUnstuffClass *class);
36 static void fr_command_unstuff_init (FrCommand *afile);
37 static void fr_command_unstuff_finalize (GObject *object);
38
39 /* Parent Class */
40
41 static FrCommandClass *parent_class = NULL;
42
43 /* recursive rmdir to remove the left-overs from unstuff */
44 static void
recursive_rmdir(const char * path)45 recursive_rmdir (const char *path)
46 {
47 GDir *dir;
48 const char *dirname;
49
50 dir = g_dir_open (path, 0, NULL);
51 if (dir == NULL)
52 return;
53
54 dirname = g_dir_read_name (dir);
55 while (dirname != NULL)
56 {
57 char *full_path;
58
59 if (strcmp (dirname, ".") == 0 || strcmp (dirname, "..") == 0)
60 continue;
61
62 full_path = g_build_filename (path, dirname, NULL);
63 recursive_rmdir (full_path);
64 g_free (full_path);
65
66 dirname = g_dir_read_name (dir);
67 }
68
69 rmdir (path);
70
71 g_dir_close (dir);
72 }
73
74
75 /* unstuff doesn't like file paths starting with /, that's so shite */
76 static char *
unstuff_is_shit_with_filenames(const char * orig)77 unstuff_is_shit_with_filenames (const char *orig)
78 {
79 int i, num_slashes;
80 char *current_dir, *filename;
81
82 g_return_val_if_fail (orig != NULL, NULL);
83
84 current_dir = g_get_current_dir ();
85 i = num_slashes = 0;
86 while (current_dir[i] != '\0') {
87 if (current_dir[i] == '/')
88 num_slashes++;
89 i++;
90 }
91 g_free (current_dir);
92
93 /* 3 characters for each ../ plus filename length plus \0 */
94 filename = g_malloc (3 * i + strlen (orig) + 1);
95 i = 0;
96 for ( ; num_slashes > 0 ; num_slashes--) {
97 memcpy (filename + i, "../", 3);
98 i+=3;
99 }
100 memcpy (filename + i, orig, strlen (orig) + 1);
101
102 return filename;
103 }
104
105
106 static void
process_line(char * line,gpointer data)107 process_line (char *line,
108 gpointer data)
109 {
110 FrCommand *comm = FR_COMMAND (data);
111 FrCommandUnstuff *unstuff_comm = FR_COMMAND_UNSTUFF (comm);
112 const char *str_start;
113 char *filename, *real_filename;
114 int i;
115 FileData *fdata;
116
117 g_return_if_fail (line != NULL);
118
119 if (strstr (line, "progressEvent - ")) {
120 const char *ssize;
121 guint size;
122
123 ssize = strstr (line, "progressEvent - ")
124 + strlen ("progressEvent - ");
125 if (ssize[0] == '\0')
126 size = 0;
127 else
128 size = g_ascii_strtoull (ssize, NULL, 10);
129
130 if (unstuff_comm->fdata != NULL)
131 unstuff_comm->fdata->size = size;
132
133 return;
134 }
135
136 if (strstr (line, "fileEvent") == NULL)
137 return;
138 if (strstr (line, unstuff_comm->target_dir + 1) == NULL)
139 return;
140
141 /* Look for the filename, ends with a comma */
142 str_start = strstr (line, unstuff_comm->target_dir + 1);
143 str_start = str_start + strlen (unstuff_comm->target_dir) - 1;
144 if (str_start[0] != '/')
145 str_start--;
146 if (str_start[0] == '.')
147 str_start--;
148 i = 0;
149 while (str_start[i] != '\0' && str_start[i] != ',') {
150 i++;
151 }
152 /* This is not supposed to happen */
153 g_return_if_fail (str_start[i] != '\0');
154 filename = g_strndup (str_start, i);
155
156 /* Same thing with the real filename */
157 str_start = strstr (line, unstuff_comm->target_dir);
158 i = 0;
159 while (str_start[i] != '\0' && str_start[i] != ',') {
160 i++;
161 }
162 real_filename = g_strndup (str_start, i);
163
164 fdata = file_data_new ();
165 fdata->full_path = filename;
166 fdata->original_path = filename;
167 fdata->link = NULL;
168 fdata->name = g_strdup (file_name_from_path (fdata->full_path));
169 fdata->path = remove_level_from_path (fdata->full_path);
170
171 fdata->size = 0;
172 fdata->modified = time (NULL);
173
174 unstuff_comm->fdata = fdata;
175 fr_command_add_file (comm, fdata);
176
177 unlink (real_filename);
178 g_free (real_filename);
179 }
180
181
182 static void
list__begin(gpointer data)183 list__begin (gpointer data)
184 {
185 FrCommandUnstuff *comm = data;
186
187 comm->fdata = NULL;
188 }
189
190
191 static void
fr_command_unstuff_list(FrCommand * comm)192 fr_command_unstuff_list (FrCommand *comm)
193 {
194 char *arg, *path;
195 char *filename;
196 char *path_dots;
197
198 fr_process_set_out_line_func (comm->process, process_line, comm);
199
200 fr_process_begin_command (comm->process, "unstuff");
201 fr_process_set_begin_func (comm->process, list__begin, comm);
202 fr_process_add_arg (comm->process, "--trace");
203
204 /* Actually unpack everything in a temporary directory */
205 path = get_temp_work_dir (NULL);
206 path_dots = unstuff_is_shit_with_filenames (path);
207 g_free (path);
208
209 arg = g_strdup_printf ("-d=%s", path_dots);
210 FR_COMMAND_UNSTUFF (comm)->target_dir = path_dots;
211 fr_process_add_arg (comm->process, arg);
212 g_free (arg);
213
214 filename = unstuff_is_shit_with_filenames (comm->filename);
215 fr_process_add_arg (comm->process, filename);
216 g_free (filename);
217 fr_process_end_command (comm->process);
218 fr_process_start (comm->process);
219 }
220
221
222 static void
fr_command_unstuff_extract(FrCommand * comm,const char * from_file,GList * file_list,const char * dest_dir,gboolean overwrite,gboolean skip_older,gboolean junk_paths)223 fr_command_unstuff_extract (FrCommand *comm,
224 const char *from_file,
225 GList *file_list,
226 const char *dest_dir,
227 gboolean overwrite,
228 gboolean skip_older,
229 gboolean junk_paths)
230 {
231 #if 0
232 GList *scan;
233 #endif
234 char *filename;
235
236 fr_process_begin_command (comm->process, "unstuff");
237
238 if (dest_dir != NULL) {
239 char *dest_dir_dots;
240 char *arg;
241
242 dest_dir_dots = unstuff_is_shit_with_filenames (dest_dir);
243 arg = g_strdup_printf ("-d=%s", dest_dir_dots);
244 fr_process_add_arg (comm->process, arg);
245 FR_COMMAND_UNSTUFF (comm)->target_dir = NULL;
246 g_free (arg);
247 g_free (dest_dir_dots);
248 }
249
250 fr_process_add_arg (comm->process, "--trace");
251
252 /* unstuff doesn't like file paths starting with /, that's so shite */
253 filename = unstuff_is_shit_with_filenames (comm->filename);
254 fr_process_add_arg (comm->process, filename);
255 g_free (filename);
256
257 /* FIXME it is not possible to unpack only some files */
258 #if 0
259 for (scan = file_list; scan; scan = scan->next)
260 fr_process_add_arg (comm->process, scan->data);
261 #endif
262
263 fr_process_end_command (comm->process);
264 }
265
266
267 static void
fr_command_unstuff_handle_error(FrCommand * comm,FrProcError * error)268 fr_command_unstuff_handle_error (FrCommand *comm,
269 FrProcError *error)
270 {
271 if ((error->type != FR_PROC_ERROR_NONE)
272 && (error->status <= 1))
273 {
274 error->type = FR_PROC_ERROR_NONE;
275 }
276 }
277
278
279 const char *unstuff_mime_type[] = { "application/x-stuffit", NULL };
280
281
282 static const char **
fr_command_unstuff_get_mime_types(FrCommand * comm)283 fr_command_unstuff_get_mime_types (FrCommand *comm)
284 {
285 return unstuff_mime_type;
286 }
287
288
289 static FrCommandCap
fr_command_unstuff_get_capabilities(FrCommand * comm,const char * mime_type,gboolean check_command)290 fr_command_unstuff_get_capabilities (FrCommand *comm,
291 const char *mime_type,
292 gboolean check_command)
293 {
294 FrCommandCap capabilities;
295
296 capabilities = FR_COMMAND_CAN_ARCHIVE_MANY_FILES;
297 if (is_program_available ("unstuff", check_command))
298 capabilities |= FR_COMMAND_CAN_READ;
299
300 return capabilities;
301 }
302
303
304 static const char *
fr_command_unstaff_get_packages(FrCommand * comm,const char * mime_type)305 fr_command_unstaff_get_packages (FrCommand *comm,
306 const char *mime_type)
307 {
308 return PACKAGES ("unstaff");
309 }
310
311
312 static void
fr_command_unstuff_class_init(FrCommandUnstuffClass * class)313 fr_command_unstuff_class_init (FrCommandUnstuffClass *class)
314 {
315 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
316 FrCommandClass *afc;
317
318 parent_class = g_type_class_peek_parent (class);
319 afc = (FrCommandClass*) class;
320
321 gobject_class->finalize = fr_command_unstuff_finalize;
322
323 afc->list = fr_command_unstuff_list;
324 afc->add = NULL;
325 afc->delete_ = NULL;
326 afc->extract = fr_command_unstuff_extract;
327 afc->handle_error = fr_command_unstuff_handle_error;
328 afc->get_mime_types = fr_command_unstuff_get_mime_types;
329 afc->get_capabilities = fr_command_unstuff_get_capabilities;
330 afc->get_packages = fr_command_unstaff_get_packages;
331 }
332
333
334 static void
fr_command_unstuff_init(FrCommand * comm)335 fr_command_unstuff_init (FrCommand *comm)
336 {
337 comm->propAddCanUpdate = FALSE;
338 comm->propAddCanReplace = FALSE;
339 comm->propExtractCanAvoidOverwrite = FALSE;
340 comm->propExtractCanSkipOlder = FALSE;
341 comm->propExtractCanJunkPaths = FALSE;
342 comm->propPassword = TRUE;
343 comm->propTest = FALSE;
344 }
345
346
347 static void
fr_command_unstuff_finalize(GObject * object)348 fr_command_unstuff_finalize (GObject *object)
349 {
350 FrCommandUnstuff *unstuff_comm = FR_COMMAND_UNSTUFF (object);
351 g_return_if_fail (object != NULL);
352 g_return_if_fail (FR_IS_COMMAND_UNSTUFF (object));
353
354 if (unstuff_comm->target_dir != NULL) {
355 recursive_rmdir (unstuff_comm->target_dir);
356 g_free (unstuff_comm->target_dir);
357 }
358
359 /* Chain up */
360 if (G_OBJECT_CLASS (parent_class)->finalize)
361 G_OBJECT_CLASS (parent_class)->finalize (object);
362 }
363
364
365 GType
fr_command_unstuff_get_type()366 fr_command_unstuff_get_type ()
367 {
368 static GType type = 0;
369
370 if (! type) {
371 GTypeInfo type_info = {
372 sizeof (FrCommandUnstuffClass),
373 NULL,
374 NULL,
375 (GClassInitFunc) fr_command_unstuff_class_init,
376 NULL,
377 NULL,
378 sizeof (FrCommandUnstuff),
379 0,
380 (GInstanceInitFunc) fr_command_unstuff_init
381 };
382
383 type = g_type_register_static (FR_TYPE_COMMAND,
384 "FRCommandUnstuff",
385 &type_info,
386 0);
387 }
388
389 return type;
390 }
391