1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 *  File-Roller
 *
 *  Copyright (C) 2012 The Free Software Foundation, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define _XOPEN_SOURCE       /* See feature_test_macros(7) */
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <json-glib/json-glib.h>
#include "file-data.h"
#include "file-utils.h"
#include "gio-utils.h"
#include "glib-utils.h"
#include "fr-command.h"
#include "fr-command-unarchiver.h"
#include "fr-error.h"

#define LSAR_SUPPORTED_FORMAT 2
#define LSAR_DATE_FORMAT "%Y-%m-%d %H:%M:%S %z"

static void fr_command_unarchiver_class_init  (FrCommandUnarchiverClass *class);
static void fr_command_unarchiver_init        (FrCommand         *afile);
static void fr_command_unarchiver_finalize    (GObject           *object);

/* Parent Class */

static FrCommandClass *parent_class = NULL;


/* -- list -- */


static void
process_line (char     *line,
	      gpointer  data)
{
	FrCommandUnarchiver *unar_comm = FR_COMMAND_UNARCHIVER (data);
	g_string_append (unar_comm->stream, line);
}


static time_t
mktime_from_string (const char *time_s)
{
	struct tm tm = {0, };
	tm.tm_isdst = -1;
	strptime (time_s, LSAR_DATE_FORMAT, &tm);
	return mktime (&tm);
}

static void
list_command_completed (gpointer data)
{
	FrCommand           *comm = FR_COMMAND (data);
	FrCommandUnarchiver *unar_comm = FR_COMMAND_UNARCHIVER (data);
	JsonParser          *parser;
	gchar               *json_data;
	GError              *error = NULL;
	gboolean             failed = FALSE;

	parser = json_parser_new ();

	json_data = g_string_free (unar_comm->stream, FALSE);

	if (json_parser_load_from_data (parser, json_data, -1, &error)) {
		JsonObject *root;

		root = json_node_get_object (json_parser_get_root (parser));

		if (json_object_get_int_member (root, "lsarFormatVersion") == LSAR_SUPPORTED_FORMAT) {
			JsonArray *content;
			guint      i;

			content = json_object_get_array_member (root, "lsarContents");
			for (i = 0; i < json_array_get_length (content); i++) {
				JsonObject *entry;
				FileData   *fdata;
				const char *filename;

				entry = json_array_get_object_element (content, i);
				fdata = file_data_new ();
				fdata->size = json_object_get_int_member (entry, "XADFileSize");
				fdata->modified = mktime_from_string (json_object_get_string_member (entry, "XADLastModificationDate"));
				if (json_object_has_member (entry, "XADIsEncrypted"))
					fdata->encrypted = json_object_get_int_member (entry, "XADIsEncrypted") == 1;

				filename = json_object_get_string_member (entry, "XADFileName");
				if (*filename == '/') {
					fdata->full_path = g_strdup (filename);
					fdata->original_path = fdata->full_path;
				}
				else {
					fdata->full_path = g_strconcat ("/", filename, NULL);
					fdata->original_path = fdata->full_path + 1;
				}

				fdata->link = NULL;
				if (json_object_has_member (entry, "XADIsDirectory"))
					fdata->dir = json_object_get_int_member (entry, "XADIsDirectory") == 1;
				if (fdata->dir)
					fdata->name = dir_name_from_path (fdata->full_path);
				else
					fdata->name = g_strdup (file_name_from_path (fdata->full_path));
				fdata->path = remove_level_from_path (fdata->full_path);

				fr_command_add_file (FR_COMMAND (unar_comm), fdata);
			}

			if ((json_object_has_member (json_object_get_object_member (root, "lsarProperties"), "XADVolumeScanningFailed")) ||
			    (json_object_has_member (json_object_get_object_member (root, "lsarProperties"), "XADIsCorrupted")))
					failed = TRUE;
		}
	}

	if (failed) {
		fr_process_stop (comm->process);

		if (comm->process->out.raw != NULL)
			comm->process->out.raw = g_list_reverse (comm->process->out.raw);

		unar_comm->error.type = FR_PROC_ERROR_COMMAND_ERROR;
		unar_comm->error.status = 1;
		g_signal_emit_by_name (G_OBJECT (comm),
				       "done",
				       comm->action,
				       &unar_comm->error);

		if (comm->process->out.raw != NULL)
			comm->process->out.raw = g_list_reverse (comm->process->out.raw);
	}

	g_object_unref (parser);
	g_free (json_data);
}


static void
fr_command_unarchiver_list (FrCommand  *comm)
{
	FrCommandUnarchiver *unar_comm = FR_COMMAND_UNARCHIVER (comm);

	unar_comm->stream = g_string_new ("");

	fr_process_set_out_line_func (comm->process, process_line, comm);

	fr_process_begin_command (comm->process, "lsar");
	fr_process_set_end_func (comm->process, list_command_completed, comm);
	fr_process_add_arg (comm->process, "-j");
	if ((comm->password != NULL) && (comm->password[0] != '\0'))
		fr_process_add_arg_concat (comm->process, "-password=", comm->password, NULL);
	fr_process_add_arg (comm->process, comm->filename);
	fr_process_end_command (comm->process);

	fr_process_start (comm->process);
}


static void
process_line__extract (char     *line,<--- Parameter 'line' can be declared as pointer to const
		       gpointer  data)
{
	FrCommand           *comm = FR_COMMAND (data);
	FrCommandUnarchiver *unar_comm = FR_COMMAND_UNARCHIVER (comm);

	if (line == NULL)
		return;

	unar_comm->n_line++;

	/* the first line is the name of the archive */
	if (unar_comm->n_line == 1)
		return;

	if (comm->n_files > 1) {
		double fraction = (double) ++comm->n_file / (comm->n_files + 1);
		fr_command_progress (comm, CLAMP (fraction, 0.0, 1.0));
	}
	else
		fr_command_message (comm, line);
}


