LCOV - code coverage report
Current view: top level - progs - odbinit.cxx (source / functions) Coverage Total Hit
Test: coverage.info Lines: 36.4 % 415 151
Test Date: 2025-11-11 10:26:08 Functions: 37.5 % 8 3

            Line data    Source code
       1              : /********************************************************************\
       2              : 
       3              :   Name:         odbinit.cxx
       4              :   Created by:   Konstantin Olchanski & Stefan Ritt
       5              : 
       6              :   Contents:     Initialize the MIDAS online data base.
       7              : 
       8              :   $Id$
       9              : 
      10              : \********************************************************************/
      11              : 
      12              : #include <stdio.h>
      13              : #include <string>
      14              : 
      15              : #include "midas.h"
      16              : #include "msystem.h"
      17              : #include "mstrlcpy.h"
      18              : 
      19            0 : static std::string to_string(int v)
      20              : {
      21              :    char buf[1024];
      22            0 :    sprintf(buf, "%d", v);
      23            0 :    return buf;
      24              : }
      25              : 
      26            0 : static std::vector<std::string> split(const char* sep, const std::string& s)
      27              : {
      28            0 :    unsigned sep_len = strlen(sep);
      29            0 :    std::vector<std::string> v;
      30            0 :    std::string::size_type pos = 0;
      31              :    while (1) {
      32            0 :       std::string::size_type next = s.find(sep, pos);
      33              :       //printf("pos %d, next %d\n", (int)pos, (int)next);
      34            0 :       if (next == std::string::npos) {
      35            0 :          v.push_back(s.substr(pos));
      36            0 :          break;
      37              :       }
      38            0 :       v.push_back(s.substr(pos, next-pos));
      39            0 :       pos = next+sep_len;
      40            0 :    }
      41            0 :    return v;
      42            0 : }
      43              : 
      44            0 : static std::string join(const char* sep, const std::vector<std::string>& v)
      45              : {
      46            0 :    std::string s;
      47              : 
      48            0 :    for (unsigned i=0; i<v.size(); i++) {
      49            0 :       if (i>0) {
      50            0 :          s += sep;
      51              :       }
      52            0 :       s += v[i];
      53              :    }
      54              : 
      55            0 :    return s;
      56            0 : }
      57              : 
      58            0 : static std::vector<std::string> remove_dot_dot(const std::vector<std::string>& v)
      59              : {
      60            0 :    std::vector<std::string> out;
      61            0 :    for (unsigned i=0; i<v.size(); i++) {
      62            0 :       if (v[i] == "..") {
      63            0 :          if (out.size() > 0) {
      64            0 :             out.pop_back();
      65              :          }
      66              :       } else {
      67            0 :          out.push_back(v[i]);
      68              :       }
      69              :    }
      70            0 :    return out;
      71            0 : }
      72              : 
      73              : /*------------------------------------------------------------------*/
      74              : 
      75            0 : static void usage()
      76              : {
      77            0 :    printf("usage: odbinit [options...]\n");
      78            0 :    printf("options:\n");
      79            0 :    printf("  [-e Experiment] --- specify experiment name\n");
      80            0 :    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            0 :    printf("  [--env] --- create new env.sh and env.csh files in the current directory\n");
      82            0 :    printf("  [--exptab] --- create new exptab file in the current directory\n");
      83            0 :    printf("  [--cleanup] --- cleanup (preserve) old (existing) ODB files\n");
      84            0 :    printf("  [-n] --- dry run, report everything that will be done, but do not actually do anything\n");
      85              :    //printf("  [-g] --- debug\n");
      86            0 :    exit(1);
      87              : }
      88              : 
      89            1 : int DecodeSize(const char* s)
      90              : {
      91              :    // The disticntion between kB and kb is ignored... 
      92              :    // We assume the user means Bytes not bits
      93              : 
      94            1 :    int size=0;
      95            1 :    if (s) {
      96              :      //strtoul ignores any no numic characters (the UNITS)
      97            1 :      size = strtoul(s, NULL, 0);
      98              :    }
      99              : 
     100            1 :    const char units[] = {'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'};
     101            1 :    int one_k=1000;
     102              :    //Search of 'i' in string (for kiB, MiB, GiB etc)
     103            9 :    for (size_t i=0; i<sizeof(s); i++)
     104              :    {
     105            8 :       if (s[i]=='i')
     106            0 :          one_k=1024;
     107              :    }
     108              :    //Search of major unit
     109            9 :    for (size_t i=0; i<sizeof(s); i++)
     110              :    {
     111           72 :       for (int j=0; j<8; j++)
     112              :       {
     113           64 :          if (s[i]==units[j])
     114              :          {
     115              :             //Only the first unit is used... kMB is meaningless
     116            0 :             while (j>-1)
     117              :             {
     118            0 :                size*=one_k;
     119            0 :                j--;
     120              :             }
     121            0 :             return size;
     122              :          }
     123              :       }
     124              :    }
     125            1 :    return size;
     126              : }
     127              : 
     128            4 : std::pair<double,std::string> HumanUnits(int odb_size)
     129              : {
     130            4 :    int unit_index=0;
     131            4 :    double odb_human_size=(double)odb_size;
     132            4 :    const char* units[] = {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
     133           10 :    while (odb_human_size > 1000) {
     134            6 :      odb_human_size /= 1000;
     135            6 :      unit_index++;
     136              :    }
     137            8 :    return {odb_human_size,units[unit_index]};
     138              : }
     139              : 
     140              : /*------------------------------------------------------------------*/
     141              : 
     142            1 : int main(int argc, char *argv[])
     143              : {
     144              :    // set unbuffered output
     145            1 :    setbuf(stdout, NULL);
     146            1 :    setbuf(stderr, NULL);
     147              : 
     148              :    char host_name[HOST_NAME_LENGTH];
     149              :    char exp_name[NAME_LENGTH];
     150              : 
     151              :    /* get default from environment */
     152            1 :    cm_get_environment(host_name, sizeof(host_name), exp_name, sizeof(exp_name));
     153              : 
     154            1 :    printf("Checking environment... experiment name is \"%s\", remote hostname is \"%s\"\n", exp_name, host_name);
     155              : 
     156            1 :    if (strlen(host_name) > 0) {
     157            0 :       printf("Error: trying to use a remote connection to host \"%s\". odbinit must run locally. Sorry.\n", host_name);
     158            0 :       exit(1);
     159              :    }
     160              : 
     161            1 :    bool cleanup = false;
     162            1 :    bool dry_run = false;
     163            1 :    bool create_exptab = false;
     164            1 :    bool create_env = false;
     165            1 :    int odb_size = 0; // DEFAULT_ODB_SIZE;
     166              : 
     167              :    /* parse command line parameters */
     168            1 :    for (int i = 1; i < argc; i++) {
     169            0 :       if (strcmp(argv[i], "-g") == 0) {
     170              :          //debug = TRUE;
     171            0 :       } else if (strcmp(argv[i], "-n") == 0) {
     172            0 :          dry_run = true;
     173            0 :       } else if (strcmp(argv[i], "--exptab") == 0) {
     174            0 :          create_exptab = true;
     175            0 :       } else if (strcmp(argv[i], "--env") == 0) {
     176            0 :          create_env = true;
     177            0 :       } else if (strcmp(argv[i], "--cleanup") == 0) {
     178            0 :          cleanup = true;
     179            0 :       } else if (strcmp(argv[i], "-e") == 0) {
     180            0 :          i++;
     181            0 :          mstrlcpy(exp_name, argv[i], sizeof(exp_name));
     182            0 :       } else if (strcmp(argv[i], "-s") == 0) {
     183            0 :          i++;
     184            0 :          odb_size = DecodeSize(argv[i]);
     185              :       } else {
     186            0 :          usage(); // DOES NOT RETURN
     187              :       }
     188              :    }
     189              : 
     190            1 :    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);
     191              : 
     192              : #ifndef NO_LOCAL_ROUTINES
     193              : 
     194            1 :    if (create_exptab) {
     195            0 :       printf("Creating a new exptab file in the current directory...\n");
     196              : 
     197            0 :       FILE *fpr = fopen("exptab", "r");
     198            0 :       if (fpr) {
     199            0 :          fclose(fpr);
     200            0 :          printf("Error: exptab already exists in the current directory. Sorry...\n");
     201            0 :          exit(1);
     202              :       }
     203              : 
     204            0 :       if (strlen(exp_name) < 1) {
     205            0 :          mstrlcpy(exp_name, "Default", sizeof(exp_name));
     206              :       }
     207            0 :       if (strchr(exp_name, ' ')) {
     208            0 :          printf("Error: experiment name \"%s\" should not contain a space character. Sorry...\n", exp_name);
     209            0 :          exit(1);
     210              :       }
     211            0 :       const char* pwd = getenv("PWD");
     212            0 :       if (!pwd || strlen(pwd)<1) {
     213            0 :          printf("Error: env.variable PWD is not defined or is empty. Sorry...\n");
     214            0 :          exit(1);
     215              :       }
     216            0 :       if (strchr(pwd, ' ')) {
     217            0 :          printf("Error: env.variable PWD value \"%s\" should not contain a space character. Sorry...\n", pwd);
     218            0 :          exit(1);
     219              :       }
     220            0 :       const char* user = getenv("USER");
     221            0 :       if (!user || strlen(user)<1) {
     222            0 :          printf("Error: env.variable USER is not defined or is empty. Sorry...\n");
     223            0 :          exit(1);
     224              :       }
     225            0 :       if (strchr(user, ' ')) {
     226            0 :          printf("Error: env.variable USER value \"%s\" should not contain a space character. Sorry...\n", user);
     227            0 :          exit(1);
     228              :       }
     229            0 :       printf("Experiment name [%s], experiment directory [%s], username [%s]\n", exp_name, pwd, user);
     230              : 
     231            0 :       FILE* fp = fopen("exptab", "w");
     232            0 :       if (!fp) {
     233            0 :          printf("Error: Cannot write exptab file, fopen() errno %d (%s). Sorry...\n", errno, strerror(errno));
     234            0 :          exit(1);
     235              :       }
     236              : 
     237            0 :       fprintf(fp, "%s %s %s\n", exp_name, pwd, user);
     238            0 :       fclose(fp);
     239              : 
     240            0 :       printf("\n");
     241            0 :       printf("Please define env.variable MIDAS_EXPTAB=%s/%s\n", pwd, "exptab");
     242            0 :       printf("\n");
     243            0 :       printf("Done\n");
     244            0 :       exit(0);
     245              :    }
     246              : 
     247            1 :    if (create_env) {
     248            0 :       printf("Creating a new environment settings file in the current directory...\n");
     249              : 
     250              :       // check if files already exist
     251              : 
     252            0 :       FILE *fpr = fopen("env.sh", "r");
     253            0 :       if (fpr) {
     254            0 :          fclose(fpr);
     255            0 :          printf("Error: env.sh already exists in the current directory. Sorry...\n");
     256            0 :          exit(1);
     257              :       }
     258              : 
     259            0 :       fpr = fopen("env.csh", "r");
     260            0 :       if (fpr) {
     261            0 :          fclose(fpr);
     262            0 :          printf("Error: env.csh already exists in the current directory. Sorry...\n");
     263            0 :          exit(1);
     264              :       }
     265              : 
     266              :       // construct MIDAS_EXPTAB
     267              : 
     268            0 :       const char* pwd = getenv("PWD");
     269            0 :       if (!pwd || strlen(pwd)<1) {
     270            0 :          printf("Error: env.variable PWD is not defined or is empty. Sorry...\n");
     271            0 :          exit(1);
     272              :       }
     273              : 
     274            0 :       std::string midas_exptab;
     275            0 :       midas_exptab += pwd;
     276            0 :       midas_exptab += DIR_SEPARATOR;
     277            0 :       midas_exptab += "exptab";
     278              : 
     279              :       // try to extract midas location from argv[0]
     280              : 
     281            0 :       std::string argv0 = argv[0];
     282              : 
     283            0 :       if (argv0[0] != '/') {
     284            0 :          printf("\n");
     285            0 :          printf("Please run odbinit using the full path, i.e. run $HOME/packages/midas/linux/bin/odbinit\n");
     286            0 :          printf("\n");
     287            0 :          printf("Bye\n");
     288            0 :          exit(1);
     289              :       }
     290              : 
     291              :       // construct MIDASSYS
     292              : 
     293            0 :       std::string midassys;
     294              : 
     295            0 :       if (argv0[0] == '/') { // absolute path
     296            0 :          midassys += argv0;
     297              :       } else {
     298            0 :          midassys += pwd;
     299            0 :          midassys += "/";
     300            0 :          midassys += argv0;
     301              :       }
     302              : 
     303            0 :       std::vector<std::string> aa = split("/", midassys);
     304            0 :       if (aa.size() > 0) { // remove "odbinit"
     305            0 :          aa.pop_back();
     306              :       }
     307            0 :       if (aa.size() > 0) { // remove "bin"
     308            0 :          aa.pop_back();
     309              :       }
     310            0 :       if (aa.size() > 0) { // remove "darwin" or "linux"
     311            0 :          aa.pop_back();
     312              :       }
     313              : 
     314            0 :       midassys = join("/", remove_dot_dot(aa));
     315              : 
     316              :       // construct MIDAS path
     317              : 
     318            0 :       std::string path;
     319              : 
     320              :       {
     321            0 :          if (argv0[0] == '/') { // absolute path
     322            0 :             path += argv0;
     323              :          } else {
     324            0 :             path += pwd;
     325            0 :             path += "/";
     326            0 :             path += argv0;
     327              :          }
     328              : 
     329            0 :          std::vector<std::string> aa = split("/", path);
     330            0 :          if (aa.size() > 0) { // remove "odbinit"
     331            0 :             aa.pop_back();
     332              :          }
     333              : 
     334            0 :          path = join("/", remove_dot_dot(aa));
     335            0 :       }
     336              : 
     337            0 :       std::string hostname = ss_gethostname();
     338              : 
     339              :       // start writing
     340              : 
     341            0 :       FILE *fpb = fopen("env.sh", "w");
     342            0 :       if (!fpb) {
     343            0 :          printf("Error: Cannot write env.sh file, fopen() errno %d (%s). Sorry...\n", errno, strerror(errno));
     344            0 :          exit(1);
     345              :       }
     346              : 
     347            0 :       FILE *fpc = fopen("env.csh", "w");
     348            0 :       if (!fpc) {
     349            0 :          printf("Error: Cannot write env.csh file, fopen() errno %d (%s). Sorry...\n", errno, strerror(errno));
     350            0 :          exit(1);
     351              :       }
     352              : 
     353            0 :       fprintf(fpb, "#!echo You must source\n");
     354            0 :       fprintf(fpc, "#!echo You must source\n");
     355              : 
     356            0 :       fprintf(fpb, "export MIDASSYS=\"%s\"\n", midassys.c_str());
     357            0 :       fprintf(fpc, "setenv MIDASSYS \"%s\"\n", midassys.c_str());
     358              : 
     359            0 :       fprintf(fpb, "export MIDAS_EXPTAB=\"%s\"\n", midas_exptab.c_str());
     360            0 :       fprintf(fpc, "setenv MIDAS_EXPTAB \"%s\"\n", midas_exptab.c_str());
     361              : 
     362            0 :       fprintf(fpb, "export PATH=$PATH:\"%s\"\n", path.c_str());
     363            0 :       fprintf(fpc, "setenv PATH $PATH\\:\"%s\"\n", path.c_str());
     364              : 
     365            0 :       fprintf(fpb, "# define mserver connection\n");
     366            0 :       fprintf(fpb, "case `hostname` in\n");
     367            0 :       fprintf(fpb, "%s) unset MIDAS_SERVER_HOST ;;\n", hostname.c_str());
     368            0 :       fprintf(fpb, "*) export MIDAS_SERVER_HOST=%s ;;\n", hostname.c_str());
     369            0 :       fprintf(fpb, "esac\n");
     370              : 
     371            0 :       fprintf(fpc, "# define mserver connection\n");
     372            0 :       fprintf(fpc, "switch (`hostname`)\n");
     373            0 :       fprintf(fpc, "case %s:\n", hostname.c_str());
     374            0 :       fprintf(fpc, "unsetenv MIDAS_SERVER_HOST\n");
     375            0 :       fprintf(fpc, "breaksw\n");
     376            0 :       fprintf(fpc, "default:\n");
     377            0 :       fprintf(fpc, "setenv MIDAS_SERVER_HOST %s\n", hostname.c_str());
     378            0 :       fprintf(fpc, "endsw\n");
     379              : 
     380            0 :       fprintf(fpb, "#end\n");
     381            0 :       fprintf(fpc, "#end\n");
     382              : 
     383            0 :       fclose(fpb);
     384            0 :       fclose(fpc);
     385              : 
     386            0 :       printf("\n");
     387            0 :       printf("Please source env.sh or env.csh\n");
     388            0 :       printf("\n");
     389            0 :       printf("Done\n");
     390            0 :       exit(0);
     391            0 :    }
     392              : 
     393              :    // check MIDASSYS
     394              : 
     395            1 :    printf("Checking MIDASSYS...");
     396            1 :    const char* midassys = getenv("MIDASSYS");
     397              : 
     398            1 :    if (!midassys) {
     399            0 :       printf("\n");
     400            0 :       printf("Error: Env.variable MIDASSYS is not defined.\n");
     401            0 :       printf("odbinit path [%s]\n", argv[0]);
     402            0 :       printf("\n");
     403            0 :       printf("Please run odbinit --env\n");
     404            0 :       printf("\n");
     405            0 :       printf("Bye.\n");
     406            0 :       exit(1);
     407              :    }
     408              : 
     409            1 :    printf("...%s\n", midassys);
     410              : 
     411            1 :    STRING_LIST exp_names;
     412              :    INT status;
     413              : 
     414            1 :    status = cm_list_experiments_local(&exp_names);
     415              : 
     416            1 :    if (status != CM_SUCCESS) {
     417            0 :       printf("Error: cm_list_experiments() status %d\n", status);
     418            0 :       printf("\n");
     419            0 :       printf("Cannot get the list of experiments, maybe the exptab file is missing.\n");
     420            0 :       printf("\n");
     421            0 :       printf("To create a new exptab file in the current directory, run odbinit --exptab -e new_experiment_name\n");
     422            0 :       printf("\n");
     423            0 :       printf("Bye...\n");
     424            0 :       exit(1);
     425              :    }
     426              : 
     427            1 :    std::string exptab_filename = cm_get_exptab_filename();
     428              : 
     429            1 :    if (exptab_filename.empty()) {
     430            0 :       printf("Cannot get the name of the exptab file. Sorry...\n");
     431            0 :       exit(1);
     432              :    }
     433              : 
     434            1 :    printf("Checking exptab... experiments defined in exptab file \"%s\":\n", exptab_filename.c_str());
     435              : 
     436            1 :    bool found_exp = false;
     437            2 :    for (unsigned i=0; i<exp_names.size(); i++) {
     438            1 :       printf("%d: \"%s\"", i, exp_names[i].c_str());
     439            1 :       if (exp_name[0] == 0)
     440            1 :          mstrlcpy(exp_name, exp_names[i].c_str(), sizeof (exp_name));
     441            1 :       if (equal_ustring(exp_names[i].c_str(), exp_name)) {
     442            1 :          printf(" <-- selected experiment");
     443            1 :          mstrlcpy(exp_name, exp_names[i].c_str(), sizeof (exp_name));
     444            1 :          found_exp = true;
     445              :       }
     446            1 :       printf("\n");
     447              :    }
     448              : 
     449            1 :    if (!found_exp) {
     450            0 :       printf("Specified experiment \"%s\" not found in exptab. Sorry...\n", exp_name);
     451            0 :       exit(1);
     452              :    }
     453              : 
     454            1 :    std::string exp_dir;
     455            1 :    std::string exp_user;
     456              : 
     457            1 :    status = cm_get_exptab(exp_name, &exp_dir, &exp_user);
     458              : 
     459            1 :    if (status != CM_SUCCESS) {
     460            0 :       printf("Specified experiment \"%s\" not found in exptab, cm_get_exptab() returned %d. Sorry...\n", exp_name, status);
     461            0 :       exit(1);
     462              :    }
     463              : 
     464            1 :    printf("\n");
     465            1 :    printf("Checking exptab... selected experiment \"%s\", experiment directory \"%s\"\n", exp_name, exp_dir.c_str());
     466              : 
     467            1 :    cm_set_experiment_name(exp_name);
     468            1 :    cm_set_path(exp_dir.c_str());
     469            1 :    ss_suspend_init_odb_port();
     470              : 
     471            1 :    printf("\n");
     472              : 
     473              :    {
     474            1 :       printf("Checking experiment directory \"%s\"\n", exp_dir.c_str());
     475              : 
     476              : #ifdef S_ISDIR
     477              :       struct stat stat_buf;
     478            1 :       int v = stat(exp_dir.c_str(), &stat_buf);
     479              : 
     480            1 :       if (v != 0) {
     481            0 :          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));
     482            0 :          printf("Sorry.\n");
     483            0 :          exit(1);
     484              :       }
     485              : 
     486            1 :       if (!S_ISDIR(stat_buf.st_mode)) {
     487            0 :          printf("Invalid experiment directory \"%s\" is not a directory\n", exp_dir.c_str());
     488            0 :          printf("Sorry.\n");
     489            0 :          exit(1);
     490              :       }
     491              : #else
     492              : #error No support for stat() and S_ISDIR on this system!
     493              : #endif
     494              :    }
     495              :    
     496            1 :    std::string odb_path;
     497              : 
     498              :    {
     499            1 :       std::string path;
     500            1 :       path += exp_dir;
     501            1 :       path += ".ODB.SHM";
     502              : 
     503            1 :       FILE *fp = fopen(path.c_str(), "r");
     504            1 :       if (fp) {
     505            0 :          fclose(fp);
     506            0 :          printf("Found existing ODB save file: \"%s\"\n", path.c_str());
     507            0 :          if (cleanup) {
     508              :             // cannot get rid of ODB save file yet - it is used as SysV semaphore key
     509              :             // so delete semaphore first, delete .ODB.SHM next.
     510              :             // hope deleting the semaphore and crashing all MIDAS programs does not corrupt the ODB save file.
     511            0 :             odb_path = path;
     512              :          } else {
     513            0 :             printf("Looks like this experiment ODB is already initialized.\n");
     514            0 :             printf("To create new empty ODB, please rerun odbinit with the \"--cleanup\" option.\n");
     515            0 :             exit(1);
     516              :          }
     517              :       } else {
     518            1 :          printf("Good: no ODB save file\n");
     519              :       }
     520            1 :    }
     521              : 
     522            1 :    printf("\n");
     523            1 :    printf("Checking shared memory...\n");
     524              : 
     525              :    {
     526            1 :       printf("Deleting old ODB shared memory...\n");
     527            1 :       if (!dry_run) {
     528            1 :          status = ss_shm_delete("ODB");
     529            1 :          if (status == SS_NO_MEMORY) {
     530            1 :             printf("Good: no ODB shared memory\n");
     531            0 :          } else if (status == SS_SUCCESS) {
     532            0 :             printf("Deleted existing ODB shared memory, please check that all MIDAS programs are stopped and try again.\n");
     533            0 :             exit(1);
     534              :          } else {
     535            0 :             printf("ss_shm_delete(ODB) status %d\n", status);
     536            0 :             printf("Please check that all MIDAS programs are stopped and try again.\n");
     537            0 :             exit(1);
     538              :          }
     539              :       }
     540              :    }
     541              :    
     542              :    {
     543            1 :       printf("Deleting old ODB semaphore...\n");
     544            1 :       if (!dry_run) {
     545              :          HNDLE sem;
     546            1 :          int cstatus = ss_semaphore_create("ODB", &sem);
     547            1 :          int dstatus = ss_semaphore_delete(sem, TRUE);
     548            1 :          printf("Deleting old ODB semaphore... create status %d, delete status %d\n", cstatus, dstatus);
     549              :       }
     550              :    }
     551              :    
     552            1 :    if (odb_path.length() > 0 && cleanup) {
     553            0 :       time_t now = time(NULL);
     554            0 :       std::string path1;
     555            0 :       path1 += odb_path;
     556            0 :       path1 += ".";
     557            0 :       path1 += to_string(now);
     558            0 :       printf("Preserving old ODB save file \"%s\" to \"%s\"\n", odb_path.c_str(), path1.c_str());
     559            0 :       if (!dry_run) {
     560            0 :          status = rename(odb_path.c_str(), path1.c_str());
     561            0 :          if (status != 0) {
     562            0 :             printf("rename(%s, %s) returned %d, errno %d (%s)\n", odb_path.c_str(), path1.c_str(), status, errno, strerror(errno));
     563            0 :             printf("Sorry.\n");
     564            0 :             exit(1);
     565              :          }
     566              :       }
     567            0 :    }
     568              : 
     569            1 :    printf("\n");
     570              : 
     571              :    {
     572            1 :       printf("Checking ODB size...\n");
     573              : 
     574            1 :       std::pair<double,std::string> odb_size_human=HumanUnits(odb_size);
     575              : 
     576            1 :       printf("Requested ODB size is %d bytes (%.2f%s)\n", odb_size,odb_size_human.first,odb_size_human.second.c_str());
     577              : 
     578            1 :       std::string path1;
     579            1 :       path1 += exp_dir;
     580            1 :       path1 += "/";
     581            1 :       path1 += ".ODB_SIZE.TXT";
     582              : 
     583            1 :       printf("ODB size file is \"%s\"\n", path1.c_str());
     584              : 
     585            1 :       FILE *fp = fopen(path1.c_str(), "r");
     586            1 :       if (!fp) {
     587            1 :          printf("ODB size file \"%s\" does not exist, creating it...\n", path1.c_str());
     588            1 :          fp = fopen(path1.c_str(), "w");
     589            1 :          if (!fp) {
     590            0 :             printf("Cannot create ODB size file \"%s\", fopen() errno %d (%s)\n", path1.c_str(), errno, strerror(errno));
     591            0 :             printf("Sorry.\n");
     592            0 :             exit(1);
     593              :          }
     594            1 :          if (odb_size == 0)
     595              :          {
     596            1 :             std::pair<double,std::string> default_odb_size_human=HumanUnits(DEFAULT_ODB_SIZE);
     597            1 :             fprintf(fp, "%d (%.2f%s)\n", DEFAULT_ODB_SIZE,default_odb_size_human.first,default_odb_size_human.second.c_str());
     598            1 :          }
     599              :          else
     600              :          {
     601            0 :             fprintf(fp, "%d (%.2f%s)\n", odb_size,odb_size_human.first,odb_size_human.second.c_str());
     602              :          }
     603            1 :          fclose(fp);
     604              : 
     605            1 :          fp = fopen(path1.c_str(), "r");
     606            1 :          if (!fp) {
     607            0 :             printf("Creation of ODB size file \"%s\" somehow failed.\n", path1.c_str());
     608            0 :             printf("Sorry.\n");
     609            0 :             exit(1);
     610              :          }
     611              :       }
     612              : 
     613            1 :       int file_odb_size = 0;
     614            1 :       std::pair<double,std::string> file_odb_size_human;
     615              :       {
     616              :          char buf[256];
     617            1 :          char *s = fgets(buf, sizeof(buf), fp);
     618            1 :          if (s) {
     619            1 :             file_odb_size = DecodeSize(s);
     620            1 :             file_odb_size_human=HumanUnits(file_odb_size);
     621              :          }
     622              :       }
     623            1 :       fclose(fp);
     624              : 
     625            1 :       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());
     626              : 
     627            1 :       if (odb_size == 0)
     628            1 :          odb_size = file_odb_size;
     629              : 
     630            1 :       if (file_odb_size != odb_size) {
     631            0 :          printf("Requested ODB size %d is different from previous ODB size %d. You have 2 choices:\n", odb_size, file_odb_size);
     632            0 :          printf("1) to create ODB with old size, please try again without the \"-s\" switch.\n");
     633            0 :          printf("2) to create ODB with new size, please delete the file \"%s\" and try again.\n", path1.c_str());
     634            0 :          exit(1);
     635              :       }
     636            1 :    }
     637            1 :    std::pair<double,std::string> odb_size_human=HumanUnits(odb_size);
     638            1 :    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());
     639            1 :    printf("\n");
     640              : 
     641              : 
     642              :    {
     643              :       HNDLE hDB;
     644            1 :       printf("Creating ODB...\n");
     645            1 :       status = db_open_database("ODB", odb_size, &hDB, "odbinit");
     646            1 :       printf("Creating ODB... db_open_database() status %d\n", status);
     647            1 :       if ((status != DB_SUCCESS) && (status != DB_CREATED)) {
     648            0 :          printf("Something went wrong... continuing...\n");
     649              :       }
     650            1 :       printf("Saving ODB...\n");
     651            1 :       status = db_close_database(hDB);
     652            1 :       printf("Saving ODB... db_close_database() status %d\n", status);
     653            1 :       if (status != DB_SUCCESS) {
     654            0 :          printf("Something went wrong... continuing...\n");
     655              :       }
     656              :    }
     657              : 
     658            1 :    printf("Connecting to experiment...\n");
     659              : 
     660            1 :    status = cm_connect_experiment1(host_name, exp_name, "ODBInit", NULL, 0, DEFAULT_WATCHDOG_TIMEOUT);
     661              : 
     662            1 :    if (status != CM_SUCCESS) {
     663            0 :       printf("Error: cm_connect_experiment() status %d\n", status);
     664            0 :       printf("Sorry...\n");
     665            0 :       exit(1);
     666              :    }
     667              : 
     668            1 :    printf("\n");
     669            1 :    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());
     670              : 
     671            1 :    cm_msg_flush_buffer();
     672              : 
     673              :    {
     674              :       HNDLE hDB;
     675            1 :       cm_get_experiment_database(&hDB, NULL);
     676            1 :       int size = NAME_LENGTH;
     677              :       char buf[NAME_LENGTH];
     678            1 :       status = db_get_value(hDB, 0, "/Experiment/Name", buf, &size, TID_STRING, FALSE);
     679            1 :       printf("Checking experiment name... status %d, found \"%s\"\n", status, buf);
     680              :       //status = db_save_json(hDB, 0, "odbinit.json");
     681              :       //printf("Saving odbinit.json... status %d\n", status);
     682              :    }
     683              : 
     684            1 :    printf("Disconnecting from experiment...\n");
     685            1 :    cm_disconnect_experiment();
     686              : 
     687            1 :    printf("\n");
     688            1 :    printf("Done\n");
     689              : 
     690              : #else
     691              :    printf("this version of odbinit is built with NO_LOCAL_ROUTINES and it will not work. odbinit only works locally!\n");
     692              : #endif // NO_LOCAL_ROUTINES
     693              : 
     694            1 :    return 0;
     695            1 : }
     696              : 
     697              : /* emacs
     698              :  * Local Variables:
     699              :  * tab-width: 8
     700              :  * c-basic-offset: 3
     701              :  * indent-tabs-mode: nil
     702              :  * End:
     703              :  */
        

Generated by: LCOV version 2.0-1