Add truncate to FileWriter routine.

This commit is contained in:
vng 2011-09-05 16:25:01 +03:00 committed by Alex Zolotarev
parent 4f31145f65
commit a0cf3c1413
7 changed files with 29 additions and 8 deletions

View file

@ -3,6 +3,7 @@
#include "../file_container.hpp"
#include "../varint.hpp"
#include "../../base/logging.hpp"
#include "../../base/string_utils.hpp"
@ -146,6 +147,7 @@ namespace
char const * key[], char const * value[], size_t count)
{
FilesContainerR reader(fName);
LOG(LINFO, ("Size=", reader.GetSize()));
for (size_t i = 0; i < count; ++i)
{

View file

@ -179,6 +179,7 @@ FileWriter FilesContainerW::GetWriter(Tag const & tag)
FileWriter writer(m_name, FileWriter::OP_WRITE_EXISTING);
writer.Seek(curr);
writer.Truncate(curr);
return writer;
}
else
@ -224,17 +225,15 @@ void FilesContainerW::Finish()
{
ASSERT(!m_bFinished, ());
{
uint64_t const curr = SaveCurrentSize();
FileWriter writer(m_name, FileWriter::OP_WRITE_EXISTING);
writer.Seek(0);
WriteToSink(writer, curr);
}
uint64_t const curr = SaveCurrentSize();
FileWriter writer(m_name, FileWriter::OP_WRITE_EXISTING);
writer.Seek(0);
WriteToSink(writer, curr);
writer.Seek(curr);
sort(m_info.begin(), m_info.end(), LessInfo());
FileWriter writer(m_name, FileWriter::OP_APPEND);
uint32_t const count = m_info.size();
WriteVarUint(writer, count);

View file

@ -98,6 +98,8 @@ public:
f(m_info[i].m_tag);
}
inline uint64_t GetSize() const { return m_source.Size(); }
private:
ReaderT m_source;
};

View file

@ -51,6 +51,11 @@ void FileWriter::Flush()
m_pFileData->Flush();
}
void FileWriter::Truncate(uint64_t sz)
{
m_pFileData->Truncate(sz);
}
void FileWriter::DeleteFileX(string const & fName)
{
my::DeleteFileX(fName);

View file

@ -33,8 +33,10 @@ public:
void Seek(int64_t pos);
int64_t Pos() const;
void Write(void const * p, size_t size);
uint64_t Size() const;
void Flush();
void Truncate(uint64_t sz);
static void DeleteFileX(string const & fName);

View file

@ -153,6 +153,15 @@ void FileData::Flush()
#endif
}
void FileData::Truncate(uint64_t sz)
{
#ifdef OMIM_OS_WINDOWS
_chsize(fileno(m_File), sz);
#else
ftruncate(fileno(m_File), sz);
#endif
}
bool GetFileSize(string const & fName, uint64_t & sz)
{
try

View file

@ -27,7 +27,9 @@ public:
void Read(uint64_t pos, void * p, size_t size);
void Write(void const * p, size_t size);
void Flush();
void Truncate(uint64_t sz);
string GetName() const { return m_FileName; }