static void
fr_command_unarchiver_extract (FrCommand  *comm,
			       const char *from_file,
			       GList      *file_list,
			       const char *dest_dir,
			       gboolean    overwrite,
			       gboolean    skip_older,
			       gboolean    junk_paths)
{
	FrCommandUnarchiver *unar_comm = FR_COMMAND_UNARCHIVER (comm);
	GList               *scan;

	unar_comm->n_line = 0;

	fr_process_use_standard_locale (comm->process, TRUE);
	fr_process_set_out_line_func (comm->process,
				      process_line__extract,<--- You might need to cast the function pointer here
				      comm);

	fr_process_begin_command (comm->process, "unar");

	if (overwrite)
		fr_process_add_arg (comm->process, "-f");
	else
		fr_process_add_arg (comm->process, "-s");

	fr_process_add_arg (comm->process, "-D");

	if ((comm->password != NULL) && (comm->password[0] != '\0'))
		fr_process_add_arg_concat (comm->process, "-password=", comm->password, NULL);

	if (dest_dir != NULL)
		fr_process_add_arg_concat (comm->process, "-output-directory=", dest_dir, NULL);

	fr_process_add_arg (comm->process, comm->filename);

	for (scan = file_list; scan; scan = scan->next) {
		char *escaped;

		escaped = escape_str (scan->data, "[?");
		fr_process_add_arg (comm->process, escaped);
		g_free (escaped);
	}

	fr_process_end_command (comm->process);
}


static gboolean
password_required (GList *raw)
{
	GList *scan;

	for (scan = g_list_last (raw); scan; scan = scan->prev) {
		char *line = scan->data;

		if ((strstr (line, "This archive requires a password to unpack.") != NULL)  ||
		    (strstr (line, "Failed! (Missing or wrong password)") != NULL))
			return TRUE;
	}

	return FALSE;
}


static void
fr_command_unarchiver_handle_error (FrCommand   *comm,
				    FrProcError *error)
{
#if 0
	{
		for (scan = g_list_last (comm->process->err.raw); scan; scan = scan->prev)
			g_print ("%s\n", (char*)scan->data);
	}
#endif

	if (error->type == FR_PROC_ERROR_NONE)
		return;

	if ((error->type == FR_PROC_ERROR_COMMAND_ERROR) &&
	    (password_required (comm->process->err.raw) || password_required (comm->process->out.raw)))
		error->type = FR_PROC_ERROR_ASK_PASSWORD;
}


const char *unarchiver_mime_type[] = { "application/zip",
				       "application/x-cbr",
				       "application/x-rar",
				       NULL };


static const char **
fr_command_unarchiver_get_mime_types (FrCommand *comm)
{
	return unarchiver_mime_type;
}


static FrCommandCap
fr_command_unarchiver_get_capabilities (FrCommand  *comm,
					const char *mime_type,
					gboolean    check_command)
{
	FrCommandCap capabilities;

	capabilities = FR_COMMAND_CAN_DO_NOTHING;
	if (is_program_available ("lsar", check_command) && is_program_available ("unar", check_command))
		capabilities |= FR_COMMAND_CAN_READ;

	return capabilities;
}


static const char *
fr_command_unarchiver_get_packages (FrCommand  *comm,
				    const char *mime_type)
{
	return PACKAGES ("unarchiver");
}


static void
fr_command_unarchiver_class_init (FrCommandUnarchiverClass *class)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (class);
	FrCommandClass *afc;

	parent_class = g_type_class_peek_parent (class);
	afc = (FrCommandClass*) class;

	gobject_class->finalize = fr_command_unarchiver_finalize;

	afc->list             = fr_command_unarchiver_list;
	afc->extract          = fr_command_unarchiver_extract;
	afc->handle_error     = fr_command_unarchiver_handle_error;
	afc->get_mime_types   = fr_command_unarchiver_get_mime_types;
	afc->get_capabilities = fr_command_unarchiver_get_capabilities;
	afc->get_packages     = fr_command_unarchiver_get_packages;
}


static void
fr_command_unarchiver_init (FrCommand *comm)
{
	FrCommandUnarchiver *unar_comm;

	comm->propExtractCanAvoidOverwrite = TRUE;
	comm->propExtractCanSkipOlder      = FALSE;
	comm->propExtractCanJunkPaths      = FALSE;
	comm->propPassword                 = TRUE;
	comm->propTest                     = FALSE;
	comm->propListFromFile             = FALSE;

	unar_comm = FR_COMMAND_UNARCHIVER (comm);
	unar_comm->stream = NULL;
}


static void
fr_command_unarchiver_finalize (GObject *object)
{
	g_return_if_fail (object != NULL);
	g_return_if_fail (FR_IS_COMMAND_UNARCHIVER (object));

	/* Chain up */
	if (G_OBJECT_CLASS (parent_class)->finalize)
		G_OBJECT_CLASS (parent_class)->finalize (object);
}


GType
fr_command_unarchiver_get_type ()
{
	static GType type = 0;

	if (! type) {
		GTypeInfo type_info = {
			sizeof (FrCommandUnarchiverClass),
			NULL,
			NULL,
			(GClassInitFunc) fr_command_unarchiver_class_init,
			NULL,
			NULL,
			sizeof (FrCommandUnarchiver),
			0,
			(GInstanceInitFunc) fr_command_unarchiver_init,
			NULL
		};

		type = g_type_register_static (FR_TYPE_COMMAND,
					       "FrCommandUnarchiver",
					       &type_info,
					       0);
	}

	return type;
}