Welcome to mirror list, hosted at ThFree Co, Russian Federation.

file_data.cpp « internal « coding - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3c3fda6195f7a1eb4e4b9dcae1ae94b96bdc034d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "coding/internal/file_data.hpp"

#include "coding/constants.hpp"
#include "coding/reader.hpp" // For Reader exceptions.
#include "coding/writer.hpp" // For Writer exceptions.

#include "base/exception.hpp"
#include "base/logging.hpp"
#include "base/string_utils.hpp"

#include "std/cerrno.hpp"
#include "std/cstring.hpp"
#include "std/exception.hpp"
#include "std/fstream.hpp"
#include "std/target_os.hpp"
#include "std/thread.hpp"

#ifdef OMIM_OS_WINDOWS
  #include <io.h>
#endif

#ifdef OMIM_OS_TIZEN
#include "tizen/inc/FIo.hpp"
#endif


namespace my
{

FileData::FileData(string const & fileName, Op op)
    : m_FileName(fileName), m_Op(op)
{
  char const * const modes [] = {"rb", "wb", "r+b", "ab"};
#ifdef OMIM_OS_TIZEN
  m_File = new Tizen::Io::File();
  result const error = m_File->Construct(fileName.c_str(), modes[op]);
  if (error == E_SUCCESS)
  {
    return;
  }
#else
  m_File = fopen(fileName.c_str(), modes[op]);
  if (m_File)
    return;

  if (op == OP_WRITE_EXISTING)
  {
    // Special case, since "r+b" fails if file doesn't exist.
    m_File = fopen(fileName.c_str(), "wb");
    if (m_File)
      return;
  }
#endif

  // if we're here - something bad is happened
  if (m_Op != OP_READ)
    MYTHROW(Writer::OpenException, (GetErrorProlog()));
  else
    MYTHROW(Reader::OpenException, (GetErrorProlog()));
}

FileData::~FileData()
{
#ifdef OMIM_OS_TIZEN
  delete m_File;
#else
  if (m_File)
  {
    if (fclose(m_File))
      LOG(LWARNING, ("Error closing file", GetErrorProlog()));
  }
#endif
}

string FileData::GetErrorProlog() const
{
  char const * s;
  switch (m_Op)
  {
  case OP_READ: s = "Read"; break;
  case OP_WRITE_TRUNCATE: s = "Write truncate"; break;
  case OP_WRITE_EXISTING: s = "Write existing"; break;
  case OP_APPEND: s = "Append"; break;
  }

  return m_FileName + "; " + s + "; " + strerror(errno);
}

static int64_t const INVALID_POS = -1;

uint64_t FileData::Size() const
{
#ifdef OMIM_OS_TIZEN
  Tizen::Io::FileAttributes attr;
  result const error = Tizen::Io::File::GetAttributes(m_FileName.c_str(), attr);
  if (IsFailed(error))
    MYTHROW(Reader::SizeException, (m_FileName, m_Op, error));
  return attr.GetFileSize();
#else
  int64_t const pos = ftell64(m_File);
  if (pos == INVALID_POS)
    MYTHROW(Reader::SizeException, (GetErrorProlog(), pos));

  if (fseek64(m_File, 0, SEEK_END))
    MYTHROW(Reader::SizeException, (GetErrorProlog()));

  int64_t const size = ftell64(m_File);
  if (size == INVALID_POS)
    MYTHROW(Reader::SizeException, (GetErrorProlog(), size));

  if (fseek64(m_File, pos, SEEK_SET))
    MYTHROW(Reader::SizeException, (GetErrorProlog(), pos));

  ASSERT_GREATER_OR_EQUAL(size, 0, ());
  return static_cast<uint64_t>(size);
#endif
}

void FileData::Read(uint64_t pos, void * p, size_t size)
{
#ifdef OMIM_OS_TIZEN
  result error = m_File->Seek(Tizen::Io::FILESEEKPOSITION_BEGIN, pos);
  if (IsFailed(error))
    MYTHROW(Reader::ReadException, (error, pos));
  int const bytesRead = m_File->Read(p, size);
  error = GetLastResult();
  if (static_cast<size_t>(bytesRead) != size || IsFailed(error))
    MYTHROW(Reader::ReadException, (m_FileName, m_Op, error, bytesRead, pos, size));
#else
  if (fseek64(m_File, pos, SEEK_SET))
    MYTHROW(Reader::ReadException, (GetErrorProlog(), pos));

  size_t const bytesRead = fread(p, 1, size, m_File);
  if (bytesRead != size || ferror(m_File))
    MYTHROW(Reader::ReadException, (GetErrorProlog(), bytesRead, pos, size));
#endif
}

uint64_t FileData::Pos() const
{
#ifdef OMIM_OS_TIZEN
  int const pos = m_File->Tell();
  result const error = GetLastResult();
  if (IsFailed(error))
    MYTHROW(Writer::PosException, (m_FileName, m_Op, error, pos));
  return pos;
#else
  int64_t const pos = ftell64(m_File);
  if (pos == INVALID_POS)
    MYTHROW(Writer::PosException, (GetErrorProlog(), pos));

  ASSERT_GREATER_OR_EQUAL(pos, 0, ());
  return static_cast<uint64_t>(pos);
#endif
}

void FileData::Seek(uint64_t pos)
{
  ASSERT_NOT_EQUAL(m_Op, OP_APPEND, (m_FileName, m_Op, pos));
#ifdef OMIM_OS_TIZEN
  result const error = m_File->Seek(Tizen::Io::FILESEEKPOSITION_BEGIN, pos);
  if (IsFailed(error))
    MYTHROW(Writer::SeekException, (m_FileName, m_Op, error, pos));
#else
  if (fseek64(m_File, pos, SEEK_SET))
    MYTHROW(Writer::SeekException, (GetErrorProlog(), pos));
#endif
}

void FileData::Write(void const * p, size_t size)
{
#ifdef OMIM_OS_TIZEN
  result const error = m_File->Write(p, size);
  if (IsFailed(error))
    MYTHROW(Writer::WriteException, (m_FileName, m_Op, error, size));
#else
  size_t const bytesWritten = fwrite(p, 1, size, m_File);
  if (bytesWritten != size || ferror(m_File))
    MYTHROW(Writer::WriteException, (GetErrorProlog(), bytesWritten, size));
#endif
}

void FileData::Flush()
{
#ifdef OMIM_OS_TIZEN
  result const error = m_File->Flush();
  if (IsFailed(error))
    MYTHROW(Writer::WriteException, (m_FileName, m_Op, error));
#else
  if (fflush(m_File))
    MYTHROW(Writer::WriteException, (GetErrorProlog()));
#endif
}

void FileData::Truncate(uint64_t sz)
{
#ifdef OMIM_OS_WINDOWS
  int const res = _chsize(fileno(m_File), sz);
#elif defined OMIM_OS_TIZEN
  result res = m_File->Truncate(sz);
#else
  int const res = ftruncate(fileno(m_File), sz);
#endif

  if (res)
    MYTHROW(Writer::WriteException, (GetErrorProlog(), sz));
}

bool GetFileSize(string const & fName, uint64_t & sz)
{
  try
  {
    typedef my::FileData fdata_t;
    fdata_t f(fName, fdata_t::OP_READ);
    sz = f.Size();
    return true;
  }
  catch (RootException const &)
  {
    // supress all exceptions here
    return false;
  }
}

namespace
{
bool CheckFileOperationResult(int res, string const & fName)
{
  if (!res)
    return true;
#if !defined(OMIM_OS_TIZEN)
  LOG(LWARNING, ("File operation error for file:", fName, "-", strerror(errno)));
#endif

  // additional check if file really was removed correctly
  uint64_t dummy;
  if (GetFileSize(fName, dummy))
  {
    LOG(LERROR, ("File exists but can't be deleted. Sharing violation?", fName));
  }

  return false;
}
}  // namespace

bool DeleteFileX(string const & fName)
{
  int res;

#ifdef OMIM_OS_TIZEN
  res = IsFailed(Tizen::Io::File::Remove(fName.c_str())) ? -1 : 0;
#else
  res = remove(fName.c_str());
#endif

  return CheckFileOperationResult(res, fName);
}

bool RenameFileX(string const & fOld, string const & fNew)
{
  int res;

#ifdef OMIM_OS_TIZEN
  res = IsFailed(Tizen::Io::File::Move(fOld.c_str(), fNew.c_str())) ? -1 : 0;
#else
  res = rename(fOld.c_str(), fNew.c_str());
#endif

  return CheckFileOperationResult(res, fOld);
}

bool WriteToTempAndRenameToFile(string const & dest, function<bool(string const &)> const & write,
                                string const & tmp)
{
  string const tmpFileName =
      tmp.empty() ? dest + ".tmp" + strings::to_string(this_thread::get_id()) : tmp;
  if (!write(tmpFileName))
  {
    LOG(LERROR, ("Can't write to", tmpFileName));
    return false;
  }
  if (!RenameFileX(tmpFileName, dest))
  {
    LOG(LERROR, ("Can't rename file", tmpFileName, "to", dest));
    DeleteFileX(tmpFileName);
    return false;
  }
  return true;
}

bool CopyFileX(string const & fOld, string const & fNew)
{
  try
  {
    ifstream ifs(fOld.c_str());
    ofstream ofs(fNew.c_str());

    if (ifs.is_open() && ofs.is_open())
    {
      ofs << ifs.rdbuf();
      ofs.flush();

      if (ofs.fail())
      {
        LOG(LWARNING, ("Bad or Fail bit is set while writing file:", fNew));
        return false;
      }

      return true;
    }
    else
      LOG(LERROR, ("Can't open files:", fOld, fNew));
  }
  catch (exception const & ex)
  {
    LOG(LERROR, ("Copy file error:", ex.what()));
  }

  return false;
}

bool IsEqualFiles(string const & firstFile, string const & secondFile)
{
  my::FileData first(firstFile, my::FileData::OP_READ);
  my::FileData second(secondFile, my::FileData::OP_READ);
  if (first.Size() != second.Size())
    return false;

  size_t const bufSize = READ_FILE_BUFFER_SIZE;
  vector<char> buf1, buf2;
  buf1.resize(bufSize);
  buf2.resize(bufSize);
  size_t const fileSize = first.Size();
  size_t currSize = 0;

  while (currSize < fileSize)
  {
    size_t const toRead = min(bufSize, fileSize - currSize);

    first.Read(currSize, &buf1[0], toRead);
    second.Read(currSize, &buf2[0], toRead);

    if (buf1 != buf2)
      return false;

    currSize += toRead;
  }

  return true;
}

}