int gen_keys(DArray * keys, int num_keys) { int i = 0; FILE *urand = fopen("/dev/urandom", "r"); check(urand != NULL, "Failed to open /dev/urandom"); int result = -1; // default to failure condition int rc = 0; struct bStream *stream = bsopen((bNread) fread, urand); check(stream != NULL, "Failed to open /dev/urandom"); bstring key = bfromcstr(""); // FNV1a histogram for (i = 0; i < num_keys; i++) { rc = bsread(key, stream, BUFFER_LEN); check(rc >= 0, "Failed to read from /dev/urandom."); DArray_push(keys, bstrcpy(key)); } result = 0; // all good error: // fallthrough if(stream) bsclose(stream); if(urand) fclose(urand); if(key) bdestroy(key); return result; }
END_TEST START_TEST(core_012) { struct tagbstring t = bsStatic("Hello world"); struct bStream * s; int ret = 0; bstring b, c; s = bsFromBstr(&t); ck_assert(s != NULL); b = bfromcstr(""); ck_assert(b != NULL); ret = bsread(b, s, 6); ck_assert_int_eq(ret, 0); ret = biseqcstr(b, "Hello "); ck_assert_int_eq(ret, 1); if (b) { b->slen = 0; } ret = bsread(b, s, 6); ck_assert_int_eq(ret, 0); ret = biseqcstr(b, "world"); ck_assert_int_eq(ret, 1); c = bsclose(s); ck_assert(c == NULL); ret = bdestroy(b); ck_assert_int_eq(ret, BSTR_OK); }
int gen_keys(DArray *keys, int num_keys) { int i = 0; FILE *urand = fopen("/dev/urandom", "r"); check(urand != NULL, "Failed to open /dev/urandom"); struct bStream *stream = bsopen((bNread)fread, urand); check(stream != NULL, "Failed to open /dev/urandom"); bstring key = bfromcstr(""); int rc = 0; // FNVla histogram for(i = 0; i < num_keys; i++) { rc = bsread(key, stream, BUFFER_LEN); check(rc >= 0, "Failed to read from /dev/urandom."); DArray_push(keys, bstrcpy(key)); } bsclose(stream); fclose(urand); return 0; error: return -1; }
int test12 (void) { struct tagbstring t = bsStatic ("Hello world"); struct bStream * s; bstring b; int ret = 0; printf ("TEST: bsFromBstr.\n"); ret = bsread (b = bfromcstr (""), s = bsFromBstr (&t), 6); ret += 1 != biseqcstr (b, "Hello "); if (b) b->slen = 0; ret = bsread (b, s, 6); ret += 1 != biseqcstr (b, "world"); bdestroy (b); bsclose (s); printf ("\t# failures: %d\n", ret); return ret; }
bool do_search(CURL* curl, bstring name, bool all) { DIR* dir; bool printed; CURLcode res; FILE* fp; list_t installed; struct bStream* stream; long httpcode = 0; bstring buffer, fname, sstr; bstring ext = bfromcstr(".lua"); bstring url = bfromcstr("http://dms.dcputoolcha.in/modules/search?q="); bstring modpath = osutil_getmodulepath(); struct dirent* entry; list_init(&installed); list_attributes_copy(&installed, list_meter_string, 1); list_attributes_comparator(&installed, list_comparator_string); // Attempt to open the modules directory. dir = opendir(modpath->data); if (dir == NULL) { printd(LEVEL_ERROR, "unable to query local repository.\n"); return 1; } // Append the temporary search file name. bcatcstr(modpath, "/.search"); bconcat(url, name); // Open the file and do the cURL transfer. printd(LEVEL_DEFAULT, "querying module repository...\n"); fp = fopen(modpath->data, "wb"); curl_easy_setopt(curl, CURLOPT_URL, url->data); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); res = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &httpcode); if (res != 0 || httpcode != 200) { bdestroy(url); bdestroy(name); bdestroy(modpath); printd(LEVEL_ERROR, "curl failed with error code %i, HTTP error code %i.\n", res, httpcode); return 1; } fclose(fp); // Print the local results. if (all) printd(LEVEL_DEFAULT, "all modules:\n"); else printd(LEVEL_DEFAULT, "search results for %s:\n", name->data); while ((entry = readdir(dir)) != NULL) { fname = bfromcstr(&entry->d_name[0]); if (binstr(fname, blength(fname) - 4, ext) == BSTR_ERR) { bdestroy(fname); continue; } if (binstr(fname, 0, name) == BSTR_ERR) { bdestroy(fname); continue; } if (entry->d_type != DT_REG) { bdestroy(fname); continue; } sstr = bmidstr(fname, 0, blength(fname) - 4); printd(LEVEL_DEFAULT, " %s (installed)\n", sstr->data); list_append(&installed, sstr->data); bdestroy(sstr); bdestroy(fname); } // Print the online results. fp = fopen(modpath->data, "r"); stream = bsopen(&read_data, fp); buffer = bfromcstr(""); printed = false; while (bsreadln(buffer, stream, '\n') != BSTR_ERR) { btrimws(buffer); sstr = bmidstr(buffer, 0, blength(buffer) - 4); if (!list_contains(&installed, sstr->data)) printd(LEVEL_DEFAULT, " %s\n", sstr->data); printed = true; bdestroy(sstr); } if (!printed) printd(LEVEL_DEFAULT, " <no online results>\n"); bsclose(stream); fclose(fp); // Clean up. curl_easy_cleanup(curl); return 0; }
bool do_install_all(CURL* curl) { // define used variables DIR* dir; FILE* fp; CURLcode res; bool printed; bool install_status; bool something_errored; bool if_something_was_installed; list_t installed; long httpcode = 0; struct dirent* entry; struct bStream* stream; bstring buffer, fname, sstr; bstring modpath = osutil_getmodulepath(); bstring ext = bfromcstr(".lua"); bstring url = bfromcstr("http://dms.dcputoolcha.in/modules/list"); list_init(&installed); list_attributes_copy(&installed, list_meter_string, 1); list_attributes_comparator(&installed, list_comparator_string); // Attempt to open the modules directory. dir = opendir(modpath->data); if (dir == NULL) { printd(LEVEL_ERROR, "unable to query local repository.\n"); return 1; } // add the filename we wish to query to the modules folder path bcatcstr(modpath, "/.all_avail"); // Open the file and do the cURL transfer. printd(LEVEL_DEFAULT, "loading a list of all the modules...\n"); fp = fopen(modpath->data, "wb"); curl_easy_setopt(curl, CURLOPT_URL, url->data); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); res = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &httpcode); if (res != 0 || httpcode != 200) { bdestroy(url); bdestroy(modpath); printd(LEVEL_ERROR, "curl failed with error code %i, HTTP error code %i.\n", res, httpcode); return 1; } fclose(fp); // create a list of already installed modules while ((entry = readdir(dir)) != NULL) { fname = bfromcstr(&entry->d_name[0]); if (binstr(fname, blength(fname) - 4, ext) == BSTR_ERR) { bdestroy(fname); continue; } if (entry->d_type != DT_REG) { bdestroy(fname); continue; } sstr = bmidstr(fname, 0, blength(fname) - 4); list_append(&installed, sstr->data); bdestroy(sstr); bdestroy(fname); } printd(LEVEL_DEFAULT, "\n"); // Print the names of the modules, and install them through the do_install function fp = fopen(modpath->data, "r"); stream = bsopen(&read_data, fp); buffer = bfromcstr(""); printed = false; if_something_was_installed = false; something_errored = false; while (bsreadln(buffer, stream, '\n') != BSTR_ERR) { btrimws(buffer); sstr = bmidstr(buffer, 0, blength(buffer) - 4); // if the module is not already installed if (!list_contains(&installed, sstr->data)) { install_status = do_install(curl, bfromcstr(sstr->data)); if_something_was_installed = true; // check whether the installation was successful if (install_status != 0) { printd(LEVEL_DEFAULT, " %s failed to install.\n", sstr->data); something_errored = true; } printd(LEVEL_DEFAULT, "\n"); } printed = true; bdestroy(sstr); } if (!printed) printd(LEVEL_DEFAULT, " <no modules available>\n"); if (something_errored) printd(LEVEL_DEFAULT, "errors occured\n"); if (!if_something_was_installed) printd(LEVEL_DEFAULT, "no changes were made\n"); bsclose(stream); fclose(fp); // Clean up. curl_easy_cleanup(curl); return 0; }