e4d7cb3816faf974f92295c4cdfbab6d10c5afac
[claws.git] / src / plugins / archive / libarchive_archive.c
1 /* vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: */
2
3 /*
4  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
5  * Copyright (C) 1999-2008 Michael Rasmussen and the Claws Mail Team
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  * 
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #include "claws-features.h"
25 #endif
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29
30 #include "libarchive_archive.h"
31
32 #ifndef _TEST
33 #       include "archiver.h"
34 #       include "utils.h"
35 #       include "mainwindow.h"
36 #       include "folder.h"
37 #endif
38
39 #include <sys/types.h>
40 #include <sys/stat.h>
41
42 #include <archive.h>
43 #include <archive_entry.h>
44 #include <fcntl.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <dirent.h>
50 #include <glib.h>
51 #include <libgen.h>
52
53 #define READ_BLOCK_SIZE 10240
54
55 struct file_info {
56         char* path;
57         char* name;
58 };
59
60 static GSList* msg_trash_list = NULL;
61 static GSList* file_list = NULL;
62 static gboolean stop_action = FALSE;
63
64 #ifdef _TEST
65 static int permissions = 0;
66 #endif
67
68 static void free_msg_trash(MsgTrash* trash) {
69     if (trash) {
70         debug_print("Freeing files in %s\n", folder_item_get_name(trash->item));
71         if (trash->msgs) {
72             g_slist_free(trash->msgs);
73         }
74         g_free(trash);
75     }
76 }
77
78 MsgTrash* new_msg_trash(FolderItem* item) {
79     MsgTrash* msg_trash;
80     FolderType  type;
81
82     g_return_val_if_fail(item != NULL, NULL);
83
84     /* FolderType must be F_MH, F_MBOX, F_MAILDIR or F_IMAP */
85     type = item->folder->klass->type;
86     if (!(type == F_MH || type == F_MBOX || 
87             type == F_MAILDIR || type == F_IMAP))
88        return NULL; 
89     msg_trash = g_new0(MsgTrash, 1);
90     msg_trash->item = item;
91     msg_trash->msgs = NULL;
92     msg_trash_list = g_slist_prepend(msg_trash_list, msg_trash);
93     
94     return msg_trash;
95     }
96
97 void archive_free_archived_files() {
98     MsgTrash* mt = NULL;
99     gint    res;
100     GSList* l = NULL;
101    
102     for (l = msg_trash_list; l; l = g_slist_next(l)) {
103         mt = (MsgTrash *) l->data;
104         debug_print("Trashing messages in folder: %s\n", 
105                 folder_item_get_name(mt->item));
106         res = folder_item_remove_msgs(mt->item, mt->msgs);
107         debug_print("Result was %d\n", res);
108         free_msg_trash(mt);
109     }
110     g_slist_free(msg_trash_list);
111     msg_trash_list = NULL;
112 }
113
114 void archive_add_msg_mark(MsgTrash* trash, MsgInfo* msg) {
115     g_return_if_fail(trash != NULL || msg != NULL);
116     debug_print("Marking msg #%d for removal\n", msg->msgnum);
117     trash->msgs = g_slist_prepend(trash->msgs, msg);
118 }
119
120 static void free_all(GDate* date, gchar** parts) {
121     if (date)
122         g_date_free(date);
123     if (parts)
124         g_strfreev(parts);
125 }
126
127 static gboolean is_iso_string(gchar** items) {
128     int i = -1;
129     gchar* item;
130
131     while (*items) {
132         i++;
133         item = *items++;
134         debug_print("Date part %d: %s\n", i, item);
135         switch(i) {
136             case 0:
137                 if (strlen(item) != 4)
138                     return FALSE;
139                 break;
140             case 1:
141             case 2:
142                 if (strlen(item) != 2)
143                     return FALSE;
144                 break;
145             default:
146                 return FALSE;
147         }
148     }
149     debug_print("Leaving\n");
150     return (i == 2);
151 }
152
153 static GDate* iso2GDate(const gchar* date) {
154     GDate*  gdate;
155     gchar** parts = NULL;
156     int     i;
157
158     g_return_val_if_fail(date != NULL, NULL);
159
160     gdate = g_date_new();
161     parts = g_strsplit(date, "-", 3);
162     if (! is_iso_string(parts))
163         return NULL;
164     if (!parts)
165         return NULL;
166     for (i = 0; i < 3; i++) {
167         int t = atoi(parts[i]);
168         switch (i) {
169             case 0: 
170                 if (t < 1 || t > 9999) {
171                     free_all(gdate, parts);
172                     return NULL;
173                 }
174                 g_date_set_year(gdate, t);
175                 break;
176             case 1:
177                 if (t < 1 || t > 12) {
178                     free_all(gdate, parts);
179                     return NULL;
180                 }
181                 g_date_set_month(gdate, t);
182                 break;
183             case 2:
184                 if (t < 1 || t > 31) {
185                     free_all(gdate, parts);
186                     return NULL;
187                 }
188                 g_date_set_day(gdate, t);
189                 break;
190         }
191     }
192     g_strfreev(parts);
193     return gdate;
194 }
195
196 gboolean before_date(time_t msg_mtime, const gchar* before) {
197     gchar*      pos = NULL;
198     GDate*      date;
199     GDate*      file_t;
200     gboolean    res;
201 #if !GLIB_CHECK_VERSION(2,10,0)
202     GTime       gtime;
203 #endif
204
205     debug_print("Cut-off date: %s\n", before);
206     if ((date = iso2GDate(before)) == NULL) {
207         g_warning("Bad date format: %s\n", before);
208         return FALSE;
209     }
210
211     file_t = g_date_new();
212 #if GLIB_CHECK_VERSION(2,10,0)
213     g_date_set_time_t(file_t, msg_mtime);
214 #else
215     gtime = (GTime) msg_mtime;
216     g_date_set_time(file_t, gtime);
217 #endif
218
219     if (debug_get_mode()) {
220         pos = g_new0(char, 100);
221         g_date_strftime(pos, 100, "%F", file_t);
222         fprintf(stderr, "File date: %s\n", pos);
223         g_free(pos);
224     }
225
226     if (! g_date_valid(file_t)) {
227         g_warning("Invalid msg date\n");
228         return FALSE;
229     }
230
231     res = (g_date_compare(file_t, date) >= 0) ? FALSE : TRUE;
232     g_date_free(file_t);
233     return res;
234 }
235    
236 static void archive_free_file_info(struct file_info* file) {
237         if (! file)
238                 return;
239         if (file->path)
240                 g_free(file->path);
241         if (file->name)
242                 g_free(file->name);
243         g_free(file);
244         file = NULL;
245 }
246
247 void stop_archiving() {
248         debug_print("stop action set to true\n");
249         stop_action = TRUE;
250 }
251
252 void archive_free_file_list(gboolean md5, gboolean rename) {
253         struct file_info* file = NULL;
254         gchar* path = NULL;
255
256         debug_print("freeing file list\n");
257         if (! file_list)
258                 return;
259         while (file_list) {
260                 file = (struct file_info *) file_list->data;
261                 if (!rename && md5 && g_str_has_suffix(file->name, ".md5")) {
262                         path = g_strdup_printf("%s/%s", file->path, file->name);
263                         debug_print("unlinking %s\n", path);
264                         g_unlink(path);
265                         g_free(path);
266                 }
267                 if (rename) {
268                         path = g_strdup_printf("%s/%s", file->path, file->name);
269                         debug_print("unlinking %s\n", path);
270                         g_unlink(path);
271                         g_free(path);
272                 }
273                 archive_free_file_info(file);
274                 file_list->data = NULL;
275                 file_list = g_slist_next(file_list);
276         }
277         if (file_list) {
278                 g_slist_free(file_list);
279                 file_list = NULL;
280         }
281 }
282
283 static struct file_info* archive_new_file_info() {
284         struct file_info* new_file_info = malloc(sizeof(struct file_info));
285
286         new_file_info->path = NULL;
287         new_file_info->name = NULL;
288         return new_file_info;
289 }
290
291 static void archive_add_to_list(struct file_info* file) {
292         if (! file)
293                 return;
294         file_list = g_slist_prepend(file_list, (gpointer) file);
295 }
296
297 static gchar* strip_leading_dot_slash(gchar* path) {
298         gchar* stripped = path;
299         gchar* result = NULL;
300
301         if (stripped && stripped[0] == '.') {
302                 ++stripped;
303         if (stripped && stripped[0] == '/')
304                 ++stripped;
305                 result = g_strdup(stripped);
306         }
307         else
308                 result = g_strdup(path);
309         return result;
310 }
311
312 static gchar* get_full_path(struct file_info* file) {
313         char* path = malloc(PATH_MAX);
314
315         if (file->path && *(file->path))
316                 sprintf(path, "%s/%s", file->path, file->name);
317         else
318                 sprintf(path, "%s", file->name);
319         return path;
320 }
321
322 #ifdef _TEST
323 static gchar* strip_leading_slash(gchar* path) {
324         gchar* stripped = path;
325         gchar* result = NULL;
326
327         if (stripped && stripped[0] == '/') {
328                 ++stripped;
329                 result = g_strdup(stripped);
330         }
331         else
332                 result = g_strdup(path);
333         return result;
334 }
335
336 static int archive_get_permissions() {
337         return permissions;
338 }
339
340
341 void archive_set_permissions(int perm) {
342         permissions = perm;
343 }
344
345 static int archive_copy_data(struct archive* in, struct archive* out) {
346         const void* buf;
347         size_t size;
348         off_t offset;
349         int res = ARCHIVE_OK;
350
351         while (res == ARCHIVE_OK) {
352                 res = archive_read_data_block(in, &buf, &size, &offset);
353                 if (res == ARCHIVE_OK) {
354                         res = archive_write_data_block(out, buf, size, offset);
355                 }
356         }
357         return (res == ARCHIVE_EOF) ? ARCHIVE_OK : res;
358 }
359 #endif
360
361 void archive_add_file(gchar* path) {
362         struct file_info* file = archive_new_file_info();
363         gchar* filename = NULL;
364
365         g_return_if_fail(path != NULL);
366
367 #ifndef _TEST
368         debug_print("add %s to list\n", path);
369 #endif
370         filename = g_strrstr_len(path, strlen(path), "/");
371         if (! filename)
372                 g_warning("%s\n", path);
373         g_return_if_fail(filename != NULL);
374
375         filename++;
376         file->name = g_strdup(filename);
377         file->path = strip_leading_dot_slash(dirname(path));
378         archive_add_to_list(file);
379 }
380
381 GSList* archive_get_file_list() {
382         return file_list;
383 }
384
385 #ifdef _TEST
386 const gchar* archive_extract(const char* archive_name, int flags) {
387         struct archive* in;
388         struct archive* out;
389         struct archive_entry* entry;
390         int res = ARCHIVE_OK;
391         gchar* buf = NULL;
392         const char* result == NULL;
393
394         g_return_val_if_fail(archive_name != NULL, ARCHIVE_FATAL);
395
396         fprintf(stdout, "%s: extracting\n", archive_name);
397         in = archive_read_new();
398         if ((res = archive_read_support_format_tar(in)) == ARCHIVE_OK) {
399                 if ((res = archive_read_support_compression_gzip(in)) == ARCHIVE_OK) {
400                         if ((res = archive_read_open_file(
401                                 in, archive_name, READ_BLOCK_SIZE)) != ARCHIVE_OK) {
402                                 buf = g_strdup_printf(
403                                                 "%s: %s\n", archive_name, archive_error_string(in));
404                                 g_warning("%s\n", buf);
405                                 g_free(buf);
406                                 result = archive_error_string(in);
407                         }
408                         else {
409                                 out = archive_write_disk_new();
410                                 if ((res = archive_write_disk_set_options(
411                                                                 out, flags)) == ARCHIVE_OK) {
412                                         res = archive_read_next_header(in, &entry);
413                                         while (res == ARCHIVE_OK) {
414                                                 fprintf(stdout, "%s\n", archive_entry_pathname(entry));
415                                                 res = archive_write_header(out, entry);
416                                                 if (res != ARCHIVE_OK) {
417                                                         buf = g_strdup_printf("%s\n", 
418                                                                                         archive_error_string(out));
419                                                         g_warning("%s\n", buf);
420                                                         g_free(buf);
421                                                         /* skip this file an continue */
422                                                         res = ARCHIVE_OK;
423                                                 }
424                                                 else {
425                                                         res = archive_copy_data(in, out);
426                                                         if (res != ARCHIVE_OK) {
427                                                                 buf = g_strdup_printf("%s\n", 
428                                                                                                 archive_error_string(in));
429                                                                 g_warning("%s\n", buf);
430                                                                 g_free(buf);
431                                                                 /* skip this file an continue */
432                                                                 res = ARCHIVE_OK;
433                                                         }
434                                                         else
435                                                                 res = archive_read_next_header(in, &entry);
436                                                 }
437                                         }
438                                         if (res == ARCHIVE_EOF)
439                                                 res = ARCHIVE_OK;
440                                         if (res != ARCHIVE_OK) {
441                                                 buf = g_strdup_printf("%s\n", archive_error_string(in));
442                                                 if (*buf == '\n') {
443                                                         g_free(buf);
444                                                         buf = g_strdup_printf("%s: Unknown error\n", archive_name);
445                                                 }
446                                                 g_warning("%s\n", buf);
447                                                 g_free(buf);
448                                                 result = archive_error_string(in);
449                                         }
450                                 }
451                                 else
452                                         result = archive_error_string(out);
453                                 archive_read_close(in);
454                         }
455                         archive_read_finish(in);
456                 }
457                 else
458                         result = archive_error_string(in);
459         }
460         else
461                 result = archive_error_string(in);
462         return result;
463 }
464 #endif
465
466 const gchar* archive_create(const char* archive_name, GSList* files,
467                         COMPRESS_METHOD method, ARCHIVE_FORMAT format) {
468         struct archive* arch;
469         struct archive_entry* entry;
470         char* buf = NULL;
471         ssize_t len;
472         int fd;
473         struct stat st;
474         struct file_info* file;
475         gchar* filename = NULL;
476         gchar* msg = NULL;
477
478 #ifndef _TEST
479         gint num = 0;
480         gint total = g_slist_length (files);
481 #endif
482
483         g_return_val_if_fail(files != NULL, "No files for archiving");
484
485         debug_print("File: %s\n", archive_name);
486         arch = archive_write_new();
487         switch (method) {
488                 case ZIP:
489                         if (archive_write_set_compression_gzip(arch) != ARCHIVE_OK)
490                                 return archive_error_string(arch);
491                         break;
492                 case BZIP2:
493                         if (archive_write_set_compression_bzip2(arch) != ARCHIVE_OK)
494                                 return archive_error_string(arch);
495                         break;
496 #if NEW_ARCHIVE_API
497                 case COMPRESS:
498                         if (archive_write_set_compression_compress(arch) != ARCHIVE_OK)
499                                 return archive_error_string(arch);
500                         break;
501 #endif
502                 case NO_COMPRESS:
503                         if (archive_write_set_compression_none(arch) != ARCHIVE_OK)
504                                 return archive_error_string(arch);
505                         break;
506         }
507         switch (format) {
508                 case TAR:
509                         if (archive_write_set_format_ustar(arch) != ARCHIVE_OK)
510                                 return archive_error_string(arch);
511                         break;
512                 case SHAR:
513                         if (archive_write_set_format_shar(arch) != ARCHIVE_OK)
514                                 return archive_error_string(arch);
515                         break;
516                 case PAX:
517                         if (archive_write_set_format_pax(arch) != ARCHIVE_OK)
518                                 return archive_error_string(arch);
519                         break;
520                 case CPIO:
521                         if (archive_write_set_format_cpio(arch) != ARCHIVE_OK)
522                                 return archive_error_string(arch);
523                         break;
524                 case NO_FORMAT:
525                         return "Missing archive format";
526         }
527         if (archive_write_open_file(arch, archive_name) != ARCHIVE_OK)
528                 return archive_error_string(arch);
529
530         while (files && ! stop_action) {
531 #ifndef _TEST
532                 set_progress_print_all(num++, total, 30);
533 #endif
534                 file = (struct file_info *) files->data;
535                 if (!file)
536                         continue;
537                 filename = get_full_path(file);
538                 /* libarchive will crash if instructed to add archive to it self */
539                 if (g_utf8_collate(archive_name, filename) == 0) {
540                         buf = NULL;
541                         buf = g_strdup_printf(
542                                                 "%s: Not dumping to %s", archive_name, filename);
543                         g_warning("%s\n", buf);
544 #ifndef _TEST
545                         debug_print("%s\n", buf);
546 #endif
547                         g_free(buf);
548                 }
549                 else {
550 #ifndef _TEST
551                         debug_print("Adding: %s\n", filename);
552                         msg = g_strdup_printf("%s", filename);
553                         set_progress_file_label(msg);
554                         g_free(msg);
555 #endif
556                         entry = archive_entry_new();
557                         lstat(filename, &st);
558                         if ((fd = open(filename, O_RDONLY)) == -1) {
559                                 perror("open file");
560                         }
561                         else {
562                                 archive_entry_copy_stat(entry, &st);
563                                 archive_entry_set_pathname(entry, filename);
564                                 if (S_ISLNK(st.st_mode)) {
565                                         buf = NULL;
566                                         buf = malloc(PATH_MAX + 1);
567                                         if ((len = readlink(filename, buf, PATH_MAX)) < 0)
568                                                 perror("error in readlink");
569                                         else
570                                                 buf[len] = '\0';
571                                         archive_entry_set_symlink(entry, buf);
572                                         g_free(buf);
573                                         archive_entry_set_size(entry, 0);
574                                         archive_write_header(arch, entry);
575                                 }
576                                 else {
577                                         if (archive_write_header(arch, entry) != ARCHIVE_OK)
578                                                 g_warning("%s", archive_error_string(arch));
579                                         buf = NULL;
580                                         buf = malloc(READ_BLOCK_SIZE);
581                                         len = read(fd, buf, READ_BLOCK_SIZE);
582                                         while (len > 0) {
583                                                 if (archive_write_data(arch, buf, len) == -1)
584                                                         g_warning("%s", archive_error_string(arch));
585                                                 memset(buf, 0, READ_BLOCK_SIZE);
586                                                 len = read(fd, buf, READ_BLOCK_SIZE);
587                                         }
588                                         g_free(buf);
589                                 }
590                                 close(fd);
591                                 archive_entry_free(entry);
592                         }
593                 }
594                 g_free(filename);
595                 files = g_slist_next(files);
596         }
597 #ifndef _TEST
598         if (stop_action)
599                 unlink(archive_name);
600         stop_action = FALSE;
601 #endif
602         archive_write_close(arch);
603         archive_write_finish(arch);
604         return NULL;
605 }
606
607 #ifdef _TEST
608 void archive_scan_folder(const char* dir) {
609         struct stat st;
610         DIR* root;
611         struct dirent* ent;
612         gchar cwd[PATH_MAX];
613         gchar path[PATH_MAX];
614         
615         getcwd(cwd, PATH_MAX);
616
617         if (g_stat(dir, &st) == -1)
618                 return;
619         if (! S_ISDIR(st.st_mode))
620                 return;
621         if (!(root = opendir(dir)))
622                 return;
623         chdir(dir);
624
625         while ((ent = readdir(root)) != NULL) {
626                 if (strcmp(".", ent->d_name) == 0 || strcmp("..", ent->d_name) == 0)
627                         continue;
628                 g_stat(ent->d_name, &st);
629                 sprintf(path, "%s/%s", dir, ent->d_name);
630                 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
631                         archive_add_file(path);
632                 }
633                 else if (S_ISDIR(st.st_mode)) {
634                         archive_scan_folder(path);
635                 }
636         }
637         chdir(cwd);
638         closedir(root);
639 }
640
641 int main(int argc, char** argv) {
642         char* archive = NULL;
643         char buf[PATH_MAX];
644         int pid;
645         int opt;
646         int perm = ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_TIME |
647                 ARCHIVE_EXTRACT_ACL | ARCHIVE_EXTRACT_FFLAGS | ARCHIVE_EXTRACT_SECURE_SYMLINKS;
648         gchar cwd[PATH_MAX];
649         gboolean remove = FALSE;
650         const char *p = NULL;
651         int res;
652
653         getcwd(cwd, PATH_MAX);
654
655         while (*++argv && **argv == '-') {
656                 p = *argv + 1;
657
658                 while ((opt = *p++) != '\0') {
659                         switch(opt) {
660                                 case 'a':
661                                         if (*p != '\0')
662                                                 archive = (char *) p;
663                                         else
664                                                 archive = *++argv;
665                                         p += strlen(p);
666                                         break;
667                                 case 'r':
668                                         remove = TRUE;
669                                         break;
670                         }
671                 }
672         }
673         if (! archive) {
674                 fprintf(stderr, "Missing archive name!\n");
675                 return EXIT_FAILURE;
676         }
677         if (!*argv) {
678                 fprintf(stderr, "Expected arguments after options!\n");
679                 return EXIT_FAILURE;
680         }
681         
682         while (*argv) {
683                 archive_scan_folder(*argv++);
684                 res = archive_create(archive, file_list);
685                 if (res != ARCHIVE_OK) {
686                         fprintf(stderr, "%s: Creating archive failed\n", archive);
687                         return EXIT_FAILURE;
688                 }
689         }
690         pid = (int) getpid();
691         sprintf(buf, "/tmp/%d", pid);
692         fprintf(stdout, "Creating: %s\n", buf);
693         mkdir(buf, 0700);
694         chdir(buf);
695         if (strcmp(dirname(archive), ".") == 0) 
696                 sprintf(buf, "%s/%s", cwd, basename(archive));
697         else
698                 sprintf(buf, "%s", archive);
699         archive_extract(buf, perm);
700         chdir(cwd);
701         if (remove) {
702                 sprintf(buf, "rm -rf /tmp/%d", pid);
703                 fprintf(stdout, "Executing: %s\n", buf);
704                 system(buf);
705         }
706         archive_free_list(file_list);
707         return EXIT_SUCCESS;
708 }
709 #endif