Welcome to mirror list, hosted at ThFree Co, Russian Federation.

contents.hpp « algorithm « v2.0 « llfio « include - github.com/windirstat/llfio.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d17f257e7ea83f3ae8da56a687db64f5a1673d5f (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* A filesystem algorithm which returns the contents of a directory tree
(C) 2020 Niall Douglas <http://www.nedproductions.biz/> (12 commits)
File Created: July 2020


Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License in the accompanying file
Licence.txt or at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


Distributed under the Boost Software License, Version 1.0.
    (See accompanying file Licence.txt or copy at
          http://www.boost.org/LICENSE_1_0.txt)
*/

#ifndef LLFIO_ALGORITHM_CONTENTS_HPP
#define LLFIO_ALGORITHM_CONTENTS_HPP

#include "traverse.hpp"

#include <memory>
#include <mutex>

//! \file contents.hpp Provides a directory tree contents algorithm.

LLFIO_V2_NAMESPACE_BEGIN

namespace algorithm
{
  /*! \brief A visitor for the filesystem contents algorithm.
   */
  struct contents_visitor : traverse_visitor
  {
    //! Whether to include files in the contents.
    bool contents_include_files{true};
    //! Whether to include directories in the contents.
    bool contents_include_directories{true};
    //! Whether to include symlinks in the contents.
    bool contents_include_symlinks{true};
    //! What `stat_t::want` to include, if enumeration doesn't provide these, they will be additionally fetched.
    stat_t::want contents_include_metadata{stat_t::want::none};

    /*! \brief Enumerated contents, and what parts of their `stat_t` is valid.
     */
    struct contents_type : public std::vector<std::pair<filesystem::path, stat_t>>
    {
      //! The metadata valid within all the `stat_t` in the contents traversed.
      stat_t::want metadata{stat_t::want::none};
    };

    //! Default construtor
    contents_visitor() = default;
    //! Construct an instance
    explicit contents_visitor(stat_t::want _metadata, bool _include_files = true, bool _include_directories = true, bool _include_symlinks = true)
        : contents_include_files(_include_files)
        , contents_include_directories(_include_directories)
        , contents_include_symlinks(_include_symlinks)
        , contents_include_metadata(_metadata)
    {
    }

    friend inline result<contents_type> contents(const path_handle &dirh, contents_visitor *visitor, size_t threads, bool force_slow_path) noexcept;

  protected:
    struct _state_type
    {
      const path_handle &rootdirh;
      std::atomic<size_t> rootdirpathlen{0};
      std::atomic<stat_t::want> metadata{stat_t::want::all};
      contents_type contents;

      std::mutex lock;
      std::vector<std::shared_ptr<contents_type>> all_thread_contents;

      explicit _state_type(const path_handle &_rootdirh)
          : rootdirh(_rootdirh)
      {
      }
    };

    static std::shared_ptr<contents_type> _thread_contents(_state_type *state) noexcept
    {
      try
      {
        static thread_local std::weak_ptr<contents_type> mycontents;
        auto ret = mycontents.lock();
        if(ret)
        {
          return ret;
        }
        ret = std::make_unique<contents_type>();
        mycontents = ret;
        std::lock_guard<std::mutex> g(state->lock);
        state->all_thread_contents.push_back(ret);
        return ret;
      }
      catch(...)
      {
        return {};
      }
    }

  public:
    /*! \brief The default implementation accumulates the contents into thread
    local storage. At traverse end, all the thread local storages are coalesced
    into a single result, the member variable `contents`.
    */
    virtual result<void> post_enumeration(void *data, const directory_handle &dirh, directory_handle::buffers_type &contents, size_t depth) noexcept
    {
      try
      {
        auto *state = (_state_type *) data;
        (void) depth;
        if(!contents.empty())
        {
          contents_type toadd;
          toadd.reserve(contents.size());
          filesystem::path dirhpath;
          for(;;)
          {
            OUTCOME_TRY(dirhpath, dirh.current_path());
            auto rootdirpathlen = state->rootdirpathlen.load(std::memory_order_relaxed);
            if(dirhpath.native().size() <= rootdirpathlen)
            {
              break;
            }
            dirhpath = dirhpath.native().substr(rootdirpathlen);
            auto r = directory_handle::directory(state->rootdirh, dirhpath);
            if(r && r.value().unique_id() == dirh.unique_id())
            {
              break;
            }
            OUTCOME_TRY(dirhpath, state->rootdirh.current_path());
            state->rootdirpathlen.store(dirhpath.native().size() + 1, std::memory_order_relaxed);
          }
          auto _metadata_ = state->metadata.load(std::memory_order_relaxed);
          if((_metadata_ & (contents_include_metadata | contents.metadata())) != _metadata_)
          {
            state->metadata.store(_metadata_ & (contents_include_metadata | contents.metadata()), std::memory_order_relaxed);
          }
          auto into = _thread_contents(state);
          for(auto &entry : contents)
          {
            auto need_stat = contents_include_metadata & ~contents.metadata();
            if((contents_include_files && entry.stat.st_type == filesystem::file_type::regular) ||
               (contents_include_directories && entry.stat.st_type == filesystem::file_type::directory) ||
               (contents_include_symlinks && entry.stat.st_type == filesystem::file_type::symlink))
            {
              if(!need_stat)
              {
                into->emplace_back(dirhpath / entry.leafname, entry.stat);
                continue;
              }
              auto r = file_handle::file(dirh, entry.leafname, file_handle::mode::attr_read);
              if(r)
              {
                OUTCOME_TRY(entry.stat.fill(r.value(), need_stat));
                into->emplace_back(dirhpath / entry.leafname, entry.stat);
              }
            }
          }
        }
        return success();
      }
      catch(...)
      {
        return error_from_exception();
      }
    }

    /*! \brief Called when a traversal finishes, this default implementation merges
    all the thread local results into `contents`, and deallocates the thread local
    results.
    */
    virtual result<size_t> finished(void *data, result<size_t> result) noexcept
    {
      try
      {
        auto *state = (_state_type *) data;
        state->contents.clear();
        state->contents.metadata = state->metadata.load(std::memory_order_relaxed);
        size_t count = 0;
        for(auto &i : state->all_thread_contents)
        {
          count += i->size();
        }
        state->contents.reserve(count);
        for(auto &i : state->all_thread_contents)
        {
          state->contents.insert(state->contents.end(), std::make_move_iterator(i->begin()), std::make_move_iterator(i->end()));
        }
        state->all_thread_contents.clear();
        return result;
      }
      catch(...)
      {
        return error_from_exception();
      }
    }
  };


  /*! \brief Calculate the contents of everything within and under `dirh`. What
  is returned is unordered.

  This is a very thin veneer over `traverse()` which came out of the fact that
  I kept writing "get me the contents" traversal visitors again and again, so
  eventually I just wrote a library edition. Its only "clever" bit is that it
  stores the contents in thread local storage, and merges the contents afterwards.

  It is race free to concurrent relocations of `dirh`. It is entirely implemented
  in header-only code, as it is very simple.
  */
  inline result<contents_visitor::contents_type> contents(const path_handle &dirh, contents_visitor *visitor = nullptr, size_t threads = 0,
                                                          bool force_slow_path = false) noexcept
  {
    contents_visitor default_visitor;
    if(visitor == nullptr)
    {
      visitor = &default_visitor;
    }
    contents_visitor::_state_type state(dirh);
    OUTCOME_TRY(auto &&dirhpath, dirh.current_path());
    state.rootdirpathlen.store(dirhpath.native().size() + 1, std::memory_order_relaxed);
    OUTCOME_TRY(traverse(dirh, visitor, threads, &state, force_slow_path));
    return {std::move(state.contents)};
  }

}  // namespace algorithm

LLFIO_V2_NAMESPACE_END

#endif