-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinuxDirectory.cpp
256 lines (220 loc) · 4.77 KB
/
LinuxDirectory.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/** This file is part of dirCompare
*
* Copyright 2017 Thomas Erbesdobler <t.erbesdobler@team103.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string>
#include <vector>
#include <memory>
#include <algorithm>
#include <exception>
#include "platform.h"
#include "SystemParameters.h"
#include "errno_exception.h"
#include "gp_exception.h"
#include "log.h"
#include "Item.h"
#include "Directory.h"
#include "ItemFactory.h"
#include "LinuxItemFactory.h"
#include "LinuxDirectory.h"
#include "LinuxFile.h"
#include "LinuxFileInfo.h"
extern "C"
{
#include <stdlib.h>
#include <stddef.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
}
LinuxDirectory::LinuxDirectory(const string& path, shared_ptr<SystemParameters> sp)
: Directory(path, sp), dir(nullptr)
{
init();
}
LinuxDirectory::LinuxDirectory(
const string& path,
shared_ptr<SystemParameters> sp,
shared_ptr<const Directory> dir)
: Directory(path, sp, dir), dir(nullptr)
{
init();
}
void LinuxDirectory::init()
{
int fd;
if (directory)
{
fd = openat(
dynamic_pointer_cast<const LinuxDirectory>(directory)->getFd(),
path.c_str(),
O_RDONLY | O_DIRECTORY);
}
else
{
fd = open(path.c_str(), O_RDONLY | O_DIRECTORY);
}
if (fd < 0)
{
throw errno_exception(errno);
}
/* create DIR stream */
dir = fdopendir(fd);
if (!dir)
{
if (close(fd) < 0)
{
throw errno_exception(
errno,
"failed to close fd, "
"closing triggered by error in initialization of directory: ");
}
throw errno_exception(errno, "failed to create DIR stream: ");
}
if (!getFileInfo().isDirectory())
{
if (closedir(dir) < 0)
{
throw errno_exception(
errno,
"Failed to close directory (triggerd by initialization error): ");
}
throw gp_exception("Is not a directory.");
}
}
LinuxDirectory::~LinuxDirectory()
{
if (dir)
{
if (closedir(dir) < 0)
{
throw errno_exception(errno);
}
}
}
LinuxFileInfo LinuxDirectory::getFileInfo() const
{
struct stat st;
if (fstat(dirfd(dir), &st) < 0)
{
throw errno_exception(errno);
}
return LinuxFileInfo(&st);
}
vector<shared_ptr<Item>> LinuxDirectory::getItems() const
{
vector <shared_ptr<Item>> v;
rewinddir(dir);
auto name_max = fpathconf(dirfd(dir), _PC_PATH_MAX);
if (name_max < 0)
{
throw errno_exception(
errno,
"failed to get maximum allowed file name length: ");
}
struct dirent *entry = static_cast<struct dirent*>(
malloc(offsetof(struct dirent, d_name) + name_max + 1));
if (!entry)
{
throw gp_exception("failed to allocate memory for directory entry");
}
struct dirent *result;
shared_ptr<ItemFactory> itemFactory = createItemFactory(sp);
if (dynamic_pointer_cast<LinuxItemFactory>(itemFactory) == nullptr)
{
throw gp_exception("Not a createItemFactory has not produced a "
"LinuxItemFactory in LinuxDirectory");
}
for (;;)
{
if (readdir_r(dir, entry, &result) < 0)
{
free(entry);
entry = nullptr;
throw errno_exception(errno, "reading directory " + path + ": ");
}
if (result)
{
/* exclude special files */
if (string(result->d_name) == ".." || string(result->d_name) == ".")
{
continue;
}
if (result->d_type == DT_DIR)
{
v.push_back(itemFactory->createDirectory(
result->d_name,
shared_from_this()));
}
else
{
shared_ptr<LinuxFile> f;
try
{
f = static_pointer_cast<LinuxFile>(
itemFactory->createFile(
result->d_name,
shared_from_this()));
}
catch (exception& e)
{
logIndentation(sp->getLog(), shared_from_this());
*(sp->getLog()) << "Directory " << path <<
": Error: " << e.what() << endl;
continue;
}
if (result->d_type == DT_UNKNOWN)
{
if (f->getFileInfo().isDirectory())
{
v.push_back(itemFactory->createDirectory(
result->d_name,
shared_from_this()));
}
}
else
{
v.push_back(f);
}
}
}
else
{
break;
}
}
free(entry);
entry = nullptr;
sort(v.begin(), v.end(),[](shared_ptr<Item> i1, shared_ptr<Item> i2){
string p1 = i1->getPath();
string p2 = i2->getPath();
if (p1.compare(p2) < 0)
{
return true;
}
else
{
return false;
}
});
return v;
}
int LinuxDirectory::getFd() const
{
return dirfd(dir);
}