Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
Filesystem.h
Go to the documentation of this file.
1 //===-- serialbox/core/Filesystem.h --------------------------------------------------*- C++
2 //-*-===//
3 //
4 // S E R I A L B O X
5 //
6 // This file is distributed under terms of BSD license.
7 // See LICENSE.txt for more information
8 //
9 //===------------------------------------------------------------------------------------------===//
10 //
14 //===------------------------------------------------------------------------------------------===//
15 
16 #ifndef SERIALBOX_CORE_FILESYSTEM_H
17 #define SERIALBOX_CORE_FILESYSTEM_H
18 
19 #include "serialbox/core/Config.h"
20 
21 #ifdef SERIALBOX_USE_STD_EXPERIMENTAL_FILESYSTEM
22 #include <experimental/filesystem>
23 namespace filesystem = std::experimental::filesystem;
24 
25 // work-around for problem in some versions of the stdc++fs
26 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71313
27 namespace serialbox {
28 inline std::uintmax_t remove_all(const filesystem::path& p, std::error_code& ec) noexcept {
29  auto fs = filesystem::symlink_status(p, ec);
30  uintmax_t count = 0;
31  if(ec.value() == 0 && fs.type() == filesystem::file_type::directory)
32  for(filesystem::directory_iterator d(p, ec), end; ec.value() == 0 && d != end; ++d)
33  count += serialbox::remove_all(d->path(), ec);
34  if(ec.value())
35  return -1;
36  return filesystem::remove(p, ec) ? ++count : -1;
37 }
38 
39 inline std::uintmax_t remove_all(const filesystem::path& p) {
40  std::error_code ec;
41  bool result = serialbox::remove_all(p, ec);
42  if(ec.value())
43  _GLIBCXX_THROW_OR_ABORT(filesystem::filesystem_error("cannot remove all", p, ec));
44  return result;
45 }
46 }
47 #elif SERIALBOX_USE_BOOST_FILESYSTEM
48 #include <boost/filesystem.hpp>
49 namespace filesystem = boost::filesystem;
50 
51 namespace serialbox {
52 inline boost::uintmax_t remove_all(const filesystem::path& p) { return filesystem::remove_all(p); }
53 
54 inline boost::uintmax_t remove_all(const filesystem::path& p,
55  boost::system::error_code& ec) BOOST_NOEXCEPT {
56  return filesystem::remove_all(p, ec);
57 }
58 }
59 #else
60 #error "Filesystem selection is not properly configured."
61 #endif
62 
63 #endif
Namespace of the serialbox library.
Definition: Archive.h:25