blob: 027cbd041fa647c10b1fbb7e1af5dec3a091fc43 [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 Lesinskia6fe3452015-12-09 15:20:52 -080023#include "util/Files.h"
24#include "util/Maybe.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080025#include "util/Util.h"
26
Adam Lesinskid5083f62017-01-16 15:07:21 -080027using android::StringPiece;
28
Adam Lesinskia6fe3452015-12-09 15:20:52 -080029namespace aapt {
30namespace io {
31
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032RegularFile::RegularFile(const Source& source) : source_(source) {}
Adam Lesinskia6fe3452015-12-09 15:20:52 -080033
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034std::unique_ptr<IData> RegularFile::OpenAsData() {
35 android::FileMap map;
36 if (Maybe<android::FileMap> map = file::MmapPath(source_.path, nullptr)) {
37 if (map.value().getDataPtr() && map.value().getDataLength() > 0) {
38 return util::make_unique<MmappedData>(std::move(map.value()));
Adam Lesinskia6fe3452015-12-09 15:20:52 -080039 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 return util::make_unique<EmptyData>();
41 }
42 return {};
Adam Lesinskia6fe3452015-12-09 15:20:52 -080043}
44
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045const Source& RegularFile::GetSource() const { return source_; }
46
47FileCollectionIterator::FileCollectionIterator(FileCollection* collection)
48 : current_(collection->files_.begin()), end_(collection->files_.end()) {}
49
50bool FileCollectionIterator::HasNext() { return current_ != end_; }
51
52IFile* FileCollectionIterator::Next() {
53 IFile* result = current_->second.get();
54 ++current_;
55 return result;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080056}
57
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058IFile* FileCollection::InsertFile(const StringPiece& path) {
Adam Lesinskid5083f62017-01-16 15:07:21 -080059 return (files_[path.to_string()] = util::make_unique<RegularFile>(Source(path))).get();
Adam Lesinskia6fe3452015-12-09 15:20:52 -080060}
61
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062IFile* FileCollection::FindFile(const StringPiece& path) {
Adam Lesinskid5083f62017-01-16 15:07:21 -080063 auto iter = files_.find(path.to_string());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 if (iter != files_.end()) {
65 return iter->second.get();
66 }
67 return nullptr;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080068}
69
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070std::unique_ptr<IFileCollectionIterator> FileCollection::Iterator() {
71 return util::make_unique<FileCollectionIterator>(this);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080072}
73
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074} // namespace io
75} // namespace aapt