blob: e1dbac22e22b63fc940ea3d973a517d01c13bc34 [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 Pothireddy53176292023-05-23 16:52:3623extern const base::FilePath kDefaultArchivePath;
24
Jae Hoon Kim0fbd6472021-04-29 19:08:3325// 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.
31std::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`.
48std::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
Vyshu25e45bf2021-09-02 20:36:3854// Gets VPD region data given a key. Returns false on failure.
55bool 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.
61std::string GetKeyboardLayout(ProcessManagerInterface* process_manager);
Vyshu70660892021-06-09 16:52:2662
Yuanpeng Ni6e6d6cf2023-03-22 04:28:3763// Read frecon created symbolic link and return the virtual terminal path.
64base::FilePath GetLogConsole();
65
Vyshue9a22a7b2021-10-08 14:55:5366bool TriggerShutdown();
67
Saketh Pothireddy2038a782023-03-03 00:00:5768// 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 << ....;`
71inline std::string AlertLogTag(const std::string& category) {
72 return base::StringPrintf("[CoreServicesAlert<%s>] ", category.c_str());
73}
74
Saketh Pothireddy53176292023-05-23 16:52:3675// Mount the stateful partition at `/stateful/` if its not currently mounted.
76// Returns true if successfully mounted, false otherwise.
Saketh Pothireddyc131f042023-05-25 18:17:0277bool MountStatefulPartition(ProcessManagerInterface* process_manager);
78
Saketh Pothireddy53176292023-05-23 16:52:3679// Compress a pre-determined list of NBR logs and save it to the provided path.
80// Returns the result of running a `tar` command.
81int CompressLogs(std::unique_ptr<ProcessManagerInterface> process_manager,
82 const base::FilePath& archive_path = kDefaultArchivePath);
Jae Hoon Kim0fbd6472021-04-29 19:08:3383
Saketh Pothireddy53176292023-05-23 16:52:3684} // namespace minios
Jae Hoon Kim0fbd6472021-04-29 19:08:3385#endif // MINIOS_UTILS_H__