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