BmnRoot 26.10.0-233-gb9bbe585
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
installLocalDB.C
Go to the documentation of this file.
1#if !defined(__CLING__) || defined(__MAKECLING__)
2#include "TString.h"
3#include "TSystem.h"
4
5#include <cstdio>
6#include <iostream>
7#include <stdio.h>
8#include <stdlib.h>
9using namespace std;
10#endif
11// the address of the Geometry Web Service is the same as for GeoSetup
12#include "../geo_db_settings.h"
13
14bool exists_file(const char* name)
15{
16 return (access(name, F_OK) != -1);
17}
18
19unsigned long int getServerRevision(const char* url)
20{
21 // the revision is read from the output of wget, so no file is written to the current directory
22 TString revision = gSystem->GetFromPipe(TString::Format("wget -qO- \"%s/get_cur_timestamp.php\" 2>/dev/null", url));
23 return strtoul(revision.Data(), nullptr, 10);
24}
25
26unsigned long int getLocalRevision(TString& localDbPath)
27{
28 unsigned long int localRev = 0;
29 char type;
30 if (exists_file(localDbPath)) {
31 ostringstream stringStream;
32 stringStream << "sqlite://" << localDbPath;
33 TSQLiteServer db(stringStream.str().c_str());
34 stringStream.str("");
35 if (db.IsConnected()) {
36 stringStream << "select value_field from config where key_field='config_time';";
37 cout << stringStream.str().c_str() << std::endl;
38 TSQLResult* resQ = db.Query(stringStream.str().c_str());
39 if (resQ != 0) {
40 TSQLRow* row = resQ->Next();
41 if (row != NULL) {
42 const char* ch_time = row->GetField(0);
43 char* ptr;
44 if (ch_time != NULL)
45 localRev = strtoul(ch_time, &ptr, 10);
46 }
47 }
48 }
49 }
50 return localRev;
51}
52
53int installLocalDB(const char* tag = nullptr, const char* url = GEO_SERVER_HOST)
54{
55 // the storage of the local geometry database is given by GEOMPATH, the database file is in its parent directory
56 const char* geomPath = getenv("GEOMPATH");
57 if (geomPath == nullptr || geomPath[0] == '\0') {
58 cerr << "GEOMPATH is not set. Please, set GEOMPATH as a path to the storage of the local database" << endl;
59 return -1;
60 }
61 TString storagePath(geomPath);
62 gSystem->ExpandPathName(storagePath);
63 while (storagePath.Length() > 1 && storagePath.EndsWith("/"))
64 storagePath.Chop();
65 TString localDbPath = gSystem->GetDirName(storagePath) + "/local.db";
66 FileStat_t file_stat;
67
68 if (tag == nullptr) {
69 ostringstream stringStream;
70 unsigned long int localRev = 0, serverRev = 0;
71 char type;
72 if (exists_file(localDbPath)) {
73 localRev = getLocalRevision(localDbPath);
74 /* std::ostringstream stringStream;
75 stringStream<<"sqlite://"<<localDbPath;
76 TSQLiteServer db(stringStream.str().c_str());
77 stringStream.str("");
78 unsigned long int localRev = 0,serverRev=0;
79 if(db.IsConnected()){
80 stringStream<<"select value_field from config where key_field='config_time';";
81 cout<<stringStream.str().c_str()<<std::endl;
82 TSQLResult* resQ = db.Query(stringStream.str().c_str());
83 if (resQ != 0)
84 {
85 TSQLRow* row = resQ->Next();
86 if (row != NULL){
87 const char* ch_time = row->GetField(0);
88 char *ptr;
89 if(ch_time!=NULL)
90 localRev = strtoul(ch_time, &ptr, 10);
91 }
92 }
93 }*/
94 stringStream.str("");
95 stringStream << " Local revision: ";
96 if (localRev == 0)
97 stringStream << "Undifined.";
98 else
99 stringStream << localRev;
100 cout << "\033[31m" << "The local database with path " << localDbPath << " already exists."
101 << stringStream.str().c_str() << "\033[0m" << endl;
102 // if(exists_file("server_revision.txt"))
103 // std::remove("server_revision.txt");
104 /* gSystem->Exec(TString::Format("wget -O server_revision.txt \"%s/get_cur_timestamp.php\" > /dev/null
105 2>&1", url)); std::ifstream myFile_Handler; std::string myLine;
106 myFile_Handler.open("server_revision.txt"); if(myFile_Handler.is_open())
107 {
108 char *ptr;
109 while(getline(myFile_Handler, myLine))
110 {
111 serverRev=strtoul(myLine.c_str(), &ptr, 10);
112 break;
113 }
114 myFile_Handler.close();
115 }*/
116 serverRev = getServerRevision(url);
117 if (serverRev == 0) {
118 cerr << "The server revision is not detected. Please contact with administrator for check the problem.";
119 return -2;
120 }
121 if (serverRev == localRev) {
122 cout << "\033[32m" << "The current local version of geometry database is last. Instalation is stopped."
123 << "\033[0m" << std::endl;
124 return 0;
125 }
126
127 cout << "\033[31m" << "The server revision: " << serverRev << "\033[0m" << endl;
128 // the existing database is replaced only after the explicit answer, so the installation is stopped if
129 // there is no answer, e.g. the input is closed in a batch job
130 type = 'n';
131 do {
132 cout << "Do you want to update it [y/n]?" << endl;
133 if (!(cin >> type)) {
134 cout << "No answer is given" << endl;
135 type = 'n';
136 break;
137 }
138 type = tolower(type);
139 } while (type != 'y' && type != 'n');
140 if (type != 'y') {
141 cout << "Break install because local database already exist" << endl;
142 return -1;
143 }
144 }
145 }
146
147 // extract directory from the path
148 TString dirName(gSystem->DirName(localDbPath.Data()));
149 // create the directory if not exists
150 if (gSystem->AccessPathName(dirName.Data(), kFileExists)) {
151 if (gSystem->mkdir(dirName, true) != 0) {
152 cerr << "ERROR: cannot create folder: " << dirName << endl;
153 return -3;
154 }
155 }
156
157 // get archive with the database file
158 // --tries: retries are done by the loop below, otherwise are multiplied by wget internal ones (20 by def.)
159 // --read-timeout is not set (900 seconds by default)
160 // --connect-timeout=10: do not hang forever on a stalled connection
161 // the archive is downloaded to the directory of the database, where it is unpacked, not to the current one
162 TString tarName = dirName + "/localdb.tar";
164 "wget --tries=1 --dns-timeout=10 --connect-timeout=10 -O \"%s\" \"%s/download_file.php", tarName.Data(), url);
165 if (tag == nullptr)
166 st += "?type=all&tag=all\"";
167 else
168 st += TString::Format("?type=s&tag=%s\"", tag);
169 st += "> /dev/null 2>&1";
170 // cerr<<st<<endl;
171
172 // try to download the archive up to 3 times, the connection may be slow or unstable
173 const int maxAttempts = 3;
174 bool downloaded = false;
175 TString failReason; // the reason of the last failed attempt, reported once after all attempts
176 for (int attempt = 1; attempt <= maxAttempts; ++attempt) {
177 // report only the retries, the first attempt is silent
178 if (attempt > 1)
179 cout << "Retrying to download the geometry database (attempt " << attempt << " of " << maxAttempts << ")..."
180 << endl;
181 gSystem->Unlink(tarName);
182 // Exec returns the raw status of system(), the wget exit code is its high byte:
183 // 4 - network failure, 8 - error response from the server (e.g. 404), see 'man wget'
184 int wgetStatus = gSystem->Exec(st);
185
186 if (wgetStatus != 0)
187 failReason = TString::Format("wget could not download the file (exit code %d)", wgetStatus / 256);
188 else if (gSystem->AccessPathName(tarName, kFileExists))
189 failReason = "the geometry database file was not downloaded";
190 else {
191 gSystem->GetPathInfo(tarName, file_stat);
192 if (file_stat.fSize == 0)
193 failReason = "the downloaded geometry database file has 0 size";
194 // the server may answer with an HTML error/redirect page (HTTP 200) instead of the archive, or the
195 // download may be truncated: such a file is not empty but is not a valid tar, so check the exit
196 // code of tar listing its content (a truncated archive prints its first entries and then fails)
197 else if (gSystem->Exec(TString::Format("tar -tf \"%s\" > /dev/null 2>&1", tarName.Data())) != 0)
198 failReason = "the downloaded file is not a valid tar archive (probably an HTML error page or an "
199 "incomplete download)";
200 else {
201 downloaded = true;
202 break;
203 }
204 }
205
206 if (attempt < maxAttempts)
207 gSystem->Sleep(5000); // wait 5 seconds before the next attempt
208 }
209
210 if (!downloaded) {
211 cerr << "ERROR: downloading the geometry database failed after " << maxAttempts << " attempts: " << failReason
212 << ", please, check url: " << url << endl;
213 return -4;
214 }
215
216 // untar archive with the database file
217 gSystem->Exec(TString::Format("tar -xvf \"%s\" -C \"%s/\" > /dev/null 2>&1", tarName.Data(), dirName.Data()));
218 if (gSystem->AccessPathName(gSystem->ConcatFileName(dirName.Data(), "local.db"), kFileExists)) {
219 cerr << "ERROR: unarchiving the geometry database failed, please, check url: " << url << endl;
220 return -6;
221 }
222
223 // remove temporary archive
224 gSystem->Unlink(tarName);
225
226 cout << "The Local Geometry Database was successfully installed to the directory: " << dirName << endl;
227 return 0;
228}
kFileExists
R__EXTERN TSystem * gSystem
virtual TSQLRow * Next()=0
virtual const char * GetField(Int_t field)=0
virtual Bool_t IsConnected() const
TSQLResult * Query(const char *sql) final
Ssiz_t Length() const
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
const char * Data() const
TString & Chop()
static TString Format(const char *fmt,...)
const char *const GEO_SERVER_HOST
unsigned long int getServerRevision(const char *url)
int installLocalDB(const char *tag=nullptr, const char *url=GEO_SERVER_HOST)
unsigned long int getLocalRevision(TString &localDbPath)
bool exists_file(const char *name)
Long64_t fSize