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