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<android::InputStream> OpenInputStream() override;
34   const android::Source& GetSource() const override;
35   bool GetModificationTime(struct tm* buf) const override;
36 
37  private:
38   DISALLOW_COPY_AND_ASSIGN(RegularFile);
39 
40   android::Source source_;
41 };
42 
43 class FileCollection;
44 
45 class FileCollectionIterator : public IFileCollectionIterator {
46  public:
47   explicit FileCollectionIterator(FileCollection* collection);
48 
49   bool HasNext() override;
50   io::IFile* Next() override;
51 
52  private:
53   DISALLOW_COPY_AND_ASSIGN(FileCollectionIterator);
54 
55   std::map<std::string, std::unique_ptr<IFile>>::const_iterator current_, end_;
56 };
57 
58 // An IFileCollection representing the file system.
59 class FileCollection : public IFileCollection {
60  public:
61   FileCollection() = default;
62 
63   /** Creates a file collection containing all files contained in the specified root directory. */
64   static std::unique_ptr<FileCollection> Create(android::StringPiece path, std::string* outError);
65 
66   // Adds a file located at path. Returns the IFile representation of that file.
67   IFile* InsertFile(android::StringPiece path);
68   IFile* FindFile(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>, std::less<>> files_;
78 };
79 
80 }  // namespace io
81 }  // namespace aapt
82 
83 #endif  // AAPT_IO_FILESYSTEM_H
84