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 Source& source);
31 
32   std::unique_ptr<IData> OpenAsData() override;
33   std::unique_ptr<io::InputStream> OpenInputStream() override;
34   const Source& GetSource() const override;
35 
36  private:
37   DISALLOW_COPY_AND_ASSIGN(RegularFile);
38 
39   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(const android::StringPiece& path,
64                                                 std::string* outError);
65 
66   // Adds a file located at path. Returns the IFile representation of that file.
67   IFile* InsertFile(const android::StringPiece& path);
68   IFile* FindFile(const android::StringPiece& path) override;
69   std::unique_ptr<IFileCollectionIterator> Iterator() override;
70   char GetDirSeparator() override;
71 
72  private:
73   DISALLOW_COPY_AND_ASSIGN(FileCollection);
74 
75   friend class FileCollectionIterator;
76 
77   std::map<std::string, std::unique_ptr<IFile>> files_;
78 };
79 
80 }  // namespace io
81 }  // namespace aapt
82 
83 #endif  // AAPT_IO_FILESYSTEM_H
84