1 /*
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 
17 #ifndef AAPT_IO_FILESYSTEM_H
18 #define AAPT_IO_FILESYSTEM_H
19 
20 #include <map>
21 
22 #include "io/File.h"
23 
24 namespace aapt {
25 namespace io {
26 
27 // A regular file from the file system. Uses mmap to open the data.
28 class RegularFile : public IFile {
29  public:
30   explicit RegularFile(const android::Source& source);
31 
32   std::unique_ptr<IData> OpenAsData() override;
33   std::unique_ptr<io::InputStream> OpenInputStream() override;
34   const android::Source& GetSource() const override;
35 
36  private:
37   DISALLOW_COPY_AND_ASSIGN(RegularFile);
38 
39   android::Source source_;
40 };
41 
42 class FileCollection;
43 
44 class FileCollectionIterator : public IFileCollectionIterator {
45  public:
46   explicit FileCollectionIterator(FileCollection* collection);
47 
48   bool HasNext() override;
49   io::IFile* Next() override;
50 
51  private:
52   DISALLOW_COPY_AND_ASSIGN(FileCollectionIterator);
53 
54   std::map<std::string, std::unique_ptr<IFile>>::const_iterator current_, end_;
55 };
56 
57 // An IFileCollection representing the file system.
58 class FileCollection : public IFileCollection {
59  public:
60   FileCollection() = default;
61 
62   /** Creates a file collection containing all files contained in the specified root directory. */
63   static std::unique_ptr<FileCollection> Create(android::StringPiece path, std::string* outError);
64 
65   // Adds a file located at path. Returns the IFile representation of that file.
66   IFile* InsertFile(android::StringPiece path);
67   IFile* FindFile(android::StringPiece path) override;
68   std::unique_ptr<IFileCollectionIterator> Iterator() override;
69   char GetDirSeparator() override;
70 
71  private:
72   DISALLOW_COPY_AND_ASSIGN(FileCollection);
73 
74   friend class FileCollectionIterator;
75 
76   std::map<std::string, std::unique_ptr<IFile>, std::less<>> files_;
77 };
78 
79 }  // namespace io
80 }  // namespace aapt
81 
82 #endif  // AAPT_IO_FILESYSTEM_H
83