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

path_discovery.ipp « impl « detail « v2.0 « llfio « include - github.com/windirstat/llfio.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 318bad07806b48546bca4e2a86a7b11bf89e8b7c (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* Discovery of various useful filesystem paths
(C) 2017 Niall Douglas <http://www.nedproductions.biz/> (20 commits)
File Created: Sept 2017


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)
*/

#include "../../path_discovery.hpp"

#include "../../directory_handle.hpp"
#include "../../statfs.hpp"

#include <mutex>
#include <regex>
#include <vector>

LLFIO_V2_NAMESPACE_EXPORT_BEGIN

namespace path_discovery
{
  struct _store
  {
    std::mutex lock;
    std::vector<discovered_path> all;
    span<discovered_path> verified;
    struct _discovered_path
    {
      filesystem::path path;
      size_t priority{0};
      std::string fstypename;
      directory_handle h;  // not retained after verification
      explicit _discovered_path(filesystem::path _path)
          : path(std::move(_path))
      {
      }
    };
    std::vector<_discovered_path> _all;
    directory_handle storage_backed, memory_backed;
  };
  inline _store &path_store()
  {
    static _store s;
    return s;
  }

  inline std::vector<std::pair<discovered_path::source_type, _store::_discovered_path>> _all_temporary_directories();

  span<discovered_path> all_temporary_directories(bool refresh) noexcept
  {
    auto &ps = path_store();
    if(!refresh && !ps.all.empty())
    {
      return ps.all;
    }
    std::lock_guard<std::mutex> g(ps.lock);
    if(refresh)
    {
      ps.all.clear();
      ps._all.clear();
      ps.verified = {};
      ps.storage_backed = {};
      ps.memory_backed = {};
    }
    if(!ps.all.empty())
    {
      return ps.all;
    }
    try
    {
      std::vector<std::pair<discovered_path::source_type, _store::_discovered_path>> raw = _all_temporary_directories();
      if(raw.empty())
      {
        LLFIO_LOG_FATAL(nullptr, "path_discovery::all_temporary_directories() sees no possible temporary directories, something has gone very wrong");
        abort();
      }
      for(size_t n = 0; n < raw.size(); n++)
      {
        raw[n].second.priority = n;
      }
      // Firstly sort by source and path so duplicates are side by side
      std::sort(raw.begin(), raw.end(), [](const auto &a, const auto &b) { return (a.first < b.first) || (a.first == b.first && a.second.path < b.second.path); });
      // Remove duplicates
      raw.erase(std::unique(raw.begin(), raw.end(), [](const auto &a, const auto &b) { return a.first == b.first && a.second.path == b.second.path; }), raw.end());
      // Put them back into the order in which they were returned
      std::sort(raw.begin(), raw.end(), [](const auto &a, const auto &b) { return a.second.priority < b.second.priority; });
      ps.all.reserve(raw.size());
      ps._all.reserve(raw.size());
      for(auto &i : raw)
      {
        ps._all.push_back(std::move(i.second));
        discovered_path dp;
        dp.path = ps._all.back().path;
        dp.source = i.first;
        ps.all.push_back(std::move(dp));
      }
    }
    catch(const std::exception &e)
    {
      std::string msg("path_discovery::all_temporary_directories() saw exception thrown: ");
      msg.append(e.what());
      LLFIO_LOG_WARN(nullptr, msg.c_str());
    }
    catch(...)
    {
      LLFIO_LOG_WARN(nullptr, "path_discovery::all_temporary_directories() saw unknown exception throw");
    }
    return ps.all;
  }

