int main(int argc, char **argv) { int ret; if(argc < 3) { fprintf(stderr, "Usage: %s old new [patch]\n", argv[0]); exit(EXIT_FAILURE); } t = NULL; if(argc >= 4) { if(!strcmp(argv[3], "-")) ret = tar_fdopen(&t, 1, "stdout", NULL, O_WRONLY|O_CREAT, 0644, TAR_GNU/*|TAR_VERBOSE*/); else ret = tar_open(&t, argv[3], NULL, O_WRONLY|O_CREAT, 0644, TAR_GNU/*|TAR_VERBOSE*/); if(ret != 0) { fprintf(stderr, "%d\n", ret); perror("tar_open"); } } base1 = argv[1]; base2 = argv[2]; cmpdir(argv[1], argv[2]); if(t) { tar_append_eof(t); tar_close(t); } return 0; }
int main(int argc, char **argv) { struct eub eub; struct eubfile file; int opt_P = 0; TAR *tarp; char buf[512]; libtar_hash_t *lthash = NULL; eub_init(&eub); ARGBEGIN { case 'P' : opt_P = 1; break; default : usage(); } ARGEND; if (argc == 0) usage(); if (eub_open(&eub, argv[0], "r")) return(eub.err); if (argc > 1) { lthash = libtar_hash_new(3, (libtar_hashfunc_t) hash_path); while (--argc) libtar_hash_add(lthash, ++argv); } if (tar_fdopen(&tarp, 1, "/dev/stdout", NULL, O_WRONLY, 0600, TAR_GNU) != 0) return(eub_err(&eub, errno, "Can't start tar output")); while (eub_read_meta(&eub, &file)) { unsigned long long len, remlen; struct libtar_node lp = { file.path, 0, 0 }; libtar_hashptr_t hp; hp.bucket = -1; hp.node = NULL; /* if (lthash && !libtar_hash_search(lthash, &hp, &lp, (libtar_matchfunc_t) compare_paths)) */ if (lthash && !libtar_hash_getkey(lthash, &hp, &lp, (libtar_matchfunc_t) compare_paths)) continue; if (eub_meta_to_stat(&eub, &file)) return(eub.err); th_set_from_stat(tarp, &file.stat); th_set_path(tarp, file.path); if (file.typechar == 'f') { /* XXX Hack! len = strlen(tarp->th_buf.name); if (tarp->th_buf.name[len-1] == '/') tarp->th_buf.name[len-1] = 0; */ if (eub_read_dataref(&eub, &file)) return(eub.err); } else if (file.typechar == 'l') { if (eub_read_dataref(&eub, &file)) return(eub.err); if (eub_seek_data(&eub, &file)) return(eub.err); if (eub_read_data(&eub, &file, eub.linkbuf, file.size)) return(eub_err(&eub, errno, "Can't read link: %s", file.path)); eub.linkbuf[file.size] = 0; th_set_link(tarp, eub.linkbuf); } th_finish(tarp); if (th_write(tarp)) return(eub_err(&eub, errno, "Can't write tar header for %s", file.path)); fflush(stdout); if (file.typechar == 'f') { if (eub_seek_data(&eub, &file)) return(eub.err); for (remlen = file.size; remlen; remlen -= len) { len = remlen < sizeof(buf) ? remlen : sizeof(buf); if (eub_read_data(&eub, &file, buf, len)) return(eub_err(&eub, errno, "Can't read file contents: %s", file.path)); if (len < sizeof(buf)) bzero(buf+len, sizeof(buf)-len); if (!tar_block_write(tarp, buf)) return(eub_err(&eub, errno, "Can't write to tar: %s", file.path)); fflush(stdout); } if (eub.err) return(eub.err); } } if (tar_append_eof(tarp)) return(eub_err(&eub, errno, "Can't finish tar")); fflush(stdout); return(eub.err); }
static int create_and_upload_archive( const char *dump_dir_name, map_string_t *settings) { int result = 1; /* error */ pid_t child; TAR* tar = NULL; const char* errmsg = NULL; char* tempfile = NULL; struct dump_dir *dd = dd_opendir(dump_dir_name, /*flags:*/ 0); if (!dd) xfunc_die(); /* error msg is already logged by dd_opendir */ /* Gzipping e.g. 0.5gig coredump takes a while. Let client know what we are doing */ log(_("Compressing data")); //TODO: //Encrypt = yes //ArchiveType = .tar.bz2 //ExcludeFiles = foo,bar*,b*z const char* opt = getenv("Upload_URL"); if (!opt) opt = get_map_string_item_or_empty(settings, "URL"); char *url = opt[0] != '\0' ? xstrdup(opt) : ask_url(_("Please enter a URL (scp, ftp, etc.) where the problem data is to be exported:")); /* Create a child gzip which will compress the data */ /* SELinux guys are not happy with /tmp, using /var/run/abrt */ /* Reverted back to /tmp for ABRT2 */ /* Changed again to /var/tmp because of Fedora feature tmp-on-tmpfs */ tempfile = concat_path_basename(LARGE_DATA_TMP_DIR, dump_dir_name); tempfile = append_to_malloced_string(tempfile, ".tar.gz"); int pipe_from_parent_to_child[2]; xpipe(pipe_from_parent_to_child); child = vfork(); if (child == 0) { /* child */ close(pipe_from_parent_to_child[1]); xmove_fd(pipe_from_parent_to_child[0], 0); xmove_fd(xopen3(tempfile, O_WRONLY | O_CREAT | O_EXCL, 0600), 1); execlp("gzip", "gzip", NULL); perror_msg_and_die("Can't execute '%s'", "gzip"); } close(pipe_from_parent_to_child[0]); /* If child died (say, in xopen), then parent might get SIGPIPE. * We want to properly unlock dd, therefore we must not die on SIGPIPE: */ signal(SIGPIPE, SIG_IGN); /* Create tar writer object */ if (tar_fdopen(&tar, pipe_from_parent_to_child[1], tempfile, /*fileops:(standard)*/ NULL, O_WRONLY | O_CREAT, 0644, TAR_GNU) != 0) { errmsg = "Can't create temporary file in "LARGE_DATA_TMP_DIR; goto ret; } /* Write data to the tarball */ { string_vector_ptr_t exclude_from_report = get_global_always_excluded_elements(); dd_init_next_file(dd); char *short_name, *full_name; while (dd_get_next_file(dd, &short_name, &full_name)) { if (exclude_from_report && is_in_string_list(short_name, (const_string_vector_const_ptr_t)exclude_from_report)) goto next; // dd_get_next_file guarantees that it's a REG: //struct stat stbuf; //if (stat(full_name, &stbuf) != 0) // || !S_ISREG(stbuf.st_mode) //) { // goto next; //} if (tar_append_file(tar, full_name, short_name) != 0) { errmsg = "Can't create temporary file in "LARGE_DATA_TMP_DIR; free(short_name); free(full_name); goto ret; } next: free(short_name); free(full_name); } } dd_close(dd); dd = NULL; /* Close tar writer... */ if (tar_append_eof(tar) != 0 || tar_close(tar) != 0) { errmsg = "Can't create temporary file in "LARGE_DATA_TMP_DIR; goto ret; } tar = NULL; /* ...and check that gzip child finished successfully */ int status; safe_waitpid(child, &status, 0); child = -1; if (status != 0) { /* We assume the error was out-of-disk-space or out-of-quota */ errmsg = "Can't create temporary file in "LARGE_DATA_TMP_DIR; goto ret; } /* Upload the tarball */ /* Upload from /tmp to /tmp + deletion -> BAD, exclude this possibility */ if (url && url[0] && strcmp(url, "file://"LARGE_DATA_TMP_DIR"/") != 0) { post_state_t *state = new_post_state(POST_WANT_ERROR_MSG); state->username = getenv("Upload_Username"); char *password_inp = NULL; if (state->username != NULL && state->username[0] != '\0') { /* Load Password only if Username is configured, it doesn't make */ /* much sense to load Password without Username. */ state->password = getenv("Upload_Password"); if (state->password == NULL) { /* Be permissive and nice, ask only once and don't check */ /* the result. User can dismiss this prompt but the upload */ /* may work somehow??? */ char *msg = xasprintf(_("Please enter password for uploading:"), state->username); state->password = password_inp = ask_password(msg); free(msg); } } char *remote_name = upload_file_ext(state, url, tempfile, UPLOAD_FILE_HANDLE_ACCESS_DENIALS); result = (remote_name == NULL); /* error if NULL */ free(remote_name); free(password_inp); free_post_state(state); /* cleanup code will delete tempfile */ } else { result = 0; /* success */ log(_("Archive is created: '%s'"), tempfile); free(tempfile); tempfile = NULL; } ret: free(url); dd_close(dd); if (tar) tar_close(tar); /* close(pipe_from_parent_to_child[1]); - tar_close() does it itself */ if (child > 0) safe_waitpid(child, NULL, 0); if (tempfile) { unlink(tempfile); free(tempfile); } if (errmsg) error_msg_and_die("%s", errmsg); return result; }