2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
16 /*---------------------------------------------------------------------------*/
17 /* Pass3 -- Validate the incoming CTM-file.
21 settime(const char *name, const struct timeval *times)
24 if (utimes(name,times)) {
25 warn("utimes(): %s", name);
34 u_char *p,*q,buf[BUFSIZ];
37 u_char *md5=0,*md5before=0,*trash=0,*name=0,*uid=0,*gid=0,*mode=0;
38 struct CTM_Syntax *sp;
43 struct timeval times[2];
44 struct CTM_Filter *filter = NULL;
46 printf("Pass3 -- Applying the CTM-patch\n");
49 GETFIELD(p,' '); if(strcmp("CTM_BEGIN",p)) WRONG
50 GETFIELD(p,' '); if(strcmp(Version,p)) WRONG
51 GETFIELD(p,' '); if(strcmp(Name,p)) WRONG
52 GETFIELD(p,' '); if(strcmp(Nbr,p)) WRONG
53 GETFIELD(p,' '); if(strcmp(TimeStamp,p)) WRONG
54 GETFIELD(p,'\n'); if(strcmp(Prefix,p)) WRONG
57 * This would be cleaner if mktime() worked in UTC rather than
66 #define SUBSTR(off,len) strncpy(buf, &TimeStamp[off], len), buf[len] = '\0'
67 #define WRONGDATE { fprintf(stderr, " %s failed date validation\n",\
70 if (strlen(TimeStamp) != 15 || TimeStamp[14] != 'Z') WRONGDATE
71 for (i = 0; i < 14; i++)
72 if (!isdigit(TimeStamp[i])) WRONGDATE
75 if (setenv("TZ", "UTC", 1) < 0) WRONG
78 tm.tm_isdst = tm.tm_gmtoff = 0;
81 tm.tm_year = atoi(buf) - 1900;
83 tm.tm_mon = atoi(buf) - 1;
84 if (tm.tm_mon < 0 || tm.tm_mon > 11) WRONGDATE
86 tm.tm_mday = atoi(buf);
87 if (tm.tm_mday < 1 || tm.tm_mday > 31) WRONG;
89 tm.tm_hour = atoi(buf);
90 if (tm.tm_hour > 24) WRONGDATE
92 tm.tm_min = atoi(buf);
93 if (tm.tm_min > 59) WRONGDATE
95 tm.tm_sec = atoi(buf);
96 if (tm.tm_min > 62) WRONGDATE /* allow leap seconds */
98 times[0].tv_sec = times[1].tv_sec = mktime(&tm);
99 if (times[0].tv_sec == -1) WRONGDATE
100 times[0].tv_usec = times[1].tv_usec = 0;
103 if (setenv("TZ", tz, 1) < 0) WRONGDATE
121 if (p[0] != 'C' || p[1] != 'T' || p[2] != 'M') WRONG
123 if(!strcmp(p+3,"_END"))
126 for(sp=Syntax;sp->Key;sp++)
127 if(!strcmp(p+3,sp->Key))
131 for(i=0;(j = sp->List[i]);i++) {
132 if (sp->List[i+1] && (sp->List[i+1] & CTM_F_MASK) != CTM_F_Bytes)
137 switch (j & CTM_F_MASK) {
138 case CTM_F_Name: GETNAMECOPY(name,sep,j, Verbose); break;
139 case CTM_F_Uid: GETFIELDCOPY(uid,sep); break;
140 case CTM_F_Gid: GETFIELDCOPY(gid,sep); break;
141 case CTM_F_Mode: GETFIELDCOPY(mode,sep); break;
143 if(j & CTM_Q_MD5_Before)
144 GETFIELDCOPY(md5before,sep);
146 GETFIELDCOPY(md5,sep);
148 case CTM_F_Count: GETBYTECNT(cnt,sep); break;
149 case CTM_F_Bytes: GETDATA(trash,cnt); break;
153 /* XXX This should go away. Disallow trailing '/' */
155 if(name[j] == '/') name[j] = '\0';
158 * If a filter list is specified, run thru the filter list and
159 * match `name' against filters. If the name matches, set the
160 * required action to that specified in the filter.
161 * The default action if no filterlist is given is to match
165 match = (FilterList ? !(FilterList->Action) : CTM_FILTER_ENABLE);
166 for (filter = FilterList; filter; filter = filter->Next) {
167 if (0 == regexec(&filter->CompiledRegex, name,
169 match = filter->Action;
173 if (CTM_FILTER_DISABLE == match) /* skip file if disabled */
177 fprintf(stderr,"> %s %s\n",sp->Key,name);
178 if(!strcmp(sp->Key,"FM") || !strcmp(sp->Key, "FS")) {
179 i = open(name,O_WRONLY|O_CREAT|O_TRUNC,0666);
184 if(cnt != write(i,trash,cnt)) {
189 if(strcmp(md5,MD5File(name,md5_1))) {
190 fprintf(stderr," %s %s MD5 didn't come out right\n",
194 if (settime(name,times)) WRONG
197 if(!strcmp(sp->Key,"FE")) {
198 ed = popen("ed","w");
202 fprintf(ed,"e %s\n",name);
203 if(cnt != fwrite(trash,1,cnt,ed)) {
208 fprintf(ed,"w %s\n",name);
213 if(strcmp(md5,MD5File(name,md5_1))) {
214 fprintf(stderr," %s %s MD5 didn't come out right\n",
218 if (settime(name,times)) WRONG
221 if(!strcmp(sp->Key,"FN")) {
224 i = ctm_edit(trash,cnt,name,buf);
226 fprintf(stderr," %s %s Edit failed with code %d.\n",
230 if(strcmp(md5,MD5File(buf,md5_1))) {
231 fprintf(stderr," %s %s Edit failed MD5 check.\n",
235 if (rename(buf,name) == -1)
237 if (settime(name,times)) WRONG
240 if(!strcmp(sp->Key,"DM")) {
241 if(0 > mkdir(name,0777)) {
242 sprintf(buf,"mkdir -p %s",name);
245 if(0 > stat(name,&st) || ((st.st_mode & S_IFMT) != S_IFDIR)) {
246 fprintf(stderr,"<%s> mkdir failed\n",name);
249 if (settime(name,times)) WRONG
252 if(!strcmp(sp->Key,"FR")) {
255 printf("<%s> not removed\n", name);
257 else if (0 != unlink(name)) {
258 fprintf(stderr,"<%s> unlink failed\n",name);
264 if(!strcmp(sp->Key,"DR")) {
266 * We cannot use rmdir() because we do not get the directories
267 * in '-depth' order (cvs-cur.0018.gz for examples)
271 printf("<%s> not removed\n", name);
274 sprintf(buf,"rm -rf %s",name);
290 q = MD5End (&ctx,md5_1);
292 if(strcmp(q,p)) WRONG
293 if (-1 != getc(fd)) WRONG