~ubuntu-branches/debian/jessie/file-roller/jessie

« back to all changes in this revision

Viewing changes to src/fr-command-unarchiver.c

  • Committer: Package Import Robot
  • Author(s): Andreas Henriksson
  • Date: 2013-05-25 13:13:26 UTC
  • mfrom: (1.8.1) (5.1.10 experimental)
  • Revision ID: package-import@ubuntu.com-20130525131326-bm7bv6b4yig6820h
Tags: 3.8.2-1
* New upstream release.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 
2
 
 
3
/*
 
4
 *  File-Roller
 
5
 *
 
6
 *  Copyright (C) 2012 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
#define _XOPEN_SOURCE       /* See feature_test_macros(7) */
 
24
#include <time.h>
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <string.h>
 
28
#include <glib.h>
 
29
#include <glib/gi18n.h>
 
30
#include <json-glib/json-glib.h>
 
31
#include "file-data.h"
 
32
#include "file-utils.h"
 
33
#include "gio-utils.h"
 
34
#include "glib-utils.h"
 
35
#include "fr-command.h"
 
36
#include "fr-command-unarchiver.h"
 
37
#include "fr-error.h"
 
38
 
 
39
#define LSAR_SUPPORTED_FORMAT 2
 
40
#define LSAR_DATE_FORMAT "%Y-%m-%d %H:%M:%S %z"
 
41
 
 
42
 
 
43
G_DEFINE_TYPE (FrCommandUnarchiver, fr_command_unarchiver, FR_TYPE_COMMAND)
 
44
 
 
45
 
 
46
/* -- list -- */
 
47
 
 
48
 
 
49
static void
 
50
process_line (char     *line,
 
51
              gpointer  data)
 
52
{
 
53
        FrCommandUnarchiver *unar_comm = FR_COMMAND_UNARCHIVER (data);
 
54
        g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (unar_comm->stream), line, -1, NULL);
 
55
}
 
56
 
 
57
 
 
58
static time_t
 
59
mktime_from_string (const char *time_s)
 
60
{
 
61
        struct tm tm = {0, };
 
62
        tm.tm_isdst = -1;
 
63
        strptime (time_s, LSAR_DATE_FORMAT, &tm);
 
64
        return mktime (&tm);
 
65
}
 
66
 
 
67
static void
 
68
list_command_completed (gpointer data)
 
69
{
 
70
        FrCommandUnarchiver *unar_comm = FR_COMMAND_UNARCHIVER (data);
 
71
        JsonParser          *parser;
 
72
        GError              *error = NULL;
 
73
 
 
74
        parser = json_parser_new ();
 
75
        if (json_parser_load_from_stream (parser, unar_comm->stream, NULL, &error)) {
 
76
                JsonObject *root;
 
77
 
 
78
                root = json_node_get_object (json_parser_get_root (parser));
 
79
 
 
80
                if (json_object_get_int_member (root, "lsarFormatVersion") == LSAR_SUPPORTED_FORMAT) {
 
81
                        JsonArray *content;
 
82
                        int        i;
 
83
 
 
84
                        content = json_object_get_array_member (root, "lsarContents");
 
85
                        for (i = 0; i < json_array_get_length (content); i++) {
 
86
                                JsonObject *entry;
 
87
                                FileData   *fdata;
 
88
                                const char *filename;
 
89
 
 
90
                                entry = json_array_get_object_element (content, i);
 
91
                                fdata = file_data_new ();
 
92
                                fdata->size = json_object_get_int_member (entry, "XADFileSize");
 
93
                                fdata->modified = mktime_from_string (json_object_get_string_member (entry, "XADLastModificationDate"));
 
94
                                if (json_object_has_member (entry, "XADIsEncrypted"))
 
95
                                        fdata->encrypted = json_object_get_int_member (entry, "XADIsEncrypted") == 1;
 
96
 
 
97
                                filename = json_object_get_string_member (entry, "XADFileName");
 
98
                                if (*filename == '/') {
 
99
                                        fdata->full_path = g_strdup (filename);
 
100
                                        fdata->original_path = fdata->full_path;
 
101
                                }
 
102
                                else {
 
103
                                        fdata->full_path = g_strconcat ("/", filename, NULL);
 
104
                                        fdata->original_path = fdata->full_path + 1;
 
105
                                }
 
106
 
 
107
                                fdata->link = NULL;
 
108
                                if (json_object_has_member (entry, "XADIsDirectory"))
 
109
                                        fdata->dir = json_object_get_int_member (entry, "XADIsDirectory") == 1;
 
110
                                if (fdata->dir)
 
111
                                        fdata->name = _g_path_get_dir_name (fdata->full_path);
 
112
                                else
 
113
                                        fdata->name = g_strdup (_g_path_get_basename (fdata->full_path));
 
114
                                fdata->path = _g_path_remove_level (fdata->full_path);
 
115
 
 
116
                                fr_archive_add_file (FR_ARCHIVE (unar_comm), fdata);
 
117
                        }
 
118
                }
 
119
        }
 
120
 
 
121
        g_object_unref (parser);
 
122
}
 
