-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileSystem.cpp
195 lines (169 loc) · 4.69 KB
/
FileSystem.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "pch.h"
#ifdef USE_FILESYSTEME
#include "FileSystem.h"
#ifdef __linux__
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/vfs.h>
#include <sys/statfs.h>
#endif
namespace FileSystem
{
void directory_entry::InitFileStatus(const CFileStatusSys& fileStatus)
{
//! convert from OS native format.
#ifdef _WIN32
m_Size = fileStatus.nFileSizeLow;
if (fileStatus.nFileSizeHigh != 0)
{
m_Size |= ((FILE_SIZE_t)fileStatus.nFileSizeHigh) << 32;
}
m_Attributes = (FILEATTR_MASK_t)fileStatus.dwFileAttributes; // truncated!
#elif defined(__linux__)
// http://linux.die.net/man/2/stat
// hidden file start with .
m_Size = fileStatus.st_size;
m_Attributes = 0; // check the read,write,execute bits?
if (S_ISREG(fileStatus.st_mode))
{
m_Attributes |= FILEATTR_Normal;
}
else if (S_ISDIR(fileStatus.st_mode))
{
m_Attributes |= FILEATTR_Directory;
}
else if (S_ISLNK(fileStatus.st_mode))
{
m_Attributes |= FILEATTR_Link;
}
else // S_ISBLK, S_ISSOCK, S_ISCHR, S_ISFIFO
{
m_Attributes |= FILEATTR_Volume; // device of some sort.
}
#endif
}
directory_iterator::directory_iterator(const path& _Path)
: m_sDirPath(_Path)
#ifdef _WIN32
, m_hContext(INVALID_HANDLE_VALUE)
#elif defined(__linux__)
, m_bReadStats(true)
, m_hContext(nullptr)
#endif
{
FindFileOpen();
FindFileNext(true);
}
bool directory_iterator::isContextOpen() const
{
#ifdef _WIN32
if (m_hContext == INVALID_HANDLE_VALUE)
return false;
#elif defined(__linux__)
if (m_hContext == nullptr)
return false;
#endif
return true;
}
void directory_iterator::CloseContext()
{
if (!isContextOpen())
return;
#ifdef _WIN32
::FindClose(m_hContext);
m_hContext = INVALID_HANDLE_VALUE;
#elif defined(__linux__)
::closedir(m_hContext);
m_hContext = nullptr;
#endif
m_FileEntry.m_sFileName = "";
m_FileEntry.m_sFilePath = "";
}
bool directory_iterator::FindFileNext(bool bFirst)
{
//! Read the next file in the directory list.
//! ASSUME cFileFind::FindFile() was called.
//! @return
//! HRESULT_WIN32_C(ERROR_NO_MORE_ITEMS) = no more files
//! @note UNICODE files are converted to '?' chars if calling the non UNICODE version.
if (!isContextOpen())
{
return false; // HRESULT_WIN32_C(ERROR_NO_MORE_ITEMS);
}
#ifdef _WIN32
if (!bFirst)
{
if (!_FN(::FindNextFile)(m_hContext, &m_FindInfo))
{
CloseContext();
return false; // HRESULT_WIN32_C(ERROR_NO_MORE_ITEMS);
}
}
m_FileEntry.m_sFileName = m_FindInfo.cFileName; // How should we deal with UNICODE names ?
if (m_FileEntry.isDots()) // ignore the . and .. that old systems can give us.
{
return FindFileNext(false);
}
m_FileEntry.m_sFilePath = m_sDirPath;
m_FileEntry.m_sFilePath.append(m_FileEntry.m_sFileName.c_str());
m_FileEntry.InitFileStatus(m_FindInfo);
#elif defined(__linux__)
(void)bFirst; // silence UNREFERENCED_PARAMETER() warningssfi /tmp
struct dirent* pFileInfo = ::readdir(m_hContext);
if (pFileInfo == nullptr)
{
CloseContext();
return false; // HRESULT_WIN32_C(ERROR_NO_MORE_ITEMS);
}
m_FileEntry.m_sFileName = pFileInfo->d_name;
if (m_FileEntry.isDots())
{
return FindFileNext(false);
}
m_FileEntry.m_sFilePath = m_sDirPath;
m_FileEntry.m_sFilePath.append(m_FileEntry.m_sFileName.c_str());
if (m_bReadStats) // some dirs don't have stat() ability. e.g. "/proc"
{
// dirent() doesn't have size stuff...it's in stat()
// use ::lstat(m_FileEntry.m_sFilePath, &fileStatus) to follow links.
CFileStatusSys fileStatus;
int iRet = ::stat(m_FileEntry.m_sFilePath.c_str(), &fileStatus);
if (iRet != 0)
{
return false; // HResult::GetLastDef(HRESULT_WIN32_C(ERROR_FILE_NOT_FOUND));
}
m_FileEntry.InitFileStatus(fileStatus);
}
#endif
m_FileEntry.UpdateLinuxHidden(m_FileEntry.m_sFileName);
return true; // S_OK
}
bool directory_iterator::FindFileOpen()
{
//! start a sequential read of the files in a list of possible matches.
//! @return
//! false = no files. HRESULT_WIN32_C(ERROR_NO_MORE_ITEMS)
CloseContext();
assert(!isContextOpen());
#ifdef _WIN32
// in _WIN32 wildcard filter is built in.
std::memset(&m_FindInfo, 0, sizeof(m_FindInfo));
m_FindInfo.dwFileAttributes = FILE_ATTRIBUTE_NORMAL; // docs say this is not needed
path sFilePath = m_sDirPath;
sFilePath.append("*");
m_hContext = _FN(::FindFirstFile)(sFilePath.c_str(), &m_FindInfo);
#elif defined(__linux__)
// Need to strip out the *.EXT part. just need the dir name here.
m_hContext = ::opendir(m_sDirPath.c_str());
#endif
if (!isContextOpen())
{
return false; // HResult::GetLastDef(HRESULT_WIN32_C(ERROR_PATH_NOT_FOUND));
}
return true; // S_OK
}
}
#endif