Add infos into target window
[ryzomcore.git] / ryzom / server / src / pd_lib / pd_server_utils.cpp
blob1ca303b0f0a1896b8b187c56c9e475642b2c7b99
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "pd_server_utils.h"
18 #include "pd_lib.h"
20 #include <nel/misc/path.h>
21 #include <nel/misc/debug.h>
22 #include <nel/net/service.h>
24 #include <nel/misc/i_xml.h>
25 #include <nel/misc/o_xml.h>
26 #include <nel/misc/file.h>
28 #include <time.h>
30 #include "db_reference_file.h"
32 using namespace NLMISC;
33 using namespace NLNET;
34 using namespace std;
38 * Load a Reference index file
40 bool CRefIndex::load(const string& filename)
42 CIFile reffile;
43 CIXml ixml;
45 if (!reffile.open(filename) || !ixml.init(reffile))
46 return false;
48 try
50 serial(ixml);
52 catch (const Exception&)
54 return false;
57 return true;
61 * Load a Reference index file
63 bool CRefIndex::load()
65 string rootpath = getRootPath();
66 if (rootpath.empty())
67 return false;
69 return load(rootpath+"ref");
73 * Save a Reference index file
75 bool CRefIndex::save(const string& filename)
77 COFile reffile;
78 COXml oxml;
80 if (!reffile.open(filename) || !oxml.init(&reffile))
81 return false;
83 try
85 serial(oxml);
87 catch (const Exception&)
89 return false;
92 return true;
96 * Save a Reference index file
98 bool CRefIndex::save()
100 string path = getPath();
101 if (path.empty())
102 return false;
104 return checkDirectory(path) && save(path+"ref");
108 * Set As Valid Reference
110 bool CRefIndex::setAsValidRef()
112 string rootpath = getRootPath();
113 if (rootpath.empty())
114 return false;
116 return save(rootpath+"ref");
121 * Build next Reference index file
123 bool CRefIndex::buildNext()
125 if (!load())
126 Index = 0;
127 else
128 ++Index;
130 setup();
132 if (!setupDirectory())
133 return false;
135 if (!save())
136 return false;
138 return true;
142 * Get next Reference index file
144 void CRefIndex::getNext()
146 ++Index;
148 setup();
154 * Get (and setup if needed) database root path
156 std::string CRefIndex::getRootPath()
158 string path = getNominalRootPath();
160 if (!CFile::isExists(path) || !CFile::isDirectory(path))
162 if (!CFile::createDirectoryTree(path))
164 nlwarning("getRootPath(): unable to create save path '%s'", path.c_str());
165 return string("");
168 if (!CFile::setRWAccess(path))
170 nlwarning("getRootPath(): failure, can't set RW access to path '%s', can't start.", path.c_str());
171 return string("");
175 return path;
180 * Get Nominal Root Path
182 std::string CRefIndex::getNominalRootPath()
184 return RY_PDS::CPDSLib::getRootDirectory(DatabaseId);
189 * Get reference path
191 std::string CRefIndex::getPath()
193 return NLMISC::CPath::standardizePath(getRootPath() + Path);
200 * Setup reference directory
202 bool CRefIndex::setupDirectory()
204 string rootpath = getRootPath();
205 if (rootpath.empty())
206 return false;
208 string path = NLMISC::CPath::standardizePath(rootpath + Path);
210 if (!checkDirectory(path))
212 nlwarning("CRefIndex::setupDirectory(): failed, can't check directory '%s'", path.c_str());
213 return false;
216 if (!checkDirectory(getHoursUpdatePath()))
218 nlwarning("CRefIndex::setupDirectory(): failed, can't check subdirectory '%s'", getHoursUpdatePath().c_str());
219 return false;
222 if (!checkDirectory(getMinutesUpdatePath()))
224 nlwarning("CRefIndex::setupDirectory(): failed, can't check subdirectory '%s'", getMinutesUpdatePath().c_str());
225 return false;
228 if (!checkDirectory(getSecondsUpdatePath()))
230 nlwarning("CRefIndex::setupDirectory(): failed, can't check subdirectory '%s'", getSecondsUpdatePath().c_str());
231 return false;
234 if (!checkDirectory(getLogPath()))
236 nlwarning("CRefIndex::setupDirectory(): failed, can't check subdirectory '%s'", getLogPath().c_str());
237 return false;
240 return true;
244 * Check directory
246 bool CRefIndex::checkDirectory(const std::string& path)
248 if (!CFile::isExists(path))
250 if (!CFile::createDirectoryTree(path))
252 nlwarning("CRefIndex::checkDirectory(): failed, can't create directory '%s'", path.c_str());
253 return false;
256 if (!CFile::setRWAccess(path))
258 nlwarning("CRefIndex::checkDirectory(): failed, can't set RW access to directory '%s'", path.c_str());
259 return false;
262 else if (!CFile::isDirectory(path))
264 nlwarning("CRefIndex::checkDirectory(): failed, directory '%s' is already a file", path.c_str());
265 return false;
268 return true;
273 * Set Time stamp
275 void CRefIndex::setTimestamp()
277 Timestamp.setToCurrent();
283 * Get Seconds update path
285 std::string CRefIndex::getSecondsUpdatePath()
287 return getRootPath() + "seconds/";
291 * Get Minutes update path
293 std::string CRefIndex::getMinutesUpdatePath()
295 return getRootPath() + "minutes/";
299 * Get Hours update path
301 std::string CRefIndex::getHoursUpdatePath()
303 return getRootPath() + "hours/";
307 * Get Log path
309 std::string CRefIndex::getLogPath()
311 return getRootPath() + "logs/";
318 * Constructor
320 CDatabaseState::CDatabaseState()
322 Id = 0xffffffff;
323 LastUpdateId = (0-1);
324 CurrentIndex = 0;
328 * Serial method
330 void CDatabaseState::serial(NLMISC::IStream& s)
332 s.xmlPush("database_state");
334 s.serialCheck(NELID("DBST"));
335 uint version = s.serialVersion(0);
337 s.xmlPush("name");
338 s.serial(Name);
339 s.xmlPop();
341 s.xmlPush("id");
342 s.serial(Id);
343 s.xmlPop();
345 s.xmlPush("lastupdateid");
346 s.serial(LastUpdateId);
347 s.xmlPop();
349 s.xmlPush("currentindex");
350 s.serial(CurrentIndex);
351 s.xmlPop();
353 s.xmlPush("endtimestamp");
354 if (s.isReading())
356 std::string ts;
357 s.serial(ts);
358 EndTimestamp.fromString(ts.c_str());
360 else
362 std::string ts = EndTimestamp.toString();
363 s.serial(ts);
365 s.xmlPop();
367 s.xmlPop();
371 * Save State
373 bool CDatabaseState::save(CRefIndex& ref)
375 COFile f;
376 COXml oxml;
378 string filename = fileName(ref);
380 if (!f.open(filename) || !oxml.init(&f))
381 return false;
385 serial(oxml);
387 catch (const Exception&)
389 return false;
392 return true;
396 * Load State
398 bool CDatabaseState::load(CRefIndex& ref, bool usePrevious)
400 return load(ref.getRootPath(), usePrevious);
405 * Load State
407 bool CDatabaseState::load(const std::string& rootpath, bool usePrevious)
409 CIFile f;
410 CIXml ixml;
412 string filename = CPath::standardizePath(rootpath);
413 if (usePrevious)
414 filename += "previous_state";
415 else
416 filename += "state";
418 if (!f.open(filename) || !ixml.init(f))
419 return false;
423 serial(ixml);
425 catch (const Exception&)
427 return false;
430 return true;
434 * State exists in path
436 bool CDatabaseState::exists(const std::string& rootpath)
438 string filename = CPath::standardizePath(rootpath) + "state";
439 return CFile::fileExists(filename);
443 uint64 CMixedStreamFile::_ReadBytes = 0;
444 uint64 CMixedStreamFile::_WrittenBytes = 0;