123
 
 
124
 
 
125
static gboolean
 
126
fr_command_unarchiver_list (FrCommand  *comm)
 
127
{
 
128
        FrCommandUnarchiver *unar_comm = FR_COMMAND_UNARCHIVER (comm);
 
129
 
 
130
        _g_object_unref (unar_comm->stream);
 
131
        unar_comm->stream = g_memory_input_stream_new ();
 
132
 
 
133
        fr_process_set_out_line_func (comm->process, process_line, comm);
 
134
 
 
135
        fr_process_begin_command (comm->process, "lsar");
 
136
        fr_process_set_end_func (comm->process, list_command_completed, comm);
 
137
        fr_process_add_arg (comm->process, "-j");
 
138
        if ((FR_ARCHIVE (comm)->password != NULL) && (FR_ARCHIVE (comm)->password[0] != '\0'))
 
139
                fr_process_add_arg_concat (comm->process, "-password=", FR_ARCHIVE (comm)->password, NULL);
 
140
        fr_process_add_arg (comm->process, comm->filename);
 
141
        fr_process_end_command (comm->process);
 
142
 
 
143
        return TRUE;
 
144
}
 
145
 
 
146
 
 
147
static void
 
148
process_line__extract (char     *line,
 
149
                       gpointer  data)
 
150
{
 
151
        FrCommand           *comm = FR_COMMAND (data);
 
152
        FrArchive           *archive = FR_ARCHIVE (comm);
 
153
        FrCommandUnarchiver *unar_comm = FR_COMMAND_UNARCHIVER (comm);
 
154
 
 
155
        if (line == NULL)
 
156
                return;
 
157
 
 
158
        unar_comm->n_line++;
 
159
 
 
160
        /* the first line is the name of the archive */
 
161
        if (unar_comm->n_line == 1)
 
162
                return;
 
163
 
 
164
        if (fr_archive_progress_get_total_files (archive) > 1)
 
165
                fr_archive_progress (archive, fr_archive_progress_inc_completed_files (archive, 1));
 
166
        else
 
167
                fr_archive_message (archive, line);
 
168
}
 
169
 
 
170
 
 
171
static void
 
172
fr_command_unarchiver_extract (FrCommand  *comm,
 
173
                               const char *from_file,
 
174
                               GList      *file_list,
 
175
                               const char *dest_dir,
 
176
                               gboolean    overwrite,
 
177
                               gboolean    skip_older,
 
178
                               gboolean    junk_paths)
 
179
{
 
180
        FrCommandUnarchiver *unar_comm = FR_COMMAND_UNARCHIVER (comm);
 
181
        GList               *scan;
 
182
 
 
183
        unar_comm->n_line = 0;
 
184
 
 
185
        fr_process_use_standard_locale (comm->process, TRUE);
 
186
        fr_process_set_out_line_func (comm->process,
 
187
                                      process_line__extract,
 
188
                                      comm);
 
189
 
 
190
        fr_process_begin_command (comm->process, "unar");
 
191
 
 
192
        if (overwrite)
 
193
                fr_process_add_arg (comm->process, "-f");
 
194
        else
 
195
                fr_process_add_arg (comm->process, "-s");
 
196
 
 
197
        fr_process_add_arg (comm->process, "-D");
 
198
 
 
199
        if ((FR_ARCHIVE (comm)->password != NULL) && (FR_ARCHIVE (comm)->password[0] != '\0'))
 
200
                fr_process_add_arg_concat (comm->process, "-password=", FR_ARCHIVE (comm)->password, NULL);
 
201
 
 
202
        if (dest_dir != NULL)
 
203
                fr_process_add_arg_concat (comm->process, "-output-directory=", dest_dir, NULL);
 
204
 
 
205
        fr_process_add_arg (comm->process, comm->filename);
 
206
 
 
207
        for (scan = file_list; scan; scan = scan->next)
 
208
                fr_process_add_arg (comm->process, scan->data);
 
209
 
 
210
        fr_process_end_command (comm->process);
 
211
}
 
212
 
 
213
 
 
214
static void
 
215
fr_command_unarchiver_handle_error (FrCommand   *comm,
 
216
                                    FrError *error)
 
