xref: /aosp_15_r20/external/libbrillo/policy/resilient_policy_util.cc (revision 1a96fba65179ea7d3f56207137718607415c5953)
1*1a96fba6SXin Li // Copyright 2017 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 #include "policy/resilient_policy_util.h"
6*1a96fba6SXin Li 
7*1a96fba6SXin Li #include <algorithm>
8*1a96fba6SXin Li 
9*1a96fba6SXin Li #include <base/files/file_enumerator.h>
10*1a96fba6SXin Li #include <base/files/file_util.h>
11*1a96fba6SXin Li #include <base/strings/string_number_conversions.h>
12*1a96fba6SXin Li #include <base/strings/string_util.h>
13*1a96fba6SXin Li 
14*1a96fba6SXin Li namespace policy {
15*1a96fba6SXin Li 
GetSortedResilientPolicyFilePaths(const base::FilePath & default_policy_path)16*1a96fba6SXin Li std::map<int, base::FilePath> GetSortedResilientPolicyFilePaths(
17*1a96fba6SXin Li     const base::FilePath& default_policy_path) {
18*1a96fba6SXin Li   std::map<int, base::FilePath> sorted_policy_file_paths;
19*1a96fba6SXin Li   base::FilePath directory = default_policy_path.DirName();
20*1a96fba6SXin Li   if (!base::PathExists(directory))
21*1a96fba6SXin Li     return sorted_policy_file_paths;
22*1a96fba6SXin Li 
23*1a96fba6SXin Li   // Iterate the list of files in the folder, identifying the policy files based
24*1a96fba6SXin Li   // on the name. Store in the map the absolute paths.
25*1a96fba6SXin Li   const std::string default_policy_file_name =
26*1a96fba6SXin Li       default_policy_path.BaseName().value();
27*1a96fba6SXin Li   base::FileEnumerator file_iter(directory, false, base::FileEnumerator::FILES);
28*1a96fba6SXin Li   for (base::FilePath child_file = file_iter.Next(); !child_file.empty();
29*1a96fba6SXin Li        child_file = file_iter.Next()) {
30*1a96fba6SXin Li     int index;
31*1a96fba6SXin Li     if (ParseResilientPolicyFilePath(child_file, default_policy_path, &index)) {
32*1a96fba6SXin Li       sorted_policy_file_paths[index] = child_file;
33*1a96fba6SXin Li     }
34*1a96fba6SXin Li   }
35*1a96fba6SXin Li 
36*1a96fba6SXin Li   return sorted_policy_file_paths;
37*1a96fba6SXin Li }
38*1a96fba6SXin Li 
ParseResilientPolicyFilePath(const base::FilePath & policy_path,const base::FilePath & default_policy_path,int * index_out)39*1a96fba6SXin Li bool ParseResilientPolicyFilePath(const base::FilePath& policy_path,
40*1a96fba6SXin Li                                   const base::FilePath& default_policy_path,
41*1a96fba6SXin Li                                   int* index_out) {
42*1a96fba6SXin Li   if (!base::StartsWith(policy_path.value(), default_policy_path.value(),
43*1a96fba6SXin Li                         base::CompareCase::SENSITIVE)) {
44*1a96fba6SXin Li     return false;
45*1a96fba6SXin Li   }
46*1a96fba6SXin Li 
47*1a96fba6SXin Li   if (policy_path == default_policy_path) {
48*1a96fba6SXin Li     *index_out = 0;
49*1a96fba6SXin Li     return true;
50*1a96fba6SXin Li   }
51*1a96fba6SXin Li 
52*1a96fba6SXin Li   const std::string extension =
53*1a96fba6SXin Li       policy_path.value().substr(default_policy_path.value().size());
54*1a96fba6SXin Li   if (extension.empty() || extension[0] != '.' ||
55*1a96fba6SXin Li       !base::StringToInt(extension.substr(1), index_out) || *index_out <= 0) {
56*1a96fba6SXin Li     return false;
57*1a96fba6SXin Li   }
58*1a96fba6SXin Li 
59*1a96fba6SXin Li   return true;
60*1a96fba6SXin Li }
61*1a96fba6SXin Li 
GetResilientPolicyFilePathForIndex(const base::FilePath & default_policy_path,int index)62*1a96fba6SXin Li base::FilePath GetResilientPolicyFilePathForIndex(
63*1a96fba6SXin Li     const base::FilePath& default_policy_path,
64*1a96fba6SXin Li     int index) {
65*1a96fba6SXin Li   if (index == 0)
66*1a96fba6SXin Li     return default_policy_path;
67*1a96fba6SXin Li   return base::FilePath(default_policy_path.value() + "." +
68*1a96fba6SXin Li                         std::to_string(index));
69*1a96fba6SXin Li }
70*1a96fba6SXin Li 
71*1a96fba6SXin Li }  // namespace policy
72