lin
2025-07-31 065ea569db06206874bbfa18eb25ff6121aec09b
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
 
#include "update_engine/payload_generator/mapfile_filesystem.h"
 
#include <algorithm>
#include <map>
 
#include <base/files/file_util.h>
#include <base/logging.h>
#include <base/memory/ptr_util.h>
#include <base/strings/string_number_conversions.h>
#include <base/strings/string_split.h>
 
#include "update_engine/common/utils.h"
#include "update_engine/payload_generator/extent_ranges.h"
#include "update_engine/payload_generator/extent_utils.h"
#include "update_engine/update_metadata.pb.h"
 
using std::string;
using std::vector;
 
namespace {
// The .map file is defined in terms of 4K blocks.
size_t kMapfileBlockSize = 4096;
}  // namespace
 
namespace chromeos_update_engine {
 
std::unique_ptr<MapfileFilesystem> MapfileFilesystem::CreateFromFile(
    const string& filename, const string& mapfile_filename) {
  if (filename.empty() || mapfile_filename.empty())
    return nullptr;
 
  off_t file_size = utils::FileSize(filename);
  if (file_size < 0)
    return nullptr;
 
  if (file_size % kMapfileBlockSize) {
    LOG(ERROR) << "Image file " << filename << " has a size of " << file_size
               << " which is not multiple of " << kMapfileBlockSize;
    return nullptr;
  }
  off_t num_blocks = file_size / kMapfileBlockSize;
 
  if (!utils::FileExists(mapfile_filename.c_str())) {
    LOG(ERROR) << "File " << mapfile_filename << " doesn't exist";
    return nullptr;
  }
 
  return base::WrapUnique(new MapfileFilesystem(mapfile_filename, num_blocks));
}
 
MapfileFilesystem::MapfileFilesystem(const string& mapfile_filename,
                                     off_t num_blocks)
    : mapfile_filename_(mapfile_filename), num_blocks_(num_blocks) {}
 
size_t MapfileFilesystem::GetBlockSize() const {
  return kMapfileBlockSize;
}
 
size_t MapfileFilesystem::GetBlockCount() const {
  return num_blocks_;
}
 
bool MapfileFilesystem::GetFiles(vector<File>* files) const {
  files->clear();
 
  string file_data;
  if (!base::ReadFileToString(base::FilePath(mapfile_filename_), &file_data)) {
    LOG(ERROR) << "Unable to read .map file: " << mapfile_filename_;
    return false;
  }
 
  // Iterate over all the lines in the file and generate one File entry per
  // line.
  vector<base::StringPiece> lines = base::SplitStringPiece(
      file_data, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
  for (const base::StringPiece& line : lines) {
    File mapped_file;
 
    mapped_file.extents = {};
    size_t delim, last_delim = line.size();
    while ((delim = line.rfind(' ', last_delim - 1)) != string::npos) {
      string blocks =
          line.substr(delim + 1, last_delim - (delim + 1)).as_string();
      size_t dash = blocks.find('-', 0);
      uint64_t block_start, block_end;
      if (dash == string::npos && base::StringToUint64(blocks, &block_start)) {
        mapped_file.extents.push_back(ExtentForRange(block_start, 1));
      } else if (dash != string::npos &&
                 base::StringToUint64(blocks.substr(0, dash), &block_start) &&
                 base::StringToUint64(blocks.substr(dash + 1), &block_end)) {
        if (block_end < block_start) {
          LOG(ERROR) << "End block " << block_end
                     << " is smaller than start block " << block_start
                     << std::endl
                     << line;
          return false;
        }
        if (block_end > static_cast<uint64_t>(num_blocks_)) {
          LOG(ERROR) << "The end block " << block_end
                     << " is past the end of the file of " << num_blocks_
                     << " blocks" << std::endl
                     << line;
          return false;
        }
        mapped_file.extents.push_back(
            ExtentForRange(block_start, block_end - block_start + 1));
      } else {
        // If we can't parse N or N-M, we assume the block is actually part of
        // the name of the file.
        break;
      }
      last_delim = delim;
    }
    // We parsed the blocks from the end of the line, so we need to reverse
    // the Extents in the file.
    std::reverse(mapped_file.extents.begin(), mapped_file.extents.end());
 
    if (last_delim == string::npos)
      continue;
    mapped_file.name = line.substr(0, last_delim).as_string();
 
    files->push_back(mapped_file);
  }
 
  return true;
}
 
bool MapfileFilesystem::LoadSettings(brillo::KeyValueStore* store) const {
  // Settings not supported in mapfile since the storage format is unknown.
  LOG(ERROR) << "mapfile doesn't support LoadSettings().";
  return false;
}
 
}  // namespace chromeos_update_engine