void handle_syscall(syscall_req_t* req, syscall_rsp_t* rsp) { switch (req->header.id) { case OPEN_ID: handle_open(req, rsp); break; case CLOSE_ID: handle_close(req, rsp); break; case READ_ID: handle_read(req, rsp); break; case WRITE_ID: handle_write(req, rsp); break; case LINK_ID: handle_link(req, rsp); break; case UNLINK_ID: handle_unlink(req, rsp); break; case LSEEK_ID: handle_lseek(req, rsp); break; case FSTAT_ID: handle_fstat(req, rsp); break; case ISATTY_ID: handle_isatty(req, rsp); break; case STAT_ID: handle_stat(req, rsp); break; default: error(-1, "Illegal syscall, should never be here..."); } rsp->header.return_errno = errno; }
/** process_directory * A recursive fuction that processes all directories * * Parameters: * struct Filename *file - the names associated with the file */ void* process_directory(void *val){ DIR *dptr; struct dirent *entry; struct dirent *ent_buf; struct Thread_args *ta; unsigned int my_flags; int max_depth; int path_max; ta = malloc(sizeof(struct Thread_args)); if(ta == NULL){ exit(EXIT_FAILURE); } ta = (struct Thread_args*)val; my_flags = ta->gl->my_flags; max_depth = ta->gl->max_depth; path_max = ta->gl->path_max; errno = 0; if((dptr = opendir(ta->fe->file->realptr)) == NULL){ /* If the file was not opened successfully then print an error and move on to the next file */ if(errno != 0){ /* If an error occurred opening the file then print a message */ if(ta->fe->file->is_cmd_line || (my_flags&QUIET) != QUIET){ /* If the file was specified on the commandline or if the -q flag was not selected then print the error */ pthread_mutex_lock(&mu); if(ta->gl->is_network){ char *output; output = calloc(sizeof(char), ta->gl->path_max); sprintf(output, "%s: %s\n", strerror(errno), ta->fe->file->name); write_error_line(output, ta->gl); free(output); } fprintf(stderr, "%s: %s\n", strerror(errno), ta->fe->file->name); pthread_mutex_unlock(&mu); } else{ /* If the file error was not printed then increment the corresponding counter */ ta->fe->file->counters->err_not_printed++; } if(ta->fe->file->counters->num_recurse == 0){ pthread_mutex_lock(&mu); (*(ta->gl->num_threads))--; pthread_mutex_unlock(&mu); } ta->fe->file->counters->num_recurse--; check_print_counters(ta->fe, ta->gl); return NULL; } } if(deal_with_cstack(ta->fe, dptr, ta->gl) == FAILURE){ /* If an error occurred adding the directory to the stack then return */ if(ta->fe->file->counters->num_recurse == 0){ pthread_mutex_lock(&mu); (*(ta->gl->num_threads))--; pthread_mutex_unlock(&mu); } ta->fe->file->counters->num_recurse--; check_print_counters(ta->fe, ta->gl); return NULL; } ent_buf = malloc(/*sizeof(struct dirent)*/path_max); if(ent_buf == NULL){ /* If memory allocation failed, print an error */ exit(EXIT_FAILURE); } while(1){ int status; struct Filename *newfile; readdir_r(dptr, ent_buf, &entry); if(entry == NULL){ break; } if(entry->d_name[0] == '.'){ /* Handle hidden files separately */ if((my_flags&DO_DOTS) == DO_DOTS){ /* Handles the -a flag if it was selected */ if(strcmp("..", entry->d_name) == 0 || strcmp(".", entry->d_name) == 0){ /* If the hidden directory is . or .. then ignore it */ continue; } ta->fe->file->counters->dot_processed++; } else{ /* If the -a flag wasn't selected then ignore the hidden file */ continue; } } newfile = malloc(sizeof(struct Filename)); if(newfile == NULL){ /* If memory allocation failed, print an error */ exit(EXIT_FAILURE); } newfile->name = malloc(path_max); newfile->realptr = malloc(path_max); newfile->fullptr = malloc(path_max); if(newfile->name == NULL || newfile->realptr == NULL || newfile->fullptr == NULL){ /* If memory allocation failed, print an error */ exit(EXIT_FAILURE); } strcpy(newfile->realptr, ta->fe->file->realptr); strcpy(newfile->name, entry->d_name); newfile->is_cmd_line = false; get_fullpath(newfile, &newfile->fullptr, ta->gl); if(newfile->fullptr == NULL){ /* Continue onto the next file if there was a failure getting the realpath of this one */ combine_counters(newfile->counters, ta->gl); continue; } newfile->counters = NULL; initialize_counters(&(newfile->counters)); if(handle_link(newfile, ta->gl) == true){ /* Try to handle a link. */ combine_counters(newfile->counters, ta->gl); continue; } status = is_directory(newfile->fullptr, newfile, ta->gl); if(status == 1){ /* Processes another directory */ struct Thread_args *newta; pthread_t threadid; DIR *newdptr; newta = malloc(sizeof(struct Thread_args)); if(newta == NULL){ exit(EXIT_FAILURE); } newta->gl = malloc(sizeof(struct Globals)); if(newta->gl == NULL){ exit(EXIT_FAILURE); } /*****HERE UPDATE*****/ newta->gl->my_flags = ta->gl->my_flags; newta->gl->num_chars = ta->gl->num_chars; newta->gl->max_lines = ta->gl->max_lines; newta->gl->max_depth = ta->gl->max_depth; newta->gl->path_max = ta->gl->path_max; newta->gl->allowed_threads = ta->gl->allowed_threads; newta->gl->num_threads = &(*(ta->gl->num_threads)); newta->gl->machine = &(*(ta->gl->machine)); newta->gl->is_printed = &(*(ta->gl->is_printed)); newta->gl->final_counters = &(*(ta->gl->final_counters)); newta->gl->donecmdln = &(*(ta->gl->donecmdln)); newta->gl->is_network = ta->gl->is_network; newta->gl->num_networking = &(*(ta->gl->num_networking)); newta->gl->fd = ta->gl->fd; newta->fe = malloc(sizeof(struct elem)); if(newta->fe == NULL){ exit(EXIT_FAILURE); } strcpy(newfile->realptr, ta->fe->file->name); if(newfile->realptr[strlen(newfile->realptr)-1] != '/') strcat(newfile->realptr, "/"); strcat(newfile->realptr, entry->d_name); strcpy(newfile->name, newfile->realptr); free(newfile->realptr); get_realpath(newfile, &newfile->realptr, ta->gl); if(newfile->realptr == NULL){ /* Continue onto the next file if there was a failure getting the realpath of this one */ combine_counters(newfile->counters, newta->gl); continue; } pthread_mutex_lock(&mu); cstack_add(newfile, ta->fe, &newta->fe); pthread_mutex_unlock(&mu); errno = 0; if((newdptr = opendir(newta->fe->file->realptr)) == NULL){ /* If the file was not opened successfully then print an error and move on to the next file */ if(errno != 0){ /* If an error occurred opening the file then print a message */ if(newta->fe->file->is_cmd_line || (my_flags&QUIET) != QUIET){ /* If the file was specified on the commandline or if the -q flag was not selected then print the error */ pthread_mutex_lock(&mu); if(newta->gl->is_network){ char *output; output = calloc(sizeof(char), newta->gl->path_max); sprintf(output, "%s: %s\n", strerror(errno), newta->fe->file->name); write_error_line(output, newta->gl); free(output); } fprintf(stderr, "%s: %s\n", strerror(errno), newta->fe->file->name); pthread_mutex_unlock(&mu); } else{ /* If the file error was not printed then increment the corresponding counter */ newta->fe->file->counters->err_not_printed++; } if(newta->fe->file->counters->num_recurse == 0){ pthread_mutex_lock(&mu); (*(ta->gl->num_threads))--; pthread_mutex_unlock(&mu); } combine_counters(newfile->counters, newta->gl); check_print_counters(newta->fe, ta->gl); continue; } } if(deal_with_cstack(newta->fe, newdptr, ta->gl) == FAILURE){ /* If an error occurred adding the directory to the stack then return */ if(ta->fe->file->counters->num_recurse == 0){ pthread_mutex_lock(&mu); (*(ta->gl->num_threads))--; pthread_mutex_unlock(&mu); } check_print_counters(newta->fe, ta->gl); continue; } newta->fe->file->counters->dr_opened--; if(max_depth == DEFAULT_DEPTH || ta->fe->file->counters->max_depth < max_depth){ if(*(newta->gl->num_threads) < newta->gl->allowed_threads || newta->gl->allowed_threads == DEFAULT_ALLOWED_THREADS){ if((status = pthread_create(&threadid, NULL, process_directory, (void*)newta))){ if(newta->gl->is_network){ char *output; output = calloc(sizeof(char), newta->gl->path_max); sprintf(output, "Could not create thread: %s\n", strerror(status)); write_error_line(output, newta->gl); free(output); } fprintf(stderr, "Could not create thread: %s\n", strerror(status)); newta->fe->file->counters->num_recurse = ta->fe->file->counters->num_recurse + 1; process_directory((void*)newta); } else{ pthread_mutex_lock(&mu); (*(ta->gl->num_threads))++; if(*(ta->gl->num_threads) > ta->fe->file->counters->max_dt_created) ta->fe->file->counters->max_dt_created = *(ta->gl->num_threads); pthread_mutex_unlock(&mu); pthread_detach(threadid); ta->fe->file->counters->num_dt_created++; } } else{ if((my_flags&QUIET) != QUIET){ /* If the file was specified on the commandline or if the -q flag was not selected then print the error */ if(ta->gl->is_network){ char *output; output = calloc(sizeof(char), ta->gl->path_max); sprintf(output, "Descent thread pruned due to a -t%d flag\n", newta->gl->allowed_threads); write_error_line(output, ta->gl); free(output); } fprintf(stderr, "Descent thread pruned due to a -t%d flag\n", newta->gl->allowed_threads); } else{ /* If the file error was not printed then increment the corresponding counter */ ta->fe->file->counters->err_not_printed++; } ta->fe->file->counters->num_dt_notcreated++; newta->fe->file->counters->num_recurse = ta->fe->file->counters->num_recurse + 1; process_directory((void*)newta); } } else{ process_directory((void*)newta); } } else if(status == 0){ /* Else process the entry as a file */ strcpy(newfile->name, newfile->fullptr); process_file(newfile, ta->gl); combine_counters(newfile->counters, ta->gl); } } if(ta->fe->file->counters->num_recurse == 0){ pthread_mutex_lock(&mu); (*(ta->gl->num_threads))--; pthread_mutex_unlock(&mu); } ta->fe->file->counters->num_recurse--; closedir(dptr); check_print_counters(ta->fe, ta->gl); return NULL; /*process_directory*/ }
bool cb_sc_display_link(GtkEntry* entry, girara_session_t* session) { return handle_link(entry, session, ZATHURA_LINK_ACTION_DISPLAY); }
bool cb_sc_follow(GtkEntry* entry, girara_session_t* session) { return handle_link(entry, session, ZATHURA_LINK_ACTION_FOLLOW); }
gboolean cb_sc_display_link(GtkEntry* entry, void* data) { girara_session_t* session = data; return handle_link(entry, session, ZATHURA_LINK_ACTION_DISPLAY); }
gboolean cb_sc_follow(GtkEntry* entry, void* data) { girara_session_t* session = data; return handle_link(entry, session, ZATHURA_LINK_ACTION_FOLLOW); }