| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <ctime> |
| 6 #include <iomanip> |
| 7 #include <sstream> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "mojo/public/cpp/application/connect.h" |
| 11 #include "services/flog/flog_directory.h" |
| 12 |
| 13 namespace mojo { |
| 14 namespace flog { |
| 15 |
| 16 FlogDirectory::FlogDirectory(Shell* shell) { |
| 17 ConnectToService(shell, "mojo:files", GetProxy(&files_)); |
| 18 files_->OpenFileSystem("app_persistent_cache", GetProxy(&file_system_), |
| 19 [this](files::Error error) { |
| 20 files_.reset(); |
| 21 if (error != files::Error::OK) { |
| 22 DCHECK(false) << "Failed to open file system: " |
| 23 << error; |
| 24 delete this; |
| 25 } |
| 26 }); |
| 27 } |
| 28 |
| 29 FlogDirectory::~FlogDirectory() {} |
| 30 |
| 31 void FlogDirectory::GetExistingFiles(GetExistingFilesCallback callback) { |
| 32 file_system_->Read([this, callback](files::Error error, |
| 33 Array<files::DirectoryEntryPtr> entries) { |
| 34 std::map<uint32_t, std::string> labels_by_id; |
| 35 for (const files::DirectoryEntryPtr& entry : entries) { |
| 36 if (entry->type == files::FileType::REGULAR_FILE) { |
| 37 uint32_t id; |
| 38 std::string label; |
| 39 if (ParseLogFileName(entry->name, &id, &label)) { |
| 40 labels_by_id.insert(std::pair<uint32_t, std::string>(id, label)); |
| 41 } |
| 42 } |
| 43 } |
| 44 |
| 45 callback(std::move(labels_by_id)); |
| 46 }); |
| 47 } |
| 48 |
| 49 files::FilePtr FlogDirectory::GetFile(uint32_t id, |
| 50 const std::string& label, |
| 51 bool create) { |
| 52 files::FilePtr file; |
| 53 file_system_->OpenFile( |
| 54 LogFileName(id, label), GetProxy(&file), |
| 55 files::kOpenFlagRead | |
| 56 (create ? (files::kOpenFlagWrite | files::kOpenFlagCreate) : 0), |
| 57 [this](files::Error error) { |
| 58 if (error != files::Error::OK) { |
| 59 DCHECK(false) << "Failed to OpenFile" << error; |
| 60 // TODO: Fail. |
| 61 return; |
| 62 } |
| 63 }); |
| 64 file_system_.WaitForIncomingResponse(); |
| 65 return file.Pass(); |
| 66 } |
| 67 |
| 68 std::string FlogDirectory::LogFileName(uint32_t id, const std::string& label) { |
| 69 // Format is "<id>_<label>.flog" where <id> is the kLogIdWidth-digit, |
| 70 // zero-padded info.id_ and <label> is the label. |
| 71 std::ostringstream file_name_stream; |
| 72 file_name_stream << std::setfill('0') << std::setw(kLogIdWidth) << id << "_" |
| 73 << label << ".flog"; |
| 74 return file_name_stream.str(); |
| 75 } |
| 76 |
| 77 bool FlogDirectory::ParseLogFileName(const std::string& name, |
| 78 uint32_t* id_out, |
| 79 std::string* label_out) { |
| 80 DCHECK(id_out != nullptr); |
| 81 DCHECK(label_out != nullptr); |
| 82 |
| 83 if (name.size() < kLogIdWidth + 2) { |
| 84 return false; |
| 85 } |
| 86 |
| 87 for (size_t i = 0; i < kLogIdWidth; i++) { |
| 88 if (!isdigit(name[i])) { |
| 89 return false; |
| 90 } |
| 91 } |
| 92 |
| 93 if (name[kLogIdWidth] != '_') { |
| 94 return false; |
| 95 } |
| 96 |
| 97 size_t after_label = name.find_first_of('.', kLogIdWidth + 1); |
| 98 if (after_label == std::string::npos) { |
| 99 return false; |
| 100 } |
| 101 |
| 102 *id_out = std::stoul(name); |
| 103 *label_out = name.substr(kLogIdWidth + 1, after_label - (kLogIdWidth + 1)); |
| 104 |
| 105 return true; |
| 106 } |
| 107 |
| 108 } // namespace flog |
| 109 } // namespace mojo |
| OLD | NEW |