2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
28 #include <creaImageIOSynchron.h>
29 #include <creaImageIOSystem.h>
30 #include <boost/filesystem.hpp>
31 #include <boost/algorithm/string.hpp>
34 //namespace fs = boost::filesystem;
36 //=====================================================================
42 //=====================================================================
43 #define QUERYSYNCDB(QUER,RES) \
46 RES = mDB->execQuery(QUER.c_str()); \
48 catch (CppSQLite3Exception& e) \
50 GimmickError("SQLite query '"<<QUER<<"' Error : " \
51 << e.errorCode() << ":" \
52 << e.errorMessage() ); \
54 //=====================================================================
55 #define UPDATESYNCDB(UP) \
58 mDB->execDML(UP.c_str()); \
60 catch (CppSQLite3Exception& e) \
62 GimmickError("SQLite update '"<<UP<<"' Error : " \
63 << e.errorCode() << ":" \
64 << e.errorMessage() ); \
66 //=====================================================================
68 Synchronizer::Synchronizer(const std::string& path)
70 pathDB = path + "maintenance_database.db3";
71 mDB = new CppSQLite3DB;
75 //=====================================================================
76 Synchronizer::~Synchronizer()
81 //=====================================================================
82 void Synchronizer::Initialize()
84 if (!boost::filesystem::exists(pathDB))
94 mDB->open(pathDB.c_str());
96 catch (CppSQLite3Exception& e)
98 GimmickError("Opening '"<<pathDB<<"' : "
99 << e.errorCode() << ":"
100 << e.errorMessage());
103 // get the ADD operations List
104 //UpdateAddList(pathDB);
107 //=====================================================================
108 void Synchronizer::CreateDB()
110 mDB->open(pathDB.c_str());
113 command = "CREATE TABLE ";
114 command += "ADD_OPS";
115 command += "\n(\nADD_KEY INTEGER PRIMARY KEY";
116 command += ",\nPATH text";
117 command += ",\nRECURSIVE boolean";
118 command += ",\nFILES_ADDED int";
119 command += ",\nREFERENCEDDB text";
121 UPDATESYNCDB(command);
123 command = "CREATE TABLE ";
124 command += "IGNORED_FILES";
125 command += "\n(\nID INTEGER PRIMARY KEY";
126 command += ",\nADD_KEY integer";
127 command += ",\nPATH text";
128 command += ",\nREMOVE boolean";
129 command += ",\nTIME datetext";
131 UPDATESYNCDB(command);
134 //=====================================================================
135 void Synchronizer::CleanName(std::string& str) const
140 pos = str.find('\\');
143 str.replace(pos, 1, "/");
149 //=====================================================================
150 void Synchronizer::GetFileList(std::vector<AddList> & list, const std::string& refdb)
156 //=====================================================================
157 void Synchronizer::GetIgnoredFiles(const std::string& key, std::vector<std::string> &ignoreList)
159 ignoreList=GetIgnoreList(key);
162 //=====================================================================
163 void Synchronizer::UpdateAddList(const std::string& refdb)
165 std::string query = "SELECT * FROM ADD_OPS WHERE REFERENCEDDB = '"+refdb+"';";
167 QUERYSYNCDB(query, res);
170 AddList temp = AddList(res);
171 mAddList.push_back(temp);
176 /////////////////////////////////////////////////////////////////////////////////////////////////
177 // remove an entry of the DB
178 //@param i_table : table where to do the remove
179 // @param i_key : the add_key reference (one entry to remove for ADD_OP table, many for IGNORED_FILES table
181 /////////////////////////////////////////////////////////////////////////////////////////////////
182 void Synchronizer::RemoveEntry(const std::string i_table, const std::string i_key)
184 std::string query = "DELETE FROM " + i_table + " WHERE ADD_KEY = '" + i_key +"'";
188 /////////////////////////////////////////////////////////////////////////////////////////////////
189 // remove several entries of the DB
190 // @param i_table : table where to do the remove
191 // @param i_attribute: attribute to match
192 // @param i_operand : operand to use
193 // @param i_val : the reference
195 /////////////////////////////////////////////////////////////////////////////////////////////////
196 void Synchronizer::RemoveEntries(const std::string i_table,
197 const std::string i_attribute,
198 const std::string i_operand,
199 const std::string i_val)
201 std::stringstream query;
202 query<<"DELETE FROM "<<i_table<<" WHERE "<<i_attribute<<" "<<i_operand<<" '"<<i_val<<"'";
203 UPDATESYNCDB(query.str());
206 /////////////////////////////////////////////////////////////////////////////////////////////////
207 // clean DataBase if an operation has no child anymore
208 // @param refdb: the database segement to clean
210 /////////////////////////////////////////////////////////////////////////////////////////////////
211 void Synchronizer::CleanList(const std::string& refdb)
214 UpdateAddList(refdb);
215 std::vector<AddList>::iterator it_add = mAddList.begin();
216 for(;it_add <mAddList.end(); ++it_add)
218 if(it_add->nbFiles == "0")
220 RemoveEntry("ADD_OPS", it_add->key);
221 RemoveEntry("IGNORED_FILES", it_add->key);
226 UpdateAddList(refdb);
229 /////////////////////////////////////////////////////////////////////////////////////////////////
230 // Inserts a new add operation in the database
231 // @param path: the path of the directory that was added
232 // @param recursive: shows if the action was called recursively or not
233 // @param nChildren: the number of files affected by the operation
234 // @param refdb: the referenced database
235 // @result : The operation has been added
236 /////////////////////////////////////////////////////////////////////////////////////////////////
237 void Synchronizer::InsertAddOp(const std::string& path, const std::string& recursive, const std::string& nChildren, const std::string& refdb)
240 std::string pat=path.c_str();
242 insert="INSERT INTO ADD_OPS (PATH,RECURSIVE,FILES_ADDED,REFERENCEDDB) VALUES('";
243 insert+=convert(pat)+"','";
244 insert+=recursive+"',";
245 insert+=nChildren+",'";
247 UPDATESYNCDB(insert);
250 /////////////////////////////////////////////////////////////////////////////////////////////////
251 // Inserts a new ignored file in the database
252 // @param add_key: the key of the add_op to which it corresponds
253 // @param path: the path of the directory that was added
254 // @param remove: shows if the file was removed or not
255 // @param time: the time in which the file was removed
256 // @result : The file has been inserted
257 /////////////////////////////////////////////////////////////////////////////////////////////////
259 void Synchronizer::InsertIgnoreFile(const std::string& addKey, const std::string& path, const std::string& remove, const std::string& time, const std::string& refdb )
261 std::string pat=path.c_str();
263 std::string id=GetAttribute("ID","IGNORED_FILES","PATH",pat,refdb);
264 if(id.compare("")==0)
267 insert="INSERT INTO IGNORED_FILES (ADD_KEY,PATH,REMOVE,TIME) VALUES('";
268 insert+=addKey+"','";
269 insert+=convert(pat)+"','";
272 UPDATESYNCDB(insert);
277 std::string ak=GetAttribute("ADD_KEY","IGNORED_FILES","ID",id,refdb);
278 //gets the parent database to check if the file has been added to the current database
279 std::string parentDB=GetAttribute("*","ADD_OPS","ADD_KEY",ak,refdb);
280 //If there is no such entry, add it
281 if(parentDB.compare("")==0)
284 insert="INSERT INTO IGNORED_FILES (ADD_KEY,PATH,REMOVE,TIME) VALUES('";
285 insert+=addKey+"','";
286 insert+=convert(pat)+"','";
289 UPDATESYNCDB(insert);
293 //Sets the new add key attribute for the file
294 SetAttribute("ADD_KEY","IGNORED_FILES",addKey,"ID", id,refdb);
295 //Sets the new remove attribute for the file
296 SetAttribute("REMOVE","IGNORED_FILES",remove,"ID", id,refdb);
297 //Sets the new time attribute for the file
298 SetAttribute("TIME","IGNORED_FILES",time,"ID", id,refdb);
303 /////////////////////////////////////////////////////////////////////////////////////////////////
304 // get the files name to ignore for a add operation synchronization
305 // @param : the add key
306 //@result : list (path) of ignore files
307 /////////////////////////////////////////////////////////////////////////////////////////////////
308 std::vector<std::string> Synchronizer::GetIgnoreList(const std::string &i_key)
311 std::vector<std::string> i_names;
312 std::string query = "SELECT * FROM IGNORED_FILES WHERE ADD_KEY = ";
315 QUERYSYNCDB(query, res);
318 RemoveList temp = RemoveList(res);
319 if(temp.remove.compare("0")==0)
321 mIgnoreList.push_back(temp);
325 std::vector<RemoveList>::iterator it;
327 for(it = mIgnoreList.begin();it != mIgnoreList.end(); ++it)
329 i_names.push_back((*it).path);
334 /////////////////////////////////////////////////////////////////////////////////////////////////
335 // Gets the required attribute in the required table
336 // @param attribute: the attribute to look for
337 // @param table: the table to look in
338 // @param searchParam: the search parameter
339 // @param searchValue: the search value
340 // @result : required attribute
341 /////////////////////////////////////////////////////////////////////////////////////////////////
342 std::string Synchronizer::GetAttribute(const std::string& attribute,
343 const std::string& table,
344 const std::string& searchParam,
345 const std::string& searchValue,
346 const std::string& refdb)
348 std::stringstream query;
350 std::string sVal=convert(searchValue.c_str());
352 query<<"SELECT "<<attribute<<" FROM "<<table<<" WHERE "<<searchParam<<" = '"<<sVal;
353 if(table.compare("ADD_OPS")==0)
355 query<<"' AND REFERENCEDDB = '"<<refdb<<"';";
362 QUERYSYNCDB(query.str(), res);
365 result=res.getStringField(0);
371 /////////////////////////////////////////////////////////////////////////////////////////////////
372 // Sets the attribute value in the required table and column
373 // @param attribute: the attribute to look for
374 // @param table: the table to look in
375 // @param value: the value to set
376 // @param searchParam: the search parameter
377 // @param searchValue: the search value
378 // @result : attribute value changed
379 /////////////////////////////////////////////////////////////////////////////////////////////////
380 void Synchronizer::SetAttribute(const std::string& attribute,
381 const std::string& table,
382 const std::string& value,
383 const std::string& searchParam,
384 const std::string& searchValue,
385 const std::string& refdb)
387 std::string val=value.c_str();
388 std::string sVal=convert(searchValue.c_str());
391 std::string sql = "UPDATE ";
401 if(table.compare("ADD_OPS")==0)
403 sql += "' AND REFERENCEDDB = '";
412 /////////////////////////////////////////////////////////////////////////////////////////////////
413 // get the files name to ignore for a add operation synchronization
414 // @param : the add key
415 //@result : list (path) of ignore files
416 /////////////////////////////////////////////////////////////////////////////////////////////////
417 void Synchronizer::GetList(const std::string i_db)
420 std::vector<std::string> i_names;
421 std::vector<std::string> keys;
423 std::string query ="SELECT ADD_KEY, REFERENCEDDB FROM ADD_OPS";
424 QUERYSYNCDB(query, res);
428 std::string key(res.getStringField(0));
429 std::string db(res.getStringField(1));
436 query = "SELECT PATH, REMOVE FROM IGNORED_FILES WHERE";
439 for (int i=0; i < keys.size(); i++)
441 query += " ADD_KEY = " + keys[i];
444 query = query.substr(0,query.size() - 4);
448 query += " ADD_KEY = -1";
450 QUERYSYNCDB(query, res);
453 std::string file(res.getStringField(0));
454 std::string ignore(res.getStringField(1));
455 mList[file] = ignore == "0"? true : false;
460 bool Synchronizer::isIndexed(const std::string filename)
463 std::string name(filename);
464 boost::algorithm::replace_all( name,"\\" , "/");
465 std::map <std::string, bool>::iterator it_list = mList.begin();
466 for(;it_list != mList.end(); it_list++)
468 if(it_list->first == name)
476 const std::string Synchronizer::convert(const std::string &i_word)
478 std::string temp = i_word;
479 boost::algorithm::replace_all(temp,"'","''");