blob: 828f34e9c88378542930d4a3227883c33b522201 [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
19#include "utils/FileMap.h"
20
21#include "Source.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080022#include "util/Files.h"
23#include "util/Maybe.h"
24#include "util/StringPiece.h"
25#include "util/Util.h"
26
Adam Lesinskia6fe3452015-12-09 15:20:52 -080027namespace aapt {
28namespace io {
29
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030RegularFile::RegularFile(const Source& source) : source_(source) {}
Adam Lesinskia6fe3452015-12-09 15:20:52 -080031
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032std::unique_ptr<IData> RegularFile::OpenAsData() {
33 android::FileMap map;
34 if (Maybe<android::FileMap> map = file::MmapPath(source_.path, nullptr)) {
35 if (map.value().getDataPtr() && map.value().getDataLength() > 0) {
36 return util::make_unique<MmappedData>(std::move(map.value()));
Adam Lesinskia6fe3452015-12-09 15:20:52 -080037 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 return util::make_unique<EmptyData>();
39 }
40 return {};
Adam Lesinskia6fe3452015-12-09 15:20:52 -080041}
42
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043const Source& RegularFile::GetSource() const { return source_; }
44
45FileCollectionIterator::FileCollectionIterator(FileCollection* collection)
46 : current_(collection->files_.begin()), end_(collection->files_.end()) {}
47
48bool FileCollectionIterator::HasNext() { return current_ != end_; }
49
50IFile* FileCollectionIterator::Next() {
51 IFile* result = current_->second.get();
52 ++current_;
53 return result;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080054}
55
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056IFile* FileCollection::InsertFile(const StringPiece& path) {
57 return (files_[path.ToString()] =
58 util::make_unique<RegularFile>(Source(path)))
59 .get();
Adam Lesinskia6fe3452015-12-09 15:20:52 -080060}
61
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062IFile* FileCollection::FindFile(const StringPiece& path) {
63 auto iter = files_.find(path.ToString());
64 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