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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
// file : tests/path-entry/driver.cxx -*- C++ -*-
// license : MIT; see accompanying LICENSE file
#include <cassert>
#ifndef __cpp_lib_modules_ts
#include <string>
#include <iostream>
#include <stdexcept> // invalid_argument
#include <system_error>
#endif
// Other includes.
#ifdef __cpp_modules_ts
#ifdef __cpp_lib_modules_ts
import std.core;
import std.io;
#endif
import butl.path;
import butl.path-io;
import butl.utility; // operator<<(ostream, exception)
import butl.optional;
import butl.timestamp;
import butl.filesystem;
#else
#include <libbutl/path.mxx>
#include <libbutl/path-io.mxx>
#include <libbutl/utility.mxx>
#include <libbutl/optional.mxx>
#include <libbutl/timestamp.mxx>
#include <libbutl/filesystem.mxx>
#endif
using namespace std;
using namespace butl;
// Usage: argv[0] [-l] [-t] [-p <permissions>] [-m <time>] [-a <time>] <path>
//
// If path entry exists then optionally modify its meta-information and print
// its type, size (meaningful for the regular file only), target path if the
// specified entry is a symlink and its path otherwise, permissions,
// modification and access times to stdout, one value per line, and exit with
// the zero code. Otherwise exit with the one code. Don't follow symlink by
// default. On failure print the error description to stderr and exit with the
// two code.
//
// -l
// Follow symlinks.
//
// -t
// Assume the path is a file and touch it. Implies -l.
//
// -p <permissions>
// Set path permissions specified in the chmod utility octal form. Implies
// -l.
//
// -m <time>
// Set path modification time specified in the "%Y-%m-%d %H:%M:%S%[.N]"
// format. Implies -l.
//
// -a <time>
// As -m but set the access time.
//
int
main (int argc, const char* argv[])
{
string stage;
try
{
using butl::optional;
bool follow_symlinks (false);
optional<permissions> perms;
optional<timestamp> mtime;
optional<timestamp> atime;
bool touch (false);
auto time = [] (const char* v)
{
return from_string (v, "%Y-%m-%d %H:%M:%S%[.N]", true /* local */);
};
int i (1);
for (; i != argc; ++i)
{
string v (argv[i]);
if (v == "-l")
follow_symlinks = true;
else if (v == "-t")
{
touch = true;
follow_symlinks = true;
}
else if (v == "-p")
{
assert (++i != argc);
v = argv[i];
size_t n;
perms = static_cast<permissions> (stoull (v, &n, 8));
assert (n == v.size ());
follow_symlinks = true;
}
else if (v == "-m")
{
assert (++i != argc);
mtime = time (argv[i]);
follow_symlinks = true;
}
else if (v == "-a")
{
assert (++i != argc);
atime = time (argv[i]);
follow_symlinks = true;
}
else
break;
}
assert (i == argc - 1);
path p (argv[i]);
if (touch)
{
stage = "touch";
touch_file (p);
}
stage = "stat entry";
pair<bool, entry_stat> es (path_entry (p, follow_symlinks));
if (!es.first)
return 1;
stage = "lstat entry";
pair<bool, entry_stat> ls (path_entry (p));
assert (ls.first);
// The entry is a directory with a symlink followed.
//
bool tdir;
if (follow_symlinks || es.second.type != entry_type::symlink)
tdir = (es.second.type == entry_type::directory);
else
{
stage = "stat target";
pair<bool, entry_stat> ts (path_entry (p, true /* follow_symlinks */));
if (!ts.first)
return 1;
tdir = (ts.second.type == entry_type::directory);
}
if (perms)
{
stage = "set permissions";
path_permissions (p, *perms);
}
if (mtime)
{
if (tdir)
{
stage = "set directory mtime";
dir_mtime (path_cast<dir_path> (p), *mtime);
}
else
{
stage = "set file mtime";
file_mtime (p, *mtime);
}
}
if (atime)
{
if (tdir)
{
stage = "set directory atime";
dir_atime (path_cast<dir_path> (p), *atime);
}
else
{
stage = "set file atime";
file_atime (p, *atime);
}
}
cout << "type: ";
switch (es.second.type)
{
case entry_type::unknown: cout << "unknown"; break;
case entry_type::regular: cout << "regular"; break;
case entry_type::directory: cout << "directory"; break;
case entry_type::symlink: cout << "symlink"; break;
case entry_type::other: cout << "other"; break;
}
cout << endl;
cout << "size: " << es.second.size << endl
<< "target: "
<< (ls.second.type == entry_type::symlink
? readsymlink (p)
: p) << endl;
stage = "get permissions";
cout << "permissions: "
<< oct << static_cast<size_t> (path_permissions (p)) << endl;
stage = tdir ? "get directory times" : "get file times";
entry_time et (tdir ? dir_time (path_cast<dir_path> (p)) : file_time (p));
cout << "mtime: " << et.modification << endl
<< "atime: " << et.access << endl;
return 0;
}
catch (const invalid_argument& e)
{
cerr << e << endl;
return 2;
}
catch (const system_error& e)
{
cerr << stage << " failed: " << e << endl;
return 2;
}
}
|