int compress(const char *i, const struct stat *i_st, int mode, struct FTW *n) { if (!((mode == FTW_F) && (S_ISREG(i_st->st_mode)))) return 0; fs::path input(i #if BOOST_VERSION <= 104600 , fs::native #endif ); fs::path input_directory(input.branch_path()); fs::path output(input_directory / "XXXXXX"); rInfo("Processing file (%s)", input.string().c_str()); // Remember times of the input file. struct stat stbuf; lstat(input.string().c_str(), &stbuf); int o_fd = mkstemp(const_cast<char *>(output.string().c_str())); if (o_fd < 0) { rError("Failed to create an temporary file in (%s) directory", input_directory.string().c_str()); return -1; } rInfo("Temporary file (%s)", output.string().c_str()); struct stat o_st; if (fstat(o_fd, &o_st) == -1) { rError("Failed to read stat of temporary file (%s)", output.string().c_str()); close(o_fd); return -1; } close(o_fd); if (!copy(i, output.string().c_str(), i_st, &o_st)) { unlink(output.string().c_str()); return -1; } if (rename(output.string().c_str(), i) == -1) { rError("Failed to rename temporary file (%s) to (%s) (%s)", output.string().c_str(), input.string().c_str(), strerror(errno)); return -1; } if (chmod(i, i_st->st_mode) == -1) { rError("Failed to change mode (%s) (%s)", input.string().c_str(), strerror(errno)); return -1; } // Write back original times. struct timespec times[2]; times[0].tv_sec = stbuf.st_atime; times[0].tv_nsec = stbuf.st_atim.tv_nsec; times[1].tv_sec = stbuf.st_mtime; times[1].tv_nsec = stbuf.st_mtim.tv_nsec; utimensat(AT_FDCWD, i, times, AT_SYMLINK_NOFOLLOW); return 0; }
int main(int argc, char **argv) { int dirn; struct stat stat_buf; char *spool; char *file; int i; int found = 1; dirn = parse_args(argc, argv); if (signal(SIGHUP, closedown) == SIG_ERR) { perror("Signal"); exit(1); } if (signal(SIGINT, closedown) == SIG_ERR) { perror("Signal"); exit(1); } if (input_conf) { inhibit_thread_flattening = 1; } /* Initialize key/data structures. */ init(); time(&start_time); if (do_output_thread) { // First output_thread(stdout, find_node("*****@*****.**"), 0); // Second //output_thread(stdout, find_node("*****@*****.**"), 0); //output_threads("gmane.discuss"); exit(0); } if (lock_user != NULL) lock_and_uid(lock_user); if (input_spool) { spool = cmalloc(strlen(news_spool) + 1); memcpy(spool, news_spool, strlen(news_spool)); inhibit_thread_flattening = 1; inhibit_file_writes = 0; *(spool+strlen(news_spool)-1) = 0; //input_directory("/mirror/var/spool/news/articles/gmane/comp/lib/glibc/bugs"); //input_directory("/mirror/var/spool/news/articles/gmane/comp/gnu/stow/bugs"); //input_directory("/mirror/var/spool/news/articles/gmane/linux"); //input_directory("/mirror/var/spool/news/articles/gmane/discuss"); //input_directory("/mirror/var/spool/news/articles/gmane/test"); //input_directory("/mirror/var/spool/news/articles/gmane/comp/graphics/ipe/general"); //input_directory(spool); input_directory("/mirror/var/spool/news/articles/gmane/comp/hardware", 1); flush(); clean_up(); clean_up_hash(); printf("Total files: %d, total nodes: %d\n", commands, current_node); exit(0); } if (input_conf) { if (skip_until_group != NULL) found = 0; inhibit_thread_flattening = 1; inhibit_file_writes = 0; for (i = 1; i < num_groups; i++) { if (found == 0 && !strcmp(get_string(groups[i].group_name), skip_until_group)) found = 1; if (found == 1) input_group(get_string(groups[i].group_name)); } flush(); clean_up(); clean_up_hash(); printf("Total files: %d, total nodes: %d\n", commands, current_node); exit(0); } if (dirn == 1) file = "/mirror/var/spool/news/articles/gmane/discuss/4001"; else file = argv[dirn]; if (stat(file, &stat_buf) == -1) { perror("interactive"); exit(0); } if (S_ISREG(stat_buf.st_mode)) thread_file(file); flush(); clean_up(); clean_up_hash(); exit(0); }
void input_directory(const char* dir_name, int recurse) { time_t now; int elapsed; DIR *dirp; struct dirent *dp; char file_name[MAX_FILE_NAME]; struct stat stat_buf; char *all_files, *files; char *all_dirs, *dirs; size_t dir_size; int total_files = 0, i = 0; char **file_array; printf("%s\n", dir_name); if ((dirp = opendir(dir_name)) == NULL) return; if (fstat(dirfd(dirp), &stat_buf) == -1) { closedir(dirp); return; } // The total size of the file names shouldn't exceed the size // of the directory file. So we just use that as the size. It's // wasteful, but meh. dir_size = stat_buf.st_size + 1024; files = all_files = malloc(dir_size); bzero(all_files, dir_size); // Go through all the files in the directory and put files // in one array and directories in another. while ((dp = readdir(dirp)) != NULL) { if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..")) { total_files++; strcpy(files, dp->d_name); files += strlen(dp->d_name) + 1; } } closedir(dirp); // Sort the files by name. files = all_files; file_array = calloc(sizeof(char*), total_files); while (*files) { file_array[i++] = files; files += strlen(files) + 1; } qsort(file_array, total_files, sizeof(char*), compare); dirs = all_dirs = malloc(dir_size); bzero(all_dirs, dir_size); // Go through all the files, stat them/thread them, and separate // out the directories. for (i = 0; i < total_files; i++) { snprintf(file_name, sizeof(file_name), "%s/%s", dir_name, file_array[i]); if (lstat(file_name, &stat_buf) != -1) { if (S_ISDIR(stat_buf.st_mode)) { strcpy(dirs, file_array[i]); dirs += strlen(file_array[i]) + 1; } else if (S_ISREG(stat_buf.st_mode)) { if (is_number(file_array[i])) { thread_file(file_name); if (! (commands++ % 10000)) { time(&now); elapsed = now - start_time; printf(" %d files (%d/s, last %d seconds)\n", commands - 1, (elapsed? (int)(10000 / elapsed): 0), elapsed); start_time = now; printf(" %lu bytes string storage per file\n", next_string / commands); mem_usage(); } } } } } free(file_array); if (recurse) { dirs = all_dirs; while (*dirs) { snprintf(file_name, sizeof(file_name), "%s/%s", dir_name, dirs); input_directory(file_name, recurse); dirs += strlen(dirs) + 1; } } free(all_files); free(all_dirs); }
void input_group(const char* group_name) { input_directory(get_group_directory(group_name), 0); }