217
{
 
218
        GList *scan;
 
219
 
 
220
#if 0
 
221
        {
 
222
                for (scan = g_list_last (comm->process->err.raw); scan; scan = scan->prev)
 
223
                        g_print ("%s\n", (char*)scan->data);
 
224
        }
 
225
#endif
 
226
 
 
227
        if (error->type == FR_ERROR_NONE)
 
228
                return;
 
229
 
 
230
        for (scan = g_list_last (comm->process->err.raw); scan; scan = scan->prev) {
 
231
                char *line = scan->data;
 
232
 
 
233
                if (strstr (line, "password") != NULL) {
 
234
                        fr_error_take_gerror (error, g_error_new_literal (FR_ERROR, FR_ERROR_ASK_PASSWORD, ""));
 
235
                        break;
 
236
                }
 
237
        }
 
238
}
 
239
 
 
240
 
 
241
const char *unarchiver_mime_type[] = { "application/x-cbr",
 
242
                                       "application/x-rar",
 
243
                                       NULL };
 
244
 
 
245
 
 
246
static const char **
 
247
fr_command_unarchiver_get_mime_types (FrArchive *archive)
 
248
{
 
249
        return unarchiver_mime_type;
 
250
}
 
251
 
 
252
 
 
253
static FrArchiveCap
 
254
fr_command_unarchiver_get_capabilities (FrArchive  *archive,
 
255
                                        const char *mime_type,
 
256
                                        gboolean    check_command)
 
257
{
 
258
        FrArchiveCap capabilities;
 
259
 
 
260
        capabilities = FR_ARCHIVE_CAN_DO_NOTHING;
 
261
        if (_g_program_is_available ("lsar", check_command) && _g_program_is_available ("unar", check_command))
 
262
                capabilities |= FR_ARCHIVE_CAN_READ;
 
263
 
 
264
        return capabilities;
 
265
}
 
266
 
 
267
 
 
268
static const char *
 
269
fr_command_unarchiver_get_packages (FrArchive  *archive,
 
270
                                    const char *mime_type)
 
271
{
 
272
        return PACKAGES ("unarchiver");
 
273
}
 
274
 
 
275
 
 
276
static void
 
277
fr_command_unarchiver_finalize (GObject *object)
 
278
{
 
279
        FrCommandUnarchiver *self;
 
280
 
 
281
        g_return_if_fail (object != NULL);
 
282
        g_return_if_fail (FR_IS_COMMAND_UNARCHIVER (object));
 
283
 
 
284
        self = FR_COMMAND_UNARCHIVER (object);
 
285
        _g_object_unref (self->stream);
 
286
 
 
287
        if (G_OBJECT_CLASS (fr_command_unarchiver_parent_class)->finalize)
 
288
                G_OBJECT_CLASS (fr_command_unarchiver_parent_class)->finalize (object);
 
289
}
 
290
 
 
291
 
 
292
static void
 
293
fr_command_unarchiver_class_init (FrCommandUnarchiverClass *klass)
 
294
{
 
295
        GObjectClass   *gobject_class;
 
296
        FrArchiveClass *archive_class;
 
297
        FrCommandClass *command_class;
 
298
 
 
299
        fr_command_unarchiver_parent_class = g_type_class_peek_parent (klass);
 
300
 
 
301
        gobject_class = G_OBJECT_CLASS (klass);
 
302
        gobject_class->finalize = fr_command_unarchiver_finalize;
 
303
 
 
304
        archive_class = FR_ARCHIVE_CLASS (klass);
 
305
        archive_class->get_mime_types   = fr_command_unarchiver_get_mime_types;
 
306
        archive_class->get_capabilities = fr_command_unarchiver_get_capabilities;
 
307
        archive_class->get_packages     = fr_command_unarchiver_get_packages;
 
308
 
 
309
        command_class = FR_COMMAND_CLASS (klass);
 
310
        command_class->list             = fr_command_unarchiver_list;
 
311
        command_class->extract          = fr_command_unarchiver_extract;
 
312
        command_class->handle_error     = fr_command_unarchiver_handle_error;
 
313
}
 
314
 
 
315
 
 
316
static void
 
317
fr_command_unarchiver_init (FrCommandUnarchiver *self)
 
318
{
 
319
        FrArchive *base = FR_ARCHIVE (self);
 
320
 
 
321
        base->propExtractCanAvoidOverwrite = TRUE;
 
322
        base->propExtractCanSkipOlder      = FALSE;
 
323
        base->propExtractCanJunkPaths      = FALSE;
 
324
        base->propPassword                 = TRUE;
 
325
        base->propTest                     = FALSE;
 
326
        base->propListFromFile             = FALSE;
 
327
 
 
328
        self->stream = NULL;
 
329
}