blob: b395e49ca44216cb3d2f4dceeb7b1125f496d329 [file] [log] [blame]
Jeff Thompson86b6d642013-10-17 15:01:56 -07001// ndnboost/filesystem/operations.hpp ---------------------------------------------------//
2
3// Copyright Beman Dawes 2002-2009
4// Copyright Jan Langer 2002
5// Copyright Dietmar Kuehl 2001
6// Copyright Vladimir Prus 2002
7
8// Distributed under the Boost Software License, Version 1.0.
9// See http://www.boost.org/LICENSE_1_0.txt
10
11// Library home page: http://www.boost.org/libs/filesystem
12
13//--------------------------------------------------------------------------------------//
14
15#ifndef NDNBOOST_FILESYSTEM3_OPERATIONS_HPP
16#define NDNBOOST_FILESYSTEM3_OPERATIONS_HPP
17
18#include <ndnboost/config.hpp>
19
20# if defined( NDNBOOST_NO_STD_WSTRING )
21# error Configuration not supported: Boost.Filesystem V3 and later requires std::wstring support
22# endif
23
24#include <ndnboost/filesystem/config.hpp>
25#include <ndnboost/filesystem/path.hpp>
26
27#include <ndnboost/detail/scoped_enum_emulation.hpp>
28#include <ndnboost/detail/bitmask.hpp>
29#include <ndnboost/system/error_code.hpp>
30#include <ndnboost/system/system_error.hpp>
31#include <ndnboost/shared_ptr.hpp>
32#include <ndnboost/utility/enable_if.hpp>
33#include <ndnboost/type_traits/is_same.hpp>
34#include <ndnboost/iterator.hpp>
35#include <ndnboost/cstdint.hpp>
36#include <ndnboost/assert.hpp>
37
38#include <string>
39#include <utility> // for pair
40#include <ctime>
41#include <vector>
42#include <stack>
43
44#ifdef NDNBOOST_WINDOWS_API
45# include <fstream>
46#endif
47
48#include <ndnboost/config/abi_prefix.hpp> // must be the last #include
49
50//--------------------------------------------------------------------------------------//
51
52namespace ndnboost
53{
54 namespace filesystem
55 {
56
57//--------------------------------------------------------------------------------------//
58// file_type //
59//--------------------------------------------------------------------------------------//
60
61 enum file_type
62 {
63 status_error,
64# ifndef NDNBOOST_FILESYSTEM_NO_DEPRECATED
65 status_unknown = status_error,
66# endif
67 file_not_found,
68 regular_file,
69 directory_file,
70 // the following may not apply to some operating systems or file systems
71 symlink_file,
72 block_file,
73 character_file,
74 fifo_file,
75 socket_file,
76 reparse_file, // Windows: FILE_ATTRIBUTE_REPARSE_POINT that is not a symlink
77 type_unknown, // file does exist, but isn't one of the above types or
78 // we don't have strong enough permission to find its type
79
80 _detail_directory_symlink // internal use only; never exposed to users
81 };
82
83//--------------------------------------------------------------------------------------//
84// perms //
85//--------------------------------------------------------------------------------------//
86
87 enum perms
88 {
89 no_perms = 0, // file_not_found is no_perms rather than perms_not_known
90
91 // POSIX equivalent macros given in comments.
92 // Values are from POSIX and are given in octal per the POSIX standard.
93
94 // permission bits
95
96 owner_read = 0400, // S_IRUSR, Read permission, owner
97 owner_write = 0200, // S_IWUSR, Write permission, owner
98 owner_exe = 0100, // S_IXUSR, Execute/search permission, owner
99 owner_all = 0700, // S_IRWXU, Read, write, execute/search by owner
100
101 group_read = 040, // S_IRGRP, Read permission, group
102 group_write = 020, // S_IWGRP, Write permission, group
103 group_exe = 010, // S_IXGRP, Execute/search permission, group
104 group_all = 070, // S_IRWXG, Read, write, execute/search by group
105
106 others_read = 04, // S_IROTH, Read permission, others
107 others_write = 02, // S_IWOTH, Write permission, others
108 others_exe = 01, // S_IXOTH, Execute/search permission, others
109 others_all = 07, // S_IRWXO, Read, write, execute/search by others
110
111 all_all = owner_all|group_all|others_all, // 0777
112
113 // other POSIX bits
114
115 set_uid_on_exe = 04000, // S_ISUID, Set-user-ID on execution
116 set_gid_on_exe = 02000, // S_ISGID, Set-group-ID on execution
117 sticky_bit = 01000, // S_ISVTX,
118 // (POSIX XSI) On directories, restricted deletion flag
119 // (V7) 'sticky bit': save swapped text even after use
120 // (SunOS) On non-directories: don't cache this file
121 // (SVID-v4.2) On directories: restricted deletion flag
122 // Also see http://en.wikipedia.org/wiki/Sticky_bit
123
124 perms_mask = all_all|set_uid_on_exe|set_gid_on_exe|sticky_bit, // 07777
125
126 perms_not_known = 0xFFFF, // present when directory_entry cache not loaded
127
128 // options for permissions() function
129
130 add_perms = 0x1000, // adds the given permission bits to the current bits
131 remove_perms = 0x2000, // removes the given permission bits from the current bits;
132 // choose add_perms or remove_perms, not both; if neither add_perms
133 // nor remove_perms is given, replace the current bits with
134 // the given bits.
135
136 symlink_perms = 0x4000 // on POSIX, don't resolve symlinks; implied on Windows
137 };
138
139 NDNBOOST_BITMASK(perms)
140
141//--------------------------------------------------------------------------------------//
142// file_status //
143//--------------------------------------------------------------------------------------//
144
145 class NDNBOOST_FILESYSTEM_DECL file_status
146 {
147 public:
148 file_status() : m_value(status_error), m_perms(perms_not_known) {}
149 explicit file_status(file_type v, perms prms = perms_not_known)
150 : m_value(v), m_perms(prms) {}
151
152 // observers
153 file_type type() const { return m_value; }
154 perms permissions() const { return m_perms; }
155
156 // modifiers
157 void type(file_type v) { m_value = v; }
158 void permissions(perms prms) { m_perms = prms; }
159
160 bool operator==(const file_status& rhs) const { return type() == rhs.type() &&
161 permissions() == rhs.permissions(); }
162 bool operator!=(const file_status& rhs) const { return !(*this == rhs); }
163
164 private:
165 file_type m_value;
166 enum perms m_perms;
167 };
168
169 inline bool type_present(file_status f) { return f.type() != status_error; }
170 inline bool permissions_present(file_status f)
171 {return f.permissions() != perms_not_known;}
172 inline bool status_known(file_status f) { return type_present(f) && permissions_present(f); }
173 inline bool exists(file_status f) { return f.type() != status_error
174 && f.type() != file_not_found; }
175 inline bool is_regular_file(file_status f){ return f.type() == regular_file; }
176 inline bool is_directory(file_status f) { return f.type() == directory_file; }
177 inline bool is_symlink(file_status f) { return f.type() == symlink_file; }
178 inline bool is_other(file_status f) { return exists(f) && !is_regular_file(f)
179 && !is_directory(f) && !is_symlink(f); }
180
181# ifndef NDNBOOST_FILESYSTEM_NO_DEPRECATED
182 inline bool is_regular(file_status f) { return f.type() == regular_file; }
183# endif
184
185 struct space_info
186 {
187 // all values are byte counts
188 ndnboost::uintmax_t capacity;
189 ndnboost::uintmax_t free; // <= capacity
190 ndnboost::uintmax_t available; // <= free
191 };
192
193 NDNBOOST_SCOPED_ENUM_START(copy_option)
194 {none, fail_if_exists = none, overwrite_if_exists};
195 NDNBOOST_SCOPED_ENUM_END
196
197//--------------------------------------------------------------------------------------//
198// implementation details //
199//--------------------------------------------------------------------------------------//
200
201 namespace detail
202 {
203 NDNBOOST_FILESYSTEM_DECL
204 file_status status(const path&p, system::error_code* ec=0);
205 NDNBOOST_FILESYSTEM_DECL
206 file_status symlink_status(const path& p, system::error_code* ec=0);
207 NDNBOOST_FILESYSTEM_DECL
208 bool is_empty(const path& p, system::error_code* ec=0);
209 NDNBOOST_FILESYSTEM_DECL
210 path initial_path(system::error_code* ec=0);
211 NDNBOOST_FILESYSTEM_DECL
212 path canonical(const path& p, const path& base, system::error_code* ec=0);
213 NDNBOOST_FILESYSTEM_DECL
214 void copy(const path& from, const path& to, system::error_code* ec=0);
215 NDNBOOST_FILESYSTEM_DECL
216 void copy_directory(const path& from, const path& to, system::error_code* ec=0);
217 NDNBOOST_FILESYSTEM_DECL
218 void copy_file(const path& from, const path& to,
219 NDNBOOST_SCOPED_ENUM(copy_option) option, // See ticket #2925
220 system::error_code* ec=0);
221 NDNBOOST_FILESYSTEM_DECL
222 void copy_symlink(const path& existing_symlink, const path& new_symlink, system::error_code* ec=0);
223 NDNBOOST_FILESYSTEM_DECL
224 bool create_directories(const path& p, system::error_code* ec=0);
225 NDNBOOST_FILESYSTEM_DECL
226 bool create_directory(const path& p, system::error_code* ec=0);
227 NDNBOOST_FILESYSTEM_DECL
228 void create_directory_symlink(const path& to, const path& from,
229 system::error_code* ec=0);
230 NDNBOOST_FILESYSTEM_DECL
231 void create_hard_link(const path& to, const path& from, system::error_code* ec=0);
232 NDNBOOST_FILESYSTEM_DECL
233 void create_symlink(const path& to, const path& from, system::error_code* ec=0);
234 NDNBOOST_FILESYSTEM_DECL
235 path current_path(system::error_code* ec=0);
236 NDNBOOST_FILESYSTEM_DECL
237 void current_path(const path& p, system::error_code* ec=0);
238 NDNBOOST_FILESYSTEM_DECL
239 bool equivalent(const path& p1, const path& p2, system::error_code* ec=0);
240 NDNBOOST_FILESYSTEM_DECL
241 ndnboost::uintmax_t file_size(const path& p, system::error_code* ec=0);
242 NDNBOOST_FILESYSTEM_DECL
243 ndnboost::uintmax_t hard_link_count(const path& p, system::error_code* ec=0);
244 NDNBOOST_FILESYSTEM_DECL
245 std::time_t last_write_time(const path& p, system::error_code* ec=0);
246 NDNBOOST_FILESYSTEM_DECL
247 void last_write_time(const path& p, const std::time_t new_time,
248 system::error_code* ec=0);
249 NDNBOOST_FILESYSTEM_DECL
250 void permissions(const path& p, perms prms, system::error_code* ec=0);
251 NDNBOOST_FILESYSTEM_DECL
252 path read_symlink(const path& p, system::error_code* ec=0);
253 NDNBOOST_FILESYSTEM_DECL
254 // For standardization, if the committee doesn't like "remove", consider "eliminate"
255 bool remove(const path& p, system::error_code* ec=0);
256 NDNBOOST_FILESYSTEM_DECL
257 ndnboost::uintmax_t remove_all(const path& p, system::error_code* ec=0);
258 NDNBOOST_FILESYSTEM_DECL
259 void rename(const path& old_p, const path& new_p, system::error_code* ec=0);
260 NDNBOOST_FILESYSTEM_DECL
261 void resize_file(const path& p, uintmax_t size, system::error_code* ec=0);
262 NDNBOOST_FILESYSTEM_DECL
263 space_info space(const path& p, system::error_code* ec=0);
264 NDNBOOST_FILESYSTEM_DECL
265 path system_complete(const path& p, system::error_code* ec=0);
266 NDNBOOST_FILESYSTEM_DECL
267 path temp_directory_path(system::error_code* ec=0);
268 NDNBOOST_FILESYSTEM_DECL
269 path unique_path(const path& p, system::error_code* ec=0);
270 } // namespace detail
271
272//--------------------------------------------------------------------------------------//
273// //
274// status query functions //
275// //
276//--------------------------------------------------------------------------------------//
277
278 inline
279 file_status status(const path& p) {return detail::status(p);}
280 inline
281 file_status status(const path& p, system::error_code& ec)
282 {return detail::status(p, &ec);}
283 inline
284 file_status symlink_status(const path& p) {return detail::symlink_status(p);}
285 inline
286 file_status symlink_status(const path& p, system::error_code& ec)
287 {return detail::symlink_status(p, &ec);}
288 inline
289 bool exists(const path& p) {return exists(detail::status(p));}
290 inline
291 bool exists(const path& p, system::error_code& ec)
292 {return exists(detail::status(p, &ec));}
293 inline
294 bool is_directory(const path& p) {return is_directory(detail::status(p));}
295 inline
296 bool is_directory(const path& p, system::error_code& ec)
297 {return is_directory(detail::status(p, &ec));}
298 inline
299 bool is_regular_file(const path& p) {return is_regular_file(detail::status(p));}
300 inline
301 bool is_regular_file(const path& p, system::error_code& ec)
302 {return is_regular_file(detail::status(p, &ec));}
303 inline
304 bool is_other(const path& p) {return is_other(detail::status(p));}
305 inline
306 bool is_other(const path& p, system::error_code& ec)
307 {return is_other(detail::status(p, &ec));}
308 inline
309 bool is_symlink(const path& p) {return is_symlink(detail::symlink_status(p));}
310 inline
311 bool is_symlink(const path& p, system::error_code& ec)
312 {return is_symlink(detail::symlink_status(p, &ec));}
313# ifndef NDNBOOST_FILESYSTEM_NO_DEPRECATED
314 inline
315 bool is_regular(const path& p) {return is_regular(detail::status(p));}
316 inline
317 bool is_regular(const path& p, system::error_code& ec)
318 {return is_regular(detail::status(p, &ec));}
319# endif
320
321 inline
322 bool is_empty(const path& p) {return detail::is_empty(p);}
323 inline
324 bool is_empty(const path& p, system::error_code& ec)
325 {return detail::is_empty(p, &ec);}
326
327//--------------------------------------------------------------------------------------//
328// //
329// operational functions //
330// in alphabetical order, unless otherwise noted //
331// //
332//--------------------------------------------------------------------------------------//
333
334 // forward declarations
335 path current_path(); // fwd declaration
336 path initial_path();
337
338 NDNBOOST_FILESYSTEM_DECL
339 path absolute(const path& p, const path& base=current_path());
340 // If base.is_absolute(), throws nothing. Thus no need for ec argument
341
342 inline
343 path canonical(const path& p, const path& base=current_path())
344 {return detail::canonical(p, base);}
345 inline
346 path canonical(const path& p, system::error_code& ec)
347 {return detail::canonical(p, current_path(), &ec);}
348 inline
349 path canonical(const path& p, const path& base, system::error_code& ec)
350 {return detail::canonical(p, base, &ec);}
351
352# ifndef NDNBOOST_FILESYSTEM_NO_DEPRECATED
353 inline
354 path complete(const path& p)
355 {
356 return absolute(p, initial_path());
357 }
358
359 inline
360 path complete(const path& p, const path& base)
361 {
362 return absolute(p, base);
363 }
364# endif
365
366 inline
367 void copy(const path& from, const path& to) {detail::copy(from, to);}
368
369 inline
370 void copy(const path& from, const path& to, system::error_code& ec)
371 {detail::copy(from, to, &ec);}
372 inline
373 void copy_directory(const path& from, const path& to)
374 {detail::copy_directory(from, to);}
375 inline
376 void copy_directory(const path& from, const path& to, system::error_code& ec)
377 {detail::copy_directory(from, to, &ec);}
378 inline
379 void copy_file(const path& from, const path& to, // See ticket #2925
380 NDNBOOST_SCOPED_ENUM(copy_option) option)
381 {detail::copy_file(from, to, option);}
382 inline
383 void copy_file(const path& from, const path& to)
384 {detail::copy_file(from, to, copy_option::fail_if_exists);}
385 inline
386 void copy_file(const path& from, const path& to, // See ticket #2925
387 NDNBOOST_SCOPED_ENUM(copy_option) option, system::error_code& ec)
388 {detail::copy_file(from, to, option, &ec);}
389 inline
390 void copy_file(const path& from, const path& to, system::error_code& ec)
391 {detail::copy_file(from, to, copy_option::fail_if_exists, &ec);}
392 inline
393 void copy_symlink(const path& existing_symlink, const path& new_symlink) {detail::copy_symlink(existing_symlink, new_symlink);}
394
395 inline
396 void copy_symlink(const path& existing_symlink, const path& new_symlink, system::error_code& ec)
397 {detail::copy_symlink(existing_symlink, new_symlink, &ec);}
398 inline
399 bool create_directories(const path& p) {return detail::create_directories(p);}
400
401 inline
402 bool create_directories(const path& p, system::error_code& ec)
403 {return detail::create_directories(p, &ec);}
404 inline
405 bool create_directory(const path& p) {return detail::create_directory(p);}
406
407 inline
408 bool create_directory(const path& p, system::error_code& ec)
409 {return detail::create_directory(p, &ec);}
410 inline
411 void create_directory_symlink(const path& to, const path& from)
412 {detail::create_directory_symlink(to, from);}
413 inline
414 void create_directory_symlink(const path& to, const path& from, system::error_code& ec)
415 {detail::create_directory_symlink(to, from, &ec);}
416 inline
417 void create_hard_link(const path& to, const path& new_hard_link) {detail::create_hard_link(to, new_hard_link);}
418
419 inline
420 void create_hard_link(const path& to, const path& new_hard_link, system::error_code& ec)
421 {detail::create_hard_link(to, new_hard_link, &ec);}
422 inline
423 void create_symlink(const path& to, const path& new_symlink) {detail::create_symlink(to, new_symlink);}
424
425 inline
426 void create_symlink(const path& to, const path& new_symlink, system::error_code& ec)
427 {detail::create_symlink(to, new_symlink, &ec);}
428 inline
429 path current_path() {return detail::current_path();}
430
431 inline
432 path current_path(system::error_code& ec) {return detail::current_path(&ec);}
433
434 inline
435 void current_path(const path& p) {detail::current_path(p);}
436
437 inline
438 void current_path(const path& p, system::error_code& ec) {detail::current_path(p, &ec);}
439
440 inline
441 bool equivalent(const path& p1, const path& p2) {return detail::equivalent(p1, p2);}
442
443 inline
444 bool equivalent(const path& p1, const path& p2, system::error_code& ec)
445 {return detail::equivalent(p1, p2, &ec);}
446 inline
447 ndnboost::uintmax_t file_size(const path& p) {return detail::file_size(p);}
448
449 inline
450 ndnboost::uintmax_t file_size(const path& p, system::error_code& ec)
451 {return detail::file_size(p, &ec);}
452 inline
453 ndnboost::uintmax_t hard_link_count(const path& p) {return detail::hard_link_count(p);}
454
455 inline
456 ndnboost::uintmax_t hard_link_count(const path& p, system::error_code& ec)
457 {return detail::hard_link_count(p, &ec);}
458 inline
459 path initial_path() {return detail::initial_path();}
460
461 inline
462 path initial_path(system::error_code& ec) {return detail::initial_path(&ec);}
463
464 template <class Path>
465 path initial_path() {return initial_path();}
466 template <class Path>
467 path initial_path(system::error_code& ec) {return detail::initial_path(&ec);}
468
469 inline
470 std::time_t last_write_time(const path& p) {return detail::last_write_time(p);}
471
472 inline
473 std::time_t last_write_time(const path& p, system::error_code& ec)
474 {return detail::last_write_time(p, &ec);}
475 inline
476 void last_write_time(const path& p, const std::time_t new_time)
477 {detail::last_write_time(p, new_time);}
478 inline
479 void last_write_time(const path& p, const std::time_t new_time, system::error_code& ec)
480 {detail::last_write_time(p, new_time, &ec);}
481 inline
482 void permissions(const path& p, perms prms)
483 {detail::permissions(p, prms);}
484 inline
485 void permissions(const path& p, perms prms, system::error_code& ec)
486 {detail::permissions(p, prms, &ec);}
487
488 inline
489 path read_symlink(const path& p) {return detail::read_symlink(p);}
490
491 inline
492 path read_symlink(const path& p, system::error_code& ec)
493 {return detail::read_symlink(p, &ec);}
494 inline
495 // For standardization, if the committee doesn't like "remove", consider "eliminate"
496 bool remove(const path& p) {return detail::remove(p);}
497
498 inline
499 bool remove(const path& p, system::error_code& ec) {return detail::remove(p, &ec);}
500
501 inline
502 ndnboost::uintmax_t remove_all(const path& p) {return detail::remove_all(p);}
503
504 inline
505 ndnboost::uintmax_t remove_all(const path& p, system::error_code& ec)
506 {return detail::remove_all(p, &ec);}
507 inline
508 void rename(const path& old_p, const path& new_p) {detail::rename(old_p, new_p);}
509
510 inline
511 void rename(const path& old_p, const path& new_p, system::error_code& ec)
512 {detail::rename(old_p, new_p, &ec);}
513 inline // name suggested by Scott McMurray
514 void resize_file(const path& p, uintmax_t size) {detail::resize_file(p, size);}
515
516 inline
517 void resize_file(const path& p, uintmax_t size, system::error_code& ec)
518 {detail::resize_file(p, size, &ec);}
519 inline
520 space_info space(const path& p) {return detail::space(p);}
521
522 inline
523 space_info space(const path& p, system::error_code& ec) {return detail::space(p, &ec);}
524
525# ifndef NDNBOOST_FILESYSTEM_NO_DEPRECATED
526 inline bool symbolic_link_exists(const path& p)
527 { return is_symlink(symlink_status(p)); }
528# endif
529
530 inline
531 path system_complete(const path& p) {return detail::system_complete(p);}
532
533 inline
534 path system_complete(const path& p, system::error_code& ec)
535 {return detail::system_complete(p, &ec);}
536 inline
537 path temp_directory_path() {return detail::temp_directory_path();}
538
539 inline
540 path temp_directory_path(system::error_code& ec)
541 {return detail::temp_directory_path(&ec);}
542 inline
543 path unique_path(const path& p="%%%%-%%%%-%%%%-%%%%")
544 { return detail::unique_path(p); }
545 inline
546 path unique_path(const path& p, system::error_code& ec)
547 { return detail::unique_path(p, &ec); }
548
549//--------------------------------------------------------------------------------------//
550// //
551// directory_entry //
552// //
553//--------------------------------------------------------------------------------------//
554
555// GCC has a problem with a member function named path within a namespace or
556// sub-namespace that also has a class named path. The workaround is to always
557// fully qualify the name path when it refers to the class name.
558
559class NDNBOOST_FILESYSTEM_DECL directory_entry
560{
561public:
562
563 // compiler generated copy constructor, copy assignment, and destructor apply
564
565 directory_entry() {}
566 explicit directory_entry(const ndnboost::filesystem::path& p,
567 file_status st = file_status(), file_status symlink_st=file_status())
568 : m_path(p), m_status(st), m_symlink_status(symlink_st)
569 {}
570
571 void assign(const ndnboost::filesystem::path& p,
572 file_status st = file_status(), file_status symlink_st = file_status())
573 { m_path = p; m_status = st; m_symlink_status = symlink_st; }
574
575 void replace_filename(const ndnboost::filesystem::path& p,
576 file_status st = file_status(), file_status symlink_st = file_status())
577 {
578 m_path.remove_filename();
579 m_path /= p;
580 m_status = st;
581 m_symlink_status = symlink_st;
582 }
583
584# ifndef NDNBOOST_FILESYSTEM_NO_DEPRECATED
585 void replace_leaf(const ndnboost::filesystem::path& p,
586 file_status st, file_status symlink_st)
587 { replace_filename(p, st, symlink_st); }
588# endif
589
590 const ndnboost::filesystem::path& path() const {return m_path;}
591 file_status status() const {return m_get_status();}
592 file_status status(system::error_code& ec) const {return m_get_status(&ec);}
593 file_status symlink_status() const {return m_get_symlink_status();}
594 file_status symlink_status(system::error_code& ec) const {return m_get_symlink_status(&ec);}
595
596 bool operator==(const directory_entry& rhs) {return m_path == rhs.m_path;}
597 bool operator!=(const directory_entry& rhs) {return m_path != rhs.m_path;}
598 bool operator< (const directory_entry& rhs) {return m_path < rhs.m_path;}
599 bool operator<=(const directory_entry& rhs) {return m_path <= rhs.m_path;}
600 bool operator> (const directory_entry& rhs) {return m_path > rhs.m_path;}
601 bool operator>=(const directory_entry& rhs) {return m_path >= rhs.m_path;}
602
603private:
604 ndnboost::filesystem::path m_path;
605 mutable file_status m_status; // stat()-like
606 mutable file_status m_symlink_status; // lstat()-like
607
608 file_status m_get_status(system::error_code* ec=0) const;
609 file_status m_get_symlink_status(system::error_code* ec=0) const;
610}; // directory_entry
611
612//--------------------------------------------------------------------------------------//
613// //
614// directory_iterator helpers //
615// //
616//--------------------------------------------------------------------------------------//
617
618class directory_iterator;
619
620namespace detail
621{
622 NDNBOOST_FILESYSTEM_DECL
623 system::error_code dir_itr_close(// never throws()
624 void *& handle
625# if defined(NDNBOOST_POSIX_API)
626 , void *& buffer
627# endif
628 );
629
630 struct dir_itr_imp
631 {
632 directory_entry dir_entry;
633 void* handle;
634
635# ifdef NDNBOOST_POSIX_API
636 void* buffer; // see dir_itr_increment implementation
637# endif
638
639 dir_itr_imp() : handle(0)
640# ifdef NDNBOOST_POSIX_API
641 , buffer(0)
642# endif
643 {}
644
645 ~dir_itr_imp() // never throws
646 {
647 dir_itr_close(handle
648# if defined(NDNBOOST_POSIX_API)
649 , buffer
650# endif
651 );
652 }
653 };
654
655 // see path::iterator: comment below
656 NDNBOOST_FILESYSTEM_DECL void directory_iterator_construct(directory_iterator& it,
657 const path& p, system::error_code* ec);
658 NDNBOOST_FILESYSTEM_DECL void directory_iterator_increment(directory_iterator& it,
659 system::error_code* ec);
660
661} // namespace detail
662
663//--------------------------------------------------------------------------------------//
664// //
665// directory_iterator //
666// //
667//--------------------------------------------------------------------------------------//
668
669 class directory_iterator
670 : public ndnboost::iterator_facade< directory_iterator,
671 directory_entry,
672 ndnboost::single_pass_traversal_tag >
673 {
674 public:
675
676 directory_iterator(){} // creates the "end" iterator
677
678 // iterator_facade derived classes don't seem to like implementations in
679 // separate translation unit dll's, so forward to detail functions
680 explicit directory_iterator(const path& p)
681 : m_imp(new detail::dir_itr_imp)
682 { detail::directory_iterator_construct(*this, p, 0); }
683
684 directory_iterator(const path& p, system::error_code& ec)
685 : m_imp(new detail::dir_itr_imp)
686 { detail::directory_iterator_construct(*this, p, &ec); }
687
688 ~directory_iterator() {} // never throws
689
690 directory_iterator& increment(system::error_code& ec)
691 {
692 detail::directory_iterator_increment(*this, &ec);
693 return *this;
694 }
695
696 private:
697 friend struct detail::dir_itr_imp;
698 friend NDNBOOST_FILESYSTEM_DECL void detail::directory_iterator_construct(directory_iterator& it,
699 const path& p, system::error_code* ec);
700 friend NDNBOOST_FILESYSTEM_DECL void detail::directory_iterator_increment(directory_iterator& it,
701 system::error_code* ec);
702
703 // shared_ptr provides shallow-copy semantics required for InputIterators.
704 // m_imp.get()==0 indicates the end iterator.
705 ndnboost::shared_ptr< detail::dir_itr_imp > m_imp;
706
707 friend class ndnboost::iterator_core_access;
708
709 ndnboost::iterator_facade<
710 directory_iterator,
711 directory_entry,
712 ndnboost::single_pass_traversal_tag >::reference dereference() const
713 {
714 NDNBOOST_ASSERT_MSG(m_imp.get(), "attempt to dereference end iterator");
715 return m_imp->dir_entry;
716 }
717
718 void increment() { detail::directory_iterator_increment(*this, 0); }
719
720 bool equal(const directory_iterator& rhs) const
721 { return m_imp == rhs.m_imp; }
722 };
723
724//--------------------------------------------------------------------------------------//
725// //
726// recursive_directory_iterator helpers //
727// //
728//--------------------------------------------------------------------------------------//
729
730 NDNBOOST_SCOPED_ENUM_START(symlink_option)
731 {
732 none,
733 no_recurse = none, // don't follow directory symlinks (default behavior)
734 recurse, // follow directory symlinks
735 _detail_no_push = recurse << 1 // internal use only
736 };
737 NDNBOOST_SCOPED_ENUM_END
738
739 NDNBOOST_BITMASK(NDNBOOST_SCOPED_ENUM(symlink_option))
740
741 namespace detail
742 {
743 struct recur_dir_itr_imp
744 {
745 typedef directory_iterator element_type;
746 std::stack< element_type, std::vector< element_type > > m_stack;
747 int m_level;
748 NDNBOOST_SCOPED_ENUM(symlink_option) m_options;
749
750 recur_dir_itr_imp() : m_level(0), m_options(symlink_option::none) {}
751
752 void increment(system::error_code* ec); // ec == 0 means throw on error
753
754 void pop();
755
756 };
757
758 // Implementation is inline to avoid dynamic linking difficulties with m_stack:
759 // Microsoft warning C4251, m_stack needs to have dll-interface to be used by
760 // clients of struct 'ndnboost::filesystem::detail::recur_dir_itr_imp'
761
762 inline
763 void recur_dir_itr_imp::increment(system::error_code* ec)
764 // ec == 0 means throw on error
765 {
766 if ((m_options & symlink_option::_detail_no_push) == symlink_option::_detail_no_push)
767 m_options &= ~symlink_option::_detail_no_push;
768
769 else
770 {
771 // Logic for following predicate was contributed by Daniel Aarno to handle cyclic
772 // symlinks correctly and efficiently, fixing ticket #5652.
773 // if (((m_options & symlink_option::recurse) == symlink_option::recurse
774 // || !is_symlink(m_stack.top()->symlink_status()))
775 // && is_directory(m_stack.top()->status())) ...
776 // The predicate code has since been rewritten to pass error_code arguments,
777 // per ticket #5653.
778 bool or_pred = (m_options & symlink_option::recurse) == symlink_option::recurse
779 || (ec == 0 ? !is_symlink(m_stack.top()->symlink_status())
780 : !is_symlink(m_stack.top()->symlink_status(*ec)));
781 if (ec != 0 && *ec)
782 return;
783 bool and_pred = or_pred && (ec == 0 ? is_directory(m_stack.top()->status())
784 : is_directory(m_stack.top()->status(*ec)));
785 if (ec != 0 && *ec)
786 return;
787
788 if (and_pred)
789 {
790 if (ec == 0)
791 m_stack.push(directory_iterator(m_stack.top()->path()));
792 else
793 {
794 m_stack.push(directory_iterator(m_stack.top()->path(), *ec));
795 if (*ec)
796 return;
797 }
798 if (m_stack.top() != directory_iterator())
799 {
800 ++m_level;
801 return;
802 }
803 m_stack.pop();
804 }
805 }
806
807 while (!m_stack.empty() && ++m_stack.top() == directory_iterator())
808 {
809 m_stack.pop();
810 --m_level;
811 }
812 }
813
814 inline
815 void recur_dir_itr_imp::pop()
816 {
817 NDNBOOST_ASSERT_MSG(m_level > 0,
818 "pop() on recursive_directory_iterator with level < 1");
819
820 do
821 {
822 m_stack.pop();
823 --m_level;
824 }
825 while (!m_stack.empty() && ++m_stack.top() == directory_iterator());
826 }
827 } // namespace detail
828
829//--------------------------------------------------------------------------------------//
830// //
831// recursive_directory_iterator //
832// //
833//--------------------------------------------------------------------------------------//
834
835 class recursive_directory_iterator
836 : public ndnboost::iterator_facade<
837 recursive_directory_iterator,
838 directory_entry,
839 ndnboost::single_pass_traversal_tag >
840 {
841 public:
842
843 recursive_directory_iterator(){} // creates the "end" iterator
844
845 explicit recursive_directory_iterator(const path& dir_path,
846 NDNBOOST_SCOPED_ENUM(symlink_option) opt = symlink_option::none)
847 : m_imp(new detail::recur_dir_itr_imp)
848 {
849 m_imp->m_options = opt;
850 m_imp->m_stack.push(directory_iterator(dir_path));
851 if (m_imp->m_stack.top() == directory_iterator())
852 { m_imp.reset (); }
853 }
854
855 recursive_directory_iterator(const path& dir_path,
856 NDNBOOST_SCOPED_ENUM(symlink_option) opt,
857 system::error_code & ec)
858 : m_imp(new detail::recur_dir_itr_imp)
859 {
860 m_imp->m_options = opt;
861 m_imp->m_stack.push(directory_iterator(dir_path, ec));
862 if (m_imp->m_stack.top() == directory_iterator())
863 { m_imp.reset (); }
864 }
865
866 recursive_directory_iterator(const path& dir_path,
867 system::error_code & ec)
868 : m_imp(new detail::recur_dir_itr_imp)
869 {
870 m_imp->m_options = symlink_option::none;
871 m_imp->m_stack.push(directory_iterator(dir_path, ec));
872 if (m_imp->m_stack.top() == directory_iterator())
873 { m_imp.reset (); }
874 }
875
876 recursive_directory_iterator& increment(system::error_code& ec)
877 {
878 NDNBOOST_ASSERT_MSG(m_imp.get(),
879 "increment() on end recursive_directory_iterator");
880 m_imp->increment(&ec);
881 if (m_imp->m_stack.empty())
882 m_imp.reset(); // done, so make end iterator
883 return *this;
884 }
885
886 int level() const
887 {
888 NDNBOOST_ASSERT_MSG(m_imp.get(),
889 "level() on end recursive_directory_iterator");
890 return m_imp->m_level;
891 }
892
893 bool no_push_pending() const
894 {
895 NDNBOOST_ASSERT_MSG(m_imp.get(),
896 "is_no_push_requested() on end recursive_directory_iterator");
897 return (m_imp->m_options & symlink_option::_detail_no_push)
898 == symlink_option::_detail_no_push;
899 }
900
901# ifndef NDNBOOST_FILESYSTEM_NO_DEPRECATED
902 bool no_push_request() const { return no_push_pending(); }
903# endif
904
905 void pop()
906 {
907 NDNBOOST_ASSERT_MSG(m_imp.get(),
908 "pop() on end recursive_directory_iterator");
909 m_imp->pop();
910 if (m_imp->m_stack.empty()) m_imp.reset(); // done, so make end iterator
911 }
912
913 void no_push(bool value=true)
914 {
915 NDNBOOST_ASSERT_MSG(m_imp.get(),
916 "no_push() on end recursive_directory_iterator");
917 if (value)
918 m_imp->m_options |= symlink_option::_detail_no_push;
919 else
920 m_imp->m_options &= ~symlink_option::_detail_no_push;
921 }
922
923 file_status status() const
924 {
925 NDNBOOST_ASSERT_MSG(m_imp.get(),
926 "status() on end recursive_directory_iterator");
927 return m_imp->m_stack.top()->status();
928 }
929
930 file_status symlink_status() const
931 {
932 NDNBOOST_ASSERT_MSG(m_imp.get(),
933 "symlink_status() on end recursive_directory_iterator");
934 return m_imp->m_stack.top()->symlink_status();
935 }
936
937 private:
938
939 // shared_ptr provides shallow-copy semantics required for InputIterators.
940 // m_imp.get()==0 indicates the end iterator.
941 ndnboost::shared_ptr< detail::recur_dir_itr_imp > m_imp;
942
943 friend class ndnboost::iterator_core_access;
944
945 ndnboost::iterator_facade<
946 recursive_directory_iterator,
947 directory_entry,
948 ndnboost::single_pass_traversal_tag >::reference
949 dereference() const
950 {
951 NDNBOOST_ASSERT_MSG(m_imp.get(),
952 "dereference of end recursive_directory_iterator");
953 return *m_imp->m_stack.top();
954 }
955
956 void increment()
957 {
958 NDNBOOST_ASSERT_MSG(m_imp.get(),
959 "increment of end recursive_directory_iterator");
960 m_imp->increment(0);
961 if (m_imp->m_stack.empty())
962 m_imp.reset(); // done, so make end iterator
963 }
964
965 bool equal(const recursive_directory_iterator& rhs) const
966 { return m_imp == rhs.m_imp; }
967
968 };
969
970# if !defined(NDNBOOST_FILESYSTEM_NO_DEPRECATED)
971 typedef recursive_directory_iterator wrecursive_directory_iterator;
972# endif
973
974//--------------------------------------------------------------------------------------//
975// //
976// class filesystem_error //
977// //
978//--------------------------------------------------------------------------------------//
979
980 class NDNBOOST_SYMBOL_VISIBLE filesystem_error : public system::system_error
981 {
982 // see http://www.boost.org/more/error_handling.html for design rationale
983
984 // all functions are inline to avoid issues with crossing dll boundaries
985
986 public:
987 // compiler generates copy constructor and copy assignment
988
989 filesystem_error(
990 const std::string & what_arg, system::error_code ec)
991 : system::system_error(ec, what_arg)
992 {
993 try
994 {
995 m_imp_ptr.reset(new m_imp);
996 }
997 catch (...) { m_imp_ptr.reset(); }
998 }
999
1000 filesystem_error(
1001 const std::string & what_arg, const path& path1_arg,
1002 system::error_code ec)
1003 : system::system_error(ec, what_arg)
1004 {
1005 try
1006 {
1007 m_imp_ptr.reset(new m_imp);
1008 m_imp_ptr->m_path1 = path1_arg;
1009 }
1010 catch (...) { m_imp_ptr.reset(); }
1011 }
1012
1013 filesystem_error(
1014 const std::string & what_arg, const path& path1_arg,
1015 const path& path2_arg, system::error_code ec)
1016 : system::system_error(ec, what_arg)
1017 {
1018 try
1019 {
1020 m_imp_ptr.reset(new m_imp);
1021 m_imp_ptr->m_path1 = path1_arg;
1022 m_imp_ptr->m_path2 = path2_arg;
1023 }
1024 catch (...) { m_imp_ptr.reset(); }
1025 }
1026
1027 ~filesystem_error() throw() {}
1028
1029 const path& path1() const
1030 {
1031 static const path empty_path;
1032 return m_imp_ptr.get() ? m_imp_ptr->m_path1 : empty_path ;
1033 }
1034 const path& path2() const
1035 {
1036 static const path empty_path;
1037 return m_imp_ptr.get() ? m_imp_ptr->m_path2 : empty_path ;
1038 }
1039
1040 const char* what() const throw()
1041 {
1042 if (!m_imp_ptr.get())
1043 return system::system_error::what();
1044
1045 try
1046 {
1047 if (m_imp_ptr->m_what.empty())
1048 {
1049 m_imp_ptr->m_what = system::system_error::what();
1050 if (!m_imp_ptr->m_path1.empty())
1051 {
1052 m_imp_ptr->m_what += ": \"";
1053 m_imp_ptr->m_what += m_imp_ptr->m_path1.string();
1054 m_imp_ptr->m_what += "\"";
1055 }
1056 if (!m_imp_ptr->m_path2.empty())
1057 {
1058 m_imp_ptr->m_what += ", \"";
1059 m_imp_ptr->m_what += m_imp_ptr->m_path2.string();
1060 m_imp_ptr->m_what += "\"";
1061 }
1062 }
1063 return m_imp_ptr->m_what.c_str();
1064 }
1065 catch (...)
1066 {
1067 return system::system_error::what();
1068 }
1069 }
1070
1071 private:
1072 struct m_imp
1073 {
1074 path m_path1; // may be empty()
1075 path m_path2; // may be empty()
1076 std::string m_what; // not built until needed
1077 };
1078 ndnboost::shared_ptr<m_imp> m_imp_ptr;
1079 };
1080
1081// test helper -----------------------------------------------------------------------//
1082
1083// Not part of the documented interface since false positives are possible;
1084// there is no law that says that an OS that has large stat.st_size
1085// actually supports large file sizes.
1086
1087 namespace detail
1088 {
1089 NDNBOOST_FILESYSTEM_DECL bool possible_large_file_size_support();
1090 }
1091
1092 } // namespace filesystem
1093} // namespace ndnboost
1094
1095#include <ndnboost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
1096#endif // NDNBOOST_FILESYSTEM3_OPERATIONS_HPP