xref: /aosp_15_r20/external/libbrillo/brillo/osrelease_reader.h (revision 1a96fba65179ea7d3f56207137718607415c5953)
1*1a96fba6SXin Li // Copyright 2014 The Chromium OS Authors. All rights reserved.
2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be
3*1a96fba6SXin Li // found in the LICENSE file.
4*1a96fba6SXin Li 
5*1a96fba6SXin Li // Wrapper around /etc/os-release and /etc/os-release.d.
6*1a96fba6SXin Li // Standard fields can come from both places depending on how we set them. They
7*1a96fba6SXin Li // should always be accessed through this interface.
8*1a96fba6SXin Li 
9*1a96fba6SXin Li #ifndef LIBBRILLO_BRILLO_OSRELEASE_READER_H_
10*1a96fba6SXin Li #define LIBBRILLO_BRILLO_OSRELEASE_READER_H_
11*1a96fba6SXin Li 
12*1a96fba6SXin Li #include <string>
13*1a96fba6SXin Li #include <vector>
14*1a96fba6SXin Li 
15*1a96fba6SXin Li #include <brillo/brillo_export.h>
16*1a96fba6SXin Li #include <brillo/key_value_store.h>
17*1a96fba6SXin Li #include <gtest/gtest_prod.h>
18*1a96fba6SXin Li 
19*1a96fba6SXin Li namespace brillo {
20*1a96fba6SXin Li 
21*1a96fba6SXin Li class BRILLO_EXPORT OsReleaseReader final {
22*1a96fba6SXin Li  public:
23*1a96fba6SXin Li   // Create an empty reader
24*1a96fba6SXin Li   OsReleaseReader() = default;
25*1a96fba6SXin Li 
26*1a96fba6SXin Li   // Loads the key=value pairs from either /etc/os-release.d/<KEY> or
27*1a96fba6SXin Li   // /etc/os-release.
28*1a96fba6SXin Li   void Load();
29*1a96fba6SXin Li 
30*1a96fba6SXin Li   // Same as the private Load method.
31*1a96fba6SXin Li   // This need to be public so that services can use it in testing mode (for
32*1a96fba6SXin Li   // autotest tests for example).
33*1a96fba6SXin Li   // This should not be used in production so suffix it with TestingOnly to
34*1a96fba6SXin Li   // make it obvious.
35*1a96fba6SXin Li   void LoadTestingOnly(const base::FilePath& root_dir);
36*1a96fba6SXin Li 
37*1a96fba6SXin Li   // Getter for the given key. Returns whether the key was found on the store.
38*1a96fba6SXin Li   bool GetString(const std::string& key, std::string* value) const;
39*1a96fba6SXin Li 
40*1a96fba6SXin Li   // Getter for all the keys in /etc/os-release.
41*1a96fba6SXin Li   std::vector<std::string> GetKeys() const;
42*1a96fba6SXin Li 
43*1a96fba6SXin Li  private:
44*1a96fba6SXin Li   // The map storing all the key-value pairs.
45*1a96fba6SXin Li   KeyValueStore store_;
46*1a96fba6SXin Li 
47*1a96fba6SXin Li   // os-release can be lazily loaded if need be.
48*1a96fba6SXin Li   bool initialized_;
49*1a96fba6SXin Li 
50*1a96fba6SXin Li   // Load the data from a given root_dir.
51*1a96fba6SXin Li   BRILLO_PRIVATE void Load(const base::FilePath& root_dir);
52*1a96fba6SXin Li 
53*1a96fba6SXin Li   DISALLOW_COPY_AND_ASSIGN(OsReleaseReader);
54*1a96fba6SXin Li };
55*1a96fba6SXin Li 
56*1a96fba6SXin Li }  // namespace brillo
57*1a96fba6SXin Li 
58*1a96fba6SXin Li #endif  // LIBBRILLO_BRILLO_OSRELEASE_READER_H_
59