2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2008 Michael Rasmussen and the Claws Mail Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
26 #include <glib/gi18n.h>
28 #include "libarchive_archive.h"
31 # include "archiver.h"
33 # include "mainwindow.h"
37 #include <sys/types.h>
41 #include <archive_entry.h>
51 #define READ_BLOCK_SIZE 10240
58 static GSList* msg_trash_list = NULL;
59 static GSList* file_list = NULL;
60 static gboolean stop_action = FALSE;
63 static int permissions = 0;
66 static void free_msg_trash(MsgTrash* trash) {
68 debug_print("Freeing files in %s\n", folder_item_get_name(trash->item));
70 g_slist_free(trash->msgs);
76 MsgTrash* new_msg_trash(FolderItem* item) {
80 g_return_val_if_fail(item != NULL, NULL);
82 /* FolderType must be F_MH, F_MBOX, F_MAILDIR or F_IMAP */
83 type = item->folder->klass->type;
84 if (!(type == F_MH || type == F_MBOX ||
85 type == F_MAILDIR || type == F_IMAP))
87 msg_trash = g_new0(MsgTrash, 1);
88 msg_trash->item = item;
89 msg_trash->msgs = NULL;
90 msg_trash_list = g_slist_prepend(msg_trash_list, msg_trash);
95 void archive_free_archived_files() {
100 for (l = msg_trash_list; l; l = g_slist_next(l)) {
101 mt = (MsgTrash *) l->data;
102 debug_print("Trashing messages in folder: %s\n",
103 folder_item_get_name(mt->item));
104 res = folder_item_remove_msgs(mt->item, mt->msgs);
105 debug_print("Result was %d\n", res);
108 g_slist_free(msg_trash_list);
109 msg_trash_list = NULL;
112 void archive_add_msg_mark(MsgTrash* trash, MsgInfo* msg) {
113 g_return_if_fail(trash != NULL || msg != NULL);
114 debug_print("Marking msg #%d for removal\n", msg->msgnum);
115 trash->msgs = g_slist_prepend(trash->msgs, msg);
118 static void free_all(GDate* date, gchar** parts) {
125 static gboolean is_iso_string(gchar** items) {
132 debug_print("Date part %d: %s\n", i, item);
135 if (strlen(item) != 4)
140 if (strlen(item) != 2)
147 debug_print("Leaving\n");
151 static GDate* iso2GDate(const gchar* date) {
153 gchar** parts = NULL;
156 g_return_val_if_fail(date != NULL, NULL);
158 gdate = g_date_new();
159 parts = g_strsplit(date, "-", 3);
160 if (! is_iso_string(parts))
164 for (i = 0; i < 3; i++) {
165 int t = atoi(parts[i]);
168 if (t < 1 || t > 9999) {
169 free_all(gdate, parts);
172 g_date_set_year(gdate, t);
175 if (t < 1 || t > 12) {
176 free_all(gdate, parts);
179 g_date_set_month(gdate, t);
182 if (t < 1 || t > 31) {
183 free_all(gdate, parts);
186 g_date_set_day(gdate, t);
194 gboolean before_date(time_t msg_mtime, const gchar* before) {
200 debug_print("Cut-off date: %s\n", before);
201 if ((date = iso2GDate(before)) == NULL) {
202 g_warning("Bad date format: %s\n", before);
206 file_t = g_date_new();
207 g_date_set_time_t(file_t, msg_mtime);
209 if (debug_get_mode()) {
210 pos = g_new0(char, 100);
211 g_date_strftime(pos, 100, "%F", file_t);
212 fprintf(stderr, "File date: %s\n", pos);
216 if (! g_date_valid(file_t)) {
217 g_warning("Invalid msg date\n");
221 res = (g_date_compare(file_t, date) >= 0) ? FALSE : TRUE;
226 static void archive_free_file_info(struct file_info* file) {
237 void stop_archiving() {
238 debug_print("stop action set to true\n");
242 void archive_free_file_list(gboolean md5, gboolean rename) {
243 struct file_info* file = NULL;
246 debug_print("freeing file list\n");
250 file = (struct file_info *) file_list->data;
251 if (!rename && md5 && g_str_has_suffix(file->name, ".md5")) {
252 path = g_strdup_printf("%s/%s", file->path, file->name);
253 debug_print("unlinking %s\n", path);
258 path = g_strdup_printf("%s/%s", file->path, file->name);
259 debug_print("unlinking %s\n", path);
263 archive_free_file_info(file);
264 file_list->data = NULL;
265 file_list = g_slist_next(file_list);
268 g_slist_free(file_list);
273 static struct file_info* archive_new_file_info() {
274 struct file_info* new_file_info = malloc(sizeof(struct file_info));
276 new_file_info->path = NULL;
277 new_file_info->name = NULL;
278 return new_file_info;
281 static void archive_add_to_list(struct file_info* file) {
284 file_list = g_slist_prepend(file_list, (gpointer) file);
287 static gchar* strip_leading_dot_slash(gchar* path) {
288 gchar* stripped = path;
289 gchar* result = NULL;
291 if (stripped && stripped[0] == '.') {
293 if (stripped && stripped[0] == '/')
295 result = g_strdup(stripped);
298 result = g_strdup(path);
302 static gchar* get_full_path(struct file_info* file) {
303 char* path = malloc(PATH_MAX);
305 if (file->path && *(file->path))
306 sprintf(path, "%s/%s", file->path, file->name);
308 sprintf(path, "%s", file->name);
313 static gchar* strip_leading_slash(gchar* path) {
314 gchar* stripped = path;
315 gchar* result = NULL;
317 if (stripped && stripped[0] == '/') {
319 result = g_strdup(stripped);
322 result = g_strdup(path);
326 static int archive_get_permissions() {
331 void archive_set_permissions(int perm) {
335 static int archive_copy_data(struct archive* in, struct archive* out) {
339 int res = ARCHIVE_OK;
341 while (res == ARCHIVE_OK) {
342 res = archive_read_data_block(in, &buf, &size, &offset);
343 if (res == ARCHIVE_OK) {
344 res = archive_write_data_block(out, buf, size, offset);
347 return (res == ARCHIVE_EOF) ? ARCHIVE_OK : res;
351 void archive_add_file(gchar* path) {
352 struct file_info* file = archive_new_file_info();
353 gchar* filename = NULL;
355 g_return_if_fail(path != NULL);
358 debug_print("add %s to list\n", path);
360 filename = g_strrstr_len(path, strlen(path), "/");
362 g_warning("no filename in path '%s'", path);
363 g_return_if_fail(filename != NULL);
366 file->name = g_strdup(filename);
367 file->path = strip_leading_dot_slash(dirname(path));
368 archive_add_to_list(file);
371 GSList* archive_get_file_list() {
376 const gchar* archive_extract(const char* archive_name, int flags) {
379 struct archive_entry* entry;
380 int res = ARCHIVE_OK;
382 const char* result == NULL;
384 g_return_val_if_fail(archive_name != NULL, ARCHIVE_FATAL);
386 fprintf(stdout, "%s: extracting\n", archive_name);
387 in = archive_read_new();
388 if ((res = archive_read_support_format_tar(in)) == ARCHIVE_OK) {
389 if ((res = archive_read_support_compression_gzip(in)) == ARCHIVE_OK) {
390 #if ARCHIVE_VERSION_NUMBER < 3000000
391 if ((res = archive_read_open_file(
393 if ((res = archive_read_open_filename(
395 in, archive_name, READ_BLOCK_SIZE)) != ARCHIVE_OK) {
396 g_warning("%s: %s", archive_name, archive_error_string(in));
397 result = archive_error_string(in);
400 out = archive_write_disk_new();
401 if ((res = archive_write_disk_set_options(
402 out, flags)) == ARCHIVE_OK) {
403 res = archive_read_next_header(in, &entry);
404 while (res == ARCHIVE_OK) {
405 res = archive_write_header(out, entry);
406 if (res != ARCHIVE_OK) {
407 g_warning("%s", archive_error_string(out));
408 /* skip this file an continue */
412 res = archive_copy_data(in, out);
413 if (res != ARCHIVE_OK) {
414 g_warning("%s", archive_error_string(in));
415 /* skip this file an continue */
419 res = archive_read_next_header(in, &entry);
422 if (res == ARCHIVE_EOF)
424 if (res != ARCHIVE_OK) {
425 gchar *e = archive_error_string(in);
426 g_warning("%s: %s", archive_name, e? e: "unknown error");
431 result = archive_error_string(out);
432 archive_read_close(in);
434 #if ARCHIVE_VERSION_NUMBER < 3000000
435 archive_read_finish(in);
437 archive_read_free(in);
441 result = archive_error_string(in);
444 result = archive_error_string(in);
449 const gchar* archive_create(const char* archive_name, GSList* files,
450 COMPRESS_METHOD method, ARCHIVE_FORMAT format) {
451 struct archive* arch;
452 struct archive_entry* entry;
457 struct file_info* file;
458 gchar* filename = NULL;
463 gint total = g_slist_length (files);
466 g_return_val_if_fail(files != NULL, "No files for archiving");
468 debug_print("File: %s\n", archive_name);
469 arch = archive_write_new();
472 #if ARCHIVE_VERSION_NUMBER < 3000000
473 if (archive_write_set_compression_gzip(arch) != ARCHIVE_OK)
475 if (archive_write_add_filter_gzip(arch) != ARCHIVE_OK)
477 return archive_error_string(arch);
480 #if ARCHIVE_VERSION_NUMBER < 3000000
481 if (archive_write_set_compression_bzip2(arch) != ARCHIVE_OK)
483 if (archive_write_add_filter_bzip2(arch) != ARCHIVE_OK)
485 return archive_error_string(arch);
488 #if ARCHIVE_VERSION_NUMBER < 3000000
489 if (archive_write_set_compression_compress(arch) != ARCHIVE_OK)
491 if (archive_write_add_filter_compress(arch) != ARCHIVE_OK)
493 return archive_error_string(arch);
496 #if ARCHIVE_VERSION_NUMBER < 3000000
497 if (archive_write_set_compression_none(arch) != ARCHIVE_OK)
499 if (archive_write_add_filter_none(arch) != ARCHIVE_OK)
501 return archive_error_string(arch);
506 if (archive_write_set_format_ustar(arch) != ARCHIVE_OK)
507 return archive_error_string(arch);
510 if (archive_write_set_format_shar(arch) != ARCHIVE_OK)
511 return archive_error_string(arch);
514 if (archive_write_set_format_pax(arch) != ARCHIVE_OK)
515 return archive_error_string(arch);
518 if (archive_write_set_format_cpio(arch) != ARCHIVE_OK)
519 return archive_error_string(arch);
522 return "Missing archive format";
524 #if ARCHIVE_VERSION_NUMBER < 3000000
525 if (archive_write_open_file(arch, archive_name) != ARCHIVE_OK)
527 if (archive_write_open_filename(arch, archive_name) != ARCHIVE_OK)
529 return archive_error_string(arch);
531 while (files && ! stop_action) {
533 set_progress_print_all(num++, total, 30);
535 file = (struct file_info *) files->data;
538 filename = get_full_path(file);
539 /* libarchive will crash if instructed to add archive to it self */
540 if (g_utf8_collate(archive_name, filename) == 0) {
541 g_warning("%s: not dumping to '%s'", archive_name, filename);
543 debug_print("%s: not dumping to '%s'\n", archive_name, filename);
548 debug_print("Adding: %s\n", filename);
549 msg = g_strdup_printf("%s", filename);
550 set_progress_file_label(msg);
553 entry = archive_entry_new();
554 lstat(filename, &st);
555 if ((fd = open(filename, O_RDONLY)) == -1) {
559 archive_entry_copy_stat(entry, &st);
560 archive_entry_set_pathname(entry, filename);
561 if (S_ISLNK(st.st_mode)) {
563 buf = malloc(PATH_MAX + 1);
564 if ((len = readlink(filename, buf, PATH_MAX)) < 0)
565 perror("error in readlink");
568 archive_entry_set_symlink(entry, buf);
570 archive_entry_set_size(entry, 0);
571 archive_write_header(arch, entry);
574 if (archive_write_header(arch, entry) != ARCHIVE_OK)
575 g_warning("%s", archive_error_string(arch));
577 buf = malloc(READ_BLOCK_SIZE);
578 len = read(fd, buf, READ_BLOCK_SIZE);
580 if (archive_write_data(arch, buf, len) == -1)
581 g_warning("%s", archive_error_string(arch));
582 memset(buf, 0, READ_BLOCK_SIZE);
583 len = read(fd, buf, READ_BLOCK_SIZE);
588 archive_entry_free(entry);
592 files = g_slist_next(files);
596 unlink(archive_name);
599 archive_write_close(arch);
600 #if ARCHIVE_VERSION_NUMBER < 3000000
601 archive_write_finish(arch);
603 archive_write_free(arch);
609 void archive_scan_folder(const char* dir) {
614 gchar path[PATH_MAX];
616 getcwd(cwd, PATH_MAX);
618 if (g_stat(dir, &st) == -1)
620 if (! S_ISDIR(st.st_mode))
622 if (!(root = opendir(dir)))
626 while ((ent = readdir(root)) != NULL) {
627 if (strcmp(".", ent->d_name) == 0 || strcmp("..", ent->d_name) == 0)
629 g_stat(ent->d_name, &st);
630 sprintf(path, "%s/%s", dir, ent->d_name);
631 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
632 archive_add_file(path);
634 else if (S_ISDIR(st.st_mode)) {
635 archive_scan_folder(path);
642 int main(int argc, char** argv) {
643 char* archive = NULL;
647 int perm = ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_TIME |
648 ARCHIVE_EXTRACT_ACL | ARCHIVE_EXTRACT_FFLAGS | ARCHIVE_EXTRACT_SECURE_SYMLINKS;
650 gboolean remove = FALSE;
651 const char *p = NULL;
654 getcwd(cwd, PATH_MAX);
656 while (*++argv && **argv == '-') {
659 while ((opt = *p++) != '\0') {
663 archive = (char *) p;
675 fprintf(stderr, "Missing archive name!\n");
679 fprintf(stderr, "Expected arguments after options!\n");
684 archive_scan_folder(*argv++);
685 res = archive_create(archive, file_list);
686 if (res != ARCHIVE_OK) {
687 fprintf(stderr, "%s: Creating archive failed\n", archive);
691 pid = (int) getpid();
692 sprintf(buf, "/tmp/%d", pid);
693 fprintf(stdout, "Creating: %s\n", buf);
696 if (strcmp(dirname(archive), ".") == 0)
697 sprintf(buf, "%s/%s", cwd, basename(archive));
699 sprintf(buf, "%s", archive);
700 archive_extract(buf, perm);
703 sprintf(buf, "rm -rf /tmp/%d", pid);
704 fprintf(stdout, "Executing: %s\n", buf);
707 archive_free_list(file_list);