xref: /aosp_15_r20/external/libbrillo/policy/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/policy_util.h"
6*1a96fba6SXin Li 
7*1a96fba6SXin Li #include <base/files/file_util.h>
8*1a96fba6SXin Li #include <base/logging.h>
9*1a96fba6SXin Li 
10*1a96fba6SXin Li namespace policy {
11*1a96fba6SXin Li 
LoadPolicyFromPath(const base::FilePath & policy_path,std::string * policy_data_str_out,enterprise_management::PolicyFetchResponse * policy_out)12*1a96fba6SXin Li LoadPolicyResult LoadPolicyFromPath(
13*1a96fba6SXin Li     const base::FilePath& policy_path,
14*1a96fba6SXin Li     std::string* policy_data_str_out,
15*1a96fba6SXin Li     enterprise_management::PolicyFetchResponse* policy_out) {
16*1a96fba6SXin Li   DCHECK(policy_data_str_out);
17*1a96fba6SXin Li   DCHECK(policy_out);
18*1a96fba6SXin Li   if (!base::PathExists(policy_path)) {
19*1a96fba6SXin Li     return LoadPolicyResult::kFileNotFound;
20*1a96fba6SXin Li   }
21*1a96fba6SXin Li 
22*1a96fba6SXin Li   if (!base::ReadFileToString(policy_path, policy_data_str_out)) {
23*1a96fba6SXin Li     PLOG(ERROR) << "Could not read policy off disk at " << policy_path.value();
24*1a96fba6SXin Li     return LoadPolicyResult::kFailedToReadFile;
25*1a96fba6SXin Li   }
26*1a96fba6SXin Li 
27*1a96fba6SXin Li   if (policy_data_str_out->empty()) {
28*1a96fba6SXin Li     LOG(ERROR) << "Empty policy file at " << policy_path.value();
29*1a96fba6SXin Li     return LoadPolicyResult::kEmptyFile;
30*1a96fba6SXin Li   }
31*1a96fba6SXin Li 
32*1a96fba6SXin Li   if (!policy_out->ParseFromString(*policy_data_str_out)) {
33*1a96fba6SXin Li     LOG(ERROR) << "Policy on disk could not be parsed, file: "
34*1a96fba6SXin Li                << policy_path.value();
35*1a96fba6SXin Li     return LoadPolicyResult::kInvalidPolicyData;
36*1a96fba6SXin Li   }
37*1a96fba6SXin Li 
38*1a96fba6SXin Li   return LoadPolicyResult::kSuccess;
39*1a96fba6SXin Li }
40*1a96fba6SXin Li 
41*1a96fba6SXin Li }  // namespace policy
42