Go to the documentation of this file.
4 #ifndef tools_wroot_file
5 #define tools_wroot_file
14 #include "../platform"
35 file& get_me() {
return *
this;}
36 static int not_open() {
return -1;}
37 static uint32 kBegin() {
return 64;}
40 static const std::string s_v(
"tools::wroot::file");
46 virtual std::ostream&
out()
const {
return m_out;}
63 #if defined(__linux__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2)
64 if (::lseek64(
m_file, a_offset, whence) < 0) {
65 #elif defined(_MSC_VER)
66 if (::_lseeki64(
m_file, a_offset, whence) < 0) {
68 if (::lseek(
m_file, a_offset, whence) < 0) {
70 m_out <<
"tools::wroot::file::set_pos :"
71 <<
" cannot set position " << a_offset
84 m_out <<
"tools::wroot::file::set_END :"
85 <<
" free_seg list should not be empty here."
90 m_out <<
"tools::wroot::file::set_END :"
91 <<
" last free_seg is not the ending of file one."
92 <<
" free_seg list looks corrupted."
106 while ((siz = ::
write(
m_file,a_buffer,a_length)) < 0 &&
110 m_out <<
"tools::wroot::file::write_buffer :"
111 <<
" error writing to file " <<
sout(
m_path) <<
"."
115 if(siz!=(ssize_t)a_length) {
116 m_out <<
"tools::wroot::file::write_buffer :"
117 <<
"error writing all requested bytes to file " <<
sout(
m_path)
118 <<
", wrote " <<
long_out(siz) <<
" of " << a_length
128 static const uint32 ROOT_MAJOR_VERSION = 4;
129 static const uint32 ROOT_MINOR_VERSION = 0;
130 static const uint32 ROOT_PATCH_VERSION = 0;
132 10000 * ROOT_MAJOR_VERSION +
133 100 * ROOT_MINOR_VERSION +
141 m_out <<
"tools::wroot::file::synchronize :"
142 <<
" in _commit() for file " <<
sout(
m_path) <<
"."
146 #elif defined(__MINGW32__) || defined(__MINGW64__)
149 if (::fsync(
m_file) < 0) {
150 m_out <<
"tools::wroot::file::synchronize :"
151 <<
" error in fsync() for file " <<
sout(
m_path) <<
"."
160 std::map<char,compress_func>::const_iterator it =
m_zipers.find(a_key);
165 a_func = (*it).second;
178 if(cxlevel && (nbytes>256)) {
180 if(!
ziper(
'Z',func)) {
184 a_kbuf = (
char*)a_buffer.
buf();
185 a_klen = a_buffer.
length();
188 const uint32 kMAXBUF = 0xffffff;
190 uint32 nbuffers = nbytes/kMAXBUF;
191 uint32 buflen = nbytes+HDRSIZE*(nbuffers+1);
192 a_kbuf =
new char[buflen];
194 char* src = (
char*)a_buffer.
buf();
197 for(
uint32 i=0;i<=nbuffers;i++) {
198 uint32 bufmax = ((i == nbuffers) ? nbytes - nzip : kMAXBUF);
200 if(!
zip(
m_out,func,cxlevel,bufmax,src,bufmax,tgt,nout)) {
202 a_kbuf = (
char*)a_buffer.
buf();
203 a_klen = a_buffer.
length();
215 a_kbuf = (
char*)a_buffer.
buf();
216 a_klen = a_buffer.
length();
221 file(std::ostream& a_out,
const std::string& a_path,
bool a_verbose =
false)
241 mem::increment(
s_class().c_str());
249 m_out <<
"tools::wroot::file::file :"
250 <<
" " <<
sout(
m_path) <<
" root directory badly created."
257 O_RDWR | O_CREAT | O_BINARY,S_IREAD | S_IWRITE
259 O_RDWR | O_CREAT,0644
263 m_out <<
"tools::wroot::file::file :"
264 <<
" can't open " <<
sout(a_path) <<
"."
290 m_out <<
"tools::wroot::file::file :"
291 <<
" can't write file header."
303 m_out <<
"tools::wroot::file::file :"
320 m_out <<
"tools::wroot::file::file :"
321 <<
" key.write_self() failed."
330 m_out <<
"tools::wroot::file::file :"
331 <<
" can't write key in file."
341 mem::decrement(
s_class().c_str());
351 mem::increment(
s_class().c_str());
370 if(
m_file==not_open())
return;
375 m_out <<
"tools::wroot::file::close :"
376 <<
" can't write free segments."
380 m_out <<
"tools::wroot::file::close :"
381 <<
" can't write file header."
386 {std::list<free_seg*>::iterator it;
413 m_out <<
"tools::wroot::file::write :"
423 m_out <<
"tools::wroot::file::write :"
424 <<
" write_streamer_infos failed."
430 m_out <<
"tools::wroot::file::write :"
431 <<
" can't write free segments."
437 m_out <<
"tools::wroot::file::write :"
438 <<
" can't write file header."
448 std::map<char,compress_func>::const_iterator it =
m_zipers.find(a_key);
468 return (::_access(a_path.c_str(),a_mode) == 0) ? true :
false;
470 return (::access(a_path.c_str(),a_mode) == 0) ? true :
false;
473 static bool unlink(
const std::string& a_path){
477 if (::stat(a_path.c_str(),&finfo) < 0)
return false;
479 if (finfo.st_mode & S_IFDIR)
480 return (::_rmdir(a_path.c_str())==-1 ?
false :
true);
482 return (::
unlink(a_path.c_str())==-1 ?
false :
true);
484 if (S_ISDIR(finfo.st_mode))
485 return (::rmdir(a_path.c_str())==-1 ?
false :
true);
487 return (::
unlink(a_path.c_str())==-1 ?
false :
true);
491 static int _open(
const char* a_name,
int a_flags,
unsigned int a_mode) {
492 #if defined(__linux__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2)
493 return ::open64(a_name,a_flags,a_mode);
495 return ::open(a_name,a_flags,a_mode);
499 const char root[] =
"root";
502 const char* eob = psave + kBegin();
504 ::memcpy(pos,root,4); pos += 4;
513 if(!wb.
write(vers))
return false;
525 if(!wb.
write(nfree))
return false;
547 if(sinfos.empty())
return false;
551 if(!sinfos.
stream(bref)) {
552 m_out <<
"tools::wroot::file::write_streamer_infos :"
553 <<
" cannot stream obj_list<streamer_info>."
572 m_out <<
"tools::wroot::file::write_streamer_infos :"
573 <<
" key.write_self() failed."
600 m_out <<
"tools::wroot::file::make_free_seg :"
601 <<
" free_seg list should not be empty here."
608 m_out <<
"tools::wroot::file::make_free_seg :"
609 <<
" add_free failed."
617 seek _nbytes = nlast-nfirst+1;
619 int nbytes = -int(_nbytes);
621 int nb =
sizeof(int);
624 const char* eob = psave + nb;
628 if(!wb.
write(nbytes))
return false;
631 if(!
set_pos(nfirst))
return false;
643 m_out <<
"tools::wroot::file::write_free_segments :"
644 <<
" key.write_self() failed."
654 nbytes += (*it)->record_size();
658 if(!nbytes)
return true;
669 if(!(*it)->fill_buffer(wb))
return false;
674 m_out <<
"tools::wroot::file::write_free_segments :"
675 <<
" key.write_self() failed."
683 m_out <<
"tools::wroot::file::write_free_segments :"
684 <<
" write key." << std::endl;
694 static bool zip(std::ostream& a_out,
697 uint32 a_srcsize,
char* a_src,
698 uint32 a_tgtsize,
char* a_tgt,
705 if(a_tgtsize<HDRSIZE) {
706 a_out <<
"tools::wroot::directory::zip :"
707 <<
" target buffer too small."
712 if(a_srcsize>0xffffff) {
713 a_out <<
"tools::wroot::directory::zip :"
714 <<
" source buffer too big."
721 if(!a_func(a_out,a_level,
723 a_tgtsize,a_tgt+HDRSIZE,
725 a_out <<
"tools::wroot::directory::zip :"
731 if((HDRSIZE+out_size)>a_tgtsize) {
732 a_out <<
"tools::wroot::directory::zip :"
733 <<
" target buffer overflow."
744 a_tgt[3] = (char)(out_size & 0xff);
745 a_tgt[4] = (char)((out_size >> 8) & 0xff);
746 a_tgt[5] = (char)((out_size >> 16) & 0xff);
748 a_tgt[6] = (char)(a_srcsize & 0xff);
749 a_tgt[7] = (char)((a_srcsize >> 8) & 0xff);
750 a_tgt[8] = (char)((a_srcsize >> 16) & 0xff);
752 a_irep = HDRSIZE+out_size;
757 #if defined(__sun) && !defined(__linux__) && (__SUNPRO_CC > 0x420)
#define tools_lforcit(a__T, a__l, a__it)