aboutsummaryrefslogtreecommitdiffstats
path: root/policy/policy_util.cc
blob: 8c17fc291c739342411784ad26b07944ea03d382 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "policy/policy_util.h"

#include <base/files/file_util.h>
#include <base/logging.h>

namespace policy {

LoadPolicyResult LoadPolicyFromPath(
    const base::FilePath& policy_path,
    std::string* policy_data_str_out,
    enterprise_management::PolicyFetchResponse* policy_out) {
  DCHECK(policy_data_str_out);
  DCHECK(policy_out);
  if (!base::PathExists(policy_path)) {
    return LoadPolicyResult::kFileNotFound;
  }

  if (!base::ReadFileToString(policy_path, policy_data_str_out)) {
    PLOG(ERROR) << "Could not read policy off disk at " << policy_path.value();
    return LoadPolicyResult::kFailedToReadFile;
  }

  if (policy_data_str_out->empty()) {
    LOG(ERROR) << "Empty policy file at " << policy_path.value();
    return LoadPolicyResult::kEmptyFile;
  }

  if (!policy_out->ParseFromString(*policy_data_str_out)) {
    LOG(ERROR) << "Policy on disk could not be parsed, file: "
               << policy_path.value();
    return LoadPolicyResult::kInvalidPolicyData;
  }

  return LoadPolicyResult::kSuccess;
}

}  // namespace policy