  span<discovered_path> verified_temporary_directories() noexcept
  {
    auto &ps = path_store();
    if(!ps.verified.empty())
    {
      return ps.verified;
    }
    (void) all_temporary_directories();
    std::lock_guard<std::mutex> g(ps.lock);
    if(!ps.verified.empty())
    {
      return ps.verified;
    }
    try
    {
      // Firstly go try to open and stat all items
      for(size_t n = 0; n < ps.all.size(); n++)
      {
        {
          log_level_guard logg(log_level::fatal);  // suppress log printing of failure
          (void) logg;
          auto _h = directory_handle::directory({}, ps.all[n].path);
          if(!_h)
          {
            // Error during opening
            continue;
          }
          ps._all[n].h = std::move(_h).value();
        }
        // Try to create a small file in that directory
        auto _fh = file_handle::random_file(ps._all[n].h, file_handle::mode::write, file_handle::caching::temporary, file_handle::flag::unlink_on_first_close);
        if(!_fh)
        {
#if LLFIO_LOGGING_LEVEL >= 3
          std::string msg("path_discovery::verified_temporary_directories() failed to create a file in ");
          msg.append(ps._all[n].path.u8string());
          msg.append(" due to ");
          msg.append(_fh.error().message().c_str());
          LLFIO_LOG_WARN(nullptr, msg.c_str());
#endif
          ps._all[n].h = {};
          continue;
        }
        ps.all[n].stat = stat_t(nullptr);
        if(!ps.all[n].stat->fill(ps._all[n].h))
        {
          LLFIO_LOG_WARN(nullptr, "path_discovery::verified_temporary_directories() failed to stat an open handle to a temp directory");
          ps.all[n].stat = {};
          ps._all[n].h = {};
          continue;
        }
        statfs_t statfs;
        auto statfsres = statfs.fill(_fh.value(), statfs_t::want::fstypename);
        if(statfsres)
        {
          ps._all[n].fstypename = std::move(statfs.f_fstypename);
        }
        else
        {
#if LLFIO_LOGGING_LEVEL >= 3
          std::string msg("path_discovery::verified_temporary_directories() failed to statfs the temp directory ");
          msg.append(ps._all[n].path.u8string());
          msg.append(" due to ");
          msg.append(statfsres.error().message().c_str());
          LLFIO_LOG_WARN(nullptr, msg.c_str());
#endif
          ps.all[n].stat = {};
          ps._all[n].h = {};
          continue;
        }
      }
      // Now partition into those with valid stat directories and those without
      std::stable_partition(ps._all.begin(), ps._all.end(), [](const _store::_discovered_path &a) { return a.h.is_valid(); });
      auto it = std::stable_partition(ps.all.begin(), ps.all.end(), [](const discovered_path &a) { return a.stat; });
      ps.verified = span<discovered_path>(ps.all.data(), it - ps.all.begin());
      if(ps.verified.empty())
      {
        LLFIO_LOG_FATAL(nullptr, "path_discovery::verified_temporary_directories() could not find at least one writable temporary directory");
        abort();
      }

      // Finally, need to choose storage and memory backed directories
      std::regex storage_backed_regex("btrfs|cifs|exfat|ext[2-4]|f2fs|hfs|jfs|lxfs|nfs|nilf2|ufs|vfat|xfs|zfs|msdosfs|newnfs|ntfs|smbfs|unionfs|fat|fat32", std::regex::icase);
      std::regex memory_backed_regex("tmpfs|ramfs", std::regex::icase);
      for(size_t n = 0; n < ps.verified.size(); n++)
      {
        if(!ps.storage_backed.is_valid() && std::regex_match(ps._all[n].fstypename, storage_backed_regex))
        {
          ps.storage_backed = std::move(ps._all[n].h);
        }
        if(!ps.memory_backed.is_valid() && std::regex_match(ps._all[n].fstypename, memory_backed_regex))
        {
          ps.memory_backed = std::move(ps._all[n].h);
        }
        ps.all[n].path = ps._all[n].path;
        (void) ps._all[n].h.close();
      }
    }
    catch(const std::exception &e)
    {
      std::string msg("path_discovery::verified_temporary_directories() saw exception thrown: ");
      msg.append(e.what());
      LLFIO_LOG_FATAL(nullptr, msg.c_str());
      abort();
    }
    catch(...)
    {
      LLFIO_LOG_FATAL(nullptr, "path_discovery::verified_temporary_directories() saw unknown exception throw");
      abort();
    }
    return ps.verified;
  }

  const path_handle &storage_backed_temporary_files_directory() noexcept
  {
    (void) verified_temporary_directories();
    auto &ps = path_store();
    return ps.storage_backed;
  }
  const path_handle &memory_backed_temporary_files_directory() noexcept
  {
    (void) verified_temporary_directories();
    auto &ps = path_store();
    return ps.memory_backed;
  }
}  // namespace path_discovery

LLFIO_V2_NAMESPACE_END

#define LLFIO_PATH_DISCOVERY_INCLUDING
#ifdef _WIN32
#include "windows/path_discovery.ipp"
#else
#include "posix/path_discovery.ipp"
#endif
#undef LLFIO_PATH_DISCOVERY_INCLUDING