11 #include <sys/types.h>
16 #include WithXattrUgly
19 #define ENOATTR ENODATA
33 int main(
int argc,
const char *argv[]) {
36 "files to be truncated");
37 #if defined(WithXattr) || defined(WithXattrUgly)
39 "name of xattr to set",
"user.truncated");
41 "name of xattr for old size",
"user.origsize");
43 "name of xattr for old ctime",
"user.origctime");
46 "set files to immutable",
false);
48 "read files from policy list");
50 "print paths of traeded files");
52 "schema used for escaping file names in policy run file",
"C");
53 parser.fParse(argc, argv);
55 if (readPolicyOutput) {
56 if (files.size() != 1) {
57 std::cerr <<
"with --policyList exactly 1 arg is required\n";
61 auto listName=files.front();
63 std::ifstream list(listName);
65 while (std::getline(list, line)) {
66 auto separatorStart = line.find(
" -- ");
67 if (separatorStart == std::string::npos) {
70 auto pathStart = separatorStart + 4;
72 escaper->deEscape(line.substr(pathStart), file);
73 files.push_back(file);
77 for (
auto file : files) {
78 struct stat initialStat;
80 if (! S_ISREG(initialStat.st_mode)) {
82 std::cout <<
"ignoring non-file " << file <<
"\n";
87 std::cout <<
"truncating " << file <<
"\n";
89 #if defined(WithXattr) || defined(WithXattrUgly)
91 auto value = std::to_string(std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::system_clock::now().time_since_epoch()).count());
92 throwcall::good0(setxattr(file.c_str(), name.c_str(), value.data(), value.size(), 0),
93 "can't set xattrs on ", file);
95 if (!sizeName.empty()) {
96 auto value = std::to_string(initialStat.st_size);
97 throwcall::good0(setxattr(file.c_str(), sizeName.c_str(), value.data(), value.size(), 0),
98 "can't set xattrs on ", file);
100 if (!ctimeName.empty()) {
101 auto value = std::to_string(std::chrono::duration_cast<std::chrono::duration<double>>(
102 std::chrono::seconds(initialStat.st_ctim.tv_sec) +
103 std::chrono::nanoseconds(initialStat.st_ctim.tv_nsec)
105 throwcall::good0(setxattr(file.c_str(), ctimeName.c_str(), value.data(), value.size(), 0),
106 "can't set xattrs on ", file);
111 struct timespec times[2];
112 times[0].tv_sec = initialStat.st_atim.tv_sec;
113 times[0].tv_nsec = initialStat.st_atim.tv_nsec;
114 times[1].tv_sec = initialStat.st_mtim.tv_sec;
115 times[1].tv_nsec = initialStat.st_mtim.tv_nsec;
117 throwcall::good0(utimensat(AT_FDCWD, file.c_str(), times, 0),
"can't reset time stamps of ", file);
121 -1,
"can't open ", file));
123 struct statfs statbuf;
125 if (statbuf.f_type == GPFS_SUPER_MAGIC) {
127 auto& hint = fcntl.
buffer.add<gpfsSetImmutable_t>(GPFS_FCNTL_SET_IMMUTABLE);
128 hint.setImmutable = 1;
129 hint.setIndefiniteRetention = 1;
130 fcntl.
call(fd, file);
135 throwcall::good0(ioctl(fd, FS_IOC_GETFLAGS, &flags),
"can't read flags on ", file);
136 flags |= FS_IMMUTABLE_FL;
137 throwcall::good0(ioctl(fd, FS_IOC_SETFLAGS, &flags),
"can't set flags on ", file);