blob: 1387d2218ed4c7bea82442651cf20073415b3330 [file] [log] [blame]
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001/*
2 * Copyright (C) 2015 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
Adam Lesinskia6fe3452015-12-09 15:20:52 -080017#include "io/FileSystem.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
Adam Lesinskid5083f62017-01-16 15:07:21 -080019#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include "utils/FileMap.h"
21
22#include "Source.h"
Adam Lesinski00451162017-10-03 07:44:08 -070023#include "io/FileStream.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080024#include "util/Files.h"
25#include "util/Maybe.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080026#include "util/Util.h"
27
Adam Lesinski00451162017-10-03 07:44:08 -070028using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080029
Adam Lesinskia6fe3452015-12-09 15:20:52 -080030namespace aapt {
31namespace io {
32
Adam Lesinskice5e56e2016-10-21 17:56:45 -070033RegularFile::RegularFile(const Source& source) : source_(source) {}
Adam Lesinskia6fe3452015-12-09 15:20:52 -080034
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035std::unique_ptr<IData> RegularFile::OpenAsData() {
36 android::FileMap map;
37 if (Maybe<android::FileMap> map = file::MmapPath(source_.path, nullptr)) {
38 if (map.value().getDataPtr() && map.value().getDataLength() > 0) {
39 return util::make_unique<MmappedData>(std::move(map.value()));
Adam Lesinskia6fe3452015-12-09 15:20:52 -080040 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 return util::make_unique<EmptyData>();
42 }
43 return {};
Adam Lesinskia6fe3452015-12-09 15:20:52 -080044}
45
Adam Lesinski00451162017-10-03 07:44:08 -070046std::unique_ptr<io::InputStream> RegularFile::OpenInputStream() {
47 return util::make_unique<FileInputStream>(source_.path);
48}
49
50const Source& RegularFile::GetSource() const {
51 return source_;
52}
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053
54FileCollectionIterator::FileCollectionIterator(FileCollection* collection)
55 : current_(collection->files_.begin()), end_(collection->files_.end()) {}
56
Adam Lesinski00451162017-10-03 07:44:08 -070057bool FileCollectionIterator::HasNext() {
58 return current_ != end_;
59}
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060
61IFile* FileCollectionIterator::Next() {
62 IFile* result = current_->second.get();
63 ++current_;
64 return result;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080065}
66
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067IFile* FileCollection::InsertFile(const StringPiece& path) {
Adam Lesinskid5083f62017-01-16 15:07:21 -080068 return (files_[path.to_string()] = util::make_unique<RegularFile>(Source(path))).get();
Adam Lesinskia6fe3452015-12-09 15:20:52 -080069}
70
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071IFile* FileCollection::FindFile(const StringPiece& path) {
Adam Lesinskid5083f62017-01-16 15:07:21 -080072 auto iter = files_.find(path.to_string());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 if (iter != files_.end()) {
74 return iter->second.get();
75 }
76 return nullptr;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080077}
78
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079std::unique_ptr<IFileCollectionIterator> FileCollection::Iterator() {
80 return util::make_unique<FileCollectionIterator>(this);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080081}
82
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083} // namespace io
84} // namespace aapt