Mike Frysinger | 3a446f2 | 2022-09-08 07:37:14 | [diff] [blame] | 1 | // Copyright 2021 The ChromiumOS Authors |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef MINIOS_UTILS_H_ |
| 6 | #define MINIOS_UTILS_H_ |
| 7 | |
Saketh Pothireddy | 5317629 | 2023-05-23 16:52:36 | [diff] [blame] | 8 | #include <memory> |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <tuple> |
| 11 | |
| 12 | #include <base/files/file_path.h> |
| 13 | |
Vyshu | 7066089 | 2021-06-09 16:52:26 | [diff] [blame] | 14 | #include "minios/process_manager.h" |
| 15 | |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 16 | namespace minios { |
| 17 | |
Saketh Pothireddy | 2038a78 | 2023-03-03 00:00:57 | [diff] [blame] | 18 | // Alert Log error categories. |
| 19 | extern const char kCategoryInit[]; |
| 20 | extern const char kCategoryReboot[]; |
| 21 | extern const char kCategoryUpdate[]; |
| 22 | |
Saketh Pothireddy | 5317629 | 2023-05-23 16:52:36 | [diff] [blame] | 23 | extern const base::FilePath kDefaultArchivePath; |
| 24 | |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 25 | // Reads the content of `file_path` from `start_offset` to `end_offset` with |
| 26 | // maximum characters per line being `max_columns` at max. If the file ends |
| 27 | // before reading all bytes between `start_offset` and `end_offset` it will |
| 28 | // return true. |
| 29 | // - bool: Success or failure. |
| 30 | // - std::string: The content read. |
| 31 | std::tuple<bool, std::string> ReadFileContentWithinRange( |
| 32 | const base::FilePath& file_path, |
| 33 | int64_t start_offset, |
| 34 | int64_t end_offset, |
| 35 | int num_cols); |
| 36 | |
| 37 | // Reads the content of `file_path` from `offset`. |
| 38 | // The `num_lines` and `num_cols` is the maximum amount of lines and characters |
| 39 | // per line that will be read. |
| 40 | // The return will include: |
| 41 | // - bool: Success or failure. |
| 42 | // - std::string: The content read. |
| 43 | // - int64_t: The number of bytes read. |
| 44 | // Note: The number of bytes read can differ than the length of the content |
| 45 | // output in the second tuple element because the content read is formatted to |
| 46 | // number of lines and columns format to fit onto the requested area of |
| 47 | // `num_lines` * `num_cols`. |
| 48 | std::tuple<bool, std::string, int64_t> ReadFileContent( |
| 49 | const base::FilePath& file_path, |
| 50 | int64_t offset, |
| 51 | int num_lines, |
| 52 | int num_cols); |
| 53 | |
Vyshu | 25e45bf | 2021-09-02 20:36:38 | [diff] [blame] | 54 | // Gets VPD region data given a key. Returns false on failure. |
| 55 | bool GetCrosRegionData(ProcessManagerInterface* process_manager, |
| 56 | std::string key, |
| 57 | std::string* value); |
| 58 | |
| 59 | // Gets XKB keyboard data and extracts country code from it. Defaults to "us" on |
| 60 | // failure. |
| 61 | std::string GetKeyboardLayout(ProcessManagerInterface* process_manager); |
Vyshu | 7066089 | 2021-06-09 16:52:26 | [diff] [blame] | 62 | |
Yuanpeng Ni | 6e6d6cf | 2023-03-22 04:28:37 | [diff] [blame] | 63 | // Read frecon created symbolic link and return the virtual terminal path. |
| 64 | base::FilePath GetLogConsole(); |
| 65 | |
Vyshu | e9a22a7b | 2021-10-08 14:55:53 | [diff] [blame] | 66 | bool TriggerShutdown(); |
| 67 | |
Saketh Pothireddy | 2038a78 | 2023-03-03 00:00:57 | [diff] [blame] | 68 | // Create a tag that can be added to an Error log message to allow easier |
| 69 | // filtering from listnr logs. Expected to be used as the first field of a log |
| 70 | // message. e.g.: `LOG(ERROR) << AlertLogTag(kCategoryName) << err_msg << ....;` |
| 71 | inline std::string AlertLogTag(const std::string& category) { |
| 72 | return base::StringPrintf("[CoreServicesAlert<%s>] ", category.c_str()); |
| 73 | } |
| 74 | |
Saketh Pothireddy | 5317629 | 2023-05-23 16:52:36 | [diff] [blame] | 75 | // Mount the stateful partition at `/stateful/` if its not currently mounted. |
| 76 | // Returns true if successfully mounted, false otherwise. |
Saketh Pothireddy | c131f04 | 2023-05-25 18:17:02 | [diff] [blame] | 77 | bool MountStatefulPartition(ProcessManagerInterface* process_manager); |
| 78 | |
Saketh Pothireddy | 5317629 | 2023-05-23 16:52:36 | [diff] [blame] | 79 | // Compress a pre-determined list of NBR logs and save it to the provided path. |
| 80 | // Returns the result of running a `tar` command. |
| 81 | int CompressLogs(std::unique_ptr<ProcessManagerInterface> process_manager, |
| 82 | const base::FilePath& archive_path = kDefaultArchivePath); |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 83 | |
Saketh Pothireddy | 5317629 | 2023-05-23 16:52:36 | [diff] [blame] | 84 | } // namespace minios |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 85 | #endif // MINIOS_UTILS_H__ |