TEST_F(IOFSTest, PrefixValid) { char v1[] = "/dev/fs/data"; char v2[] = "/dev/fs/"; char iv1[] = "data"; char iv2[] = "/data"; char iv3[] = "/dev"; char iv4[] = "/dev/fs"; char iv5[] = "/dev/files/x"; char iv6[] = ""; EXPECT_TRUE(prefix_valid(v1)); EXPECT_TRUE(prefix_valid(v2)); EXPECT_FALSE(prefix_valid(iv1)); EXPECT_FALSE(prefix_valid(iv2)); EXPECT_FALSE(prefix_valid(iv3)); EXPECT_FALSE(prefix_valid(iv4)); EXPECT_FALSE(prefix_valid(iv5)); EXPECT_FALSE(prefix_valid(iv6)); }
/** * init module function */ static int mod_init(void) { db_res_t* db_res = NULL; code_t i, code; dc_t* cell; DBG("PDT: initializing...\n"); if(hs_two_pow<0) { LOG(L_ERR, "PDT: mod_init: hash_size_two_pow must be" " positive and less than %d\n", MAX_HSIZE_TWO_POW); return -1; } if(code_terminator>9 || code_terminator<0) { LOG(L_ERR, "PDT: mod_init: code_terminator must be a digit\n"); return -1; } if(!prefix_valid()) return -1; next_code = (code_t*)shm_malloc(sizeof(code_t)); if(!next_code) { LOG(L_ERR, "PDT: mod_init: cannot allocate next_code!\n"); return -1; } if(lock_init(&l) == 0) { shm_free(next_code); LOG(L_ERR, "PDT: mod_init: cannot init the lock\n"); return -1; } if(register_fifo_cmd(get_domainprefix, "get_domainprefix", 0)<0) { LOG(L_ERR, "PDT: mod_init: cannot register fifo command 'get_domaincode'\n"); goto error1; } /* binding to mysql module */ if(bind_dbmod()) { LOG(L_ERR, "PDT: mod_init: Database module not found\n"); goto error1; } /* open a connection with the database */ db_con = db_init(db_url); if(!db_con) { LOG(L_ERR, "PDT: mod_init: Error while connecting to database\n"); goto error1; } else { db_use_table(db_con, db_table); DBG("PDT: mod_init: Database connection opened successfully\n"); } /* init hashes in share memory */ if( (hash = init_double_hash(hs_two_pow)) == NULL) { LOG(L_ERR, "PDT: mod_init: hash could not be allocated\n"); goto error2; } /* loading all information from database */ *next_code = 0; if(db_query(db_con, NULL, NULL, NULL, NULL, 0, 0, "code", &db_res)==0) { for(i=0; i<RES_ROW_N(db_res); i++) { code = RES_ROWS(db_res)[i].values[0].val.int_val; if (!code_valid(code)) { LOG(L_ERR, "PDT: mod_init: existing code contains the terminator\n"); goto error; } if (*next_code < code) *next_code = code; cell=new_cell( (char*)(RES_ROWS(db_res)[i].values[1].val.string_val), code); if(cell == NULL) goto error; if(add_to_double_hash(hash, cell)<0) { LOG(L_ERR, "PDT: mod_init: could not add information from database" " into shared-memory hashes\n"); goto error; } } // clear up here //print_hash(hash->dhash, hash->hash_size); //print_hash(hash->chash, hash->hash_size); (*next_code)++; if (*next_code < start_range) *next_code = start_range; *next_code = apply_correction(*next_code); DBG("PDT: mod_init: next_code:%d\n", *next_code); /* free up the space allocated for response */ if(db_free_query(db_con, db_res)<0) { LOG(L_ERR, "PDT: mod_init: error when freeing" " up the response space\n"); } } else { /* query to database failed */ LOG(L_ERR, "PDT: mod_init: query to database failed\n"); goto error; } db_close(db_con); /* janakj - close the connection */ /* success code */ return 0; error: free_double_hash(hash); error2: db_close(db_con); error1: shm_free(next_code); lock_destroy(&l); return -1; }