26static std::vector<std::string>
split(
const char* sep,
const std::string& s)
28 unsigned sep_len = strlen(sep);
29 std::vector<std::string> v;
30 std::string::size_type pos = 0;
32 std::string::size_type next = s.find(sep, pos);
34 if (next == std::string::npos) {
35 v.push_back(s.substr(pos));
38 v.push_back(s.substr(pos, next-pos));
142int main(
int argc,
char *argv[])
145 setbuf(stdout, NULL);
146 setbuf(stderr, NULL);
154 printf(
"Checking environment... experiment name is \"%s\", remote hostname is \"%s\"\n",
exp_name,
host_name);
157 printf(
"Error: trying to use a remote connection to host \"%s\". odbinit must run locally. Sorry.\n",
host_name);
161 bool cleanup =
false;
162 bool dry_run =
false;
163 bool create_exptab =
false;
164 bool create_env =
false;
168 for (
int i = 1;
i < argc;
i++) {
169 if (strcmp(argv[
i],
"-g") == 0) {
171 }
else if (strcmp(argv[
i],
"-n") == 0) {
173 }
else if (strcmp(argv[
i],
"--exptab") == 0) {
174 create_exptab =
true;
175 }
else if (strcmp(argv[
i],
"--env") == 0) {
177 }
else if (strcmp(argv[
i],
"--cleanup") == 0) {
179 }
else if (strcmp(argv[
i],
"-e") == 0) {
182 }
else if (strcmp(argv[
i],
"-s") == 0) {
190 printf(
"Checking command line... experiment \"%s\", cleanup %d, dry_run %d, create_exptab %d, create_env %d\n",
exp_name, cleanup, dry_run, create_exptab, create_env);
192#ifndef NO_LOCAL_ROUTINES
195 printf(
"Creating a new exptab file in the current directory...\n");
197 FILE *fpr = fopen(
"exptab",
"r");
200 printf(
"Error: exptab already exists in the current directory. Sorry...\n");
208 printf(
"Error: experiment name \"%s\" should not contain a space character. Sorry...\n",
exp_name);
211 const char*
pwd = getenv(
"PWD");
212 if (!
pwd || strlen(
pwd)<1) {
213 printf(
"Error: env.variable PWD is not defined or is empty. Sorry...\n");
216 if (strchr(
pwd,
' ')) {
217 printf(
"Error: env.variable PWD value \"%s\" should not contain a space character. Sorry...\n",
pwd);
220 const char* user = getenv(
"USER");
221 if (!user || strlen(user)<1) {
222 printf(
"Error: env.variable USER is not defined or is empty. Sorry...\n");
225 if (strchr(user,
' ')) {
226 printf(
"Error: env.variable USER value \"%s\" should not contain a space character. Sorry...\n", user);
229 printf(
"Experiment name [%s], experiment directory [%s], username [%s]\n",
exp_name,
pwd, user);
231 FILE*
fp = fopen(
"exptab",
"w");
233 printf(
"Error: Cannot write exptab file, fopen() errno %d (%s). Sorry...\n", errno, strerror(errno));
241 printf(
"Please define env.variable MIDAS_EXPTAB=%s/%s\n",
pwd,
"exptab");
248 printf(
"Creating a new environment settings file in the current directory...\n");
252 FILE *fpr = fopen(
"env.sh",
"r");
255 printf(
"Error: env.sh already exists in the current directory. Sorry...\n");
259 fpr = fopen(
"env.csh",
"r");
262 printf(
"Error: env.csh already exists in the current directory. Sorry...\n");
268 const char*
pwd = getenv(
"PWD");
269 if (!
pwd || strlen(
pwd)<1) {
270 printf(
"Error: env.variable PWD is not defined or is empty. Sorry...\n");
274 std::string midas_exptab;
277 midas_exptab +=
"exptab";
281 std::string argv0 = argv[0];
283 if (argv0[0] !=
'/') {
285 printf(
"Please run odbinit using the full path, i.e. run $HOME/packages/midas/linux/bin/odbinit\n");
293 std::string midassys;
295 if (argv0[0] ==
'/') {
303 std::vector<std::string> aa =
split(
"/", midassys);
321 if (argv0[0] ==
'/') {
329 std::vector<std::string> aa =
split(
"/", path);
341 FILE *fpb = fopen(
"env.sh",
"w");
343 printf(
"Error: Cannot write env.sh file, fopen() errno %d (%s). Sorry...\n", errno, strerror(errno));
347 FILE *fpc = fopen(
"env.csh",
"w");
349 printf(
"Error: Cannot write env.csh file, fopen() errno %d (%s). Sorry...\n", errno, strerror(errno));
353 fprintf(fpb,
"#!echo You must source\n");
354 fprintf(fpc,
"#!echo You must source\n");
356 fprintf(fpb,
"export MIDASSYS=\"%s\"\n", midassys.c_str());
357 fprintf(fpc,
"setenv MIDASSYS \"%s\"\n", midassys.c_str());
359 fprintf(fpb,
"export MIDAS_EXPTAB=\"%s\"\n", midas_exptab.c_str());
360 fprintf(fpc,
"setenv MIDAS_EXPTAB \"%s\"\n", midas_exptab.c_str());
362 fprintf(fpb,
"export PATH=$PATH:\"%s\"\n", path.c_str());
363 fprintf(fpc,
"setenv PATH $PATH\\:\"%s\"\n", path.c_str());
365 fprintf(fpb,
"# define mserver connection\n");
366 fprintf(fpb,
"case `hostname` in\n");
367 fprintf(fpb,
"%s) unset MIDAS_SERVER_HOST ;;\n", hostname.c_str());
368 fprintf(fpb,
"*) export MIDAS_SERVER_HOST=%s ;;\n", hostname.c_str());
369 fprintf(fpb,
"esac\n");
371 fprintf(fpc,
"# define mserver connection\n");
372 fprintf(fpc,
"switch (`hostname`)\n");
373 fprintf(fpc,
"case %s:\n", hostname.c_str());
374 fprintf(fpc,
"unsetenv MIDAS_SERVER_HOST\n");
375 fprintf(fpc,
"breaksw\n");
376 fprintf(fpc,
"default:\n");
377 fprintf(fpc,
"setenv MIDAS_SERVER_HOST %s\n", hostname.c_str());
378 fprintf(fpc,
"endsw\n");
380 fprintf(fpb,
"#end\n");
381 fprintf(fpc,
"#end\n");
387 printf(
"Please source env.sh or env.csh\n");
395 printf(
"Checking MIDASSYS...");
396 const char* midassys = getenv(
"MIDASSYS");
400 printf(
"Error: Env.variable MIDASSYS is not defined.\n");
401 printf(
"odbinit path [%s]\n", argv[0]);
403 printf(
"Please run odbinit --env\n");
409 printf(
"...%s\n", midassys);
417 printf(
"Error: cm_list_experiments() status %d\n",
status);
419 printf(
"Cannot get the list of experiments, maybe the exptab file is missing.\n");
421 printf(
"To create a new exptab file in the current directory, run odbinit --exptab -e new_experiment_name\n");
429 if (exptab_filename.empty()) {
430 printf(
"Cannot get the name of the exptab file. Sorry...\n");
434 printf(
"Checking exptab... experiments defined in exptab file \"%s\":\n", exptab_filename.c_str());
436 bool found_exp =
false;
437 for (
unsigned i=0;
i<exp_names.size();
i++) {
438 printf(
"%d: \"%s\"",
i, exp_names[
i].c_str());
442 printf(
" <-- selected experiment");
450 printf(
"Specified experiment \"%s\" not found in exptab. Sorry...\n",
exp_name);
455 std::string exp_user;
460 printf(
"Specified experiment \"%s\" not found in exptab, cm_get_exptab() returned %d. Sorry...\n",
exp_name,
status);
465 printf(
"Checking exptab... selected experiment \"%s\", experiment directory \"%s\"\n",
exp_name, exp_dir.c_str());
474 printf(
"Checking experiment directory \"%s\"\n", exp_dir.c_str());
477 struct stat stat_buf;
478 int v = stat(exp_dir.c_str(), &stat_buf);
481 printf(
"Invalid experiment directory \"%s\" does not seem to exist, stat() returned %d, errno %d (%s)\n", exp_dir.c_str(), v, errno, strerror(errno));
486 if (!S_ISDIR(stat_buf.st_mode)) {
487 printf(
"Invalid experiment directory \"%s\" is not a directory\n", exp_dir.c_str());
492#error No support for stat() and S_ISDIR on this system!
496 std::string odb_path;
503 FILE *
fp = fopen(path.c_str(),
"r");
506 printf(
"Found existing ODB save file: \"%s\"\n", path.c_str());
513 printf(
"Looks like this experiment ODB is already initialized.\n");
514 printf(
"To create new empty ODB, please rerun odbinit with the \"--cleanup\" option.\n");
518 printf(
"Good: no ODB save file\n");
523 printf(
"Checking shared memory...\n");
526 printf(
"Deleting old ODB shared memory...\n");
530 printf(
"Good: no ODB shared memory\n");
532 printf(
"Deleted existing ODB shared memory, please check that all MIDAS programs are stopped and try again.\n");
535 printf(
"ss_shm_delete(ODB) status %d\n",
status);
536 printf(
"Please check that all MIDAS programs are stopped and try again.\n");
543 printf(
"Deleting old ODB semaphore...\n");
548 printf(
"Deleting old ODB semaphore... create status %d, delete status %d\n", cstatus, dstatus);
552 if (odb_path.length() > 0 && cleanup) {
553 time_t now = time(NULL);
558 printf(
"Preserving old ODB save file \"%s\" to \"%s\"\n", odb_path.c_str(), path1.c_str());
560 status = rename(odb_path.c_str(), path1.c_str());
562 printf(
"rename(%s, %s) returned %d, errno %d (%s)\n", odb_path.c_str(), path1.c_str(),
status, errno, strerror(errno));
572 printf(
"Checking ODB size...\n");
576 printf(
"Requested ODB size is %d bytes (%.2f%s)\n",
odb_size,odb_size_human.first,odb_size_human.second.c_str());
581 path1 +=
".ODB_SIZE.TXT";
583 printf(
"ODB size file is \"%s\"\n", path1.c_str());
585 FILE *
fp = fopen(path1.c_str(),
"r");
587 printf(
"ODB size file \"%s\" does not exist, creating it...\n", path1.c_str());
588 fp = fopen(path1.c_str(),
"w");
590 printf(
"Cannot create ODB size file \"%s\", fopen() errno %d (%s)\n", path1.c_str(), errno, strerror(errno));
597 fprintf(
fp,
"%d (%.2f%s)\n",
DEFAULT_ODB_SIZE,default_odb_size_human.first,default_odb_size_human.second.c_str());
601 fprintf(
fp,
"%d (%.2f%s)\n",
odb_size,odb_size_human.first,odb_size_human.second.c_str());
605 fp = fopen(path1.c_str(),
"r");
607 printf(
"Creation of ODB size file \"%s\" somehow failed.\n", path1.c_str());
613 int file_odb_size = 0;
614 std::pair<double,std::string> file_odb_size_human;
617 char *s = fgets(buf,
sizeof(buf),
fp);
620 file_odb_size_human=
HumanUnits(file_odb_size);
625 printf(
"Saved ODB size from \"%s\" is %d bytes (%.2f%s)\n", path1.c_str(), file_odb_size,file_odb_size_human.first,file_odb_size_human.second.c_str());
631 printf(
"Requested ODB size %d is different from previous ODB size %d. You have 2 choices:\n",
odb_size, file_odb_size);
632 printf(
"1) to create ODB with old size, please try again without the \"-s\" switch.\n");
633 printf(
"2) to create ODB with new size, please delete the file \"%s\" and try again.\n", path1.c_str());
638 printf(
"We will initialize ODB for experiment \"%s\" on host \"%s\" with size %d bytes (%.2f%s)\n",
exp_name,
host_name,
odb_size, odb_size_human.first, odb_size_human.second.c_str());
644 printf(
"Creating ODB...\n");
646 printf(
"Creating ODB... db_open_database() status %d\n",
status);
648 printf(
"Something went wrong... continuing...\n");
650 printf(
"Saving ODB...\n");
652 printf(
"Saving ODB... db_close_database() status %d\n",
status);
654 printf(
"Something went wrong... continuing...\n");
658 printf(
"Connecting to experiment...\n");
663 printf(
"Error: cm_connect_experiment() status %d\n",
status);
664 printf(
"Sorry...\n");
669 printf(
"Connected to ODB for experiment \"%s\" on host \"%s\" with size %d bytes (%.2f%s)\n",
exp_name,
host_name,
odb_size,odb_size_human.first,odb_size_human.second.c_str());
679 printf(
"Checking experiment name... status %d, found \"%s\"\n",
status, buf);
684 printf(
"Disconnecting from experiment...\n");
691 printf(
"this version of odbinit is built with NO_LOCAL_ROUTINES and it will not work. odbinit only works locally!\n");
INT db_get_value(HNDLE hDB, HNDLE hKeyRoot, const char *key_name, void *data, INT *buf_size, DWORD type, BOOL create)