2 * SPDX-License-Identifier: Beerware
4 * ----------------------------------------------------------------------------
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
9 * ----------------------------------------------------------------------------
18 /*---------------------------------------------------------------------------*/
19 /* Pass3 -- Validate the incoming CTM-file.
23 settime(const char *name, const struct timeval *times)
26 if (utimes(name,times)) {
27 warn("utimes(): %s", name);
36 u_char *p,*q,buf[BUFSIZ];
39 u_char *md5=0,*md5before=0,*trash=0,*name=0,*uid=0,*gid=0,*mode=0;
40 struct CTM_Syntax *sp;
45 struct timeval times[2];
46 struct CTM_Filter *filter = NULL;
48 printf("Pass3 -- Applying the CTM-patch\n");
51 GETFIELD(p,' '); if(strcmp("CTM_BEGIN",p)) WRONG
52 GETFIELD(p,' '); if(strcmp(Version,p)) WRONG
53 GETFIELD(p,' '); if(strcmp(Name,p)) WRONG
54 GETFIELD(p,' '); if(strcmp(Nbr,p)) WRONG
55 GETFIELD(p,' '); if(strcmp(TimeStamp,p)) WRONG
56 GETFIELD(p,'\n'); if(strcmp(Prefix,p)) WRONG
59 * This would be cleaner if mktime() worked in UTC rather than
68 #define SUBSTR(off,len) strncpy(buf, &TimeStamp[off], len), buf[len] = '\0'
69 #define WRONGDATE { fprintf(stderr, " %s failed date validation\n",\
72 if (strlen(TimeStamp) != 15 || TimeStamp[14] != 'Z') WRONGDATE
73 for (i = 0; i < 14; i++)
74 if (!isdigit(TimeStamp[i])) WRONGDATE
77 if (setenv("TZ", "UTC", 1) < 0) WRONG
80 tm.tm_isdst = tm.tm_gmtoff = 0;
83 tm.tm_year = atoi(buf) - 1900;
85 tm.tm_mon = atoi(buf) - 1;
86 if (tm.tm_mon < 0 || tm.tm_mon > 11) WRONGDATE
88 tm.tm_mday = atoi(buf);
89 if (tm.tm_mday < 1 || tm.tm_mday > 31) WRONG;
91 tm.tm_hour = atoi(buf);
92 if (tm.tm_hour > 24) WRONGDATE
94 tm.tm_min = atoi(buf);
95 if (tm.tm_min > 59) WRONGDATE
97 tm.tm_sec = atoi(buf);
98 if (tm.tm_min > 62) WRONGDATE /* allow leap seconds */
100 times[0].tv_sec = times[1].tv_sec = mktime(&tm);
101 if (times[0].tv_sec == -1) WRONGDATE
102 times[0].tv_usec = times[1].tv_usec = 0;
105 if (setenv("TZ", tz, 1) < 0) WRONGDATE
123 if (p[0] != 'C' || p[1] != 'T' || p[2] != 'M') WRONG
125 if(!strcmp(p+3,"_END"))
128 for(sp=Syntax;sp->Key;sp++)
129 if(!strcmp(p+3,sp->Key))
133 for(i=0;(j = sp->List[i]);i++) {
134 if (sp->List[i+1] && (sp->List[i+1] & CTM_F_MASK) != CTM_F_Bytes)
139 switch (j & CTM_F_MASK) {
140 case CTM_F_Name: GETNAMECOPY(name,sep,j, Verbose); break;
141 case CTM_F_Uid: GETFIELDCOPY(uid,sep); break;
142 case CTM_F_Gid: GETFIELDCOPY(gid,sep); break;
143 case CTM_F_Mode: GETFIELDCOPY(mode,sep); break;
145 if(j & CTM_Q_MD5_Before)
146 GETFIELDCOPY(md5before,sep);
148 GETFIELDCOPY(md5,sep);
150 case CTM_F_Count: GETBYTECNT(cnt,sep); break;
151 case CTM_F_Bytes: GETDATA(trash,cnt); break;
155 /* XXX This should go away. Disallow trailing '/' */
157 if(name[j] == '/') name[j] = '\0';
160 * If a filter list is specified, run thru the filter list and
161 * match `name' against filters. If the name matches, set the
162 * required action to that specified in the filter.
163 * The default action if no filterlist is given is to match
167 match = (FilterList ? !(FilterList->Action) : CTM_FILTER_ENABLE);
168 for (filter = FilterList; filter; filter = filter->Next) {
169 if (0 == regexec(&filter->CompiledRegex, name,
171 match = filter->Action;
175 if (CTM_FILTER_DISABLE == match) /* skip file if disabled */
179 fprintf(stderr,"> %s %s\n",sp->Key,name);
180 if(!strcmp(sp->Key,"FM") || !strcmp(sp->Key, "FS")) {
181 i = open(name,O_WRONLY|O_CREAT|O_TRUNC,0666);
186 if(cnt != write(i,trash,cnt)) {
191 if(strcmp(md5,MD5File(name,md5_1))) {
192 fprintf(stderr," %s %s MD5 didn't come out right\n",
196 if (settime(name,times)) WRONG
199 if(!strcmp(sp->Key,"FE")) {
200 ed = popen("ed","w");
204 fprintf(ed,"e %s\n",name);
205 if(cnt != fwrite(trash,1,cnt,ed)) {
210 fprintf(ed,"w %s\n",name);
215 if(strcmp(md5,MD5File(name,md5_1))) {
216 fprintf(stderr," %s %s MD5 didn't come out right\n",
220 if (settime(name,times)) WRONG
223 if(!strcmp(sp->Key,"FN")) {
226 i = ctm_edit(trash,cnt,name,buf);
228 fprintf(stderr," %s %s Edit failed with code %d.\n",
232 if(strcmp(md5,MD5File(buf,md5_1))) {
233 fprintf(stderr," %s %s Edit failed MD5 check.\n",
237 if (rename(buf,name) == -1)
239 if (settime(name,times)) WRONG
242 if(!strcmp(sp->Key,"DM")) {
243 if(0 > mkdir(name,0777)) {
244 sprintf(buf,"mkdir -p %s",name);
247 if(0 > stat(name,&st) || ((st.st_mode & S_IFMT) != S_IFDIR)) {
248 fprintf(stderr,"<%s> mkdir failed\n",name);
251 if (settime(name,times)) WRONG
254 if(!strcmp(sp->Key,"FR")) {
257 printf("<%s> not removed\n", name);
259 else if (0 != unlink(name)) {
260 fprintf(stderr,"<%s> unlink failed\n",name);
266 if(!strcmp(sp->Key,"DR")) {
268 * We cannot use rmdir() because we do not get the directories
269 * in '-depth' order (cvs-cur.0018.gz for examples)
273 printf("<%s> not removed\n", name);
276 sprintf(buf,"rm -rf %s",name);
292 q = MD5End (&ctx,md5_1);
294 if(strcmp(q,p)) WRONG
295 if (-1 != getc(fd)) WRONG