-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouch.c
192 lines (158 loc) · 3.78 KB
/
touch.c
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
/** fcom: change file date/time
2022, Simon Zolin */
static const char* touch_help()
{
return "\
Change file date/time.\n\
By default uses the current date/time.\n\
Usage:\n\
`fcom touch` INPUT... [OPTIONS]\n\
\n\
OPTIONS:\n\
`-d`, `--date` STR Set local date/time\n\
Format:\n\
yyyy-MM-dd [hh:mm:ss]\n\
`-r`, `--reference` FILE\n\
Set date/time from this file\n\
";
}
#include <fcom.h>
static const fcom_core *core;
struct touch {
fcom_cominfo cominfo;
uint st;
fcom_cominfo *cmd;
uint stop;
fcom_file_obj *in;
fftime mtime;
const char *reference_fn;
};
static int arg_date(struct touch *t, ffstr val)
{
ffdatetime dt = {};
if (val.len == fftime_fromstr1(&dt, val.ptr, val.len, FFTIME_YMD))
{}
else if (val.len == fftime_fromstr1(&dt, val.ptr, val.len, FFTIME_DATE_YMD))
{}
else
return 0xbad;
fftime_join1(&t->mtime, &dt);
t->mtime.sec -= FFTIME_1970_SECONDS + core->tz.real_offset;
return 0;
}
#define O(member) (void*)FF_OFF(struct touch, member)
static int args_parse(struct touch *t, fcom_cominfo *cmd)
{
static const struct ffarg args[] = {
{ "--date", 'S', arg_date },
{ "--reference",'s', O(reference_fn) },
{ "-d", 'S', arg_date },
{ "-r", 's', O(reference_fn) },
{}
};
return core->com->args_parse(cmd, args, t, FCOM_COM_AP_INOUT);
}
static void touch_close(fcom_op *op)
{
struct touch *t = op;
core->file->destroy(t->in);
ffmem_free(t);
}
static fcom_op* touch_create(fcom_cominfo *cmd)
{
struct touch *t = ffmem_new(struct touch);
if (0 != args_parse(t, cmd))
goto end;
struct fcom_file_conf fc = {};
fc.buffer_size = cmd->buffer_size;
t->in = core->file->create(&fc);
t->cmd = cmd;
return t;
end:
touch_close(t);
return NULL;
}
static int touch_do(struct touch *t, ffstr iname)
{
const char *fn = iname.ptr;
uint oflags = FCOM_FILE_READ;
if (!t->cmd->recursive) {
if (0 == fffile_set_mtime_path(fn, &t->mtime)) {
goto end;
}
if (!fferr_notexist(fferr_last())) {
fcom_syserrlog("fffile_set_mtime_path: %s", fn);
return 0xbad;
}
oflags = FCOM_FILE_CREATENEW | FCOM_FILE_WRITE;
}
int r = core->file->open(t->in, fn, oflags);
if (r == FCOM_FILE_ERR) return 0xbad;
fffileinfo fi;
r = core->file->info(t->in, &fi);
if (r == FCOM_FILE_ERR) return 0xbad;
if (0 != core->com->input_allowed(t->cmd, iname, fffile_isdir(fffileinfo_attr(&fi))))
return 0;
if (fffile_isdir(fffileinfo_attr(&fi))) {
fffd fd = core->file->fd(t->in, FCOM_FILE_ACQUIRE);
core->com->input_dir(t->cmd, fd);
}
core->file->close(t->in);
if (0 != fffile_set_mtime_path(fn, &t->mtime)) {
fcom_syserrlog("fffile_set_mtime_path: %s", fn);
return 0xbad;
}
end:
fcom_verblog("touch: %s", fn);
return 0;
}
static void touch_run(fcom_op *op)
{
struct touch *t = op;
int r, rc = 1;
enum { I_INIT, I_IN, };
while (!FFINT_READONCE(t->stop)) {
switch (t->st) {
case I_INIT:
if (t->reference_fn != NULL) {
fffileinfo fi;
if (0 != fffile_info_path(t->reference_fn, &fi)) {
fcom_syserrlog("%s", t->reference_fn);
goto end;
}
t->mtime = fffileinfo_mtime(&fi);
} else if (t->mtime.sec == 0) {
fftime_now(&t->mtime);
}
t->st++;
// fallthrough
case I_IN: {
ffstr in;
if (0 > (r = core->com->input_next(t->cmd, &in, NULL, 0))) {
if (r == FCOM_COM_RINPUT_NOMORE)
rc = 0;
goto end;
}
if (0 != (r = touch_do(t, in))) goto end;
continue;
}
}
}
end:
{
fcom_cominfo *cmd = t->cmd;
touch_close(t);
core->com->complete(cmd, rc);
}
}
static void touch_signal(fcom_op *op, uint signal)
{
struct touch *t = op;
FFINT_WRITEONCE(t->stop, 1);
}
static const fcom_operation fcom_op_touch = {
touch_create, touch_close,
touch_run, touch_signal,
touch_help,
};
FCOM_MOD_DEFINE(touch, fcom_op_touch, core)