1 /*
2  * Copyright (C) 2018 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 "idmap2/CommandUtils.h"
18 
19 #include <fstream>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "idmap2/Idmap.h"
25 #include "idmap2/Result.h"
26 #include "idmap2/SysTrace.h"
27 
28 using android::idmap2::Error;
29 using android::idmap2::IdmapHeader;
30 using android::idmap2::OverlayResourceContainer;
31 using android::idmap2::Result;
32 using android::idmap2::TargetResourceContainer;
33 using android::idmap2::Unit;
34 
Verify(const std::string & idmap_path,const std::string & target_path,const std::string & overlay_path,const std::string & overlay_name,PolicyBitmask fulfilled_policies,bool enforce_overlayable)35 Result<Unit> Verify(const std::string& idmap_path, const std::string& target_path,
36                     const std::string& overlay_path, const std::string& overlay_name,
37                     PolicyBitmask fulfilled_policies, bool enforce_overlayable) {
38   SYSTRACE << "Verify " << idmap_path;
39   std::ifstream fin(idmap_path);
40   const std::unique_ptr<const IdmapHeader> header = IdmapHeader::FromBinaryStream(fin);
41   fin.close();
42   if (!header) {
43     return Error("failed to parse idmap header");
44   }
45 
46   auto target = TargetResourceContainer::FromPath(target_path);
47   if (!target) {
48     return Error("failed to load target '%s'", target_path.c_str());
49   }
50 
51   auto overlay = OverlayResourceContainer::FromPath(overlay_path);
52   if (!overlay) {
53     return Error("failed to load overlay '%s'", overlay_path.c_str());
54   }
55 
56   const auto header_ok = header->IsUpToDate(**target, **overlay, overlay_name, fulfilled_policies,
57                                             enforce_overlayable);
58   if (!header_ok) {
59     return Error(header_ok.GetError(), "idmap not up to date");
60   }
61   return Unit{};
62 }
63