[r20]: cbrpager-maemo / branches / MAEMO_0_9_20 / src / garchivereadercommandline.c Maximize Restore History

Download this file

garchivereadercommandline.c    494 lines (445 with data), 13.1 kB

  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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
* garchivereadercommandline.c - routines to manipulate archive files
* using command line programs.
*
* Copyright (C) 2008 Benoit Goudreault-Emond (bgoudreaultemond@gmail.com).
* Inspired by code from John Coppens (john@jcoppens.com).
*
* 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <glib.h>
#include <unistd.h>
#include <sys/types.h> /* open */
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h> /* wait */
#include <string.h>
#include "garchivereadercommandline.h"
extern int debug;
static gboolean extension_handled(const char* filename)
{
static const char* extensions[] = {
".jpg", ".jpeg", ".png", ".gif",
".JPG", ".JPEG", ".PNG", ".GIF"
};
static const int nr_extensions = 8;
int i;
for(i = 0; i < nr_extensions; ++i)
{
if(strstr(filename, extensions[i]) != NULL)
return TRUE;
}
return FALSE;
}
static void set_last_error(GArchiveReaderCommandLine* reader, gchar* data)
{
if(reader->last_error)
g_free(reader->last_error);
reader->last_error = data;
}
static void
free_page_list(GArchiveReaderCommandLine* reader)
{
int i;
if(!reader->page_list)
return;
for(i = 0; i < reader->page_list->len; ++i)
g_free(g_array_index(reader->page_list, gchar*, i));
g_array_free(reader->page_list, TRUE);
reader->page_list = NULL;
}
static int
register_path(GArchiveReader* reader_iface,
const gchar* path)
{
GArchiveReaderCommandLine* reader = G_ARCHIVE_READER_COMMAND_LINE(reader_iface);
int check_result = g_archive_reader_check_magic(path, &reader->magic);
if(debug)
printf(_("Check result for type %s -> %d\n"), reader->magic.sign, check_result);
if(check_result <= 0)
return check_result;
if(reader->current_filename)
{
g_free(reader->current_filename);
free_page_list(reader);
}
reader->current_filename = g_strdup(path);
return 1;
}
static GArray*
get_file_list(GArchiveReader* reader_iface)
{
static const char* all_extensions =
"\\.jpg\\|\\.jpeg\\|\\.png\\|\\.gif\\|"
"\\.JPG\\|\\.JPEG\\|\\.PNG\\|\\.GIF";
int pfd[2], pfd2[2];
int pid_i, pid_j;
char *bff = NULL, **names = NULL;
int i;
gchar* prev_filename = NULL;
GIOChannel* channel;
gsize length;
GArchiveReaderCommandLine* reader = G_ARCHIVE_READER_COMMAND_LINE(reader_iface);
if(reader->page_list)
return reader->page_list;
if(!reader->current_filename)
return NULL;
prev_filename = reader->list_command[reader->list_command_archive_index];
if(prev_filename)
g_free(prev_filename);
reader->list_command[reader->list_command_archive_index] = g_strdup(reader->current_filename);
if(pipe(pfd) == -1)
{
printf(_("pipe failed\n"));
return NULL;
}
if(pipe(pfd2) == -1)
{
printf(_("pipe failed\n"));
close(pfd[0]);
close(pfd[1]);
return NULL;
}
pid_i = fork();
if(pid_i == -1)
{
printf(_("fork failed\n"));
return NULL;
}
/* Child 1: do command */
else if(pid_i == 0)
{
close(pfd[0]);
close(1);
if(dup(pfd[1]) != 1)
{
printf(_("dup failed\n"));
return NULL;
}
close(pfd[1]);
execvp(reader->list_command[0], reader->list_command);
/* never reached */
return NULL;
}
else
{
pid_j = fork();
if(pid_j == -1)
{
printf(_("fork failed\n"));
return NULL;
}
/* child 2: do grep */
else if(pid_j == 0)
{
close(pfd[1]);
close(0); /* close stdin */
if(dup(pfd[0]) != 0)
{
printf(_("dup failed\n"));
return NULL;
}
close(pfd[0]);
close(pfd2[0]);
close(1);
if(dup(pfd2[1]) != 1)
{
printf(_("dup failed\n"));
return NULL;
}
close(pfd2[1]);
execlp("grep", "grep", all_extensions, NULL);
/* never reached */
return NULL;
}
}
/* main process */
/* close all pipe ends */
close(pfd[0]);
close(pfd[1]);
close(pfd2[1]);
channel = g_io_channel_unix_new(pfd2[0]);
g_io_channel_read_to_end(channel, &bff, &length, NULL);
g_io_channel_unref(channel);
close(pfd2[0]);
/* wait children */
waitpid(pid_i, 0, 0);
waitpid(pid_j, 0, 0);
names = g_strsplit(bff, "\n", 0);
g_free(bff);
for(i = 0; names[i] != NULL; ++i)
{
gchar* filename;
if(!strlen(names[i]))
continue;
if(!extension_handled(names[i] + reader->magic.fn_offs))
continue;
/* assume 16 elements at first */
if(!reader->page_list)
reader->page_list = g_array_sized_new(FALSE, TRUE, sizeof(gchar*), 16);
filename = g_strdup(names[i] + reader->magic.fn_offs);
reader->page_list = g_array_append_val(reader->page_list, filename);
}
g_strfreev(names);
if(!reader->page_list)
{
set_last_error(reader, g_strdup_printf(_("No pages in archive '%s'"), reader->current_filename));
}
else
{
g_array_sort(reader->page_list,
g_archive_reader_translate_strategy(reader->strategy));
}
return reader->page_list;
}
static GdkPixbuf*
get_page(GArchiveReader* reader_iface,
int page_number,
gdouble* w,
gdouble* h)
{
const gchar* filename;
gchar* filename_esc = NULL;
gchar* prev_filename = NULL;
int sz;
int i;
int index = 0;
int pid;
GdkPixbuf* retval = NULL;
GdkPixbufLoader* loader;
int pfd[2];
ssize_t num_bytes;
GArchiveReaderCommandLine* reader = G_ARCHIVE_READER_COMMAND_LINE(reader_iface);
GArray* file_list = get_file_list(reader_iface);
if(page_number < 0 || !file_list || page_number >= file_list->len)
{
set_last_error(reader, g_strdup_printf(_("Page number %d out of range"), page_number));
return NULL;
}
filename = (const gchar*)g_array_index(file_list, gchar*, page_number);
if(reader->decompress_command_file_needs_escape)
{
sz = strlen(filename);
filename_esc = g_malloc(sz * 2 + 1);
for(i = 0; i < sz; ++i)
{
const char c = filename[i];
switch(c)
{
case '[':
case ']':
case '{':
case '}':
case '"':
filename_esc[index++] = '\\';
}
filename_esc[index++] = c;
}
filename_esc[index] = '\0';
filename = filename_esc;
}
prev_filename = reader->decompress_command[reader->decompress_command_archive_index];
if(prev_filename)
g_free(prev_filename);
reader->decompress_command[reader->decompress_command_archive_index] = g_strdup(reader->current_filename);
prev_filename = reader->decompress_command[reader->decompress_command_file_index];
if(prev_filename)
g_free(prev_filename);
reader->decompress_command[reader->decompress_command_file_index] = g_strdup(filename);
if(pipe(pfd) == -1)
{
set_last_error(reader, g_strdup(_("Pipe failed")));
return NULL;
}
/* The following patch was received from Mamoru Tasaka, 2008/05/22
to solve potential security problems with the system() call */
pid = fork();
switch(pid)
{
case -1:
set_last_error(reader, g_strdup(_("Fork failed")));
close(pfd[0]);
close(pfd[1]);
return NULL;
case 0:
close(pfd[0]);
close(1);
if(dup(pfd[1]) != 1)
{
return NULL;
}
close(pfd[1]);
execvp(reader->decompress_command[0],
reader->decompress_command);
/* never reached */
return NULL;
default:
close(pfd[1]);
}
/* if we are here, we're in the parent process */
/* create a pixbuf loader */
loader = gdk_pixbuf_loader_new();
/* read from buffer in 1KB chunks until we get to the end */
while((num_bytes = read(pfd[0], reader->buf, reader->buf_size)) > 0)
gdk_pixbuf_loader_write(loader, reader->buf, num_bytes, NULL);
close(pfd[0]);
gdk_pixbuf_loader_close(loader, NULL);
waitpid(pid, NULL, 0);
if(filename_esc)
{
g_free(filename_esc);
}
retval = gdk_pixbuf_loader_get_pixbuf(loader);
if(retval)
{
g_object_ref(retval);
if(w)
*w = gdk_pixbuf_get_width(retval);
if(h)
*h = gdk_pixbuf_get_height(retval);
}
else
{
set_last_error(reader, g_strdup_printf(_("Unable to load page %d"), page_number));
}
g_object_unref(loader);
return retval;
}
static gchar*
get_last_error(GArchiveReader* reader_iface)
{
GArchiveReaderCommandLine* reader = G_ARCHIVE_READER_COMMAND_LINE(reader_iface);
gchar* last_error = reader->last_error;
reader->last_error = NULL;
return last_error;
}
static void
set_sort_strategy(GArchiveReader* reader_iface, SortStrategy strategy)
{
GArchiveReaderCommandLine* reader = G_ARCHIVE_READER_COMMAND_LINE(reader_iface);
SortStrategy old_strategy = reader->strategy;
reader->strategy = strategy;
if(strategy != old_strategy && reader->page_list && reader->page_list->len > 0)
g_array_sort(reader->page_list, g_archive_reader_translate_strategy(strategy));
}
/* class instance housekeeping stuff */
static void
g_archive_reader_command_line_interface_init(gpointer g_iface,
gpointer iface_data)
{
GArchiveReaderClass* klass = (GArchiveReaderClass*)g_iface;
klass->register_path = register_path;
klass->get_file_list = get_file_list;
klass->get_page = get_page;
klass->get_last_error = get_last_error;
klass->set_sort_strategy = set_sort_strategy;
}
static void
g_archive_reader_command_line_init(GArchiveReaderCommandLine* reader)
{
reader->list_command = NULL;
reader->list_command_archive_index = 0;
reader->decompress_command = NULL;
reader->decompress_command_archive_index = 0;
reader->decompress_command_file_index = 0;
reader->decompress_command_file_needs_escape = FALSE;
reader->buf = NULL;
reader->buf_size = 0;
reader->magic.offs = 0;
reader->magic.len = 0;
reader->magic.fn_offs = 0;
reader->magic.sign = NULL;
reader->current_filename = NULL;
reader->last_error = NULL;
reader->page_list = NULL;
reader->strategy = SORT_ASCII;
}
G_DEFINE_TYPE_EXTENDED(GArchiveReaderCommandLine,
g_archive_reader_command_line,
G_TYPE_OBJECT,
0,
G_IMPLEMENT_INTERFACE(G_TYPE_ARCHIVE_READER,
g_archive_reader_command_line_interface_init));
static void
g_archive_reader_command_line_finalize (GObject* object)
{
GArchiveReaderCommandLine* reader = G_ARCHIVE_READER_COMMAND_LINE(object);
g_strfreev(reader->list_command);
g_strfreev(reader->decompress_command);
if(reader->current_filename)
g_free(reader->current_filename);
if(reader->last_error)
g_free(reader->last_error);
free_page_list(reader);
}
static void
g_archive_reader_command_line_class_init(GArchiveReaderCommandLineClass* klass)
{
GObjectClass* object_class = (GObjectClass*)klass;
object_class->finalize = g_archive_reader_command_line_finalize;
}
/* public interface implementation */
GArchiveReader* g_archive_reader_command_line_new(guchar* buf,
int buf_size,
magic_sign magic,
gchar** list_command,
gchar** decompress_command)
{
gpointer data = NULL;
GArchiveReaderCommandLine* reader = NULL;
gchar** iterator;
int index;
g_return_val_if_fail(list_command, NULL);
g_return_val_if_fail(decompress_command, NULL);
g_return_val_if_fail(buf, NULL);
g_return_val_if_fail(buf_size > 0, NULL);
/* copy properties from the parameters */
data = g_object_new(G_TYPE_ARCHIVE_READER_COMMAND_LINE, NULL);
reader = G_ARCHIVE_READER_COMMAND_LINE(data);
reader->magic = magic;
reader->current_filename = NULL;
reader->last_error = NULL;
reader->page_list = NULL;
reader->buf = buf;
reader->buf_size = buf_size;
reader->strategy = SORT_ASCII;
reader->list_command = g_strdupv(list_command);
reader->decompress_command = g_strdupv(decompress_command);
for(iterator = reader->list_command, index = 0; *iterator; ++iterator, ++index)
{
if(!strcmp("%a", *iterator))
{
reader->list_command_archive_index = index;
break;
}
}
for(iterator = reader->decompress_command, index = 0; *iterator; ++iterator, ++index)
{
if(!strcmp("%a", *iterator))
reader->decompress_command_archive_index = index;
else if(!strcmp("%f", *iterator))
{
reader->decompress_command_file_index = index;
reader->decompress_command_file_needs_escape = FALSE;
}
else if(!strcmp("%F", *iterator))
{
reader->decompress_command_file_index = index;
reader->decompress_command_file_needs_escape = TRUE;
}
}
return G_ARCHIVE_READER(data);
}