void search_files(DIR *root, const char *rel_path, char *current_path, int level) { int index = 0; struct dirent *current; char full_path_name[MAXPATHLEN]; chdir(rel_path); strcpy(full_path_name, current_path); while ((current = readdir(root)) != NULL) { join_paths(full_path_name, full_path_name, current->d_name); if (current->d_type == DT_DIR && CHECK_IGNORED(current->d_name)) { search_files(opendir(current->d_name), current->d_name, full_path_name, ++level); chdir(".."); } else if (CHECK_IGNORED(current->d_name)) { if ((index = find_file(full_path_name)) >= 0) printf("%-20s %s\n", obvious_projects[index], full_path_name); } remove_component(full_path_name, current->d_name); } closedir(root); }
static int root_search_files( volume *vol, int recursive, match_proc_t proc, const void *match_data, hfsp_file_t *pt ) { record r; record_init_root( &r, &vol->catalog ); return search_files( &r, recursive, proc, match_data, pt ); }
int main(int argc, char** argv) { int child_status, i; if(argc < 4) { printf("Error: Malformed\n"); exit(0); } dirin = argv[1]; dirout = argv[2]; group_proceses = atoi(argv[3]); if(group_proceses <= 0) { printf("Hay %d validadores para crear, por lo que el programa termina aqui\n", group_proceses); exit(0); } search_files(dirin); if(group_proceses > queue->data.size) { group_proceses = queue->data.size; printf("Hay mas validadores que el numero de tareas (%d). Se crearan solo %d validadores\n", group_proceses,group_proceses); } sons = (pid_t*) realloc(NULL, sizeof(pid_t)*group_proceses); position_queue = queue->data.size-1; i = 0; while(i < group_proceses) { printf("Se va a validar el fichero: %s\n", queue->list[position_queue].list); create_child_process(sons, i); i++; position_queue--; nproceses++; } while(nproceses > 0) { pid_t wpid = wait(&child_status); nproceses--; if(position_queue >= 0) { int index_free = get_index_from_value(sons, group_proceses, wpid); printf("Se va a validar el fichero: %s\n", queue->list[position_queue].list); create_child_process(sons, index_free); position_queue--; nproceses++; } } exit(0); }
TEST(Filesystem, SearchFiles) { // search files with an extension std::vector<std::string> str_file_name_list; search_files("..\\filesystem\\", ".hpp", str_file_name_list); std::cout << str_file_name_list.size() << " files." << std::endl; for(size_t i = 0; i < str_file_name_list.size(); i++) { std::cout << str_file_name_list[i] << std::endl; } }
static int match_rom( record *r, record *par, const void *match_data, hfsp_file_t *pt ) { hfsp_cat_file *file = &r->record.u.file; FInfo *fi = &file->user_info; int ret = 1; char buf[256]; if( r->record.type == HFSP_FILE && fi->fdCreator == MAC_OS_ROM_CREATOR && fi->fdType == MAC_OS_ROM_TYPE ) { ret = search_files( par, 0, match_file, "System", NULL ) || search_files( par, 0, match_file, "Finder", NULL ); (void) unicode_uni2asc(buf, &r->key.name, sizeof(buf)); if( !strcasecmp("BootX", buf) ) return 1; if( !ret && pt ) pt->rec = *r; } return ret; }
/** Check the directory for new file events. * * Note: * If any directory entries are sub-directories and the recursive flag is set, * then this function recurses on the sub-directories. */ void check_directory(const char *path, struct hooks_t *hooks, struct files_t *files, bool recursive, const time_t timestamp) { //struct dirent **entries; struct dirent *entry; DIR *dp; char fullpath[PATH_MAX]; dp=opendir(path); if(dp == NULL){ fprintf(stderr,"Could not open directory: %s\n", path); return; } while((entry = readdir(dp)) != NULL){ if(streq(entry->d_name, ".") || streq(entry->d_name, "..")){ continue; } snprintf(fullpath, PATH_MAX, "%s/%s", path, entry->d_name); //printf("%s\n", fullpath); struct stat st; lstat(fullpath, &st); if(S_ISDIR(st.st_mode) && recursive){ printf("DIRECTORY: %s\n", entry->d_name); if(strcmp(entry->d_name,"..") != 0 && strcmp(entry->d_name, ".") != 0){ check_directory(fullpath, hooks, files, recursive, timestamp); } }else{ printf("\t%s\n", entry->d_name); printf("\t\t FILE\n"); if(search_files(files, fullpath) == NULL){ add_file(files, fullpath, st.st_mtime, timestamp); printf("CREATE EVENT"); //CREATE EVENT } else{ printf("We have a file match? WTF"); // compare mtime // check timestamp } //look at hooks and files //use n as count of files? } } closedir(dp); }
int main(int argc, char *argv[]) { DIR *root = opendir("."); if (!root) { return -1; } search_files(root, ".", ".", 0); return 0; }
/** * Check file for MODIFY and CREATE events * * Note: * The file's mtime should be updated if it has been modified. Additionally, * the file's timestamp should be updated if the file is in the files list. */ void check_file(const char *path, const time_t mtime, struct hooks_t *hooks, struct files_t *files, const time_t timestamp) { struct file_t *file; file = search_files(files, path); if(file == NULL){ //CREATE EVENT //add file }else if(mtime != file->mtime){ //MODIFY EVENT } }
int accept_client() { fd_set set; struct timeval tv; int ret; int fd; pthread_t threads[1024]; int thread_count = 0; search_files("/adm"); FD_ZERO(&set); while(1) { FD_SET(listenfd, &set); tv.tv_sec = 10; tv.tv_usec = 0; ret = select(listenfd+1, &set, NULL, NULL, &tv); if(ret < 0) { printf("select error\n"); exit(1); } else if(ret == 0) { break; } else { fd = accept(listenfd, NULL, NULL); if(fd > 0) { pthread_create(&threads[thread_count++], NULL, send_func, (void*)fd); printf("thread counts : %d\n", thread_count); } } } // wait threads for(ret=0; ret<thread_count; ret++) { pthread_join(threads[ret], NULL); } for(ret=0; ret<max_files; ret++) { free(files[ret]); } close(listenfd); }
/* ( -- success? ) */ static void hfsp_files_open( hfsp_info_t *mi ) { int fd; char *path = my_args_copy(); if ( ! path ) RET( 0 ); fd = open_ih( my_parent() ); if ( fd == -1 ) { free( path ); RET( 0 ); } mi->vol = malloc( sizeof(volume) ); if (volume_open(mi->vol, fd)) { free( path ); close_io( fd ); RET( 0 ); } mi->hfspfile = malloc( sizeof(hfsp_file_t) ); /* Leading \\ means system folder. The finder info block has * the following meaning. * * [0] Prefered boot directory ID * [3] MacOS 9 boot directory ID * [5] MacOS X boot directory ID */ if( !strncmp(path, "\\\\", 2) ) { int *p = (int*)&(mi->vol)->vol.finder_info[0]; int cnid = p[0]; /* printk(" p[0] = %x, p[3] = %x, p[5] = %x\n", p[0], p[3], p[5] ); */ if( p[0] == p[5] && p[3] ) cnid = p[3]; if( record_init_cnid(&(mi->hfspfile->rec), &(mi->vol)->catalog, cnid) ) RET ( 0 ); path += 2; } else { record_init_root( &(mi->hfspfile->rec), &(mi->vol)->catalog ); } if( !search_files(&(mi->hfspfile->rec), 0, match_path, path, mi->hfspfile ) ) RET ( -1 ); RET ( -1 ); }
static int match_path( record *r, record *par, const void *match_data, hfsp_file_t *pt ) { char name[256], *s, *next, *org; int ret=1; next = org = strdup( (char*)match_data ); while( (s=strsep( &next, "\\/" )) && !strlen(s) ) ; if( !s ) { free( org ); return 1; } if( *s == ':' && strlen(s) == 5 ) { if( r->record.type == HFSP_FILE && !next ) { /* match type */ hfsp_cat_file *file = &r->record.u.file; FInfo *fi = &file->user_info; int i, type=0; for( i=1; s[i] && i<=4; i++ ) type = (type << 8) | s[i]; /* printk("fi->fdType: %s / %s\n", s+1, b ); */ if( fi->fdType == type ) { if( pt ) pt->rec = *r; ret = 0; } } } else { (void) unicode_uni2asc(name, &r->key.name, sizeof(name)); if( !strcasecmp(s, name) ) { if( r->record.type == HFSP_FILE && !next ) { if( pt ) pt->rec = *r; ret = 0; } else /* must be a directory */ ret = search_files( r, 0, match_path, next, pt ); } } free( org ); return ret; }
static int search_files( record *par, int recursive, match_proc_t proc, const void *match_data, hfsp_file_t *pt ) { hfsp_file_t t; record r; int ret = 1; t.path = NULL; record_init_parent( &r, par ); do{ if( r.record.type == HFSP_FOLDER || r.record.type == HFSP_FILE ) ret = (*proc)( &r, par, match_data, &t ); if( ret && r.record.type == HFSP_FOLDER && recursive ) ret = search_files( &r, 1, proc, match_data, &t ); } while( ret && !record_next(&r) ); if( !ret && pt ) { char name[256]; const char *s2 = t.path ? t.path : ""; unicode_uni2asc( name, &r.key.name, sizeof(name)); pt->rec = t.rec; pt->path = malloc( strlen(name) + strlen(s2) + 2 ); strcpy( pt->path, name ); if( strlen(s2) ) { strcat( pt->path, "\\" ); strcat( pt->path, s2 ); } } if( t.path ) free( t.path ); return ret; }
void search_files(char* path) { DIR* dir; struct dirent* entry; char* filename; dir = opendir(path); while(1) { entry = readdir(dir); if(entry == NULL) break; if(*entry->d_name == '.') continue; filename = malloc(256); strcpy(filename, path); strcat(filename, "/"); strcat(filename, entry->d_name); if(entry->d_type == DT_REG) { //send_file(fd, filename); files[max_files++] = filename; } else if(entry->d_type == DT_DIR) { search_files(filename); free(filename); } else { printf("something terreble\n"); } } }
int src(int ac, char *av[]) { po::options_description rec_desc("dicom parsing options"); rec_desc.add_options() ("help", "help message") ("action", po::value<std::string>(), "src:dicom parsing") ("source", po::value<std::string>(), "assign the directory for the dicom files") ("recursive", po::value<std::string>(), "search subdirectories") ("b_table", po::value<std::string>(), "assign the b-table") ("bval", po::value<std::string>(), "assign the b value") ("bvec", po::value<std::string>(), "assign the b vector") ("output", po::value<std::string>(), "assign the output filename") ; if(!ac) { std::cout << rec_desc << std::endl; return 1; } po::variables_map vm; po::store(po::command_line_parser(ac, av).options(rec_desc).run(), vm); po::notify(vm); std::string source = vm["source"].as<std::string>(); std::string ext; if(source.size() > 4) ext = std::string(source.end()-4,source.end()); boost::ptr_vector<DwiHeader> dwi_files; QStringList file_list; if(ext ==".nii" || ext == ".dcm" || ext == "dseq" || ext == "i.gz") { std::cout << "image=" << source.c_str() << std::endl; file_list << source.c_str(); } else { std::cout << "load files in directory " << source.c_str() << std::endl; QDir directory = QString(source.c_str()); if(vm.count("recursive")) { std::cout << "search recursively in the subdir" << std::endl; file_list = search_files(source.c_str(),"*.dcm"); } else { file_list = directory.entryList(QStringList("*.dcm"),QDir::Files|QDir::NoSymLinks); if(file_list.empty()) file_list = directory.entryList(QStringList("*.nii.gz"),QDir::Files|QDir::NoSymLinks); for (unsigned int index = 0;index < file_list.size();++index) file_list[index] = QString(source.c_str()) + "/" + file_list[index]; } std::cout << "A total of " << file_list.size() <<" files found in the directory" << std::endl; } if(file_list.empty()) { std::cout << "No file found for creating src" << std::endl; return -1; } if(!load_all_files(file_list,dwi_files)) { std::cout << "Invalid file format" << std::endl; return -1; } if(vm.count("b_table")) { std::string table_file_name = vm["b_table"].as<std::string>(); std::ifstream in(table_file_name.c_str()); if(!in) { std::cout << "Failed to open b-table" <<std::endl; return -1; } std::string line; std::vector<double> b_table; while(std::getline(in,line)) { std::istringstream read_line(line); std::copy(std::istream_iterator<double>(read_line), std::istream_iterator<double>(), std::back_inserter(b_table)); } if(b_table.size() != dwi_files.size()*4) { std::cout << "Mismatch between b-table and the loaded images" << std::endl; return -1; } for(unsigned int index = 0,b_index = 0;index < dwi_files.size();++index,b_index += 4) { dwi_files[index].set_bvalue(b_table[b_index]); dwi_files[index].set_bvec(b_table[b_index+1],b_table[b_index+2],b_table[b_index+3]); } std::cout << "B-table " << table_file_name << " loaded" << std::endl; } if(vm.count("bval") && vm.count("bvec")) { std::vector<double> bval,bvec; std::cout << "load bval=" << vm["bval"].as<std::string>() << std::endl; std::cout << "load bvec=" << vm["bvec"].as<std::string>() << std::endl; load_bval(vm["bval"].as<std::string>().c_str(),bval); load_bvec(vm["bvec"].as<std::string>().c_str(),bvec); if(bval.size() != dwi_files.size()) { std::cout << "Mismatch between bval file and the loaded images" << std::endl; return -1; } if(bvec.size() != dwi_files.size()*3) { std::cout << "Mismatch between bvec file and the loaded images" << std::endl; return -1; } for(unsigned int index = 0;index < dwi_files.size();++index) { dwi_files[index].set_bvalue(bval[index]); dwi_files[index].set_bvec(bvec[index*3],bvec[index*3+1],bvec[index*3+2]); } } if(dwi_files.empty()) { std::cout << "No file readed. Abort." << std::endl; return 1; } double max_b = 0; for(unsigned int index = 0;index < dwi_files.size();++index) { if(dwi_files[index].get_bvalue() < 100) dwi_files[index].set_bvalue(0); max_b = std::max(max_b,(double)dwi_files[index].get_bvalue()); } if(max_b == 0.0) { std::cout << "Cannot find b-table from the header. You may need to load an external b-table using--b_table or --bval and --bvec." << std::endl; return 1; } std::cout << "Output src " << vm["output"].as<std::string>().c_str() << std::endl; DwiHeader::output_src(vm["output"].as<std::string>().c_str(),dwi_files,0); return 0; }
int src(void) { std::string source = po.get("source"); std::string ext; if(source.size() > 4) ext = std::string(source.end()-4,source.end()); std::string output; if(po.has("output")) output = po.get("output"); if(QFileInfo(output.c_str()).exists()) { std::cout << output << " exists. skipping..." << std::endl; return 1; } std::vector<std::shared_ptr<DwiHeader> > dwi_files; QStringList file_list; if(ext ==".nii" || ext == ".dcm" || ext == "dseq" || ext == "i.gz") { std::cout << "image=" << source.c_str() << std::endl; std::istringstream ss(source); std::string token; while(std::getline(ss, token, ',')) file_list << source.c_str(); if(!po.has("output")) output = file_list.front().toStdString() + ".src.gz"; } else { std::cout << "load files in directory " << source.c_str() << std::endl; QDir directory = QString(source.c_str()); if(po.has("recursive")) { std::cout << "search recursively in the subdir" << std::endl; file_list = search_files(source.c_str(),"*.dcm"); } else { file_list = directory.entryList(QStringList("*.dcm"),QDir::Files|QDir::NoSymLinks); if(file_list.empty()) file_list = directory.entryList(QStringList("*.nii.gz"),QDir::Files|QDir::NoSymLinks); if(file_list.empty()) file_list = directory.entryList(QStringList("*.fdf"),QDir::Files|QDir::NoSymLinks); for (unsigned int index = 0;index < file_list.size();++index) file_list[index] = QString(source.c_str()) + "/" + file_list[index]; } std::cout << "A total of " << file_list.size() <<" files found in the directory" << std::endl; if(!po.has("output")) output = source + ".src.gz"; } if(file_list.empty()) { std::cout << "No file found for creating src" << std::endl; return 1; } if(!load_all_files(file_list,dwi_files)) { std::cout << "Invalid file format" << std::endl; return 1; } if(po.has("b_table")) { std::string table_file_name = po.get("b_table"); std::ifstream in(table_file_name.c_str()); if(!in) { std::cout << "Failed to open b-table" <<std::endl; return 1; } std::string line; std::vector<double> b_table; while(std::getline(in,line)) { std::istringstream read_line(line); std::copy(std::istream_iterator<double>(read_line), std::istream_iterator<double>(), std::back_inserter(b_table)); } if(b_table.size() != dwi_files.size()*4) { std::cout << "Mismatch between b-table and the loaded images" << std::endl; return 1; } for(unsigned int index = 0,b_index = 0;index < dwi_files.size();++index,b_index += 4) { dwi_files[index]->set_bvalue(b_table[b_index]); dwi_files[index]->set_bvec(b_table[b_index+1],b_table[b_index+2],b_table[b_index+3]); } std::cout << "B-table " << table_file_name << " loaded" << std::endl; } if(po.has("bval") && po.has("bvec")) { std::vector<double> bval,bvec; std::cout << "load bval=" << po.get("bval") << std::endl; std::cout << "load bvec=" << po.get("bvec") << std::endl; load_bval(po.get("bval").c_str(),bval); load_bvec(po.get("bvec").c_str(),bvec); if(bval.size() != dwi_files.size()) { std::cout << "Mismatch between bval file and the loaded images" << std::endl; return 1; } if(bvec.size() != dwi_files.size()*3) { std::cout << "Mismatch between bvec file and the loaded images" << std::endl; return 1; } for(unsigned int index = 0;index < dwi_files.size();++index) { dwi_files[index]->set_bvalue(bval[index]); dwi_files[index]->set_bvec(bvec[index*3],bvec[index*3+1],bvec[index*3+2]); } } if(dwi_files.empty()) { std::cout << "No file readed. Abort." << std::endl; return 1; } double max_b = 0; for(unsigned int index = 0;index < dwi_files.size();++index) { if(dwi_files[index]->get_bvalue() < 100) dwi_files[index]->set_bvalue(0); max_b = std::max(max_b,(double)dwi_files[index]->get_bvalue()); } if(max_b == 0.0) { std::cout << "Cannot find b-table from the header. You may need to load an external b-table using--b_table or --bval and --bvec." << std::endl; return 1; } std::cout << "Output src to " << output << std::endl; DwiHeader::output_src(output.c_str(),dwi_files, po.get<int>("up_sampling",0), po.get<int>("sort_b_table",0)); return 0; }
/** * Main method */ int main(int argc, char **argv) { int runs = 100; FILE *out = stderr; int quiet = 0; int warmup_runs = 10; int opt_code; int outliers = 0; double *run_times = malloc(runs * sizeof(double)); double sum = 0.0; double mean = 0.0; double standard_deviation = 0.0; int median_index = 0; double median = 0.0; int i; while((opt_code = getopt(argc, argv, "hn:o:qw:")) != -1) { switch(opt_code) { case 'h': print_usage(argc, argv); return 0; case 'n': runs = atoi(optarg); break; case 'o': out = fopen(optarg, "w"); break; case 'q': quiet = 1; break; case 'w': warmup_runs = atoi(optarg); break; default: return 1; } } /* Warmup runs */ for(i = 0; i < warmup_runs; i++) { if(!quiet) fprintf(out, "Warmup: %2d/%d...\n", i + 1, warmup_runs); search_files(argv[optind], argv + optind + 1, argc - optind - 1); } /* Timed runs */ for(i = 0; i < runs; i++) { double start, stop; if(!quiet) fprintf(out, "Run: %2d/%d...\n", i + 1, runs); start = get_time(); search_files(argv[optind], argv + optind + 1, argc - optind - 1); stop = get_time(); run_times[i] = stop - start; if(!quiet) fprintf(out, "Time: %f\n", run_times[i]); mean += run_times[i] / (double) runs; } /* Calculate mean */ fprintf(out, "Mean: %f\n", mean); /* Calculate standard deviation */ for(i = 0; i < runs; i++) { standard_deviation += SQUARE(run_times[i] - mean); } standard_deviation = sqrt(standard_deviation / runs); fprintf(out, "Standard deviation: %f\n", standard_deviation); /* Calculate outliers */ for(i = 0; i < runs; i++) { if(ABS(mean - run_times[i]) > standard_deviation * 3) outliers++; } fprintf(out, "Outliers: %d\n", outliers); /* Sort the run times to find the median */ qsort(run_times, runs, sizeof(double), compare_doubles); median_index = runs / 2; if(runs % 2) { median = 0.5 * (run_times[median_index] + run_times[median_index + 1]); } else { median = run_times[median_index]; } fprintf(out, "Median: %f\n", median); free(run_times); return 0; }