1a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski/* 2a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski * Copyright (C) 2015 The Android Open Source Project 3a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski * 4a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski * Licensed under the Apache License, Version 2.0 (the "License"); 5a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski * you may not use this file except in compliance with the License. 6a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski * You may obtain a copy of the License at 7a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski * 8a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski * http://www.apache.org/licenses/LICENSE-2.0 9a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski * 10a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski * Unless required by applicable law or agreed to in writing, software 11a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski * distributed under the License is distributed on an "AS IS" BASIS, 12a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski * See the License for the specific language governing permissions and 14a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski * limitations under the License. 15a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski */ 16a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski 17a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski#include "io/FileSystem.h" 18ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 19d5083f6f6b9bc76bbe64052bcec639eee752a321Adam Lesinski#include "androidfw/StringPiece.h" 20ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski#include "utils/FileMap.h" 21ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 22ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski#include "Source.h" 23004511660671511ae88e0e837a6f92db28eadaefAdam Lesinski#include "io/FileStream.h" 24a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski#include "util/Files.h" 25a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski#include "util/Maybe.h" 26a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski#include "util/Util.h" 27a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski 28004511660671511ae88e0e837a6f92db28eadaefAdam Lesinskiusing ::android::StringPiece; 29d5083f6f6b9bc76bbe64052bcec639eee752a321Adam Lesinski 30a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinskinamespace aapt { 31a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinskinamespace io { 32a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski 33ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam LesinskiRegularFile::RegularFile(const Source& source) : source_(source) {} 34a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski 35ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinskistd::unique_ptr<IData> RegularFile::OpenAsData() { 36ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski android::FileMap map; 37ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski if (Maybe<android::FileMap> map = file::MmapPath(source_.path, nullptr)) { 38ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski if (map.value().getDataPtr() && map.value().getDataLength() > 0) { 39ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski return util::make_unique<MmappedData>(std::move(map.value())); 40a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski } 41ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski return util::make_unique<EmptyData>(); 42ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski } 43ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski return {}; 44a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski} 45a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski 46004511660671511ae88e0e837a6f92db28eadaefAdam Lesinskistd::unique_ptr<io::InputStream> RegularFile::OpenInputStream() { 47004511660671511ae88e0e837a6f92db28eadaefAdam Lesinski return util::make_unique<FileInputStream>(source_.path); 48004511660671511ae88e0e837a6f92db28eadaefAdam Lesinski} 49004511660671511ae88e0e837a6f92db28eadaefAdam Lesinski 50004511660671511ae88e0e837a6f92db28eadaefAdam Lesinskiconst Source& RegularFile::GetSource() const { 51004511660671511ae88e0e837a6f92db28eadaefAdam Lesinski return source_; 52004511660671511ae88e0e837a6f92db28eadaefAdam Lesinski} 53a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski 54ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam LesinskiFileCollectionIterator::FileCollectionIterator(FileCollection* collection) 55ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski : current_(collection->files_.begin()), end_(collection->files_.end()) {} 56a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski 57004511660671511ae88e0e837a6f92db28eadaefAdam Lesinskibool FileCollectionIterator::HasNext() { 58004511660671511ae88e0e837a6f92db28eadaefAdam Lesinski return current_ != end_; 59004511660671511ae88e0e837a6f92db28eadaefAdam Lesinski} 60a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski 61ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam LesinskiIFile* FileCollectionIterator::Next() { 62ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski IFile* result = current_->second.get(); 63ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski ++current_; 64ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski return result; 65a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski} 66a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski 67ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam LesinskiIFile* FileCollection::InsertFile(const StringPiece& path) { 68d5083f6f6b9bc76bbe64052bcec639eee752a321Adam Lesinski return (files_[path.to_string()] = util::make_unique<RegularFile>(Source(path))).get(); 69a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski} 70a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski 71ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam LesinskiIFile* FileCollection::FindFile(const StringPiece& path) { 72d5083f6f6b9bc76bbe64052bcec639eee752a321Adam Lesinski auto iter = files_.find(path.to_string()); 73ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski if (iter != files_.end()) { 74ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski return iter->second.get(); 75ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski } 76ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski return nullptr; 77a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski} 78a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski 79ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinskistd::unique_ptr<IFileCollectionIterator> FileCollection::Iterator() { 80ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski return util::make_unique<FileCollectionIterator>(this); 81a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski} 82a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski 83ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski} // namespace io 84ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski} // namespace aapt 85