xref: /netbsd-src/usr.bin/cvslatest/cvslatest.c (revision 9415b8de41067ccb79fe37604dfde915e8c0151a)
1*9415b8deSchristos /*	$NetBSD: cvslatest.c,v 1.11 2023/03/07 21:24:19 christos Exp $	*/
21a7e9bc1Schristos 
31a7e9bc1Schristos /*-
41a7e9bc1Schristos  * Copyright (c) 2016 The NetBSD Foundation, Inc.
51a7e9bc1Schristos  * All rights reserved.
61a7e9bc1Schristos  *
71a7e9bc1Schristos  * This code is derived from software contributed to The NetBSD Foundation
81a7e9bc1Schristos  * by Christos Zoulas.
91a7e9bc1Schristos  *
101a7e9bc1Schristos  * Redistribution and use in source and binary forms, with or without
111a7e9bc1Schristos  * modification, are permitted provided that the following conditions
121a7e9bc1Schristos  * are met:
131a7e9bc1Schristos  * 1. Redistributions of source code must retain the above copyright
141a7e9bc1Schristos  *    notice, this list of conditions and the following disclaimer.
151a7e9bc1Schristos  * 2. Redistributions in binary form must reproduce the above copyright
161a7e9bc1Schristos  *    notice, this list of conditions and the following disclaimer in the
171a7e9bc1Schristos  *    documentation and/or other materials provided with the distribution.
181a7e9bc1Schristos  *
191a7e9bc1Schristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201a7e9bc1Schristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211a7e9bc1Schristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221a7e9bc1Schristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231a7e9bc1Schristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241a7e9bc1Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251a7e9bc1Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261a7e9bc1Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271a7e9bc1Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281a7e9bc1Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291a7e9bc1Schristos  * POSSIBILITY OF SUCH DAMAGE.
301a7e9bc1Schristos  */
3183163b53Schristos 
32d03e05b4Sjoerg #ifdef __linux__
33d03e05b4Sjoerg #define _GNU_SOURCE
34d03e05b4Sjoerg #endif
35d03e05b4Sjoerg 
3683163b53Schristos #ifdef HAVE_NBTOOL_CONFIG_H
3783163b53Schristos #include "nbtool_config.h"
3883163b53Schristos #endif
3983163b53Schristos 
401a7e9bc1Schristos #include <sys/cdefs.h>
41*9415b8deSchristos __RCSID("$NetBSD: cvslatest.c,v 1.11 2023/03/07 21:24:19 christos Exp $");
421a7e9bc1Schristos 
431a7e9bc1Schristos /*
441a7e9bc1Schristos  * Find the latest timestamp in a set of CVS trees, by examining the
451a7e9bc1Schristos  * Entries files
461a7e9bc1Schristos  */
471a7e9bc1Schristos 
481a7e9bc1Schristos #include <sys/param.h>
491a7e9bc1Schristos #include <sys/types.h>
50b9b1da1fSmartin #include <sys/stat.h>
511a7e9bc1Schristos 
521a7e9bc1Schristos #include <stdio.h>
531a7e9bc1Schristos #include <string.h>
541a7e9bc1Schristos #include <stdlib.h>
551a7e9bc1Schristos #include <unistd.h>
56ecaf5f24Schristos #include <dirent.h>
571a7e9bc1Schristos #include <err.h>
581a7e9bc1Schristos #include <fts.h>
591a7e9bc1Schristos #include <time.h>
601a7e9bc1Schristos 
611a7e9bc1Schristos static int debug = 0;
621a7e9bc1Schristos static int ignore = 0;
631a7e9bc1Schristos 
641a7e9bc1Schristos struct latest {
651a7e9bc1Schristos 	time_t time;
661a7e9bc1Schristos 	char path[MAXPATHLEN];
671a7e9bc1Schristos };
681a7e9bc1Schristos 
691a7e9bc1Schristos static void
printlat(const struct latest * lat)701a7e9bc1Schristos printlat(const struct latest *lat)
711a7e9bc1Schristos {
721a7e9bc1Schristos 	fprintf(stderr, "%s %s", lat->path, ctime(&lat->time));
731a7e9bc1Schristos }
741a7e9bc1Schristos 
751a7e9bc1Schristos static void
getrepo(const FTSENT * e,char * repo,size_t maxrepo)76d59d37f7Schristos getrepo(const FTSENT *e, char *repo, size_t maxrepo)
771a7e9bc1Schristos {
781a7e9bc1Schristos 	FILE *fp;
79d59d37f7Schristos 	char name[MAXPATHLEN], ename[MAXPATHLEN];
801a7e9bc1Schristos 	char *ptr;
811a7e9bc1Schristos 
82d59d37f7Schristos 	snprintf(name, sizeof(name), "%s/Repository", e->fts_accpath);
83d59d37f7Schristos 	snprintf(ename, sizeof(ename), "%s/Repository", e->fts_path);
841a7e9bc1Schristos 	if ((fp = fopen(name, "r")) == NULL)
85d59d37f7Schristos 		err(EXIT_FAILURE, "Can't open `%s'", ename);
861a7e9bc1Schristos 	if (fgets(repo, (int)maxrepo, fp) == NULL)
87d59d37f7Schristos 		err(EXIT_FAILURE, "Can't read `%s'", ename);
881a7e9bc1Schristos 	if ((ptr = strchr(repo, '\n')) == NULL)
89d59d37f7Schristos 		errx(EXIT_FAILURE, "Malformed line in `%s'", ename);
901a7e9bc1Schristos 	*ptr = '\0';
911a7e9bc1Schristos 	fclose(fp);
921a7e9bc1Schristos }
931a7e9bc1Schristos 
941a7e9bc1Schristos static void
notimestamp(const char * fn,const char * ename,int uncommitted)95*9415b8deSchristos notimestamp(const char *fn, const char *ename, int uncommitted)
96*9415b8deSchristos {
97*9415b8deSchristos 	warnx("Can't get timestamp from %s file `%s' in `%s'",
98*9415b8deSchristos 	    uncommitted ? "uncommitted" : "locally modified", fn, ename);
99*9415b8deSchristos }
100*9415b8deSchristos 
101*9415b8deSchristos static void
getlatest(const FTSENT * e,const char * repo,struct latest * lat)102d59d37f7Schristos getlatest(const FTSENT *e, const char *repo, struct latest *lat)
1031a7e9bc1Schristos {
1041a7e9bc1Schristos 	static const char fmt[] = "%a %b %d %H:%M:%S %Y";
105d59d37f7Schristos 	char name[MAXPATHLEN], ename[MAXPATHLEN];
1061a7e9bc1Schristos 	char entry[MAXPATHLEN * 2];
1071a7e9bc1Schristos 	char *fn, *dt, *p;
1081a7e9bc1Schristos 	time_t t;
1091a7e9bc1Schristos 	struct tm tm;
110b9b1da1fSmartin 	struct stat sb;
1111a7e9bc1Schristos 	FILE *fp;
1121a7e9bc1Schristos 
113d59d37f7Schristos 	snprintf(name, sizeof(name), "%s/Entries", e->fts_accpath);
114d59d37f7Schristos 	snprintf(ename, sizeof(ename), "%s/Entries", e->fts_path);
1151a7e9bc1Schristos 	if ((fp = fopen(name, "r")) == NULL)
116d59d37f7Schristos 		err(EXIT_FAILURE, "Can't open `%s'", ename);
1171a7e9bc1Schristos 
1181a7e9bc1Schristos 	while (fgets(entry, (int)sizeof(entry), fp) != NULL) {
1191a7e9bc1Schristos 		if (entry[0] != '/')
1201a7e9bc1Schristos 		    continue;
1211a7e9bc1Schristos 		if ((fn = strtok(entry, "/")) == NULL)
1221a7e9bc1Schristos 			goto mal;
1231a7e9bc1Schristos 		if (strtok(NULL, "/") == NULL)
1241a7e9bc1Schristos 			goto mal;
1251a7e9bc1Schristos 		if ((dt = strtok(NULL, "/")) == NULL)
1261a7e9bc1Schristos 			goto mal;
127*9415b8deSchristos 		if (strncmp(dt, "dummy timestamp", 14) == 0) {
128*9415b8deSchristos 			notimestamp(fn, ename, 1);
129b9b1da1fSmartin 			if (!ignore)
130b9b1da1fSmartin 				exit(EXIT_FAILURE);
131b9b1da1fSmartin 			continue;
132b9b1da1fSmartin 		}
133b9b1da1fSmartin 		if (strcmp(dt, "Result of merge") == 0) {
134*9415b8deSchristos 			notimestamp(fn, ename, 0);
135b9b1da1fSmartin 			if (fstat(fileno(fp), &sb) == 0) {
136b9b1da1fSmartin 				t = sb.st_mtime;
137b9b1da1fSmartin 				goto compare;
138b9b1da1fSmartin 			}
139476a50b7Schristos 			if (!ignore)
140476a50b7Schristos 				exit(EXIT_FAILURE);
141476a50b7Schristos 			continue;
142476a50b7Schristos 		}
1431a7e9bc1Schristos 		if ((p = strptime(dt, fmt, &tm)) == NULL || *p) {
144d59d37f7Schristos 			warnx("Malformed time `%s' in `%s'", dt, ename);
1451a7e9bc1Schristos 			if (!ignore)
1461a7e9bc1Schristos 				exit(EXIT_FAILURE);
147476a50b7Schristos 			continue;
1481a7e9bc1Schristos 		}
149b074a61aSchristos 		tm.tm_isdst = 0;	// We are in GMT anyway
1501a7e9bc1Schristos 		if ((t = mktime(&tm)) == (time_t)-1)
1511a7e9bc1Schristos 			errx(EXIT_FAILURE, "Time conversion `%s' in `%s'",
152d59d37f7Schristos 			    dt, ename);
153b9b1da1fSmartin compare:
1541a7e9bc1Schristos 		if (lat->time == 0 || lat->time < t) {
1551a7e9bc1Schristos 			lat->time = t;
1561a7e9bc1Schristos 			snprintf(lat->path, sizeof(lat->path),
1571a7e9bc1Schristos 			    "%s/%s", repo, fn);
158f0153383Schristos 			if (debug > 1)
1591a7e9bc1Schristos 				printlat(lat);
1601a7e9bc1Schristos 		}
1611a7e9bc1Schristos 	}
162476a50b7Schristos 	if (ferror(fp))
163476a50b7Schristos 		err(EXIT_FAILURE, "Can't read `%s'", ename);
1641a7e9bc1Schristos 
1651a7e9bc1Schristos 	fclose(fp);
1661a7e9bc1Schristos 	return;
1671a7e9bc1Schristos 
1681a7e9bc1Schristos mal:
169d59d37f7Schristos 	errx(EXIT_FAILURE, "Malformed line in `%s'", ename);
1701a7e9bc1Schristos }
1711a7e9bc1Schristos 
1721a7e9bc1Schristos static void
cvsscan(char ** pathv,const char * name,struct latest * lat)1731a7e9bc1Schristos cvsscan(char **pathv, const char *name, struct latest *lat)
1741a7e9bc1Schristos {
1751a7e9bc1Schristos         FTS *dh;
1761a7e9bc1Schristos 	char repo[MAXPATHLEN];
1771a7e9bc1Schristos         FTSENT *entry;
1781a7e9bc1Schristos 
1791a7e9bc1Schristos 	lat->time = 0;
1801a7e9bc1Schristos 
1811a7e9bc1Schristos         dh = fts_open(pathv, FTS_PHYSICAL, NULL);
1821a7e9bc1Schristos         if (dh == NULL)
1831a7e9bc1Schristos 		err(EXIT_FAILURE, "fts_open `%s'", pathv[0]);
1841a7e9bc1Schristos 
1851a7e9bc1Schristos         while ((entry = fts_read(dh)) != NULL) {
1861a7e9bc1Schristos                 if (entry->fts_info != FTS_D)
1871a7e9bc1Schristos 			continue;
1881a7e9bc1Schristos 
1891a7e9bc1Schristos 		if (strcmp(entry->fts_name, name) != 0)
1901a7e9bc1Schristos                         continue;
1911a7e9bc1Schristos 
192d59d37f7Schristos 		getrepo(entry, repo, sizeof(repo));
193d59d37f7Schristos 		getlatest(entry, repo, lat);
1941a7e9bc1Schristos         }
1951a7e9bc1Schristos 
1961a7e9bc1Schristos         (void)fts_close(dh);
1971a7e9bc1Schristos }
1981a7e9bc1Schristos 
1991a7e9bc1Schristos static __dead void
usage(void)2001a7e9bc1Schristos usage(void)
2011a7e9bc1Schristos {
2021a7e9bc1Schristos 	fprintf(stderr, "Usage: %s [-di] [-n <name>] <path> ...\n",
2031a7e9bc1Schristos 	    getprogname());
2041a7e9bc1Schristos 	exit(EXIT_FAILURE);
2051a7e9bc1Schristos }
2061a7e9bc1Schristos 
207ecaf5f24Schristos static int
checkDir(char * path,size_t pathlen,const char * name)208ecaf5f24Schristos checkDir(char *path, size_t pathlen, const char *name)
209ecaf5f24Schristos {
210ecaf5f24Schristos 	static const char *files[] = {
211ecaf5f24Schristos 		"Entries", "Root", "Repository",
212ecaf5f24Schristos 	};
213ecaf5f24Schristos 	size_t i;
214ecaf5f24Schristos 
215ecaf5f24Schristos 	for (i = 0; i < __arraycount(files); i++) {
216ecaf5f24Schristos 		snprintf(path, pathlen, "%s/%s", name, files[i]);
217ecaf5f24Schristos 		if (access(path, F_OK) == -1)
218ecaf5f24Schristos 			return 0;
219ecaf5f24Schristos 	}
220ecaf5f24Schristos 
221ecaf5f24Schristos 	return 1;
222ecaf5f24Schristos }
223ecaf5f24Schristos 
224ecaf5f24Schristos static const char *
findCVSDir(char * path,size_t pathlen,const char * name)225ecaf5f24Schristos findCVSDir(char *path, size_t pathlen, const char *name)
226ecaf5f24Schristos {
227ecaf5f24Schristos 	DIR *dirp;
228ecaf5f24Schristos 	struct dirent *dp;
229ecaf5f24Schristos 	const char *n;
230ecaf5f24Schristos 
231ecaf5f24Schristos 	if ((dirp = opendir(name)) == NULL)
232ecaf5f24Schristos 		err(EXIT_FAILURE, "Can't open `%s'", name);
233ecaf5f24Schristos 
234ecaf5f24Schristos 	while ((dp = readdir(dirp)) != NULL) {
235ecaf5f24Schristos 		n = dp->d_name;
236ecaf5f24Schristos 		if (n[0] == '.' && (n[1] == '\0' ||
237ecaf5f24Schristos 		    (n[1] == '.' && n[2] == '\0')))
238ecaf5f24Schristos 			continue;
239ecaf5f24Schristos 		if (checkDir(path, pathlen, n))
240ecaf5f24Schristos 			goto out;
241ecaf5f24Schristos 	}
242ecaf5f24Schristos 	n = "CVS";
243ecaf5f24Schristos out:
244ecaf5f24Schristos 	strlcpy(path, n, pathlen);
24536c60184Schristos 	closedir(dirp);
246ecaf5f24Schristos 	return path;
247ecaf5f24Schristos }
248ecaf5f24Schristos 
249ecaf5f24Schristos 
2501a7e9bc1Schristos int
main(int argc,char * argv[])2511a7e9bc1Schristos main(int argc, char *argv[])
2521a7e9bc1Schristos {
2531a7e9bc1Schristos 	struct latest lat;
254ecaf5f24Schristos 	const char *name = NULL;
255ecaf5f24Schristos 	char path[MAXPATHLEN];
2561a7e9bc1Schristos 	int c;
2571a7e9bc1Schristos 
2581a7e9bc1Schristos 	while ((c = getopt(argc, argv, "din:")) != -1)
2591a7e9bc1Schristos 		switch (c) {
2601a7e9bc1Schristos 		case 'i':
2611a7e9bc1Schristos 			ignore++;
2621a7e9bc1Schristos 			break;
2631a7e9bc1Schristos 		case 'd':
2641a7e9bc1Schristos 			debug++;
2651a7e9bc1Schristos 			break;
2661a7e9bc1Schristos 		case 'n':
2671a7e9bc1Schristos 			name = optarg;
2681a7e9bc1Schristos 			break;
2691a7e9bc1Schristos 		default:
2701a7e9bc1Schristos 			usage();
2711a7e9bc1Schristos 		}
2721a7e9bc1Schristos 
2731a7e9bc1Schristos 	if (argc == optind)
2741a7e9bc1Schristos 		usage();
2751a7e9bc1Schristos 
276b074a61aSchristos 	// So that mktime behaves consistently
277b074a61aSchristos 	setenv("TZ", "UTC", 1);
278b074a61aSchristos 
279ecaf5f24Schristos 	if (name == NULL)
280ecaf5f24Schristos 		name = findCVSDir(path, sizeof(path), argv[optind]);
281ecaf5f24Schristos 
2821a7e9bc1Schristos 	cvsscan(argv + optind, name, &lat);
2831a7e9bc1Schristos 	if (debug)
2841a7e9bc1Schristos 		printlat(&lat);
2851a7e9bc1Schristos 	printf("%jd\n", (intmax_t)lat.time);
2861a7e9bc1Schristos 	return 0;
2871a7e9bc1Schristos }
288