static void create_archive(const char *filepath, const char *tarfile) { struct archive *a; struct archive_entry *entry; struct stat st; char buff[8192]; int len; int fd; char **filenames, **filename; filenames = list_filenames(filepath); filename = filenames; a = archive_write_new(); archive_write_set_compression_bzip2(a); archive_write_set_format_ustar(a); archive_write_open_filename(a, tarfile); while (*filename) { stat(*filename, &st); entry = archive_entry_new(); archive_entry_set_pathname(entry, *filename); archive_entry_set_size(entry, st.st_size); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0644); archive_write_header(a, entry); fd = open(*filename, O_RDONLY); len = read(fd, buff, sizeof(buff)); while ( len > 0 ) { archive_write_data(a, buff, len); len = read(fd, buff, sizeof(buff)); } close(fd); archive_entry_free(entry); filename++; } free_str_array(filenames); archive_write_close(a); #if ARCHIVE_VERSION_NUMBER < 4000000 archive_write_finish(a); #else archive_write_free(a); #endif }
int run_program(int argc, char **argv) { CommandLineArgument<int> number_of_list_files; int next_command_line_argument = 0; bool echo_commands = false; for (int i = 1; ((i < argc) && (!have_argument_p(number_of_list_files))); i++) { std::string argument(argv[i]); if (argument == "--help") { print_usage(); return 0; } else if (argument == "--echo") { echo_commands = true; } else if (!have_argument_p(number_of_list_files)) { number_of_list_files = argument; next_command_line_argument = i + 1; } } if (!have_argument_p(number_of_list_files)) { print_usage(); return -1; } if (*number_of_list_files == 0) return 0; const int minimum_number_of_arguments = 0 + *number_of_list_files + 1 // the command; + 0; if ((argc - next_command_line_argument) < minimum_number_of_arguments) throw make_runtime_error("Not enough arguments available for processing.\n"); std::list<std::string> standard_input_list; bool have_read_standard_input_list = false; std::vector<std::string> list_filenames(*number_of_list_files); std::vector<std::list<std::string> > lists(*number_of_list_files); for (int i = 0; i < *number_of_list_files; i++) { int argument_index = i + next_command_line_argument; std::string list_file = argv[argument_index]; std::list<std::string> l; if ((list_file == "-") && (have_read_standard_input_list)) { l = standard_input_list; } else if ((list_file == "-")) { standard_input_list = read_list(std::cin); have_read_standard_input_list = true; l = standard_input_list; } else if (!file_exists_p(list_file)) { throw make_runtime_error("List file %s does not exist.", list_file.c_str()); } else { l = read_list(list_file.c_str()); } list_filenames[i] = list_file; lists[i] = l; } next_command_line_argument += *number_of_list_files; // read the command and its options. std::string command(argv[next_command_line_argument]); std::list<std::string> command_arguments; std::copy(argv + next_command_line_argument + 1, argv + argc, std::back_inserter(command_arguments)); // check all lists are the same size. for (size_t i = 0; i < lists.size(); i++) { if (lists[i].size() != lists[0].size()) throw make_runtime_error("The number of entires in list %s (%d) differs to %s (%d).", list_filenames[i].c_str(), lists[i].size(), list_filenames[0].c_str(), lists[0].size()); } EchoRunner echo_runner(std::cout); ForkRunner fork_runner; Runner *runner = 0; if (echo_commands) runner = &echo_runner; else runner = &fork_runner; assert(runner); std::vector<std::list<std::string>::const_iterator> iterators(*number_of_list_files); for (int i = 0; i < *number_of_list_files; i++) iterators[i] = lists[i].begin(); for (size_t i = 0; i < lists[0].size(); i++) { std::string invocation_command = command; std::list<std::string> invocation_arguments; std::copy(command_arguments.begin(), command_arguments.end(), std::back_inserter(invocation_arguments)); for (size_t j = 0; j < iterators.size(); j++) { invocation_arguments.push_back(*iterators[j]); iterators[j]++; } runner->perform(invocation_command, invocation_arguments); } return 0; }