blob: b85cd0da440968a0316153fa05004fc295b26be9 [file] [log] [blame]
Mike Frysinger3a446f22022-09-08 07:37:141// Copyright 2021 The ChromiumOS Authors
Jae Hoon Kim0fbd6472021-04-29 19:08:332// 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 Pothireddy53176292023-05-23 16:52:368#include <memory>
Jae Hoon Kim0fbd6472021-04-29 19:08:339#include <string>
10#include <tuple>
11
12#include <base/files/file_path.h>
13
Vyshu70660892021-06-09 16:52:2614#include "minios/process_manager.h"
15
Jae Hoon Kim0fbd6472021-04-29 19:08:3316namespace minios {
17
Saketh Pothireddy2038a782023-03-03 00:00:5718// Alert Log error categories.
19extern const char kCategoryInit[];
20extern const char kCategoryReboot[];
21extern const char kCategoryUpdate[];
22
Saketh Pothireddy7230b622023-06-23 23:55:0923extern const char kLogFilePath[];
24
Saketh Pothireddy53176292023-05-23 16:52:3625extern const base::FilePath kDefaultArchivePath;
26
Jae Hoon Kim0fbd6472021-04-29 19:08:3327// Reads the content of `file_path` from `start_offset` to `end_offset` with
28// maximum characters per line being `max_columns` at max. If the file ends
29// before reading all bytes between `start_offset` and `end_offset` it will
30// return true.
31// - bool: Success or failure.
32// - std::string: The content read.
33std::tuple<bool, std::string> ReadFileContentWithinRange(
34 const base::FilePath& file_path,
35 int64_t start_offset,
36 int64_t end_offset,
37 int num_cols);
38
39// Reads the content of `file_path` from `offset`.
40// The `num_lines` and `num_cols` is the maximum amount of lines and characters
41// per line that will be read.
42// The return will include:
43// - bool: Success or failure.
44// - std::string: The content read.
45// - int64_t: The number of bytes read.
46// Note: The number of bytes read can differ than the length of the content
47// output in the second tuple element because the content read is formatted to
48// number of lines and columns format to fit onto the requested area of
49// `num_lines` * `num_cols`.
50std::tuple<bool, std::string, int64_t> ReadFileContent(
51 const base::FilePath& file_path,
52 int64_t offset,
53 int num_lines,
54 int num_cols);
55
Vyshu25e45bf2021-09-02 20:36:3856// Gets VPD region data given a key. Returns false on failure.
57bool GetCrosRegionData(ProcessManagerInterface* process_manager,
58 std::string key,
59 std::string* value);
60
61// Gets XKB keyboard data and extracts country code from it. Defaults to "us" on
62// failure.
63std::string GetKeyboardLayout(ProcessManagerInterface* process_manager);
Vyshu70660892021-06-09 16:52:2664
Yuanpeng Ni6e6d6cf2023-03-22 04:28:3765// Read frecon created symbolic link and return the virtual terminal path.
66base::FilePath GetLogConsole();
67
Vyshue9a22a7b2021-10-08 14:55:5368bool TriggerShutdown();
69
Saketh Pothireddy2038a782023-03-03 00:00:5770// Create a tag that can be added to an Error log message to allow easier
71// filtering from listnr logs. Expected to be used as the first field of a log
72// message. e.g.: `LOG(ERROR) << AlertLogTag(kCategoryName) << err_msg << ....;`
73inline std::string AlertLogTag(const std::string& category) {
74 return base::StringPrintf("[CoreServicesAlert<%s>] ", category.c_str());
75}
76
Saketh Pothireddy53176292023-05-23 16:52:3677// Mount the stateful partition at `/stateful/` if its not currently mounted.
78// Returns true if successfully mounted, false otherwise.
Saketh Pothireddyc131f042023-05-25 18:17:0279bool MountStatefulPartition(ProcessManagerInterface* process_manager);
80
Saketh Pothireddy53176292023-05-23 16:52:3681// Compress a pre-determined list of NBR logs and save it to the provided path.
82// Returns the result of running a `tar` command.
83int CompressLogs(std::unique_ptr<ProcessManagerInterface> process_manager,
84 const base::FilePath& archive_path = kDefaultArchivePath);
Jae Hoon Kim0fbd6472021-04-29 19:08:3385
Saketh Pothireddy53176292023-05-23 16:52:3686} // namespace minios
Jae Hoon Kim0fbd6472021-04-29 19:08:3387#endif // MINIOS_UTILS_H__