-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpath.cc
122 lines (104 loc) · 2.72 KB
/
path.cc
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
/*
* Copyright (C) 2019 SUSE Software Solutions Germany GmbH
*
* This file is part of klp-ccp.
*
* klp-ccp is free software: you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* klp-ccp is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with klp-ccp. If not, see <https://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <cerrno>
#include <system_error>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "path.hh"
std::string klp::ccp::normalize_path(std::string &&p)
{
bool last_is_sep = false;
for (auto it = p.begin(); it != p.end();) {
if (*it == '/') {
if (last_is_sep) {
it = p.erase(it);
} else {
last_is_sep = true;
++it;
}
} else if (last_is_sep && *it == '.') {
if (p.end() - it >= 2 && *(it + 1) == '.' &&
(p.end() - it == 2 || (*(it + 2) == '/'))) {
std::string::reverse_iterator rit{it};
if (it != p.begin() + 1) {
++rit;
// If the preceeding patch component is also a '..', then
// don't remove the current one.
if (p.rend() - rit == 2 && *rit == '.' && *(rit + 1) == '.') {
last_is_sep = false;
++it;
continue;
}
rit = std::find(rit, p.rend(), '/');
}
if (p.end() - it == 2)
it = p.end();
else
it += 3;
it = p.erase(rit.base(), it);
} else if (p.end() - it == 1 || *(it + 1) == '/') {
it = p.erase(it, p.end() - it == 1 ? p.end() : it + 2);
} else {
++it;
last_is_sep = false;
}
} else {
last_is_sep = false;
++it;
}
}
return std::move(p);
}
bool klp::ccp::path_is_absolute(const std::string &p)
{
return !p.empty() && p[0] == '/';
}
std::string klp::ccp::dirname(const std::string &p)
{
std::string _p{p};
_p = normalize_path(std::move(_p));
auto end_pos = _p.rfind('/', _p.find_last_not_of('/'));
if (!end_pos) {
end_pos = 1;
} else if (end_pos == std::string::npos) {
return std::string{};
}
return std::string(std::move(_p), 0, end_pos);
}
bool klp::ccp::file_readable(const std::string &p)
{
if (access(p.c_str(), R_OK))
return false;
struct stat s;
if (stat(p.c_str(), &s)) {
throw std::system_error(errno, std::system_category(),
p);
}
return S_ISREG(s.st_mode);
}
std::string klp::ccp::getcwd()
{
char *p = ::getcwd(nullptr, 0);
if (!p)
throw std::system_error(errno, std::system_category(), "getcwd");
std::string sp(p);
free(p);
return sp;
}