| 
									
										
										
										
											2015-05-24 06:55:12 +02:00
										 |  |  | // Copyright 2008 Dolphin Emulator Project
 | 
					
						
							| 
									
										
										
										
											2015-05-18 01:08:10 +02:00
										 |  |  | // Licensed under GPLv2+
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-10 13:54:46 -05:00
										 |  |  | #pragma once
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-08 19:59:33 +02:00
										 |  |  | #include <iterator>
 | 
					
						
							| 
									
										
										
										
											2015-12-06 23:15:51 -05:00
										 |  |  | #include <memory>
 | 
					
						
							| 
									
										
										
										
											2017-06-04 10:33:14 +02:00
										 |  |  | #include <optional>
 | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | #include <string>
 | 
					
						
							| 
									
										
										
										
											2014-02-21 01:47:53 +01:00
										 |  |  | #include <vector>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Common/CommonTypes.h"
 | 
					
						
							| 
									
										
										
										
											2015-06-13 12:51:24 +02:00
										 |  |  | #include "DiscIO/Volume.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace DiscIO | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | // file info of an FST entry
 | 
					
						
							| 
									
										
										
										
											2015-07-28 16:56:25 +02:00
										 |  |  | class FileInfo | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-08 19:59:33 +02:00
										 |  |  |   friend class const_iterator; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 16:56:25 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2015-08-08 19:59:33 +02:00
										 |  |  |   class const_iterator final | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |   public: | 
					
						
							|  |  |  |     using iterator_category = std::forward_iterator_tag; | 
					
						
							|  |  |  |     using value_type = const FileInfo; | 
					
						
							|  |  |  |     using difference_type = std::ptrdiff_t; | 
					
						
							|  |  |  |     using pointer = value_type*; | 
					
						
							|  |  |  |     using reference = value_type&; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const_iterator() : m_file_info(nullptr) {} | 
					
						
							|  |  |  |     const_iterator(std::unique_ptr<FileInfo> file_info) : m_file_info(std::move(file_info)) {} | 
					
						
							|  |  |  |     const_iterator(const const_iterator& it) : m_file_info(it.m_file_info->clone()) {} | 
					
						
							| 
									
										
										
										
											2017-08-05 12:28:24 -04:00
										 |  |  |     const_iterator(const_iterator&& it) noexcept : m_file_info(std::move(it.m_file_info)) {} | 
					
						
							| 
									
										
										
										
											2017-06-13 23:05:30 +02:00
										 |  |  |     ~const_iterator() = default; | 
					
						
							| 
									
										
										
										
											2015-08-08 19:59:33 +02:00
										 |  |  |     const_iterator& operator=(const const_iterator& it) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       m_file_info = it.m_file_info ? it.m_file_info->clone() : nullptr; | 
					
						
							|  |  |  |       return *this; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-08-05 12:28:24 -04:00
										 |  |  |     const_iterator& operator=(const_iterator&& it) noexcept | 
					
						
							| 
									
										
										
										
											2015-08-08 19:59:33 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |       m_file_info = std::move(it.m_file_info); | 
					
						
							|  |  |  |       return *this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     const_iterator& operator++() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       ++*m_file_info; | 
					
						
							|  |  |  |       return *this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     const_iterator operator++(int) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       const_iterator old = *this; | 
					
						
							|  |  |  |       ++*m_file_info; | 
					
						
							|  |  |  |       return old; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     bool operator==(const const_iterator& it) const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       return m_file_info ? (it.m_file_info && *m_file_info == *it.m_file_info) : (!it.m_file_info); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     bool operator!=(const const_iterator& it) const { return !operator==(it); } | 
					
						
							|  |  |  |     // Incrementing or destroying an iterator will invalidate its returned references and
 | 
					
						
							|  |  |  |     // pointers, but will not invalidate copies of the iterator or file info object.
 | 
					
						
							|  |  |  |     const FileInfo& operator*() const { return *m_file_info.get(); } | 
					
						
							|  |  |  |     const FileInfo* operator->() const { return m_file_info.get(); } | 
					
						
							| 
									
										
										
										
											2018-04-12 14:18:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-08 19:59:33 +02:00
										 |  |  |   private: | 
					
						
							|  |  |  |     std::unique_ptr<FileInfo> m_file_info; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 16:56:25 +02:00
										 |  |  |   virtual ~FileInfo(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-08 19:59:33 +02:00
										 |  |  |   bool operator==(const FileInfo& other) const { return GetAddress() == other.GetAddress(); } | 
					
						
							|  |  |  |   bool operator!=(const FileInfo& other) const { return !operator==(other); } | 
					
						
							|  |  |  |   virtual std::unique_ptr<FileInfo> clone() const = 0; | 
					
						
							|  |  |  |   virtual const_iterator cbegin() const { return begin(); } | 
					
						
							|  |  |  |   virtual const_iterator cend() const { return end(); } | 
					
						
							|  |  |  |   virtual const_iterator begin() const = 0; | 
					
						
							|  |  |  |   virtual const_iterator end() const = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // The offset of a file on the disc (inside the partition, if there is one).
 | 
					
						
							|  |  |  |   // Not guaranteed to return a meaningful value for directories.
 | 
					
						
							| 
									
										
										
										
											2015-07-28 16:56:25 +02:00
										 |  |  |   virtual u64 GetOffset() const = 0; | 
					
						
							| 
									
										
										
										
											2015-08-08 19:59:33 +02:00
										 |  |  |   // The size of a file.
 | 
					
						
							|  |  |  |   // Not guaranteed to return a meaningful value for directories.
 | 
					
						
							| 
									
										
										
										
											2015-07-30 22:18:20 +02:00
										 |  |  |   virtual u32 GetSize() const = 0; | 
					
						
							| 
									
										
										
										
											2015-07-28 16:56:25 +02:00
										 |  |  |   virtual bool IsDirectory() const = 0; | 
					
						
							| 
									
										
										
										
											2015-08-08 19:59:33 +02:00
										 |  |  |   // The number of files and directories in a directory, including those in subdirectories.
 | 
					
						
							|  |  |  |   // Not guaranteed to return a meaningful value for files.
 | 
					
						
							|  |  |  |   virtual u32 GetTotalChildren() const = 0; | 
					
						
							| 
									
										
										
										
											2015-07-30 22:18:20 +02:00
										 |  |  |   virtual std::string GetName() const = 0; | 
					
						
							| 
									
										
										
										
											2015-08-08 19:59:33 +02:00
										 |  |  |   // GetPath will find the parents of the current object and call GetName on them,
 | 
					
						
							|  |  |  |   // so it's slower than other functions. If you're traversing through folders
 | 
					
						
							|  |  |  |   // to get a file and its path, building the path while traversing is faster.
 | 
					
						
							|  |  |  |   virtual std::string GetPath() const = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  |   // Only used for comparisons with other file info objects
 | 
					
						
							|  |  |  |   virtual uintptr_t GetAddress() const = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Called by iterators
 | 
					
						
							|  |  |  |   virtual FileInfo& operator++() = 0; | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-06 11:49:01 +02:00
										 |  |  | class FileSystem | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2017-06-06 11:49:01 +02:00
										 |  |  |   virtual ~FileSystem(); | 
					
						
							| 
									
										
										
										
											2015-08-08 19:59:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-02 18:16:56 +02:00
										 |  |  |   // If IsValid is false, GetRoot must not be called.
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   virtual bool IsValid() const = 0; | 
					
						
							| 
									
										
										
										
											2015-08-08 19:59:33 +02:00
										 |  |  |   // The object returned by GetRoot and all objects created from it
 | 
					
						
							|  |  |  |   // are only valid for as long as the file system object is valid.
 | 
					
						
							|  |  |  |   virtual const FileInfo& GetRoot() const = 0; | 
					
						
							|  |  |  |   // Returns nullptr if not found
 | 
					
						
							|  |  |  |   virtual std::unique_ptr<FileInfo> FindFileInfo(const std::string& path) const = 0; | 
					
						
							|  |  |  |   // Returns nullptr if not found
 | 
					
						
							|  |  |  |   virtual std::unique_ptr<FileInfo> FindFileInfo(u64 disc_offset) const = 0; | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-02 18:16:56 +02:00
										 |  |  | // Calling Volume::GetFileSystem instead of manually constructing a filesystem is recommended,
 | 
					
						
							|  |  |  | // because it will check IsValid for you, will automatically pick the right type of filesystem,
 | 
					
						
							|  |  |  | // and will cache the filesystem in case it's needed again later.
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | }  // namespace
 |