blob: 0f87ef0c4ea918e2d1a3a18d04d8b7c9f8cdef14 [file] [log] [blame]
MÃ¥rten Kongstadd10d06d2019-01-07 17:26:25 -08001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <iterator>
18#include <map>
19#include <sstream>
20#include <string>
21#include <vector>
22
23#include "androidfw/ResourceTypes.h"
24
25#include "idmap2/Idmap.h"
26#include "idmap2/Policies.h"
27#include "idmap2/Result.h"
28
29namespace android::idmap2 {
30
31namespace {
32
33const std::map<android::StringPiece, PolicyFlags> kStringToFlag = {
34 {"public", PolicyFlags::POLICY_PUBLIC},
35 {"product", PolicyFlags::POLICY_PRODUCT_PARTITION},
36 {"system", PolicyFlags::POLICY_SYSTEM_PARTITION},
37 {"vendor", PolicyFlags::POLICY_VENDOR_PARTITION},
38};
39} // namespace
40
41Result<PolicyBitmask> PoliciesToBitmask(const std::vector<std::string>& policies,
42 std::ostream& err) {
43 PolicyBitmask bitmask = 0;
44 for (const std::string& policy : policies) {
45 const auto iter = kStringToFlag.find(policy);
46 if (iter != kStringToFlag.end()) {
47 bitmask |= iter->second;
48 } else {
49 err << "error: unknown policy \"" << policy << "\"";
50 return kResultError;
51 }
52 }
53
54 return Result<PolicyBitmask>(bitmask);
55}
56
57} // namespace android::idmap2