| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/syslogs/lsbrelease_fetcher.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/string_util.h" |
| 13 |
| 14 |
| 15 namespace chromeos { |
| 16 |
| 17 // TODO(tudalex): Replace this code with a more simpler code based on a string |
| 18 // tokenizer |
| 19 |
| 20 // The following code is copied form SysLogsProvider |
| 21 const char kMultilineQuote[] = "\"\"\""; |
| 22 const char kNewLineChars[] = "\r\n"; |
| 23 const char kInvalidLogEntry[] = "<invalid characters in log entry>"; |
| 24 const char kEmptyLogEntry[] = "<no value>"; |
| 25 |
| 26 // Reads a key from the input string erasing the read values + delimiters read |
| 27 // from the initial string |
| 28 std::string ReadKey(std::string* data) { |
| 29 size_t equal_sign = data->find("="); |
| 30 if (equal_sign == std::string::npos) |
| 31 return std::string(""); |
| 32 std::string key = data->substr(0, equal_sign); |
| 33 data->erase(0, equal_sign); |
| 34 if (data->size() > 0) { |
| 35 // erase the equal to sign also |
| 36 data->erase(0,1); |
| 37 return key; |
| 38 } |
| 39 return std::string(); |
| 40 } |
| 41 |
| 42 // Reads a value from the input string; erasing the read values from |
| 43 // the initial string; detects if the value is multiline and reads |
| 44 // accordingly |
| 45 std::string ReadValue(std::string* data) { |
| 46 // Trim the leading spaces and tabs. In order to use a multi-line |
| 47 // value, you have to place the multi-line quote on the same line as |
| 48 // the equal sign. |
| 49 // |
| 50 // Why not use TrimWhitespace? Consider the following input: |
| 51 // |
| 52 // KEY1= |
| 53 // KEY2=VALUE |
| 54 // |
| 55 // If we use TrimWhitespace, we will incorrectly trim the new line |
| 56 // and assume that KEY1's value is "KEY2=VALUE" rather than empty. |
| 57 TrimString(*data, " \t", data); |
| 58 |
| 59 // If multiline value |
| 60 if (StartsWithASCII(*data, std::string(kMultilineQuote), false)) { |
| 61 data->erase(0, strlen(kMultilineQuote)); |
| 62 size_t next_multi = data->find(kMultilineQuote); |
| 63 if (next_multi == std::string::npos) { |
| 64 // Error condition, clear data to stop further processing |
| 65 data->erase(); |
| 66 return std::string(); |
| 67 } |
| 68 std::string value = data->substr(0, next_multi); |
| 69 data->erase(0, next_multi + 3); |
| 70 return value; |
| 71 } else { // single line value |
| 72 size_t endl_pos = data->find_first_of(kNewLineChars); |
| 73 // if we don't find a new line, we just return the rest of the data |
| 74 std::string value = data->substr(0, endl_pos); |
| 75 data->erase(0, endl_pos); |
| 76 return value; |
| 77 } |
| 78 } |
| 79 |
| 80 void LSBReleaseFetcher::Fetch(SysLogsFetcherCallback request) { |
| 81 SysLogsResponse* response = new SysLogsResponse; |
| 82 FilePath lsb_release_file("/etc/lsb-release"); |
| 83 std::string data; |
| 84 bool read_success = file_util::ReadFileToString(lsb_release_file, |
| 85 &data); |
| 86 // if we were using an internal temp file, the user does not need the |
| 87 // logs to stay past the ReadFile call - delete the file |
| 88 if (!read_success) |
| 89 { |
| 90 LOG(ERROR)<<"Can't access /etc/lsb-release file."; |
| 91 request.Run(response); |
| 92 return; |
| 93 } |
| 94 while (data.length() > 0) { |
| 95 std::string key = ReadKey(&data); |
| 96 TrimWhitespaceASCII(key, TRIM_ALL, &key); |
| 97 if (!key.empty()) { |
| 98 std::string value = ReadValue(&data); |
| 99 if (IsStringUTF8(value)) { |
| 100 TrimWhitespaceASCII(value, TRIM_ALL, &value); |
| 101 if (value.empty()) |
| 102 (*response)[key] = kEmptyLogEntry; |
| 103 else |
| 104 (*response)[key] = value; |
| 105 } else { |
| 106 LOG(WARNING) << "Invalid characters in system log entry: " << key; |
| 107 (*response)[key] = kInvalidLogEntry; |
| 108 } |
| 109 } else { |
| 110 // no more keys, we're done |
| 111 break; |
| 112 } |
| 113 } |
| 114 request.Run(response); |
| 115 } |
| 116 |
| 117 } // namespace chromeos |
| OLD | NEW |