xref: /dragonfly/usr.bin/dsynth/repo.c (revision 68dc2eea)
18e25f19bSMatthew Dillon /*
28e25f19bSMatthew Dillon  * Copyright (c) 2019 The DragonFly Project.  All rights reserved.
38e25f19bSMatthew Dillon  *
48e25f19bSMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
58e25f19bSMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
68e25f19bSMatthew Dillon  *
78e25f19bSMatthew Dillon  * This code uses concepts and configuration based on 'synth', by
88e25f19bSMatthew Dillon  * John R. Marino <draco@marino.st>, which was written in ada.
98e25f19bSMatthew Dillon  *
108e25f19bSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
118e25f19bSMatthew Dillon  * modification, are permitted provided that the following conditions
128e25f19bSMatthew Dillon  * are met:
138e25f19bSMatthew Dillon  *
148e25f19bSMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
158e25f19bSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
168e25f19bSMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
178e25f19bSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
188e25f19bSMatthew Dillon  *    the documentation and/or other materials provided with the
198e25f19bSMatthew Dillon  *    distribution.
208e25f19bSMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
218e25f19bSMatthew Dillon  *    contributors may be used to endorse or promote products derived
228e25f19bSMatthew Dillon  *    from this software without specific, prior written permission.
238e25f19bSMatthew Dillon  *
248e25f19bSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
258e25f19bSMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
268e25f19bSMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
278e25f19bSMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
288e25f19bSMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
298e25f19bSMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
308e25f19bSMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
318e25f19bSMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
328e25f19bSMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
338e25f19bSMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
348e25f19bSMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
358e25f19bSMatthew Dillon  * SUCH DAMAGE.
368e25f19bSMatthew Dillon  */
378e25f19bSMatthew Dillon #include "dsynth.h"
388e25f19bSMatthew Dillon 
391645cafeSMatthew Dillon typedef struct pinfo {
401645cafeSMatthew Dillon 	struct pinfo *next;
411645cafeSMatthew Dillon 	char *spath;
421645cafeSMatthew Dillon 	int foundit;
431645cafeSMatthew Dillon } pinfo_t;
441645cafeSMatthew Dillon 
45f4094b20SMatthew Dillon static void removePackagesMetaRecurse(pkg_t *pkg);
461645cafeSMatthew Dillon static int pinfocmp(const void *s1, const void *s2);
471645cafeSMatthew Dillon static void scanit(const char *path, const char *subpath,
481645cafeSMatthew Dillon 			int *countp, pinfo_t ***list_tailp);
491645cafeSMatthew Dillon pinfo_t *pinfofind(pinfo_t **ary, int count, char *spath);
50066daf2aSMatthew Dillon static void childRebuildRepo(bulk_t *bulk);
51f7f25838SMatthew Dillon static void scandeletenew(const char *path);
521645cafeSMatthew Dillon 
53066daf2aSMatthew Dillon static void rebuildTerminateSignal(int signo);
54066daf2aSMatthew Dillon 
55066daf2aSMatthew Dillon static char *RebuildRemovePath;
56066daf2aSMatthew Dillon 
578e25f19bSMatthew Dillon void
581645cafeSMatthew Dillon DoRebuildRepo(int ask)
598e25f19bSMatthew Dillon {
60066daf2aSMatthew Dillon 	bulk_t *bulk;
61066daf2aSMatthew Dillon 	FILE *fp;
62066daf2aSMatthew Dillon 	int fd;
63066daf2aSMatthew Dillon 	char tpath[256];
64*68dc2eeaSMatthew Dillon 	const char *sufx;
651645cafeSMatthew Dillon 
661645cafeSMatthew Dillon 	if (ask) {
671645cafeSMatthew Dillon 		if (askyn("Rebuild the repository? ") == 0)
681645cafeSMatthew Dillon 			return;
691645cafeSMatthew Dillon 	}
70f7f25838SMatthew Dillon 
71f7f25838SMatthew Dillon 	/*
72f7f25838SMatthew Dillon 	 * Scan the repository for temporary .new files and delete them.
73f7f25838SMatthew Dillon 	 */
74f7f25838SMatthew Dillon 	scandeletenew(RepositoryPath);
75f7f25838SMatthew Dillon 
76f7f25838SMatthew Dillon 	/*
77066daf2aSMatthew Dillon 	 * Generate temporary file
78f7f25838SMatthew Dillon 	 */
79066daf2aSMatthew Dillon 	snprintf(tpath, sizeof(tpath), "/tmp/meta.XXXXXXXX.conf");
80066daf2aSMatthew Dillon 
81066daf2aSMatthew Dillon 	signal(SIGTERM, rebuildTerminateSignal);
82066daf2aSMatthew Dillon 	signal(SIGINT, rebuildTerminateSignal);
83066daf2aSMatthew Dillon 	signal(SIGHUP, rebuildTerminateSignal);
84066daf2aSMatthew Dillon 
85066daf2aSMatthew Dillon 	RebuildRemovePath = tpath;
86066daf2aSMatthew Dillon 
87*68dc2eeaSMatthew Dillon 	sufx = USE_PKG_SUFX;
88066daf2aSMatthew Dillon 	fd = mkostemps(tpath, 5, 0);
89066daf2aSMatthew Dillon 	if (fd < 0)
90066daf2aSMatthew Dillon 		dfatal_errno("Cannot create %s", tpath);
91066daf2aSMatthew Dillon 	fp = fdopen(fd, "w");
92066daf2aSMatthew Dillon 	fprintf(fp, "version = 1;\n");
93*68dc2eeaSMatthew Dillon 	fprintf(fp, "packing_format = \"%s\";\n", sufx + 1);
94066daf2aSMatthew Dillon 	fclose(fp);
95066daf2aSMatthew Dillon 
96066daf2aSMatthew Dillon 	/*
97066daf2aSMatthew Dillon 	 * Run the operation under our bulk infrastructure to
98066daf2aSMatthew Dillon 	 * get the correct environment.
99066daf2aSMatthew Dillon 	 */
100066daf2aSMatthew Dillon 	initbulk(childRebuildRepo, 1);
101066daf2aSMatthew Dillon 	queuebulk(tpath, NULL, NULL, NULL);
102066daf2aSMatthew Dillon 	bulk = getbulk();
103066daf2aSMatthew Dillon 
104066daf2aSMatthew Dillon 	if (bulk->r1)
1051645cafeSMatthew Dillon 		printf("Rebuild succeeded\n");
106066daf2aSMatthew Dillon 	else
107066daf2aSMatthew Dillon 		printf("Rebuild failed\n");
108066daf2aSMatthew Dillon 	donebulk();
109066daf2aSMatthew Dillon 
110066daf2aSMatthew Dillon 	remove(tpath);
111066daf2aSMatthew Dillon }
112066daf2aSMatthew Dillon 
113066daf2aSMatthew Dillon static void
114066daf2aSMatthew Dillon childRebuildRepo(bulk_t *bulk)
115066daf2aSMatthew Dillon {
116066daf2aSMatthew Dillon 	FILE *fp;
117066daf2aSMatthew Dillon 	char *ptr;
118066daf2aSMatthew Dillon 	size_t len;
119066daf2aSMatthew Dillon 	pid_t pid;
120066daf2aSMatthew Dillon 	const char *cav[MAXCAC];
121066daf2aSMatthew Dillon 	int cac;
122066daf2aSMatthew Dillon 
123066daf2aSMatthew Dillon 	cac = 0;
124066daf2aSMatthew Dillon 	cav[cac++] = PKG_BINARY;
125066daf2aSMatthew Dillon 	cav[cac++] = "repo";
126066daf2aSMatthew Dillon 	cav[cac++] = "-m";
127066daf2aSMatthew Dillon 	cav[cac++] = bulk->s1;
128066daf2aSMatthew Dillon 	cav[cac++] = "-o";
129066daf2aSMatthew Dillon 	cav[cac++] = PackagesPath;
130066daf2aSMatthew Dillon 	cav[cac++] = RepositoryPath;
131066daf2aSMatthew Dillon 
132066daf2aSMatthew Dillon 	printf("pkg repo -m %s -o %s %s\n",
133066daf2aSMatthew Dillon 	       bulk->s1, PackagesPath, RepositoryPath);
134066daf2aSMatthew Dillon 
135*68dc2eeaSMatthew Dillon 	fp = dexec_open(cav, cac, &pid, NULL, 1, 0);
136066daf2aSMatthew Dillon 	while ((ptr = fgetln(fp, &len)) != NULL)
137066daf2aSMatthew Dillon 		fwrite(ptr, 1, len, stdout);
138066daf2aSMatthew Dillon 	if (dexec_close(fp, pid) == 0) {
139066daf2aSMatthew Dillon 		bulk->r1 = strdup("");
1401645cafeSMatthew Dillon 	}
1418e25f19bSMatthew Dillon }
1428e25f19bSMatthew Dillon 
1438e25f19bSMatthew Dillon void
1448e25f19bSMatthew Dillon DoUpgradePkgs(pkg_t *pkgs __unused, int ask __unused)
1458e25f19bSMatthew Dillon {
1468e25f19bSMatthew Dillon 	dfatal("Not Implemented");
1478e25f19bSMatthew Dillon }
1488e25f19bSMatthew Dillon 
1498e25f19bSMatthew Dillon void
1501645cafeSMatthew Dillon PurgeDistfiles(pkg_t *pkgs)
1518e25f19bSMatthew Dillon {
1521645cafeSMatthew Dillon 	pinfo_t *list;
1531645cafeSMatthew Dillon 	pinfo_t *item;
1541645cafeSMatthew Dillon 	pinfo_t **list_tail;
1551645cafeSMatthew Dillon 	pinfo_t **ary;
1561645cafeSMatthew Dillon 	char *dstr;
1571645cafeSMatthew Dillon 	char *buf;
1581645cafeSMatthew Dillon 	int count;
1591645cafeSMatthew Dillon 	int delcount;
1601645cafeSMatthew Dillon 	int i;
1611645cafeSMatthew Dillon 
1621645cafeSMatthew Dillon 	printf("Scanning distfiles... ");
1631645cafeSMatthew Dillon 	fflush(stdout);
1641645cafeSMatthew Dillon 	count = 0;
1651645cafeSMatthew Dillon 	list = NULL;
1661645cafeSMatthew Dillon 	list_tail = &list;
1671645cafeSMatthew Dillon 	scanit(DistFilesPath, NULL, &count, &list_tail);
1681645cafeSMatthew Dillon 	printf("Checking %d distfiles\n", count);
1691645cafeSMatthew Dillon 	fflush(stdout);
1701645cafeSMatthew Dillon 
1711645cafeSMatthew Dillon 	ary = calloc(count, sizeof(pinfo_t *));
1721645cafeSMatthew Dillon 	for (i = 0; i < count; ++i) {
1731645cafeSMatthew Dillon 		ary[i] = list;
1741645cafeSMatthew Dillon 		list = list->next;
1751645cafeSMatthew Dillon 	}
1761645cafeSMatthew Dillon 	ddassert(list == NULL);
1771645cafeSMatthew Dillon 	qsort(ary, count, sizeof(pinfo_t *), pinfocmp);
1781645cafeSMatthew Dillon 
1791645cafeSMatthew Dillon 	for (; pkgs; pkgs = pkgs->bnext) {
1801645cafeSMatthew Dillon 		if (pkgs->distfiles == NULL || pkgs->distfiles[0] == 0)
1811645cafeSMatthew Dillon 			continue;
1821645cafeSMatthew Dillon 		ddprintf(0, "distfiles %s\n", pkgs->distfiles);
1831645cafeSMatthew Dillon 		dstr = strtok(pkgs->distfiles, " \t");
1841645cafeSMatthew Dillon 		while (dstr) {
1851645cafeSMatthew Dillon 			for (;;) {
1864ea2ee4dSMatthew Dillon 				if (pkgs->distsubdir) {
1871645cafeSMatthew Dillon 					asprintf(&buf, "%s/%s",
1881645cafeSMatthew Dillon 						 pkgs->distsubdir, dstr);
1891645cafeSMatthew Dillon 					item = pinfofind(ary, count, buf);
1901645cafeSMatthew Dillon 					ddprintf(0, "TEST %s %p\n", buf, item);
1911645cafeSMatthew Dillon 					free(buf);
1921645cafeSMatthew Dillon 					buf = NULL;
1931645cafeSMatthew Dillon 				} else {
1941645cafeSMatthew Dillon 					item = pinfofind(ary, count, dstr);
1951645cafeSMatthew Dillon 					ddprintf(0, "TEST %s %p\n", dstr, item);
1961645cafeSMatthew Dillon 				}
1971645cafeSMatthew Dillon 				if (item) {
1981645cafeSMatthew Dillon 					item->foundit = 1;
1991645cafeSMatthew Dillon 					break;
2001645cafeSMatthew Dillon 				}
2011645cafeSMatthew Dillon 				if (strrchr(dstr, ':') == NULL)
2021645cafeSMatthew Dillon 					break;
2031645cafeSMatthew Dillon 				*strrchr(dstr, ':') = 0;
2041645cafeSMatthew Dillon 			}
2051645cafeSMatthew Dillon 			dstr = strtok(NULL, " \t");
2061645cafeSMatthew Dillon 		}
2071645cafeSMatthew Dillon 	}
2081645cafeSMatthew Dillon 
2091645cafeSMatthew Dillon 	delcount = 0;
2101645cafeSMatthew Dillon 	for (i = 0; i < count; ++i) {
2111645cafeSMatthew Dillon 		item = ary[i];
2121645cafeSMatthew Dillon 		if (item->foundit == 0) {
2131645cafeSMatthew Dillon 			++delcount;
2141645cafeSMatthew Dillon 		}
2151645cafeSMatthew Dillon 	}
2161645cafeSMatthew Dillon 	if (askyn("Delete %d of %d items? ", delcount, count)) {
2171645cafeSMatthew Dillon 		printf("Deleting %d/%d obsolete source distfiles\n",
2181645cafeSMatthew Dillon 		       delcount, count);
2191645cafeSMatthew Dillon 		for (i = 0; i < count; ++i) {
2201645cafeSMatthew Dillon 			item = ary[i];
2211645cafeSMatthew Dillon 			if (item->foundit == 0) {
2221645cafeSMatthew Dillon 				asprintf(&buf, "%s/%s",
2231645cafeSMatthew Dillon 					 DistFilesPath, item->spath);
2241645cafeSMatthew Dillon 				if (remove(buf) < 0)
2251645cafeSMatthew Dillon 					printf("Cannot delete %s\n", buf);
2261645cafeSMatthew Dillon 				free(buf);
2271645cafeSMatthew Dillon 			}
2281645cafeSMatthew Dillon 		}
2291645cafeSMatthew Dillon 	}
2301645cafeSMatthew Dillon 
2311645cafeSMatthew Dillon 
2321645cafeSMatthew Dillon 	free(ary);
2338e25f19bSMatthew Dillon }
2348e25f19bSMatthew Dillon 
2358e25f19bSMatthew Dillon void
236f4094b20SMatthew Dillon RemovePackages(pkg_t *list)
2378e25f19bSMatthew Dillon {
238f4094b20SMatthew Dillon 	pkg_t *scan;
239f4094b20SMatthew Dillon 	char *path;
240f4094b20SMatthew Dillon 
241f4094b20SMatthew Dillon 	for (scan = list; scan; scan = scan->bnext) {
242f4094b20SMatthew Dillon 		if ((scan->flags & PKGF_MANUALSEL) == 0)
243f4094b20SMatthew Dillon 			continue;
244f4094b20SMatthew Dillon 		if (scan->pkgfile) {
245f4094b20SMatthew Dillon 			scan->flags &= ~PKGF_PACKAGED;
246f4094b20SMatthew Dillon 			scan->pkgfile_size = 0;
247f4094b20SMatthew Dillon 			asprintf(&path, "%s/%s", RepositoryPath, scan->pkgfile);
248f4094b20SMatthew Dillon 			if (remove(path) == 0)
249f4094b20SMatthew Dillon 				printf("Removed: %s\n", path);
250f4094b20SMatthew Dillon 			free(path);
251f4094b20SMatthew Dillon 		}
252f4094b20SMatthew Dillon 		if (scan->pkgfile == NULL ||
253f4094b20SMatthew Dillon 		    (scan->flags & (PKGF_DUMMY | PKGF_META))) {
254f4094b20SMatthew Dillon 			removePackagesMetaRecurse(scan);
255f4094b20SMatthew Dillon 		}
256f4094b20SMatthew Dillon 	}
257f4094b20SMatthew Dillon }
258f4094b20SMatthew Dillon 
259f4094b20SMatthew Dillon static void
260f4094b20SMatthew Dillon removePackagesMetaRecurse(pkg_t *pkg)
261f4094b20SMatthew Dillon {
262f4094b20SMatthew Dillon 	pkglink_t *link;
263f4094b20SMatthew Dillon 	pkg_t *scan;
264f4094b20SMatthew Dillon 	char *path;
265f4094b20SMatthew Dillon 
266f4094b20SMatthew Dillon 	PKGLIST_FOREACH(link, &pkg->idepon_list) {
267f4094b20SMatthew Dillon 		scan = link->pkg;
268f4094b20SMatthew Dillon 		if (scan == NULL)
269f4094b20SMatthew Dillon 			continue;
270f4094b20SMatthew Dillon 		if (scan->pkgfile == NULL ||
271f4094b20SMatthew Dillon 		    (scan->flags & (PKGF_DUMMY | PKGF_META))) {
272f4094b20SMatthew Dillon 			removePackagesMetaRecurse(scan);
273f4094b20SMatthew Dillon 			continue;
274f4094b20SMatthew Dillon 		}
275f4094b20SMatthew Dillon 		scan->flags &= ~PKGF_PACKAGED;
276f4094b20SMatthew Dillon 		scan->pkgfile_size = 0;
277f4094b20SMatthew Dillon 
278f4094b20SMatthew Dillon 		asprintf(&path, "%s/%s", RepositoryPath, scan->pkgfile);
279f4094b20SMatthew Dillon 		if (remove(path) == 0)
280f4094b20SMatthew Dillon 			printf("Removed: %s\n", path);
281f4094b20SMatthew Dillon 		free(path);
282f4094b20SMatthew Dillon 	}
2838e25f19bSMatthew Dillon }
2841645cafeSMatthew Dillon 
2851645cafeSMatthew Dillon static int
2861645cafeSMatthew Dillon pinfocmp(const void *s1, const void *s2)
2871645cafeSMatthew Dillon {
2881645cafeSMatthew Dillon 	const pinfo_t *item1 = *(const pinfo_t *const*)s1;
2891645cafeSMatthew Dillon 	const pinfo_t *item2 = *(const pinfo_t *const*)s2;
2901645cafeSMatthew Dillon 
2911645cafeSMatthew Dillon 	return (strcmp(item1->spath, item2->spath));
2921645cafeSMatthew Dillon }
2931645cafeSMatthew Dillon 
2941645cafeSMatthew Dillon pinfo_t *
2951645cafeSMatthew Dillon pinfofind(pinfo_t **ary, int count, char *spath)
2961645cafeSMatthew Dillon {
2971645cafeSMatthew Dillon 	pinfo_t *item;
2981645cafeSMatthew Dillon 	int res;
2991645cafeSMatthew Dillon 	int b;
3001645cafeSMatthew Dillon 	int e;
3011645cafeSMatthew Dillon 	int m;
3021645cafeSMatthew Dillon 
3031645cafeSMatthew Dillon 	b = 0;
3041645cafeSMatthew Dillon 	e = count;
3051645cafeSMatthew Dillon 	while (b != e) {
3061645cafeSMatthew Dillon 		m = b + (e - b) / 2;
3071645cafeSMatthew Dillon 		item = ary[m];
3081645cafeSMatthew Dillon 		res = strcmp(spath, item->spath);
3091645cafeSMatthew Dillon 		if (res == 0)
3101645cafeSMatthew Dillon 			return item;
3111645cafeSMatthew Dillon 		if (res < 0) {
3121645cafeSMatthew Dillon 			e = m;
3131645cafeSMatthew Dillon 		} else {
3141645cafeSMatthew Dillon 			b = m + 1;
3151645cafeSMatthew Dillon 		}
3161645cafeSMatthew Dillon 	}
3171645cafeSMatthew Dillon 	return NULL;
3181645cafeSMatthew Dillon }
3191645cafeSMatthew Dillon 
3201645cafeSMatthew Dillon void
3211645cafeSMatthew Dillon scanit(const char *path, const char *subpath,
3221645cafeSMatthew Dillon        int *countp, pinfo_t ***list_tailp)
3231645cafeSMatthew Dillon {
3241645cafeSMatthew Dillon 	struct dirent *den;
3251645cafeSMatthew Dillon 	pinfo_t *item;
3261645cafeSMatthew Dillon 	char *npath;
3271645cafeSMatthew Dillon 	char *spath;
3281645cafeSMatthew Dillon 	DIR *dir;
3291645cafeSMatthew Dillon 	struct stat st;
3301645cafeSMatthew Dillon 
3311645cafeSMatthew Dillon 	if ((dir = opendir(path)) != NULL) {
3321645cafeSMatthew Dillon 		while ((den = readdir(dir)) != NULL) {
3331645cafeSMatthew Dillon 			if (den->d_namlen == 1 && den->d_name[0] == '.')
3341645cafeSMatthew Dillon 				continue;
3351645cafeSMatthew Dillon 			if (den->d_namlen == 2 && den->d_name[0] == '.' &&
3361645cafeSMatthew Dillon 			    den->d_name[1] == '.')
3371645cafeSMatthew Dillon 				continue;
3381645cafeSMatthew Dillon 			asprintf(&npath, "%s/%s", path, den->d_name);
3391645cafeSMatthew Dillon 			if (lstat(npath, &st) < 0) {
3401645cafeSMatthew Dillon 				free(npath);
3411645cafeSMatthew Dillon 				continue;
3421645cafeSMatthew Dillon 			}
3431645cafeSMatthew Dillon 			if (S_ISDIR(st.st_mode)) {
3441645cafeSMatthew Dillon 				if (subpath) {
3451645cafeSMatthew Dillon 					asprintf(&spath, "%s/%s",
3461645cafeSMatthew Dillon 						 subpath, den->d_name);
3471645cafeSMatthew Dillon 					scanit(npath, spath,
3481645cafeSMatthew Dillon 					       countp, list_tailp);
3491645cafeSMatthew Dillon 					free(spath);
3501645cafeSMatthew Dillon 				} else {
3511645cafeSMatthew Dillon 					scanit(npath, den->d_name,
3521645cafeSMatthew Dillon 					       countp, list_tailp);
3531645cafeSMatthew Dillon 				}
3541645cafeSMatthew Dillon 			} else if (S_ISREG(st.st_mode)) {
3551645cafeSMatthew Dillon 				item = calloc(1, sizeof(*item));
3561645cafeSMatthew Dillon 				if (subpath) {
3571645cafeSMatthew Dillon 					asprintf(&item->spath, "%s/%s",
3581645cafeSMatthew Dillon 						 subpath, den->d_name);
3591645cafeSMatthew Dillon 				} else {
3601645cafeSMatthew Dillon 					item->spath = strdup(den->d_name);
3611645cafeSMatthew Dillon 				}
3621645cafeSMatthew Dillon 				**list_tailp = item;
3631645cafeSMatthew Dillon 				*list_tailp = &item->next;
3641645cafeSMatthew Dillon 				++*countp;
3651645cafeSMatthew Dillon 				ddprintf(0, "scan   %s\n", item->spath);
3661645cafeSMatthew Dillon 			}
3676a3a20b1SMatthew Dillon 			free(npath);
3681645cafeSMatthew Dillon 		}
3691645cafeSMatthew Dillon 		closedir(dir);
3701645cafeSMatthew Dillon 	}
3711645cafeSMatthew Dillon }
372f7f25838SMatthew Dillon 
373f7f25838SMatthew Dillon /*
374f7f25838SMatthew Dillon  * This removes any .new files left over in the repo.  These can wind
375f7f25838SMatthew Dillon  * being left around when dsynth is killed.
376f7f25838SMatthew Dillon  */
377f7f25838SMatthew Dillon static void
378f7f25838SMatthew Dillon scandeletenew(const char *path)
379f7f25838SMatthew Dillon {
380f7f25838SMatthew Dillon 	struct dirent *den;
381f7f25838SMatthew Dillon 	const char *ptr;
382f7f25838SMatthew Dillon 	DIR *dir;
383f7f25838SMatthew Dillon 	char *buf;
384f7f25838SMatthew Dillon 
385f7f25838SMatthew Dillon 	if ((dir = opendir(path)) == NULL)
386f7f25838SMatthew Dillon 		dfatal_errno("Cannot scan directory %s", path);
387f7f25838SMatthew Dillon 	while ((den = readdir(dir)) != NULL) {
388f7f25838SMatthew Dillon 		if ((ptr = strrchr(den->d_name, '.')) != NULL &&
389f7f25838SMatthew Dillon 		    strcmp(ptr, ".new") == 0) {
390f7f25838SMatthew Dillon 			asprintf(&buf, "%s/%s", path, den->d_name);
391f7f25838SMatthew Dillon 			if (remove(buf) < 0)
392f7f25838SMatthew Dillon 				dfatal_errno("remove: Garbage %s\n", buf);
393f7f25838SMatthew Dillon 			printf("Deleted Garbage %s\n", buf);
394f7f25838SMatthew Dillon 			free(buf);
395f7f25838SMatthew Dillon 		}
396f7f25838SMatthew Dillon 	}
397f7f25838SMatthew Dillon 	closedir(dir);
398f7f25838SMatthew Dillon }
399066daf2aSMatthew Dillon 
400066daf2aSMatthew Dillon static void
401066daf2aSMatthew Dillon rebuildTerminateSignal(int signo __unused)
402066daf2aSMatthew Dillon {
403066daf2aSMatthew Dillon 	if (RebuildRemovePath)
404066daf2aSMatthew Dillon 		remove(RebuildRemovePath);
405066daf2aSMatthew Dillon 	exit(1);
406066daf2aSMatthew Dillon 
407066daf2aSMatthew Dillon }
408