void recursive_add_files(char* dir_path, struct queue_head* file_queue){ fprintf(stderr,"Reading directory path %s\n",dir_path); DIR* dir = opendir(dir_path); struct dirent * f; while((f = readdir(dir))!=NULL){ if(strcmp(".",f->d_name) == 0 || strcmp("..",f->d_name) == 0 || f->d_name[0] == '.') continue; char path[MAX_FILE_NAME]; int len = snprintf(path,sizeof(path)-1,"%s/%s",dir_path,f->d_name); path[len]='\0'; if(f->d_type == DT_DIR) { recursive_add_files(path,file_queue); continue; } if(!DT_REG == f->d_type){ continue; } char* file_path = strndup(path,MAX_FILE_NAME); struct queue* node = queue_create_node(file_path,strlen(file_path)+1); queue_prepend(file_queue,node); } closedir(dir); }
struct queue* find_file_paths(void* obj, int id, void* priv, struct queue_head* in_q,struct queue_head* file_queue) { char* read_from = priv; // add files to file_queue if(strcmp(read_from,"-") == 0) { // read a list of files from stdin char* filename = NULL; size_t sz = 1024; while(getline(&filename,&sz,stdin) > 0 ) { int l = strlen(filename); // printf("file [%s] last char[%c]\n",filename,filename[l-1]); //Remove new line if(filename[l-1] == '\n') { filename[l-1] = '\0'; } char* f = strdup(filename); int fl = strlen(f); struct queue* node = queue_create_node(f,fl+1); queue_prepend(file_queue,node); } } else{ struct stat file_info; if(stat(read_from,&file_info)!=0) { fprintf(stderr, "Couldnt locate file %s\n",read_from); perror("fstat"); return NULL; } if(S_ISDIR(file_info.st_mode)){ recursive_add_files(read_from,file_queue); } else { //regular file struct queue* node = queue_create_node(read_from,strlen(read_from)+1); queue_prepend(file_queue,node); } } // we are done filling this queue queue_mark_finish_filling(file_queue); return NULL; }
struct queue* search_transform(void* obj, int id, void* priv,struct queue_head* in_q,struct queue_head* oq) { struct search_queue_node* sqn = obj; char* search_term = priv; fprintf(stderr,"Call search_transform [%s] on file %s \n", search_term,sqn->file_name); search_buffer(id,sqn->file_name,search_term,sqn->iovec_num,sqn->vec); return queue_create_node(sqn,sizeof(struct search_queue_node)); }
queue_t *queue_init(int len) { queue_t *que; que = malloc(sizeof(queue_t)); que->head = queue_create_node(0); que->tail = que->head; que->clen = 0; que->mlen = len; return que; }
queue_node* queue_remove(queue_node** head) { //clone the struct **head into *node, node needs to be freed elsewhere queue_node* node = queue_create_node((*head)->buffer); *node = **head; node->next = NULL; //change head and free old head queue_node* oldHead = *head; (*head) = (*head)->next; free(oldHead); return node; }