2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/port/FileFd.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
2018-09-13 19:41:26 +02:00
|
|
|
#include <functional>
|
2019-05-22 20:17:24 +02:00
|
|
|
#include <type_traits>
|
2018-09-12 05:26:05 +02:00
|
|
|
#include <utility>
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
|
|
|
|
|
|
|
Status mkdir(CSlice dir, int32 mode = 0700) TD_WARN_UNUSED_RESULT;
|
2018-09-13 19:41:26 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status mkpath(CSlice path, int32 mode = 0700) TD_WARN_UNUSED_RESULT;
|
2018-09-13 19:41:26 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status rename(CSlice from, CSlice to) TD_WARN_UNUSED_RESULT;
|
2018-09-13 19:41:26 +02:00
|
|
|
|
2018-01-26 14:47:46 +01:00
|
|
|
Result<string> realpath(CSlice slice, bool ignore_access_denied = false) TD_WARN_UNUSED_RESULT;
|
2018-09-13 19:41:26 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status chdir(CSlice dir) TD_WARN_UNUSED_RESULT;
|
2018-09-13 19:41:26 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status rmdir(CSlice dir) TD_WARN_UNUSED_RESULT;
|
2018-09-13 19:41:26 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status unlink(CSlice path) TD_WARN_UNUSED_RESULT;
|
2018-09-13 19:41:26 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
Status rmrf(CSlice path) TD_WARN_UNUSED_RESULT;
|
2018-09-13 19:41:26 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status set_temporary_dir(CSlice dir) TD_WARN_UNUSED_RESULT;
|
2018-09-13 19:41:26 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
CSlice get_temporary_dir();
|
2018-09-13 19:41:26 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Result<std::pair<FileFd, string>> mkstemp(CSlice dir) TD_WARN_UNUSED_RESULT;
|
2018-09-13 19:41:26 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Result<string> mkdtemp(CSlice dir, Slice prefix) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
2019-05-01 11:23:19 +02:00
|
|
|
class WalkPath {
|
|
|
|
public:
|
|
|
|
enum class Action { Continue, Abort, SkipDir };
|
|
|
|
enum class Type { EnterDir, ExitDir, NotDir };
|
|
|
|
|
|
|
|
template <class F, class R = decltype(std::declval<F>()("", Type::ExitDir))>
|
2019-05-01 22:31:07 +02:00
|
|
|
static TD_WARN_UNUSED_RESULT std::enable_if_t<std::is_same<R, Action>::value, Status> run(CSlice path, F &&func) {
|
2019-05-01 11:23:19 +02:00
|
|
|
return do_run(path, func);
|
|
|
|
}
|
|
|
|
template <class F, class R = decltype(std::declval<F>()("", Type::ExitDir))>
|
2019-05-22 20:17:24 +02:00
|
|
|
static TD_WARN_UNUSED_RESULT std::enable_if_t<!std::is_same<R, Action>::value, Status> run(CSlice path, F &&func) {
|
2019-05-01 11:23:19 +02:00
|
|
|
return do_run(path, [&](CSlice name, Type type) {
|
|
|
|
func(name, type);
|
|
|
|
return Action::Continue;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static TD_WARN_UNUSED_RESULT Status do_run(CSlice path,
|
|
|
|
const std::function<WalkPath::Action(CSlice name, Type type)> &func);
|
|
|
|
};
|
|
|
|
|
|
|
|
// deprecated interface
|
|
|
|
template <class F>
|
|
|
|
TD_WARN_UNUSED_RESULT Status walk_path(CSlice path, F &&func) {
|
|
|
|
return WalkPath::run(path, func);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
} // namespace td
|