int API::api_read(Json::Value &request, Json::Value &response, Json::Value &errors) { if (validate_read(request["data"], errors) < 0) { response["status"] = Json::Value(STATUS_STRUCTURE); return -1; } int user = authenticate(request["auth"]); if (user == -1) { response["status"] = STATUS_AUTH; errors.append(Json::Value("Authentication failed")); return -1; } create_session(response, user); Json::Value call_data; if (strcmp(request["data"]["view"].asCString(), "list") == 0) { if (strcmp(request["data"]["type"].asCString(), "profile") == 0) call_data = read_profile_list(request["data"], user, errors); else if (strcmp(request["data"]["type"].asCString(), "department") == 0) call_data = read_department_list(request["data"], user, errors); else if (strcmp(request["data"]["type"].asCString(), "user") == 0) call_data = read_user_list(request["data"], user, errors); else if (strcmp(request["data"]["type"].asCString(), "pictogram") == 0) call_data = read_pictogram_list(request["data"], user, errors); else if (strcmp(request["data"]["type"].asCString(), "application") == 0) call_data = read_application_list(request["data"], user, errors); else if (strcmp(request["data"]["type"].asCString(), "category") == 0) call_data = read_category_list(request["data"], user, errors); else { response["status"] = STATUS_STRUCTURE; errors.append(Json::Value("Invalid data type requested")); } } else { if (strcmp(request["data"]["type"].asCString(), "profile") == 0) call_data = read_profile_details(request["data"], user, errors); else if (strcmp(request["data"]["type"].asCString(), "department") == 0) call_data = read_department_details(request["data"], user, errors); else if (strcmp(request["data"]["type"].asCString(), "user") == 0) call_data = read_user_details(request["data"], user, errors); else if (strcmp(request["data"]["type"].asCString(), "pictogram") == 0) call_data = read_pictogram_details(request["data"], user, errors); else if (strcmp(request["data"]["type"].asCString(), "application") == 0) call_data = read_application_details(request["data"], user, errors); else if (strcmp(request["data"]["type"].asCString(), "category") == 0) call_data = read_category_details(request["data"], user, errors); else { response["status"] = STATUS_STRUCTURE; errors.append(Json::Value("Invalid data type requested")); } } if (!errors.empty()) { response["status"] = Json::Value(STATUS_ACCESS); return -1; } response["data"] = call_data; return 0; }
static void blocking_test(void) { thread_t th; int rc; for (int i = 0; i < BUF_SIZE; i++) write_buf[i] = i & 0x7F; syslog(LOG_INFO, "fifo blocking:\n wr_open..."); if ((wr_fd = open(fifo_name, O_WRONLY, 0)) < 0) err(1, "open(%s)", fifo_name); syslog(LOG_INFO, "write open ok\n write1..."); if ((rc = write(wr_fd, write_buf, WR1_SIZE)) != -1) errx(1, "write1 %d, expected %d", rc, 0); else if (errno != EPIPE) err(1, "write1 %d", WR1_SIZE); syslog(LOG_INFO, "write1 ok\n rd_open..."); if ((rd_fd = open(fifo_name, O_RDONLY, 0)) < 0) err(1, "open(%s)", fifo_name); syslog(LOG_INFO, "read open ok\n write1a..."); if ((rc = write(wr_fd, write_buf, WR1_SIZE)) < 0) err(1, "write1a %d", WR1_SIZE); else if (rc != WR1_SIZE) errx(1, "wrote1a %d, expected %d", rc, WR1_SIZE); syslog(LOG_INFO, "write1a ok\n thread_run..."); th = thread_run("read", read_thread, th_stack, sizeof(th_stack), 1); syslog(LOG_INFO, "thread_run ok\n write2..."); rc = write(wr_fd, write_buf + WR1_SIZE, WR2_SIZE); if (rc < 0) err(1, "write2 %d", WR2_SIZE); else if (rc != WR2_SIZE) errx(1, "wrote2 %d, expected %d", rc, WR2_SIZE); syslog(LOG_INFO, "write2 ok\n close..."); if (close(wr_fd) < 0) err(1, "close(%d)", wr_fd); syslog(LOG_INFO, "write close ok\n sleep..."); timer_sleep(1000/*ms*/, NULL); syslog(LOG_INFO, "sleep done\n data check..."); if (validate_write(0, BUF_SIZE) || validate_read(0, BUF_SIZE)) errx(1, "data corrupt"); syslog(LOG_INFO, "data check ok\nblocking test complete\n"); }
HashTable * hash_table_read_dumped_memory(char * filename ){ HashTable * hash = calloc(1, sizeof(HashTable)); FILE * fp = fopen(filename, "rb"); int magic_size = strlen(MAGIC_TEXT); char * magic = calloc(magic_size, sizeof(char)); int size_ht; int size_e = sizeof(Element); size_t readed; short version; long long number_buckets; int bucket_size; long long hash_size; if(fp == NULL){ exit_while_reading(fp, filename); } //Header stuff, this is enough information to prepare the hash table. readed = fread(magic, sizeof(char), magic_size, fp); validate_read(readed, magic_size, fp, filename); if(strcmp(magic, MAGIC_TEXT) != 0){ log_and_screen_printf( "[hash_table_read_dumped_memory] Invalid magic number!\n"); fclose(fp); exit(-1); } //#printf("%s\n", magic); readed = fread(&version, sizeof(short), 1, fp); validate_read(readed, 1, fp, filename); //#printf("%d\n", version); if(version != HASH_VERSION){ log_and_screen_printf( "[hash_table_read_dumped_memory] Invalid version number!\n"); exit_while_reading(fp, filename); } readed = fread(&size_ht, sizeof(int), 1,fp); validate_read(readed, 1, fp, filename); //#printf("%d\n", size_ht); if(size_ht != sizeof(HashTable)){ log_and_screen_printf( "[hash_table_read_dumped_memory] Invalid size of hash table!\n"); exit_while_reading(fp, filename); } readed = fread(&size_e, sizeof(int), 1,fp); validate_read(readed, 1, fp, filename); //#printf("%d\n", size_e); if(size_e != sizeof(Element)){ log_and_screen_printf( "[hash_table_read_dumped_memory] Invalid size of element!\n"); exit_while_reading(fp, filename); } readed = fread(&hash->kmer_size, sizeof(short), 1, fp); //printf("kmer size %d\n", hash->kmer_size); validate_read(readed, 1, fp, filename); readed = fread(&number_buckets, sizeof(long long), 1, fp); validate_read(readed, 1, fp, filename); //printf("number of buckets %lld\n",number_buckets); readed = fread(&bucket_size, sizeof(int), 1, fp); validate_read(readed, 1, fp, filename); //printf("bucket size%d \n",bucket_size); readed = fread(&hash->max_rehash_tries, sizeof(int), 1, fp); validate_read(readed, 1, fp, filename); //printf("hash->max_rehash_tries %d\n", hash->max_rehash_tries); readed = fread(&hash->unique_kmers, sizeof(long long), 1, fp); validate_read(readed, 1, fp, filename); // printf("hash->unique_kmers %lld\n", hash->unique_kmers); hash->number_buckets = number_buckets ; hash->bucket_size = bucket_size; hash_size=number_buckets * bucket_size; //printf("Hash size %lld \n", hash_size); //Allocating the table according to the description of the file hash->table = calloc(hash_size, sizeof(Element)); hash->next_element = calloc(number_buckets, sizeof(int)); hash->collisions = calloc(number_buckets, sizeof(long long)); if(hash->table == NULL){ log_and_screen_printf( "Unable to create hash table\n "); exit_while_reading(fp, filename); } //Reading the actual data. readed = fread(hash->table, size_e, hash_size, fp); validate_read(readed, hash_size, fp, filename); readed = fread(hash->next_element, sizeof(int), number_buckets, fp); validate_read(readed, number_buckets, fp, filename); readed = fread(hash->collisions, sizeof(long long), hash->max_rehash_tries, fp); validate_read(readed, hash->max_rehash_tries, fp, filename); fclose(fp); log_and_screen_printf("Hash readed from: %s \n" , filename); hash_table_print_stats(hash); return hash; }
static void nonblock_test(void) { int rc; char *rd = read_buf, *wr = write_buf; memset(read_buf, 0, BUF_SIZE); syslog(LOG_INFO, "fifo non-blocking test start...\n"); syslog(LOG_INFO, "ok\n rd open..."); if ((rd_fd = open(fifo_name, O_RDONLY | O_NONBLOCK, 0)) < 0) err(1, "open(%s)", fifo_name); syslog(LOG_INFO, "ok\n read1 expecting EOF..."); if ((rc = read(rd_fd, rd, RD1_SIZE)) < 0) err(1, "read1 %d", RD1_SIZE); else if (rc != 0) errx(1, "read1 %d, expected %d", rc, 0); syslog(LOG_INFO, "ok\n wr open..."); if ((wr_fd = open(fifo_name, O_WRONLY | O_NONBLOCK, 0)) < 0) err(1, "open(%s)", fifo_name); syslog(LOG_INFO, "ok\n read1a expecting EAGAIN..."); if ((rc = read(rd_fd, rd, RD1_SIZE)) != -1) errx(1, "read1a %d, expected %d", rc, 0); else if (errno != EAGAIN) err(1, "read1a %d", RD1_SIZE); syslog(LOG_INFO, "ok\n write1..."); if ((rc = write(wr_fd, wr, WR1_SIZE)) < 0) err(1, "write1 %d", WR1_SIZE); else if (rc != WR1_SIZE) errx(1, "write1 %d, expected %d", rc, WR1_SIZE); wr += rc; syslog(LOG_INFO, "ok\n read1b..."); if ((rc = read(rd_fd, rd, RD1_SIZE)) < 0) err(1, "read1b %d", RD1_SIZE); else if (rc != WR1_SIZE) /* expect what was written */ errx(1, "read1b %d, expected %d", rc, WR1_SIZE); rd += rc; syslog(LOG_INFO, "ok\n write2..."); if ((rc = write(wr_fd, wr, WR2_SIZE)) < 0) err(1, "write2 %d", WR2_SIZE); else if (rc != WR2_SIZE) errx(1, "write2 %d, expected %d", rc, WR2_SIZE); wr += rc; syslog(LOG_INFO, "ok\n read2..."); if ((rc = read(rd_fd, rd, RD2_SIZE)) < 0) err(1, "read2 %d", RD2_SIZE); else if (rc != RD2_SIZE) errx(1, "read2 %d, expected %d", rc, RD2_SIZE); rd += rc; syslog(LOG_INFO, "ok\n write3..."); rc = write(wr_fd, wr, WR3_SIZE); if (rc < 0) err(1, "write3 %d", WR3_SIZE); else if (rc != WR3_SIZE) errx(1, "write3 %d, expected %d", rc, WR3_SIZE); wr += rc; syslog(LOG_INFO, "ok\n close wr..."); if (close(wr_fd) < 0) err(1, "close(%d)", wr_fd); syslog(LOG_INFO, "ok\n read3..."); if ((rc = read(rd_fd, rd, RD3_SIZE)) < 0) err(1, "read3 %d", RD3_SIZE); else if (rc != RD3_SIZE) errx(1, "read3 %d, expected %d", rc, RD3_SIZE); rd += rc; syslog(LOG_INFO, "ok\n close rd..."); if (close(rd_fd) < 0) err(1, "close(%d)", rd_fd); syslog(LOG_INFO, "ok\n data check..."); if (validate_write(0, BUF_SIZE) || validate_read(0, BUF_SIZE)) errx(1, "data corrupt"); syslog(LOG_INFO, "ok\nnon-blocking test complete\n"); }