forked from artyom-beilis/cppcms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdir.h
More file actions
189 lines (161 loc) · 3.62 KB
/
Copy pathdir.h
File metadata and controls
189 lines (161 loc) · 3.62 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
180
181
182
183
184
185
186
187
188
189
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef CPPCMS_IMPL_DIR_H
#define CPPCMS_IMPL_DIR_H
#if defined(__sun)
#define _POSIX_PTHREAD_SEMANTICS
#endif
#include <cppcms/defs.h>
#ifdef CPPCMS_WIN_NATIVE
# include <booster/locale/encoding.h>
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <windows.h>
#else
# include <stdlib.h>
# include <sys/types.h>
# include <dirent.h>
# include <unistd.h>
#endif
#include <booster/noncopyable.h>
#include <string>
#include <assert.h>
namespace cppcms {
namespace impl {
class directory : public booster::noncopyable {
public:
directory();
~directory();
bool open(std::string const &dir_name);
void close();
bool next();
char const *name();
private:
#ifdef CPPCMS_WIN_NATIVE
HANDLE dir_;
_WIN32_FIND_DATAW entry_;
std::string utf_name_;
#else // POSIX
DIR *dir_;
#if defined(__linux) || defined(__FreeBSD__) || defined(__APPLE__)
struct dirent de_;
#else
#define CPPCMS_DIR_ALLOCATE_DE
struct dirent *de_;
#endif
struct dirent *entry_p_;
#endif
};
inline directory::~directory()
{
close();
}
#ifdef CPPCMS_WIN_NATIVE
inline directory::directory() : dir_(INVALID_HANDLE_VALUE)
{
}
inline void directory::close()
{
if(dir_!=INVALID_HANDLE_VALUE) {
FindClose(dir_);
dir_ = INVALID_HANDLE_VALUE;
}
}
inline bool directory::open(std::string const &name)
{
std::wstring search = booster::locale::conv::utf_to_utf<wchar_t>(name.c_str()) +L"/*";
dir_ = FindFirstFileW(search.c_str(),&entry_);
if(dir_ == INVALID_HANDLE_VALUE) {
if(GetLastError() == ERROR_FILE_NOT_FOUND)
return true;
else
return false;
}
return true;
}
inline bool directory::next()
{
if(dir_ == INVALID_HANDLE_VALUE)
return false;
utf_name_ = booster::locale::conv::utf_to_utf<char>(entry_.cFileName);
if(!FindNextFileW(dir_,&entry_)) {
close();
}
return true;
}
inline char const *directory::name()
{
return utf_name_.c_str();
}
#else // POSIX
inline directory::directory() : dir_(0), entry_p_(0)
{
#ifdef CPPCMS_DIR_ALLOCATE_DE
de_ = 0;
#endif
}
inline bool directory::open(std::string const &dir_name)
{
close();
dir_ = opendir(dir_name.c_str());
if(!dir_)
return false;
#ifdef CPPCMS_DIR_ALLOCATE_DE
//
// This pathconf/opendir exploit can be used with only file systems
// with small file names like fat... So we require
// at least 4K so it would be impossible to exploit
// this
//
int name_len = pathconf(dir_name.c_str(),_PC_NAME_MAX);
if(name_len < 4096) // -1 or small value
name_len = 4096; // guess
#ifdef NAME_MAX
if(name_len < NAME_MAX)
name_len = NAME_MAX;
#endif
de_ = static_cast<struct dirent*>(malloc(sizeof(struct dirent) + name_len + 1 - sizeof(de_->d_name)));
if(!de_)
throw std::bad_alloc();
#endif // alloc de
return true;
}
inline void directory::close()
{
#ifdef CPPCMS_DIR_ALLOCATE_DE
if(de_) {
free(de_);
de_ = 0;
}
#endif
entry_p_ = 0;
if(dir_) {
closedir(dir_);
dir_=0;
}
}
inline bool directory::next()
{
assert(dir_);
#ifdef CPPCMS_DIR_ALLOCATE_DE
struct dirent *d = de_;
#else
struct dirent *d = &de_;
#endif
return readdir_r(dir_,d,&entry_p_) == 0 && entry_p_ != 0;
}
inline char const *directory::name()
{
assert(entry_p_);
return entry_p_->d_name;
}
#endif // POSIX
} // impl
} // cppcms
#endif