blob: f975196b9cfa71fa169f42d8ba31fd75a6dabcd4 [file] [log] [blame]
Adam Lesinskia40e9722015-11-24 19:11:46 -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
17#ifndef AAPT_IO_FILESYSTEM_H
18#define AAPT_IO_FILESYSTEM_H
19
Adam Lesinskia6fe3452015-12-09 15:20:52 -080020#include <map>
Adam Lesinskia40e9722015-11-24 19:11:46 -080021
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "io/File.h"
23
Adam Lesinskia40e9722015-11-24 19:11:46 -080024namespace aapt {
25namespace io {
26
Adam Lesinski00451162017-10-03 07:44:08 -070027// A regular file from the file system. Uses mmap to open the data.
Adam Lesinskia40e9722015-11-24 19:11:46 -080028class RegularFile : public IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070029 public:
Jeremy Meyer56f36e82022-05-20 20:35:42 +000030 explicit RegularFile(const android::Source& source);
Adam Lesinskia40e9722015-11-24 19:11:46 -080031
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032 std::unique_ptr<IData> OpenAsData() override;
Adam Lesinski00451162017-10-03 07:44:08 -070033 std::unique_ptr<io::InputStream> OpenInputStream() override;
Jeremy Meyer56f36e82022-05-20 20:35:42 +000034 const android::Source& GetSource() const override;
Mark Punzalane5671592023-09-02 00:00:30 +000035 bool GetModificationTime(struct tm* buf) const override;
Adam Lesinskia40e9722015-11-24 19:11:46 -080036
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 private:
Adam Lesinski00451162017-10-03 07:44:08 -070038 DISALLOW_COPY_AND_ASSIGN(RegularFile);
39
Jeremy Meyer56f36e82022-05-20 20:35:42 +000040 android::Source source_;
Adam Lesinskia40e9722015-11-24 19:11:46 -080041};
42
Adam Lesinskia6fe3452015-12-09 15:20:52 -080043class FileCollection;
44
45class FileCollectionIterator : public IFileCollectionIterator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 public:
47 explicit FileCollectionIterator(FileCollection* collection);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080048
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 bool HasNext() override;
50 io::IFile* Next() override;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080051
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 private:
Adam Lesinski00451162017-10-03 07:44:08 -070053 DISALLOW_COPY_AND_ASSIGN(FileCollectionIterator);
54
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 std::map<std::string, std::unique_ptr<IFile>>::const_iterator current_, end_;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080056};
57
Adam Lesinski00451162017-10-03 07:44:08 -070058// An IFileCollection representing the file system.
Adam Lesinskia40e9722015-11-24 19:11:46 -080059class FileCollection : public IFileCollection {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 public:
Adam Lesinski00451162017-10-03 07:44:08 -070061 FileCollection() = default;
62
Ryan Mitchellf3649d62018-08-02 16:16:45 -070063 /** Creates a file collection containing all files contained in the specified root directory. */
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070064 static std::unique_ptr<FileCollection> Create(android::StringPiece path, std::string* outError);
Ryan Mitchellf3649d62018-08-02 16:16:45 -070065
Adam Lesinski00451162017-10-03 07:44:08 -070066 // Adds a file located at path. Returns the IFile representation of that file.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070067 IFile* InsertFile(android::StringPiece path);
68 IFile* FindFile(android::StringPiece path) override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 std::unique_ptr<IFileCollectionIterator> Iterator() override;
Ryan Mitchell0ce89732018-10-03 09:20:57 -070070 char GetDirSeparator() override;
Adam Lesinskia40e9722015-11-24 19:11:46 -080071
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 private:
Adam Lesinski00451162017-10-03 07:44:08 -070073 DISALLOW_COPY_AND_ASSIGN(FileCollection);
74
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 friend class FileCollectionIterator;
Adam Lesinski00451162017-10-03 07:44:08 -070076
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070077 std::map<std::string, std::unique_ptr<IFile>, std::less<>> files_;
Adam Lesinskia40e9722015-11-24 19:11:46 -080078};
79
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080} // namespace io
81} // namespace aapt
Adam Lesinskia40e9722015-11-24 19:11:46 -080082
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083#endif // AAPT_IO_FILESYSTEM_H