/src/logging-log4cxx/src/main/cpp/file.cpp
Line | Count | Source |
1 | | /* |
2 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | | * contributor license agreements. See the NOTICE file distributed with |
4 | | * this work for additional information regarding copyright ownership. |
5 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | | * (the "License"); you may not use this file except in compliance with |
7 | | * the License. You may obtain a copy of the License at |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | |
18 | | #include <log4cxx/logstring.h> |
19 | | #include <log4cxx/file.h> |
20 | | #include <apr_file_io.h> |
21 | | #include <apr_file_info.h> |
22 | | #include <log4cxx/helpers/transcoder.h> |
23 | | #include <log4cxx/helpers/pool.h> |
24 | | #include <assert.h> |
25 | | #include <log4cxx/helpers/exception.h> |
26 | | |
27 | | using namespace LOG4CXX_NS; |
28 | | using namespace LOG4CXX_NS::helpers; |
29 | | |
30 | | struct File::FilePrivate{ |
31 | | FilePrivate() |
32 | 4.56k | { |
33 | 4.56k | set_apr_path(); |
34 | 4.56k | } |
35 | | |
36 | | FilePrivate(const LogString& path) |
37 | 27.3k | : path(path) |
38 | 27.3k | { |
39 | 27.3k | set_apr_path(); |
40 | 27.3k | } log4cxx::File::FilePrivate::FilePrivate(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) Line | Count | Source | 37 | 21.1k | : path(path) | 38 | 21.1k | { | 39 | 21.1k | set_apr_path(); | 40 | 21.1k | } |
log4cxx::File::FilePrivate::FilePrivate(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Line | Count | Source | 37 | 6.22k | : path(path) | 38 | 6.22k | { | 39 | 6.22k | set_apr_path(); | 40 | 6.22k | } |
|
41 | | |
42 | | FilePrivate(const LogString& path, bool autoDelete) |
43 | 6.51k | : path(path) |
44 | 6.51k | , autoDelete(autoDelete) |
45 | 6.51k | { |
46 | 6.51k | set_apr_path(); |
47 | 6.51k | } log4cxx::File::FilePrivate::FilePrivate(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, bool) Line | Count | Source | 43 | 5.42k | : path(path) | 44 | 5.42k | , autoDelete(autoDelete) | 45 | 5.42k | { | 46 | 5.42k | set_apr_path(); | 47 | 5.42k | } |
log4cxx::File::FilePrivate::FilePrivate(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool) Line | Count | Source | 43 | 1.08k | : path(path) | 44 | 1.08k | , autoDelete(autoDelete) | 45 | 1.08k | { | 46 | 1.08k | set_apr_path(); | 47 | 1.08k | } |
|
48 | | |
49 | | LogString path; |
50 | | bool autoDelete{ false }; |
51 | | Pool p; |
52 | | const char* apr_path{ nullptr }; |
53 | | std::string encodedPath; |
54 | | |
55 | | const char* getPath() const; |
56 | | void set_apr_path(); |
57 | | }; |
58 | | |
59 | | File::File() : |
60 | 4.56k | m_priv(std::make_unique<FilePrivate>()) |
61 | 4.56k | { |
62 | 4.56k | } |
63 | | |
64 | | template<class S> |
65 | | static LogString decodeLS(const S* src) |
66 | 6.42k | { |
67 | 6.42k | LogString dst; |
68 | | |
69 | 6.42k | if (src != 0) |
70 | 6.42k | { |
71 | 6.42k | Transcoder::decode(src, dst); |
72 | 6.42k | } |
73 | | |
74 | 6.42k | return dst; |
75 | 6.42k | } file.cpp:std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > decodeLS<char>(char const*) Line | Count | Source | 66 | 4.74k | { | 67 | 4.74k | LogString dst; | 68 | | | 69 | 4.74k | if (src != 0) | 70 | 4.74k | { | 71 | 4.74k | Transcoder::decode(src, dst); | 72 | 4.74k | } | 73 | | | 74 | 4.74k | return dst; | 75 | 4.74k | } |
Unexecuted instantiation: file.cpp:std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > decodeLS<wchar_t>(wchar_t const*) file.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > decodeLS<char>(char const*) Line | Count | Source | 66 | 1.67k | { | 67 | 1.67k | LogString dst; | 68 | | | 69 | 1.67k | if (src != 0) | 70 | 1.67k | { | 71 | 1.67k | Transcoder::decode(src, dst); | 72 | 1.67k | } | 73 | | | 74 | 1.67k | return dst; | 75 | 1.67k | } |
Unexecuted instantiation: file.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > decodeLS<wchar_t>(wchar_t const*) |
76 | | |
77 | | template<class S> |
78 | | static LogString decodeLS(const std::basic_string<S>& src) |
79 | 20.9k | { |
80 | 20.9k | LogString dst; |
81 | 20.9k | Transcoder::decode(src, dst); |
82 | 20.9k | return dst; |
83 | 20.9k | } Unexecuted instantiation: file.cpp:std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > decodeLS<char>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) file.cpp:std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > decodeLS<wchar_t>(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) Line | Count | Source | 79 | 16.4k | { | 80 | 16.4k | LogString dst; | 81 | 16.4k | Transcoder::decode(src, dst); | 82 | 16.4k | return dst; | 83 | 16.4k | } |
file.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > decodeLS<char>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Line | Count | Source | 79 | 4.55k | { | 80 | 4.55k | LogString dst; | 81 | 4.55k | Transcoder::decode(src, dst); | 82 | 4.55k | return dst; | 83 | 4.55k | } |
Unexecuted instantiation: file.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > decodeLS<wchar_t>(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) |
84 | | |
85 | | |
86 | | File::File(const std::string& name1) |
87 | 4.55k | : m_priv(std::make_unique<FilePrivate>(decodeLS(name1))) |
88 | 4.55k | { |
89 | 4.55k | } |
90 | | |
91 | | File::File(const char* name1) |
92 | 6.42k | : m_priv(std::make_unique<FilePrivate>(decodeLS(name1))) |
93 | 6.42k | { |
94 | 6.42k | } |
95 | | |
96 | | #if LOG4CXX_WCHAR_T_API |
97 | | File::File(const std::wstring& name1) |
98 | 16.4k | : m_priv(std::make_unique<FilePrivate>(decodeLS(name1))) |
99 | 16.4k | { |
100 | 16.4k | } |
101 | | |
102 | | File::File(const wchar_t* name1) |
103 | 0 | : m_priv(std::make_unique<FilePrivate>(decodeLS(name1))) |
104 | 0 | { |
105 | 0 | } |
106 | | #endif |
107 | | |
108 | | |
109 | | #if LOG4CXX_CFSTRING_API |
110 | | File::File(const CFStringRef& name1) |
111 | | : m_priv(std::make_unique<FilePrivate>(decodeLS(name1))) |
112 | | { |
113 | | } |
114 | | #endif |
115 | | |
116 | | File::File(const File& src) |
117 | 6.51k | : m_priv(std::make_unique<FilePrivate>(src.m_priv->path, src.m_priv->autoDelete)) |
118 | 6.51k | { |
119 | 6.51k | } |
120 | | |
121 | | File& File::operator=(const File& src) |
122 | 0 | { |
123 | 0 | if (this == &src) |
124 | 0 | { |
125 | 0 | return *this; |
126 | 0 | } |
127 | | |
128 | 0 | m_priv->path.assign(src.m_priv->path); |
129 | 0 | m_priv->autoDelete = src.m_priv->autoDelete; |
130 | 0 | m_priv->set_apr_path(); |
131 | 0 | return *this; |
132 | 0 | } |
133 | | |
134 | | |
135 | | File::~File() |
136 | 38.4k | { |
137 | 38.4k | if(m_priv->autoDelete){ |
138 | 0 | deleteFile(); |
139 | 0 | } |
140 | 38.4k | } |
141 | | |
142 | | bool File::hasPath() const |
143 | 4.46k | { |
144 | 4.46k | return !m_priv->path.empty(); |
145 | 4.46k | } |
146 | | |
147 | | LogString File::getPath() const |
148 | 25.5k | { |
149 | 25.5k | return m_priv->path; |
150 | 25.5k | } |
151 | | |
152 | | File& File::setPath(const LogString& newName) |
153 | 11.1k | { |
154 | 11.1k | m_priv->path.assign(newName); |
155 | 11.1k | m_priv->set_apr_path(); |
156 | 11.1k | return *this; |
157 | 11.1k | } log4cxx::File::setPath(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) Line | Count | Source | 153 | 5.74k | { | 154 | 5.74k | m_priv->path.assign(newName); | 155 | 5.74k | m_priv->set_apr_path(); | 156 | 5.74k | return *this; | 157 | 5.74k | } |
log4cxx::File::setPath(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Line | Count | Source | 153 | 5.40k | { | 154 | 5.40k | m_priv->path.assign(newName); | 155 | 5.40k | m_priv->set_apr_path(); | 156 | 5.40k | return *this; | 157 | 5.40k | } |
|
158 | | |
159 | | LogString File::getName() const |
160 | 0 | { |
161 | 0 | const logchar slashes[] = { 0x2F, 0x5C, 0 }; |
162 | 0 | size_t lastSlash = m_priv->path.find_last_of(slashes); |
163 | |
|
164 | 0 | if (lastSlash != LogString::npos) |
165 | 0 | { |
166 | 0 | return m_priv->path.substr(lastSlash + 1); |
167 | 0 | } |
168 | | |
169 | 0 | return m_priv->path; |
170 | 0 | } |
171 | | |
172 | | const char* File::FilePrivate::getPath() const |
173 | 31.1k | { |
174 | 31.1k | return this->apr_path; |
175 | 31.1k | } |
176 | | |
177 | | void File::FilePrivate::set_apr_path() |
178 | 49.6k | { |
179 | 49.6k | this->encodedPath.clear(); |
180 | 49.6k | int style = APR_FILEPATH_ENCODING_UNKNOWN; |
181 | 49.6k | apr_filepath_encoding(&style, this->p.getAPRPool()); |
182 | 49.6k | if (style == APR_FILEPATH_ENCODING_UTF8) |
183 | 0 | { |
184 | 0 | Transcoder::encodeUTF8(this->path, this->encodedPath); |
185 | 0 | } |
186 | 49.6k | else |
187 | 49.6k | { |
188 | 49.6k | Transcoder::encode(this->path, this->encodedPath); |
189 | 49.6k | } |
190 | | |
191 | 49.6k | for (auto& c : this->encodedPath) |
192 | 73.9M | { |
193 | 73.9M | if (c == '\\') |
194 | 248k | { |
195 | 248k | c = '/'; |
196 | 248k | } |
197 | 73.9M | } |
198 | 49.6k | this->apr_path = this->encodedPath.c_str(); |
199 | 49.6k | } |
200 | | |
201 | | const char* File::getAPRPath() const |
202 | 10.9k | { |
203 | 10.9k | return m_priv->getPath(); |
204 | 10.9k | } |
205 | | |
206 | | |
207 | | bool File::exists() const |
208 | 15.9k | { |
209 | 15.9k | apr_finfo_t finfo; |
210 | 15.9k | apr_status_t rv = apr_stat(&finfo, m_priv->getPath(), 0, m_priv->p.getAPRPool()); |
211 | 15.9k | return rv == APR_SUCCESS; |
212 | 15.9k | } |
213 | | |
214 | | bool File::deleteFile() const |
215 | 73 | { |
216 | 73 | apr_status_t rv = apr_file_remove(m_priv->getPath(), m_priv->p.getAPRPool()); |
217 | 73 | return rv == APR_SUCCESS; |
218 | 73 | } |
219 | | |
220 | | bool File::renameTo(const File& dest) const |
221 | 312 | { |
222 | 312 | apr_status_t rv = apr_file_rename(m_priv->getPath(), dest.m_priv->getPath(), m_priv->p.getAPRPool()); |
223 | 312 | return rv == APR_SUCCESS; |
224 | 312 | } |
225 | | |
226 | | |
227 | | size_t File::length() const |
228 | 2.44k | { |
229 | 2.44k | apr_finfo_t finfo; |
230 | 2.44k | apr_status_t rv = apr_stat(&finfo, m_priv->getPath(), APR_FINFO_SIZE, m_priv->p.getAPRPool()); |
231 | | |
232 | 2.44k | if (rv == APR_SUCCESS) |
233 | 1.61k | { |
234 | 1.61k | return (size_t) finfo.size; |
235 | 1.61k | } |
236 | | |
237 | 831 | return 0; |
238 | 2.44k | } |
239 | | |
240 | | |
241 | | log4cxx_time_t File::lastModified() const |
242 | 766 | { |
243 | 766 | apr_finfo_t finfo; |
244 | 766 | apr_status_t rv = apr_stat(&finfo, m_priv->getPath(), APR_FINFO_MTIME, m_priv->p.getAPRPool()); |
245 | | |
246 | 766 | if (rv == APR_SUCCESS) |
247 | 766 | { |
248 | 766 | return finfo.mtime; |
249 | 766 | } |
250 | | |
251 | 0 | return 0; |
252 | 766 | } |
253 | | |
254 | | |
255 | | std::vector<LogString> File::list() const |
256 | 0 | { |
257 | 0 | apr_dir_t* dir; |
258 | 0 | apr_finfo_t entry; |
259 | 0 | std::vector<LogString> filenames; |
260 | |
|
261 | 0 | apr_status_t stat = apr_dir_open(&dir, m_priv->getPath(), m_priv->p.getAPRPool()); |
262 | |
|
263 | 0 | if (stat == APR_SUCCESS) |
264 | 0 | { |
265 | 0 | int style = APR_FILEPATH_ENCODING_UNKNOWN; |
266 | 0 | apr_filepath_encoding(&style, m_priv->p.getAPRPool()); |
267 | 0 | stat = apr_dir_read(&entry, APR_FINFO_DIRENT, dir); |
268 | |
|
269 | 0 | while (stat == APR_SUCCESS) |
270 | 0 | { |
271 | 0 | if (entry.name != NULL) |
272 | 0 | { |
273 | 0 | LogString filename; |
274 | |
|
275 | 0 | if (style == APR_FILEPATH_ENCODING_UTF8) |
276 | 0 | { |
277 | 0 | Transcoder::decodeUTF8(entry.name, filename); |
278 | 0 | } |
279 | 0 | else |
280 | 0 | { |
281 | 0 | Transcoder::decode(entry.name, filename); |
282 | 0 | } |
283 | |
|
284 | 0 | filenames.push_back(filename); |
285 | 0 | } |
286 | |
|
287 | 0 | stat = apr_dir_read(&entry, APR_FINFO_DIRENT, dir); |
288 | 0 | } |
289 | |
|
290 | 0 | stat = apr_dir_close(dir); |
291 | 0 | } |
292 | |
|
293 | 0 | return filenames; |
294 | 0 | } |
295 | | |
296 | | LogString File::getParent() const |
297 | 502 | { |
298 | 502 | LogString::size_type slashPos = m_priv->path.rfind(LOG4CXX_STR('/')); |
299 | 502 | LogString::size_type backPos = m_priv->path.rfind(LOG4CXX_STR('\\')); |
300 | | |
301 | 502 | if (slashPos == LogString::npos) |
302 | 302 | { |
303 | 302 | slashPos = backPos; |
304 | 302 | } |
305 | 200 | else |
306 | 200 | { |
307 | 200 | if (backPos != LogString::npos && backPos > slashPos) |
308 | 54 | { |
309 | 54 | slashPos = backPos; |
310 | 54 | } |
311 | 200 | } |
312 | | |
313 | 502 | LogString parent; |
314 | | |
315 | 502 | if (slashPos != LogString::npos && slashPos > 0) |
316 | 298 | { |
317 | 298 | parent.assign(m_priv->path, 0, slashPos); |
318 | 298 | } |
319 | | |
320 | 502 | return parent; |
321 | 502 | } |
322 | | |
323 | | bool File::mkdirs() const |
324 | 290 | { |
325 | 290 | apr_status_t stat = apr_dir_make_recursive(m_priv->getPath(), APR_OS_DEFAULT, m_priv->p.getAPRPool()); |
326 | 290 | return stat == APR_SUCCESS; |
327 | 290 | } |
328 | | |
329 | 0 | void File::setAutoDelete(bool autoDelete){ |
330 | 0 | m_priv->autoDelete = autoDelete; |
331 | 0 | } |
332 | | |
333 | 0 | bool File::getAutoDelete() const{ |
334 | 0 | return m_priv->autoDelete; |
335 | 0 | } |
336 | | |