1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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"
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>
30 #include "db_reference_file.h"
32 using namespace NLMISC
;
33 using namespace NLNET
;
38 * Load a Reference index file
40 bool CRefIndex::load(const string
& filename
)
45 if (!reffile
.open(filename
) || !ixml
.init(reffile
))
52 catch (const Exception
&)
61 * Load a Reference index file
63 bool CRefIndex::load()
65 string rootpath
= getRootPath();
69 return load(rootpath
+"ref");
73 * Save a Reference index file
75 bool CRefIndex::save(const string
& filename
)
80 if (!reffile
.open(filename
) || !oxml
.init(&reffile
))
87 catch (const Exception
&)
96 * Save a Reference index file
98 bool CRefIndex::save()
100 string path
= getPath();
104 return checkDirectory(path
) && save(path
+"ref");
108 * Set As Valid Reference
110 bool CRefIndex::setAsValidRef()
112 string rootpath
= getRootPath();
113 if (rootpath
.empty())
116 return save(rootpath
+"ref");
121 * Build next Reference index file
123 bool CRefIndex::buildNext()
132 if (!setupDirectory())
142 * Get next Reference index file
144 void CRefIndex::getNext()
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());
168 if (!CFile::setRWAccess(path
))
170 nlwarning("getRootPath(): failure, can't set RW access to path '%s', can't start.", path
.c_str());
180 * Get Nominal Root Path
182 std::string
CRefIndex::getNominalRootPath()
184 return RY_PDS::CPDSLib::getRootDirectory(DatabaseId
);
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())
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());
216 if (!checkDirectory(getHoursUpdatePath()))
218 nlwarning("CRefIndex::setupDirectory(): failed, can't check subdirectory '%s'", getHoursUpdatePath().c_str());
222 if (!checkDirectory(getMinutesUpdatePath()))
224 nlwarning("CRefIndex::setupDirectory(): failed, can't check subdirectory '%s'", getMinutesUpdatePath().c_str());
228 if (!checkDirectory(getSecondsUpdatePath()))
230 nlwarning("CRefIndex::setupDirectory(): failed, can't check subdirectory '%s'", getSecondsUpdatePath().c_str());
234 if (!checkDirectory(getLogPath()))
236 nlwarning("CRefIndex::setupDirectory(): failed, can't check subdirectory '%s'", getLogPath().c_str());
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());
256 if (!CFile::setRWAccess(path
))
258 nlwarning("CRefIndex::checkDirectory(): failed, can't set RW access to directory '%s'", path
.c_str());
262 else if (!CFile::isDirectory(path
))
264 nlwarning("CRefIndex::checkDirectory(): failed, directory '%s' is already a file", path
.c_str());
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/";
309 std::string
CRefIndex::getLogPath()
311 return getRootPath() + "logs/";
320 CDatabaseState::CDatabaseState()
323 LastUpdateId
= (0-1);
330 void CDatabaseState::serial(NLMISC::IStream
& s
)
332 s
.xmlPush("database_state");
334 s
.serialCheck(NELID("DBST"));
335 uint version
= s
.serialVersion(0);
345 s
.xmlPush("lastupdateid");
346 s
.serial(LastUpdateId
);
349 s
.xmlPush("currentindex");
350 s
.serial(CurrentIndex
);
353 s
.xmlPush("endtimestamp");
358 EndTimestamp
.fromString(ts
.c_str());
362 std::string ts
= EndTimestamp
.toString();
373 bool CDatabaseState::save(CRefIndex
& ref
)
378 string filename
= fileName(ref
);
380 if (!f
.open(filename
) || !oxml
.init(&f
))
387 catch (const Exception
&)
398 bool CDatabaseState::load(CRefIndex
& ref
, bool usePrevious
)
400 return load(ref
.getRootPath(), usePrevious
);
407 bool CDatabaseState::load(const std::string
& rootpath
, bool usePrevious
)
412 string filename
= CPath::standardizePath(rootpath
);
414 filename
+= "previous_state";
418 if (!f
.open(filename
) || !ixml
.init(f
))
425 catch (const Exception
&)
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;