26static std::vector<std::string>
split(
const char*
sep,
const std::string& s)
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));
44static std::string
join(
const char*
sep,
const std::vector<std::string>& v)
48 for (
unsigned i=0;
i<v.size();
i++) {
58static std::vector<std::string>
remove_dot_dot(
const std::vector<std::string>& v)
60 std::vector<std::string>
out;
61 for (
unsigned i=0;
i<v.size();
i++) {
77 printf(
"usage: odbinit [options...]\n");
79 printf(
" [-e Experiment] --- specify experiment name\n");
80 printf(
" [-s size] --- specify new size of ODB in bytes, default is %d (optional units: B, kB, MB, kiB, MiB)\n",
DEFAULT_ODB_SIZE);
81 printf(
" [--env] --- create new env.sh and env.csh files in the current directory\n");
82 printf(
" [--exptab] --- create new exptab file in the current directory\n");
83 printf(
" [--cleanup] --- cleanup (preserve) old (existing) ODB files\n");
84 printf(
" [-n] --- dry run, report everything that will be done, but do not actually do anything\n");
100 const char units[] = {
'k',
'M',
'G',
'T',
'P',
'E',
'Z',
'Y'};
103 for (
size_t i=0;
i<
sizeof(s);
i++)
109 for (
size_t i=0;
i<
sizeof(s);
i++)
111 for (
int j=0;
j<8;
j++)
132 const char*
units[] = {
"B",
"kB",
"MB",
"GB",
"TB",
"PB",
"EB",
"ZB",
"YB"};
157 printf(
"Error: trying to use a remote connection to host \"%s\". odbinit must run locally. Sorry.\n",
host_name);
168 for (
int i = 1;
i <
argc;
i++) {
192#ifndef NO_LOCAL_ROUTINES
195 printf(
"Creating a new exptab file in the current directory...\n");
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);
213 printf(
"Error: env.variable PWD is not defined or is empty. Sorry...\n");
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");
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);
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");
255 printf(
"Error: env.sh already exists in the current directory. Sorry...\n");
262 printf(
"Error: env.csh already exists in the current directory. Sorry...\n");
270 printf(
"Error: env.variable PWD is not defined or is empty. Sorry...\n");
283 if (
argv0[0] !=
'/') {
285 printf(
"Please run odbinit using the full path, i.e. run $HOME/packages/midas/linux/bin/odbinit\n");
295 if (
argv0[0] ==
'/') {
321 if (
argv0[0] ==
'/') {
329 std::vector<std::string>
aa =
split(
"/", path);
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");
367 fprintf(
fpb,
"%s) unset MIDAS_SERVER_HOST ;;\n", hostname.c_str());
368 fprintf(
fpb,
"*) export MIDAS_SERVER_HOST=%s ;;\n", hostname.c_str());
371 fprintf(
fpc,
"# define mserver connection\n");
377 fprintf(
fpc,
"setenv MIDAS_SERVER_HOST %s\n", hostname.c_str());
387 printf(
"Please source env.sh or env.csh\n");
395 printf(
"Checking MIDASSYS...");
400 printf(
"Error: Env.variable MIDASSYS is not defined.\n");
403 printf(
"Please run odbinit --env\n");
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");
430 printf(
"Cannot get the name of the exptab file. Sorry...\n");
442 printf(
" <-- selected experiment");
450 printf(
"Specified experiment \"%s\" not found in exptab. Sorry...\n",
exp_name);
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());
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;
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");
536 printf(
"Please check that all MIDAS programs are stopped and try again.\n");
543 printf(
"Deleting old ODB semaphore...\n");
552 if (odb_path.length() > 0 &&
cleanup) {
558 printf(
"Preserving old ODB save file \"%s\" to \"%s\"\n", odb_path.c_str(),
path1.c_str());
572 printf(
"Checking ODB size...\n");
581 path1 +=
".ODB_SIZE.TXT";
583 printf(
"ODB size file is \"%s\"\n",
path1.c_str());
587 printf(
"ODB size file \"%s\" does not exist, creating it...\n",
path1.c_str());
607 printf(
"Creation of ODB size file \"%s\" somehow failed.\n",
path1.c_str());
617 char *s =
fgets(buf,
sizeof(buf),
fp);
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());
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);
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 cm_set_path(const char *path)
INT cm_get_experiment_database(HNDLE *hDB, HNDLE *hKeyClient)
INT cm_list_experiments_local(STRING_LIST *exp_names)
INT cm_connect_experiment1(const char *host_name, const char *default_exp_name, const char *client_name, void(*func)(char *), INT odb_size, DWORD watchdog_timeout)
INT cm_disconnect_experiment(void)
int cm_get_exptab(const char *expname, std::string *dir, std::string *user)
std::string cm_get_exptab_filename()
INT cm_get_environment(char *host_name, int host_name_size, char *exp_name, int exp_name_size)
INT cm_set_experiment_name(const char *name)
std::string ss_gethostname()
INT ss_suspend_init_odb_port()
INT ss_semaphore_create(const char *name, HNDLE *semaphore_handle)
INT ss_shm_delete(const char *name)
INT ss_semaphore_delete(HNDLE semaphore_handle, INT destroy_flag)
INT cm_msg_flush_buffer()
BOOL equal_ustring(const char *str1, const char *str2)
INT db_get_value(HNDLE hDB, HNDLE hKeyRoot, const char *key_name, void *data, INT *buf_size, DWORD type, BOOL create)
INT db_open_database(const char *xdatabase_name, INT database_size, HNDLE *hDB, const char *client_name)
INT db_close_database(HNDLE hDB)
char exp_name[NAME_LENGTH]
char host_name[HOST_NAME_LENGTH]
#define DEFAULT_WATCHDOG_TIMEOUT
std::vector< std::string > STRING_LIST
static std::string join(const char *sep, const std::vector< std::string > &v)
static std::string to_string(int v)
static std::vector< std::string > remove_dot_dot(const std::vector< std::string > &v)
int DecodeSize(const char *s)
std::pair< double, std::string > HumanUnits(int odb_size)
static std::vector< std::string > split(const char *sep, const std::string &s)
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)