-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathfilesystem.cpp
More file actions
95 lines (85 loc) · 2.14 KB
/
filesystem.cpp
File metadata and controls
95 lines (85 loc) · 2.14 KB
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
/*
* Copyright (C) 2016-2017, Egor Pugin
*
* 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 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.
*/
#include "filesystem.h"
path get_config_filename()
{
return get_root_directory() / CPPAN_FILENAME;
}
path get_root_directory()
{
return get_home_directory() / ".cppan";
}
path temp_directory_path(const path &subdir)
{
auto p = fs::temp_directory_path() / "cppan" / subdir;
fs::create_directories(p);
return p;
}
path get_temp_filename(const path &subdir)
{
return temp_directory_path(subdir) / unique_path();
}
String get_stamp_filename(const String &prefix)
{
return prefix + ".hash";
}
String make_archive_name(const String &fn)
{
if (!fn.empty())
return fn + ".tar.gz";
return "cppan.tar.gz";
}
void findRootDirectory1(const path &p, path &root, int depth = 0)
{
// limit recursion
if (depth++ > 10)
return;
std::vector<path> pfiles;
std::vector<path> pdirs;
for (auto &pi : fs::directory_iterator(p))
{
auto f = pi.path().filename().string();
if (f == CPPAN_FILENAME)
continue;
if (fs::is_regular_file(pi))
{
pfiles.push_back(pi);
break;
}
else if (fs::is_directory(pi))
{
pdirs.push_back(pi);
if (pdirs.size() > 1)
break;
}
}
if (pfiles.empty() && pdirs.size() == 1)
{
auto d = fs::relative(*pdirs.begin(), p);
root /= d;
findRootDirectory1(p / d, root);
}
else if (depth == 1)
{
root = p;
}
}
path findRootDirectory(const path &p)
{
path root;
findRootDirectory1(p, root);
return root;
}