File: metadata.cpp

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (234 lines) | stat: -rw-r--r-- 7,682 bytes parent folder | download | duplicates (2)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//
// Copyright (C) 2019 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 "metadata.h"

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <android-base/file.h>
#include <android-base/logging.h>
#include <liblp/builder.h>

#include "utility.h"

namespace android {
namespace fiemap {

using namespace android::fs_mgr;
using android::base::unique_fd;

static constexpr uint32_t kMaxMetadataSize = 256 * 1024;

std::string GetMetadataFile(const std::string& metadata_dir) {
    return JoinPaths(metadata_dir, "lp_metadata");
}

bool MetadataExists(const std::string& metadata_dir) {
    auto metadata_file = GetMetadataFile(metadata_dir);
    if (access(metadata_file.c_str(), F_OK)) {
        if (errno != ENOENT) {
            PLOG(ERROR) << "Access " << metadata_file << " failed:";
        }
        return false;
    }
    return true;
}

std::unique_ptr<LpMetadata> OpenMetadata(const std::string& metadata_dir) {
    auto metadata_file = GetMetadataFile(metadata_dir);
    auto metadata = ReadFromImageFile(metadata_file);
    if (!metadata) {
        LOG(ERROR) << "Could not read metadata file " << metadata_file;
        return nullptr;
    }
    return metadata;
}

// :TODO: overwrite on create if open fails
std::unique_ptr<MetadataBuilder> OpenOrCreateMetadata(const std::string& metadata_dir,
                                                      SplitFiemap* file) {
    auto metadata_file = GetMetadataFile(metadata_dir);

    PartitionOpener opener;
    std::unique_ptr<MetadataBuilder> builder;
    if (access(metadata_file.c_str(), R_OK)) {
        if (errno != ENOENT) {
            PLOG(ERROR) << "Access " << metadata_file << " failed:";
            return nullptr;
        }

        auto data_device = GetDevicePathForFile(file);

        BlockDeviceInfo device_info;
        if (!opener.GetInfo(data_device, &device_info)) {
            LOG(ERROR) << "Could not read partition: " << data_device;
            return nullptr;
        }

        std::vector<BlockDeviceInfo> block_devices = {device_info};
        auto super_name = android::base::Basename(data_device);
        builder = MetadataBuilder::New(block_devices, super_name, kMaxMetadataSize, 1);
    } else {
        auto metadata = OpenMetadata(metadata_dir);
        if (!metadata) {
            return nullptr;
        }
        builder = MetadataBuilder::New(*metadata.get(), &opener);
    }

    if (!builder) {
        LOG(ERROR) << "Could not create metadata builder";
        return nullptr;
    }
    return builder;
}

bool SaveMetadata(MetadataBuilder* builder, const std::string& metadata_dir) {
    auto exported = builder->Export();
    if (!exported) {
        LOG(ERROR) << "Unable to export new metadata";
        return false;
    }

    // If there are no more partitions in the metadata, just delete the file.
    auto metadata_file = GetMetadataFile(metadata_dir);
    if (exported->partitions.empty() && android::base::RemoveFileIfExists(metadata_file)) {
        return true;
    }

    unique_fd fd(open(metadata_file.c_str(), O_CREAT | O_RDWR | O_TRUNC | O_CLOEXEC | O_BINARY | O_SYNC, 0644));
    if (fd < 0) {
        LOG(ERROR) << "open failed: " << metadata_file;
        return false;
    }

    if (!WriteToImageFile(fd, *exported.get())) {
        LOG(ERROR) << "Unable to save new metadata";
        return false;
    }

    return true;
}

bool RemoveAllMetadata(const std::string& dir) {
    auto metadata_file = GetMetadataFile(dir);
    std::string err;
    if (!android::base::RemoveFileIfExists(metadata_file, &err)) {
        LOG(ERROR) << "Could not remove metadata file: " << err;
        return false;
    }
    return true;
}

bool FillPartitionExtents(MetadataBuilder* builder, Partition* partition, SplitFiemap* file,
                          uint64_t partition_size) {
    auto block_device = android::base::Basename(GetDevicePathForFile(file));

    uint64_t sectors_needed = partition_size / LP_SECTOR_SIZE;
    for (const auto& extent : file->extents()) {
        if (extent.fe_length % LP_SECTOR_SIZE != 0) {
            LOG(ERROR) << "Extent is not sector-aligned: " << extent.fe_length;
            return false;
        }
        if (extent.fe_physical % LP_SECTOR_SIZE != 0) {
            LOG(ERROR) << "Extent physical sector is not sector-aligned: " << extent.fe_physical;
            return false;
        }

        uint64_t num_sectors =
                std::min(static_cast<uint64_t>(extent.fe_length / LP_SECTOR_SIZE), sectors_needed);
        if (!num_sectors || !sectors_needed) {
            // This should never happen, but we include it just in case. It would
            // indicate that the last filesystem block had multiple extents.
            LOG(WARNING) << "FiemapWriter allocated extra blocks";
            break;
        }

        uint64_t physical_sector = extent.fe_physical / LP_SECTOR_SIZE;
        if (!builder->AddLinearExtent(partition, block_device, num_sectors, physical_sector)) {
            LOG(ERROR) << "Could not add extent to lp metadata";
            return false;
        }

        sectors_needed -= num_sectors;
    }
    return true;
}

bool RemoveImageMetadata(const std::string& metadata_dir, const std::string& partition_name) {
    if (!MetadataExists(metadata_dir)) {
        return true;
    }
    auto metadata = OpenMetadata(metadata_dir);
    if (!metadata) {
        return false;
    }

    PartitionOpener opener;
    auto builder = MetadataBuilder::New(*metadata.get(), &opener);
    if (!builder) {
        return false;
    }
    builder->RemovePartition(partition_name);
    return SaveMetadata(builder.get(), metadata_dir);
}

bool UpdateMetadata(const std::string& metadata_dir, const std::string& partition_name,
                    SplitFiemap* file, uint64_t partition_size, bool readonly) {
    auto builder = OpenOrCreateMetadata(metadata_dir, file);
    if (!builder) {
        return false;
    }
    auto partition = builder->FindPartition(partition_name);
    if (!partition) {
        int attrs = 0;
        if (readonly) attrs |= LP_PARTITION_ATTR_READONLY;

        if ((partition = builder->AddPartition(partition_name, attrs)) == nullptr) {
            LOG(ERROR) << "Could not add partition " << partition_name << " to metadata";
            return false;
        }
    }
    partition->RemoveExtents();

    if (!FillPartitionExtents(builder.get(), partition, file, partition_size)) {
        return false;
    }
    return SaveMetadata(builder.get(), metadata_dir);
}

bool AddAttributes(const std::string& metadata_dir, const std::string& partition_name,
                   uint32_t attributes) {
    auto metadata = OpenMetadata(metadata_dir);
    if (!metadata) {
        return false;
    }
    auto builder = MetadataBuilder::New(*metadata.get());
    if (!builder) {
        return false;
    }
    auto partition = builder->FindPartition(partition_name);
    if (!partition) {
        return false;
    }
    partition->set_attributes(partition->attributes() | attributes);
    return SaveMetadata(builder.get(), metadata_dir);
}

}  // namespace fiemap
}  // namespace android