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
|
// Copyright 2019 Roman Perepelitsa.
//
// This file is part of GitStatus.
//
// GitStatus is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GitStatus 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 GitStatus. If not, see <https://www.gnu.org/licenses/>.
#include "check_dir_mtime.h"
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <cerrno>
#include <cstring>
#include <ctime>
#include <string>
#include <vector>
#include "check.h"
#include "dir.h"
#include "logging.h"
#include "print.h"
#include "scope_guard.h"
#include "stat.h"
namespace gitstatus {
namespace {
constexpr char kDirPrefix[] = ".gitstatus.";
void Touch(const char* path) {
int fd = creat(path, 0444);
VERIFY(fd >= 0) << Errno();
CHECK(!close(fd)) << Errno();
}
bool StatChanged(const char* path, const struct stat& prev) {
struct stat cur;
VERIFY(!lstat(path, &cur)) << Errno();
return !StatEq(prev, cur);
}
void RemoveStaleDirs(const char* root_dir) {
int dir_fd = open(root_dir, O_DIRECTORY | O_CLOEXEC);
if (dir_fd < 0) return;
ON_SCOPE_EXIT(&) { CHECK(!close(dir_fd)) << Errno(); };
Arena arena;
std::vector<char*> entries;
const std::time_t now = std::time(nullptr);
if (!ListDir(dir_fd, arena, entries,
/* precompose_unicode = */ false,
/* case_sensitive = */ true)) {
return;
}
std::string path = root_dir;
const size_t root_dir_len = path.size();
for (const char* entry : entries) {
if (std::strlen(entry) < std::strlen(kDirPrefix)) continue;
if (std::memcmp(entry, kDirPrefix, std::strlen(kDirPrefix))) continue;
struct stat st;
if (fstatat(dir_fd, entry, &st, AT_SYMLINK_NOFOLLOW)) {
LOG(WARN) << "Cannot stat " << Print(entry) << " in " << Print(root_dir) << ": " << Errno();
continue;
}
if (MTim(st).tv_sec + 10 > now) continue;
path.resize(root_dir_len);
path += entry;
size_t dir_len = path.size();
path += "/b/1";
if (unlink(path.c_str()) && errno != ENOENT) {
LOG(WARN) << "Cannot unlink " << Print(path) << ": " << Errno();
continue;
}
for (const char* d : {"/a/1", "/a", "/b", ""}) {
path.resize(dir_len);
path += d;
if (rmdir(path.c_str()) && errno != ENOENT) {
LOG(WARN) << "Cannot remove " << Print(path) << ": " << Errno();
break;
}
}
}
}
} // namespace
bool CheckDirMtime(const char* root_dir) {
try {
RemoveStaleDirs(root_dir);
std::string tmp = std::string() + root_dir + kDirPrefix + "XXXXXX";
VERIFY(mkdtemp(&tmp[0])) << Errno();
ON_SCOPE_EXIT(&) { rmdir(tmp.c_str()); };
std::string a_dir = tmp + "/a";
VERIFY(!mkdir(a_dir.c_str(), 0755)) << Errno();
ON_SCOPE_EXIT(&) { rmdir(a_dir.c_str()); };
struct stat a_st;
VERIFY(!lstat(a_dir.c_str(), &a_st)) << Errno();
std::string b_dir = tmp + "/b";
VERIFY(!mkdir(b_dir.c_str(), 0755)) << Errno();
ON_SCOPE_EXIT(&) { rmdir(b_dir.c_str()); };
struct stat b_st;
VERIFY(!lstat(b_dir.c_str(), &b_st)) << Errno();
while (sleep(1)) {
// zzzz
}
std::string a1 = a_dir + "/1";
VERIFY(!mkdir(a1.c_str(), 0755)) << Errno();
ON_SCOPE_EXIT(&) { rmdir(a1.c_str()); };
if (!StatChanged(a_dir.c_str(), a_st)) {
LOG(WARN) << "Creating a directory doesn't change mtime of the parent: " << Print(root_dir);
return false;
}
std::string b1 = b_dir + "/1";
Touch(b1.c_str());
ON_SCOPE_EXIT(&) { unlink(b1.c_str()); };
if (!StatChanged(b_dir.c_str(), b_st)) {
LOG(WARN) << "Creating a file doesn't change mtime of the parent: " << Print(root_dir);
return false;
}
LOG(INFO) << "All mtime checks have passes. Enabling untracked cache: " << Print(root_dir);
return true;
} catch (const Exception&) {
LOG(WARN) << "Error while testing for mtime capability: " << Print(root_dir);
return false;
}
}
} // namespace gitstatus
|