aboutsummaryrefslogtreecommitdiff
path: root/butl/filesystem
blob: 63b19ffa08cd5cf24be8ec3fe8916c1ab1e2fa19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// file      : butl/filesystem -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef BUTL_FILESYSTEM
#define BUTL_FILESYSTEM

#include <sys/types.h> // mode_t

#ifndef _WIN32
#  include <dirent.h> // DIR, struct dirent, *dir()
#endif

#include <cstddef>  // ptrdiff_t
#include <utility>  // move()
#include <iterator>

#include <butl/path>
#include <butl/timestamp>

namespace butl
{
  // Return timestamp_nonexistent if the entry at the specified path
  // does not exist or is not a path. All other errors are reported
  // by throwing std::system_error. Note that this function resolves
  // symlinks.
  //
  timestamp
  file_mtime (const path&);

  // Return true if the path is to an existing directory. Note that
  // this function resolves symlinks.
  //
  bool
  dir_exists (const path&);

  // Return true if the path is to an existing regular file. Note that
  // this function resolves symlinks.
  //
  bool
  file_exists (const path&);

  // Try to create a directory unless it already exists. If you expect
  // the directory to exist and performance is important, then you
  // should first call dir_exists() above since that's what this
  // implementation will do to make sure the path is actually a
  // directory.
  //
  // You should also probably use the default mode 0777 and let the
  // umask mechanism adjust it to the user's preferences.
  //
  // Errors are reported by throwing std::system_error.
  //
  enum class mkdir_status {success, already_exists};

  mkdir_status
  try_mkdir (const path&, mode_t = 0777);

  // The '-p' version of the above (i.e., it creates the parent
  // directories if necessary).
  //
  mkdir_status
  try_mkdir_p (const path&, mode_t = 0777);

  // Try to remove the directory returning not_exist if it does not
  // exist and not_empty if it is not empty. All other errors are
  // reported by throwing std::system_error.
  //
  enum class rmdir_status {success, not_exist, not_empty};

  rmdir_status
  try_rmdir (const path&);

  // Try to remove the file (or symlinks) returning not_exist if
  // it does not exist. All other errors are reported by throwing
  // std::system_error.
  //
  enum class rmfile_status {success, not_exist};

  rmfile_status
  try_rmfile (const path&);

  // Directory entry iteration.
  //
  enum class entry_type
  {
    unknown,
    regular,
    directory,
    symlink,
    other
  };

  class dir_entry
  {
  public:
    typedef butl::path path_type;

    entry_type
    type () const;

    // Symlink target type in case of the symlink, type() otherwise.
    //
    entry_type
    ltype () const;

    // Entry path (excluding the base). To get the full path, do
    // base () / path ().
    //
    const path_type&
    path () const {return p_;}

    const dir_path&
    base () const {return b_;}

    dir_entry () = default;
    dir_entry (entry_type t, path_type p, dir_path b)
        : t_ (t), p_ (std::move (p)), b_ (std::move (b)) {}

  private:
    entry_type
    type (bool link) const;

  private:
    friend class dir_iterator;

    mutable entry_type t_ = entry_type::unknown;  // Lazy evaluation.
    mutable entry_type lt_ = entry_type::unknown; // Lazy evaluation.
    path_type p_;
    dir_path b_;
  };

  class dir_iterator
  {
  public:
    typedef dir_entry value_type;
    typedef const dir_entry* pointer;
    typedef const dir_entry& reference;
    typedef std::ptrdiff_t difference_type;
    typedef std::input_iterator_tag iterator_category;

    ~dir_iterator ();
    dir_iterator () = default;

    explicit
    dir_iterator (const dir_path&);

    dir_iterator (const dir_iterator&) = delete;
    dir_iterator& operator= (const dir_iterator&) = delete;

    dir_iterator (dir_iterator&& x);
    dir_iterator& operator= (dir_iterator&&);

    dir_iterator& operator++ () {next (); return *this;}

    reference operator* () const {return e_;}
    pointer operator-> () const {return &e_;}

    friend bool operator== (const dir_iterator&, const dir_iterator&);
    friend bool operator!= (const dir_iterator&, const dir_iterator&);

  private:
    void
    next ();

  private:
    dir_entry e_;

#ifndef _WIN32
    DIR* h_ {nullptr};
#else
    // Check move implementation.
    //
#  error Win32 implementation lacking
#endif
  };

  // Range-based for loop support.
  //
  // for (const auto& de: dir_iterator (dir_path ("/tmp"))) ...
  //
  // Note that the "range" (which is the "begin" iterator), is no
  // longer usable. In other words, don't do this:
  //
  // dir_iterator i (...);
  // for (...: i) ...
  // ++i; // Invalid.
  //
  inline dir_iterator begin (dir_iterator& i) {return std::move (i);}
  inline dir_iterator end (const dir_iterator&) {return dir_iterator ();}
}

#include <butl/filesystem.ixx>

#endif // BUTL_FILESYSTEM