1 /* $NetBSD: cvslatest.c,v 1.9 2020/11/03 22:21:43 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2016 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #ifdef __linux__ 33 #define _GNU_SOURCE 34 #endif 35 36 #ifdef HAVE_NBTOOL_CONFIG_H 37 #include "nbtool_config.h" 38 #endif 39 40 #include <sys/cdefs.h> 41 __RCSID("$NetBSD: cvslatest.c,v 1.9 2020/11/03 22:21:43 christos Exp $"); 42 43 /* 44 * Find the latest timestamp in a set of CVS trees, by examining the 45 * Entries files 46 */ 47 48 #include <sys/param.h> 49 #include <sys/types.h> 50 51 #include <stdio.h> 52 #include <string.h> 53 #include <stdlib.h> 54 #include <unistd.h> 55 #include <dirent.h> 56 #include <err.h> 57 #include <fts.h> 58 #include <time.h> 59 60 static int debug = 0; 61 static int ignore = 0; 62 63 struct latest { 64 time_t time; 65 char path[MAXPATHLEN]; 66 }; 67 68 static void 69 printlat(const struct latest *lat) 70 { 71 fprintf(stderr, "%s %s", lat->path, ctime(&lat->time)); 72 } 73 74 static void 75 getrepo(const FTSENT *e, char *repo, size_t maxrepo) 76 { 77 FILE *fp; 78 char name[MAXPATHLEN], ename[MAXPATHLEN]; 79 char *ptr; 80 81 snprintf(name, sizeof(name), "%s/Repository", e->fts_accpath); 82 snprintf(ename, sizeof(ename), "%s/Repository", e->fts_path); 83 if ((fp = fopen(name, "r")) == NULL) 84 err(EXIT_FAILURE, "Can't open `%s'", ename); 85 if (fgets(repo, (int)maxrepo, fp) == NULL) 86 err(EXIT_FAILURE, "Can't read `%s'", ename); 87 if ((ptr = strchr(repo, '\n')) == NULL) 88 errx(EXIT_FAILURE, "Malformed line in `%s'", ename); 89 *ptr = '\0'; 90 fclose(fp); 91 } 92 93 static void 94 getlatest(const FTSENT *e, const char *repo, struct latest *lat) 95 { 96 static const char fmt[] = "%a %b %d %H:%M:%S %Y"; 97 char name[MAXPATHLEN], ename[MAXPATHLEN]; 98 char entry[MAXPATHLEN * 2]; 99 char *fn, *dt, *p; 100 time_t t; 101 struct tm tm; 102 FILE *fp; 103 104 snprintf(name, sizeof(name), "%s/Entries", e->fts_accpath); 105 snprintf(ename, sizeof(ename), "%s/Entries", e->fts_path); 106 if ((fp = fopen(name, "r")) == NULL) 107 err(EXIT_FAILURE, "Can't open `%s'", ename); 108 109 while (fgets(entry, (int)sizeof(entry), fp) != NULL) { 110 if (entry[0] != '/') 111 continue; 112 if ((fn = strtok(entry, "/")) == NULL) 113 goto mal; 114 if (strtok(NULL, "/") == NULL) 115 goto mal; 116 if ((dt = strtok(NULL, "/")) == NULL) 117 goto mal; 118 if (strcmp(dt, "dummy timestamp") == 0) { 119 warnx("Can't get timestamp from uncommitted file `%s'", 120 ename); 121 if (!ignore) 122 exit(EXIT_FAILURE); 123 continue; 124 } 125 if ((p = strptime(dt, fmt, &tm)) == NULL || *p) { 126 warnx("Malformed time `%s' in `%s'", dt, ename); 127 if (!ignore) 128 exit(EXIT_FAILURE); 129 continue; 130 } 131 tm.tm_isdst = 0; // We are in GMT anyway 132 if ((t = mktime(&tm)) == (time_t)-1) 133 errx(EXIT_FAILURE, "Time conversion `%s' in `%s'", 134 dt, ename); 135 if (lat->time == 0 || lat->time < t) { 136 lat->time = t; 137 snprintf(lat->path, sizeof(lat->path), 138 "%s/%s", repo, fn); 139 if (debug > 1) 140 printlat(lat); 141 } 142 } 143 if (ferror(fp)) 144 err(EXIT_FAILURE, "Can't read `%s'", ename); 145 146 fclose(fp); 147 return; 148 149 mal: 150 errx(EXIT_FAILURE, "Malformed line in `%s'", ename); 151 } 152 153 static void 154 cvsscan(char **pathv, const char *name, struct latest *lat) 155 { 156 FTS *dh; 157 char repo[MAXPATHLEN]; 158 FTSENT *entry; 159 160 lat->time = 0; 161 162 dh = fts_open(pathv, FTS_PHYSICAL, NULL); 163 if (dh == NULL) 164 err(EXIT_FAILURE, "fts_open `%s'", pathv[0]); 165 166 while ((entry = fts_read(dh)) != NULL) { 167 if (entry->fts_info != FTS_D) 168 continue; 169 170 if (strcmp(entry->fts_name, name) != 0) 171 continue; 172 173 getrepo(entry, repo, sizeof(repo)); 174 getlatest(entry, repo, lat); 175 } 176 177 (void)fts_close(dh); 178 } 179 180 static __dead void 181 usage(void) 182 { 183 fprintf(stderr, "Usage: %s [-di] [-n <name>] <path> ...\n", 184 getprogname()); 185 exit(EXIT_FAILURE); 186 } 187 188 static int 189 checkDir(char *path, size_t pathlen, const char *name) 190 { 191 static const char *files[] = { 192 "Entries", "Root", "Repository", 193 }; 194 size_t i; 195 196 for (i = 0; i < __arraycount(files); i++) { 197 snprintf(path, pathlen, "%s/%s", name, files[i]); 198 if (access(path, F_OK) == -1) 199 return 0; 200 } 201 202 return 1; 203 } 204 205 static const char * 206 findCVSDir(char *path, size_t pathlen, const char *name) 207 { 208 DIR *dirp; 209 struct dirent *dp; 210 const char *n; 211 212 if ((dirp = opendir(name)) == NULL) 213 err(EXIT_FAILURE, "Can't open `%s'", name); 214 215 while ((dp = readdir(dirp)) != NULL) { 216 n = dp->d_name; 217 if (n[0] == '.' && (n[1] == '\0' || 218 (n[1] == '.' && n[2] == '\0'))) 219 continue; 220 if (checkDir(path, pathlen, n)) 221 goto out; 222 } 223 n = "CVS"; 224 out: 225 strlcpy(path, n, pathlen); 226 closedir(dirp); 227 return path; 228 } 229 230 231 int 232 main(int argc, char *argv[]) 233 { 234 struct latest lat; 235 const char *name = NULL; 236 char path[MAXPATHLEN]; 237 int c; 238 239 while ((c = getopt(argc, argv, "din:")) != -1) 240 switch (c) { 241 case 'i': 242 ignore++; 243 break; 244 case 'd': 245 debug++; 246 break; 247 case 'n': 248 name = optarg; 249 break; 250 default: 251 usage(); 252 } 253 254 if (argc == optind) 255 usage(); 256 257 // So that mktime behaves consistently 258 setenv("TZ", "UTC", 1); 259 260 if (name == NULL) 261 name = findCVSDir(path, sizeof(path), argv[optind]); 262 263 cvsscan(argv + optind, name, &lat); 264 if (debug) 265 printlat(&lat); 266 printf("%jd\n", (intmax_t)lat.time); 267 return 0; 268 } 269