2017-04-01 01:09:34 -07:00
|
|
|
#pragma once
|
|
|
|
|
2017-10-16 11:44:04 -07:00
|
|
|
#include <algorithm>
|
2019-08-20 08:47:26 -07:00
|
|
|
#include <functional>
|
2017-07-12 17:40:41 -07:00
|
|
|
#include <map>
|
2017-08-25 16:03:57 -07:00
|
|
|
#include <mutex>
|
[vcpkg] Make Filesystem::remove_all faster #7570
I added benchmarks to measure how fast the parallel remove_all code was
-- it turns out, about 3x slower than stdfs::remove_all. Since this was
the case, I removed all of the parallelism and rewrote it serially, and
ended up about 30% faster than stdfs::remove_all (in addition to
supporting symlinks).
In addition, I did the following three orthogonal changes:
- simplified the work queue, basing it on Billy O'Neal's idea
- Fix warnings on older versions of compilers in tests, by splitting
the pragmas out of pch.h.
- Ran clang-format on some files
In fixing up remove_all, the following changes were made:
- On Windows, regular symlinks and directory symlinks are distinct;
as an example, to remove directory symlinks (and junctions, for that
matter), one must use RemoveDirectory. Only on Windows, I added new
`file_type` and `file_status` types, with `file_type` including a new
`directory_symlink` enumerator, and `file_status` being exactly the
same as the old one except using the new `file_type`. On Unix, I
didn't make that change since they don't make a distinction.
- I added new `symlink_status` and `status` functions which use the
new `file_status` on Windows.
- I made `Filesystem::exists` call `fs::exists(status(p))`, as opposed
to the old version which called `stdfs::exists` directly.
- Added benchmarks to `vcpkg-test/files.cpp`. They test the
performance of `remove_all` on small directories (~20 files), with
symlinks and without, and on large directories (~2000 files), with
symlinks and without.
2019-08-02 16:49:45 -07:00
|
|
|
#include <type_traits>
|
2018-03-14 16:48:18 -07:00
|
|
|
#include <unordered_map>
|
2017-04-01 01:09:34 -07:00
|
|
|
#include <utility>
|
2017-04-27 17:56:06 -07:00
|
|
|
#include <vector>
|
2017-04-01 01:09:34 -07:00
|
|
|
|
|
|
|
namespace vcpkg::Util
|
|
|
|
{
|
2017-10-15 23:53:35 -07:00
|
|
|
template<class Container>
|
2019-04-08 23:26:18 -07:00
|
|
|
using ElementT =
|
|
|
|
std::remove_reference_t<decltype(*std::declval<typename std::remove_reference_t<Container>::iterator>())>;
|
2017-10-15 23:53:35 -07:00
|
|
|
|
|
|
|
namespace Vectors
|
|
|
|
{
|
|
|
|
template<class Container, class T = ElementT<Container>>
|
2020-02-03 14:22:52 -08:00
|
|
|
void append(std::vector<T>* augend, const Container& addend)
|
2017-10-15 23:53:35 -07:00
|
|
|
{
|
|
|
|
augend->insert(augend->end(), addend.begin(), addend.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-26 18:59:11 -07:00
|
|
|
namespace Sets
|
|
|
|
{
|
2018-03-14 10:30:27 -07:00
|
|
|
template<class Container, class Key>
|
|
|
|
bool contains(const Container& container, const Key& item)
|
2017-10-26 18:59:11 -07:00
|
|
|
{
|
2018-03-14 10:30:27 -07:00
|
|
|
return container.find(item) != container.end();
|
2017-10-26 18:59:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 16:48:18 -07:00
|
|
|
namespace Maps
|
|
|
|
{
|
|
|
|
template<class K, class V1, class V2, class Func>
|
|
|
|
void transform_values(const std::unordered_map<K, V1>& container, std::unordered_map<K, V2>& output, Func func)
|
|
|
|
{
|
|
|
|
std::for_each(container.cbegin(), container.cend(), [&](const std::pair<const K, V1>& p) {
|
|
|
|
output[p.first] = func(p.second);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 23:26:18 -07:00
|
|
|
template<class Range, class Func>
|
|
|
|
using FmapOut = std::remove_reference_t<decltype(std::declval<Func&>()(*std::declval<Range>().begin()))>;
|
2017-04-01 01:09:34 -07:00
|
|
|
|
2019-04-08 23:26:18 -07:00
|
|
|
template<class Range, class Func, class Out = FmapOut<Range, Func>>
|
|
|
|
std::vector<Out> fmap(Range&& xs, Func&& f)
|
2017-04-01 01:09:34 -07:00
|
|
|
{
|
2017-08-23 15:47:42 -07:00
|
|
|
std::vector<Out> ret;
|
2017-04-01 01:09:34 -07:00
|
|
|
ret.reserve(xs.size());
|
|
|
|
|
|
|
|
for (auto&& x : xs)
|
|
|
|
ret.push_back(f(x));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2017-04-12 22:12:37 -07:00
|
|
|
|
2017-08-23 15:47:42 -07:00
|
|
|
template<class Cont, class Func>
|
|
|
|
using FmapFlattenOut = std::decay_t<decltype(*begin(std::declval<Func>()(*begin(std::declval<Cont>()))))>;
|
|
|
|
|
|
|
|
template<class Cont, class Func, class Out = FmapFlattenOut<Cont, Func>>
|
|
|
|
std::vector<Out> fmap_flatten(Cont&& xs, Func&& f)
|
|
|
|
{
|
|
|
|
std::vector<Out> ret;
|
|
|
|
|
|
|
|
for (auto&& x : xs)
|
|
|
|
for (auto&& y : f(x))
|
|
|
|
ret.push_back(std::move(y));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-12 22:12:37 -07:00
|
|
|
template<class Container, class Pred>
|
2017-04-13 15:37:24 -07:00
|
|
|
void erase_remove_if(Container& cont, Pred pred)
|
2017-04-12 22:12:37 -07:00
|
|
|
{
|
|
|
|
cont.erase(std::remove_if(cont.begin(), cont.end(), pred), cont.end());
|
|
|
|
}
|
2017-04-13 17:53:09 -07:00
|
|
|
|
2017-06-06 16:08:36 -07:00
|
|
|
template<class Container, class V>
|
2017-10-13 18:37:41 -07:00
|
|
|
auto find(Container&& cont, V&& v)
|
2017-06-06 16:08:36 -07:00
|
|
|
{
|
2017-10-13 18:37:41 -07:00
|
|
|
using std::begin;
|
|
|
|
using std::end;
|
|
|
|
return std::find(begin(cont), end(cont), v);
|
2017-06-06 16:08:36 -07:00
|
|
|
}
|
|
|
|
|
2017-04-13 17:53:09 -07:00
|
|
|
template<class Container, class Pred>
|
2017-10-13 18:37:41 -07:00
|
|
|
auto find_if(Container&& cont, Pred pred)
|
2017-04-13 17:53:09 -07:00
|
|
|
{
|
2017-10-13 18:37:41 -07:00
|
|
|
using std::begin;
|
|
|
|
using std::end;
|
|
|
|
return std::find_if(begin(cont), end(cont), pred);
|
2017-04-13 17:53:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class Container, class Pred>
|
2017-10-13 18:37:41 -07:00
|
|
|
auto find_if_not(Container&& cont, Pred pred)
|
2017-04-13 17:53:09 -07:00
|
|
|
{
|
2017-10-13 18:37:41 -07:00
|
|
|
using std::begin;
|
|
|
|
using std::end;
|
|
|
|
return std::find_if_not(begin(cont), end(cont), pred);
|
2017-04-13 17:53:09 -07:00
|
|
|
}
|
2017-04-14 14:31:17 -07:00
|
|
|
|
|
|
|
template<class K, class V, class Container, class Func>
|
2017-10-16 11:44:04 -07:00
|
|
|
void group_by(const Container& cont, std::map<K, std::vector<const V*>>* output, Func&& f)
|
2017-04-14 14:31:17 -07:00
|
|
|
{
|
|
|
|
for (const V& element : cont)
|
|
|
|
{
|
|
|
|
K key = f(element);
|
|
|
|
(*output)[key].push_back(&element);
|
|
|
|
}
|
|
|
|
}
|
2017-08-21 20:06:47 -07:00
|
|
|
|
2019-08-20 08:47:26 -07:00
|
|
|
template<class Range, class Comp = std::less<typename Range::value_type>>
|
|
|
|
void sort(Range& cont, Comp comp = Comp())
|
2017-10-30 23:06:04 -07:00
|
|
|
{
|
|
|
|
using std::begin;
|
|
|
|
using std::end;
|
2019-08-20 08:47:26 -07:00
|
|
|
std::sort(begin(cont), end(cont), comp);
|
2017-10-30 23:06:04 -07:00
|
|
|
}
|
|
|
|
|
2020-02-03 14:22:52 -08:00
|
|
|
template<class Range, class Pred>
|
|
|
|
bool any_of(Range&& rng, Pred pred)
|
|
|
|
{
|
|
|
|
return std::any_of(rng.begin(), rng.end(), std::move(pred));
|
|
|
|
}
|
|
|
|
|
2018-02-25 10:13:57 -08:00
|
|
|
template<class Range>
|
2019-08-20 08:47:26 -07:00
|
|
|
Range&& sort_unique_erase(Range&& cont)
|
2018-02-25 10:13:57 -08:00
|
|
|
{
|
|
|
|
using std::begin;
|
|
|
|
using std::end;
|
|
|
|
std::sort(begin(cont), end(cont));
|
|
|
|
cont.erase(std::unique(begin(cont), end(cont)), end(cont));
|
2019-08-20 08:47:26 -07:00
|
|
|
|
|
|
|
return std::forward<Range>(cont);
|
2018-02-25 10:13:57 -08:00
|
|
|
}
|
|
|
|
|
2017-10-30 23:06:04 -07:00
|
|
|
template<class Range1, class Range2>
|
|
|
|
bool all_equal(const Range1& r1, const Range2& r2)
|
|
|
|
{
|
|
|
|
using std::begin;
|
|
|
|
using std::end;
|
|
|
|
return std::equal(begin(r1), end(r1), begin(r2), end(r2));
|
|
|
|
}
|
|
|
|
|
2017-10-13 18:37:41 -07:00
|
|
|
template<class AssocContainer, class K = std::decay_t<decltype(begin(std::declval<AssocContainer>())->first)>>
|
|
|
|
std::vector<K> extract_keys(AssocContainer&& input_map)
|
|
|
|
{
|
|
|
|
return fmap(input_map, [](auto&& p) { return p.first; });
|
|
|
|
}
|
|
|
|
|
2017-08-21 20:06:47 -07:00
|
|
|
struct MoveOnlyBase
|
|
|
|
{
|
|
|
|
MoveOnlyBase() = default;
|
|
|
|
MoveOnlyBase(const MoveOnlyBase&) = delete;
|
|
|
|
MoveOnlyBase(MoveOnlyBase&&) = default;
|
|
|
|
|
|
|
|
MoveOnlyBase& operator=(const MoveOnlyBase&) = delete;
|
|
|
|
MoveOnlyBase& operator=(MoveOnlyBase&&) = default;
|
2018-03-16 16:53:48 -07:00
|
|
|
|
|
|
|
~MoveOnlyBase() = default;
|
2017-08-21 20:06:47 -07:00
|
|
|
};
|
2017-08-22 15:14:15 -07:00
|
|
|
|
|
|
|
struct ResourceBase
|
|
|
|
{
|
|
|
|
ResourceBase() = default;
|
|
|
|
ResourceBase(const ResourceBase&) = delete;
|
|
|
|
ResourceBase(ResourceBase&&) = delete;
|
|
|
|
|
|
|
|
ResourceBase& operator=(const ResourceBase&) = delete;
|
|
|
|
ResourceBase& operator=(ResourceBase&&) = delete;
|
2018-03-16 16:53:48 -07:00
|
|
|
|
|
|
|
~ResourceBase() = default;
|
2017-08-22 15:14:15 -07:00
|
|
|
};
|
2017-08-25 16:03:57 -07:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct LockGuardPtr;
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct LockGuarded
|
|
|
|
{
|
|
|
|
friend struct LockGuardPtr<T>;
|
|
|
|
|
|
|
|
LockGuardPtr<T> lock() { return *this; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::mutex m_mutex;
|
|
|
|
T m_t;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct LockGuardPtr
|
|
|
|
{
|
|
|
|
T& operator*() { return m_ptr; }
|
|
|
|
T* operator->() { return &m_ptr; }
|
|
|
|
|
|
|
|
T* get() { return &m_ptr; }
|
|
|
|
|
|
|
|
LockGuardPtr(LockGuarded<T>& sync) : m_lock(sync.m_mutex), m_ptr(sync.m_t) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_lock<std::mutex> m_lock;
|
|
|
|
T& m_ptr;
|
|
|
|
};
|
2017-11-16 19:29:32 -08:00
|
|
|
|
|
|
|
namespace Enum
|
|
|
|
{
|
|
|
|
template<class E>
|
|
|
|
E to_enum(bool b)
|
|
|
|
{
|
|
|
|
return b ? E::YES : E::NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class E>
|
|
|
|
bool to_bool(E e)
|
|
|
|
{
|
|
|
|
return e == E::YES;
|
|
|
|
}
|
|
|
|
}
|
2018-03-16 16:53:48 -07:00
|
|
|
|
2019-08-09 11:16:35 -07:00
|
|
|
template<class... Ts>
|
|
|
|
void unused(const Ts&...)
|
2018-03-16 16:53:48 -07:00
|
|
|
{
|
|
|
|
}
|
2020-02-03 14:22:52 -08:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T copy(const T& t)
|
|
|
|
{
|
|
|
|
return t;
|
|
|
|
}
|
2017-10-15 23:53:35 -07:00
|
|
|
}
|