-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathfs_memory_file_system.cpp
More file actions
179 lines (140 loc) · 4.39 KB
/
fs_memory_file_system.cpp
File metadata and controls
179 lines (140 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "fs/impl/memory/fs_memory_file_system.hpp"
#include "fs/impl/memory/fs_memory_file.hpp"
#include "fs/impl/memory/fs_memory_folder.hpp"
#include "exception/ih/exc_internal_error.hpp"
#include <memory>
#include <std_fs>
//------------------------------------------------------------------------------
namespace fs::memory
{
//------------------------------------------------------------------------------
MemoryFileSystem::FilePtr MemoryFileSystem::openFile( const Path & _path ) const
{
const Path path = toAbsolutePath( _path );
FolderPtr dirPtr = getFolder( path.parent_path() );
if( !dirPtr )
{
return nullptr;
}
const Path fileName = _path.filename();
return dirPtr->getFile( fileName.string() );
}
//------------------------------------------------------------------------------
MemoryFileSystem::FilePtr MemoryFileSystem::createFile( const Path & _path )
{
const Path path = toAbsolutePath( _path );
MemoryFolder & folder = ensureFolder( path.parent_path() );
const Path fileName = _path.filename();
return folder.ensureFile( fileName.string() );
}
//------------------------------------------------------------------------------
bool MemoryFileSystem::isExistFile( const Path & _path ) const
{
return openFile( _path ).get() != nullptr;
}
//------------------------------------------------------------------------------
MemoryFileSystem::Path MemoryFileSystem::getCurrentPath() const
{
return "/tmp/";
}
//------------------------------------------------------------------------------
MemoryFileSystem::Path MemoryFileSystem::toAbsolute( const Path & _path ) const
{
return getCurrentPath() / _path;
}
//------------------------------------------------------------------------------
void MemoryFileSystem::forEachItem(
const Path & _dirPath, ItemCallback _callback ) const
{
FolderPtr folderPtr = getFolder( _dirPath );
INTERNAL_CHECK_WARRING( folderPtr != nullptr );
if( folderPtr == nullptr )
{
return;
}
MemoryFolder & folder = *folderPtr;
folder.forEachItem( [&]( std::string_view _name, ItemType _type ) {
Path path = _dirPath / _name;
_callback( path, _type );
} );
}
//------------------------------------------------------------------------------
MemoryFolder & MemoryFileSystem::ensureRoot( const Path & _path )
{
auto pair = m_roots.try_emplace(
_path,
FolderPtr{ std::make_shared< MemoryFolder >( _path.string() ) } );
auto it = pair.first;
FolderPtr & rootPtr = it->second;
INTERNAL_CHECK_ERROR( rootPtr );
return *rootPtr;
}
//------------------------------------------------------------------------------
MemoryFileSystem::FolderPtr
MemoryFileSystem::getRoot( const Path & _path ) const
{
if( auto it = m_roots.find( _path ); it != m_roots.end() )
{
return it->second;
}
return nullptr;
}
//------------------------------------------------------------------------------
MemoryFileSystem::FolderPtr
MemoryFileSystem::getFolder( const Path & _path ) const
{
FolderPtr rootPtr = getRoot( _path.root_directory() );
if( !rootPtr )
{
return nullptr;
}
FolderPtr currentFolder = rootPtr;
const Path pathWitoutRoot = _path.relative_path();
for( const auto & currentName: pathWitoutRoot )
{
if( stdfs::is_dir_filename( currentName ) )
{
continue;
}
INTERNAL_CHECK_ERROR( currentFolder );
auto subDirPtr = currentFolder->getSubFolder( currentName.string() );
currentFolder = subDirPtr;
if( !currentFolder )
{
break;
}
}
return currentFolder;
}
//------------------------------------------------------------------------------
MemoryFolder & MemoryFileSystem::ensureFolder( const Path & _path )
{
Path pathWitoutRoot = _path.relative_path();
MemoryFolder & root = ensureRoot( _path.root_directory() );
MemoryFolder * currentFolder = &root;
for( const auto & currentName: pathWitoutRoot )
{
const std::string folderName = currentName.string();
if( folderName.empty() )
{
continue;
}
INTERNAL_CHECK_ERROR( currentFolder );
auto subDirPtr = currentFolder->ensureSubFolder( currentName.string() );
currentFolder = subDirPtr.get();
}
INTERNAL_CHECK_ERROR( currentFolder );
return *currentFolder;
}
//------------------------------------------------------------------------------
MemoryFileSystem::Path
MemoryFileSystem::toAbsolutePath( const Path & _path ) const
{
if( !_path.is_absolute() )
{
return getCurrentPath() / _path;
}
return _path;
}
//------------------------------------------------------------------------------
}