| 392 | EnvWrapper::~EnvWrapper() {} |
| 393 | |
| 394 | Status ReadFileToString(Env* env, const string& fname, string* data) { |
| 395 | uint64 file_size; |
| 396 | Status s = env->GetFileSize(fname, &file_size); |
| 397 | if (!s.ok()) { |
| 398 | return s; |
| 399 | } |
| 400 | std::unique_ptr<RandomAccessFile> file; |
| 401 | s = env->NewRandomAccessFile(fname, &file); |
| 402 | if (!s.ok()) { |
| 403 | return s; |
| 404 | } |
| 405 | gtl::STLStringResizeUninitialized(data, file_size); |
| 406 | char* p = gtl::string_as_array(data); |
| 407 | StringPiece result; |
| 408 | s = file->Read(0, file_size, &result, p); |
| 409 | if (!s.ok()) { |
| 410 | data->clear(); |
| 411 | } else if (result.size() != file_size) { |
| 412 | s = errors::Aborted("File ", fname, " changed while reading: ", file_size, |
| 413 | " vs. ", result.size()); |
| 414 | data->clear(); |
| 415 | } else if (result.data() == p) { |
| 416 | // Data is already in the correct location |
| 417 | } else { |
| 418 | memmove(p, result.data(), result.size()); |
| 419 | } |
| 420 | return s; |
| 421 | } |
| 422 | |
| 423 | Status WriteStringToFile(Env* env, const string& fname, |
| 424 | const StringPiece& data) { |