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]; 6468dc2eeaSMatthew 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 8768dc2eeaSMatthew 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"); 9368dc2eeaSMatthew 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 114*cda252a4SMatthew Dillon repackage(const char *basepath, const char *basefile, const char *sufx, 115*cda252a4SMatthew Dillon const char *comp, const char *decomp); 116*cda252a4SMatthew Dillon 117*cda252a4SMatthew Dillon static void 118066daf2aSMatthew Dillon childRebuildRepo(bulk_t *bulk) 119066daf2aSMatthew Dillon { 120066daf2aSMatthew Dillon FILE *fp; 121066daf2aSMatthew Dillon char *ptr; 122066daf2aSMatthew Dillon size_t len; 123066daf2aSMatthew Dillon pid_t pid; 124066daf2aSMatthew Dillon const char *cav[MAXCAC]; 125066daf2aSMatthew Dillon int cac; 126066daf2aSMatthew Dillon 127066daf2aSMatthew Dillon cac = 0; 128066daf2aSMatthew Dillon cav[cac++] = PKG_BINARY; 129066daf2aSMatthew Dillon cav[cac++] = "repo"; 130066daf2aSMatthew Dillon cav[cac++] = "-m"; 131066daf2aSMatthew Dillon cav[cac++] = bulk->s1; 132066daf2aSMatthew Dillon cav[cac++] = "-o"; 133066daf2aSMatthew Dillon cav[cac++] = PackagesPath; 134*cda252a4SMatthew Dillon 135*cda252a4SMatthew Dillon /* 136*cda252a4SMatthew Dillon * The yaml needs to generate paths relative to PackagePath 137*cda252a4SMatthew Dillon */ 138*cda252a4SMatthew Dillon if (strncmp(PackagesPath, RepositoryPath, strlen(PackagesPath)) == 0) 139*cda252a4SMatthew Dillon cav[cac++] = PackagesPath; 140*cda252a4SMatthew Dillon else 141066daf2aSMatthew Dillon cav[cac++] = RepositoryPath; 142066daf2aSMatthew Dillon 143*cda252a4SMatthew Dillon printf("pkg repo -m %s -o %s %s\n", bulk->s1, cav[cac-2], cav[cac-1]); 144066daf2aSMatthew Dillon 14568dc2eeaSMatthew Dillon fp = dexec_open(cav, cac, &pid, NULL, 1, 0); 146066daf2aSMatthew Dillon while ((ptr = fgetln(fp, &len)) != NULL) 147066daf2aSMatthew Dillon fwrite(ptr, 1, len, stdout); 148066daf2aSMatthew Dillon if (dexec_close(fp, pid) == 0) { 149066daf2aSMatthew Dillon bulk->r1 = strdup(""); 1501645cafeSMatthew Dillon } 151*cda252a4SMatthew Dillon 152*cda252a4SMatthew Dillon /* 153*cda252a4SMatthew Dillon * Repackage the .txz files created by pkg repo if necessary 154*cda252a4SMatthew Dillon */ 155*cda252a4SMatthew Dillon if (strcmp(USE_PKG_SUFX, ".txz") != 0) { 156*cda252a4SMatthew Dillon const char *comp; 157*cda252a4SMatthew Dillon const char *decomp; 158*cda252a4SMatthew Dillon 159*cda252a4SMatthew Dillon if (strcmp(USE_PKG_SUFX, ".tar") == 0) { 160*cda252a4SMatthew Dillon decomp = "unxz"; 161*cda252a4SMatthew Dillon comp = "cat"; 162*cda252a4SMatthew Dillon } else if (strcmp(USE_PKG_SUFX, ".tgz") == 0) { 163*cda252a4SMatthew Dillon decomp = "unxz"; 164*cda252a4SMatthew Dillon comp = "gzip"; 165*cda252a4SMatthew Dillon } else if (strcmp(USE_PKG_SUFX, ".tbz") == 0) { 166*cda252a4SMatthew Dillon decomp = "unxz"; 167*cda252a4SMatthew Dillon comp = "bzip"; 168*cda252a4SMatthew Dillon } else { 169*cda252a4SMatthew Dillon dfatal("repackaging as %s not supported", USE_PKG_SUFX); 170*cda252a4SMatthew Dillon decomp = "unxz"; 171*cda252a4SMatthew Dillon comp = "cat"; 172*cda252a4SMatthew Dillon } 173*cda252a4SMatthew Dillon repackage(PackagesPath, "digests", USE_PKG_SUFX, 174*cda252a4SMatthew Dillon comp, decomp); 175*cda252a4SMatthew Dillon repackage(PackagesPath, "packagesite", USE_PKG_SUFX, 176*cda252a4SMatthew Dillon comp, decomp); 177*cda252a4SMatthew Dillon } 178*cda252a4SMatthew Dillon } 179*cda252a4SMatthew Dillon 180*cda252a4SMatthew Dillon static 181*cda252a4SMatthew Dillon void 182*cda252a4SMatthew Dillon repackage(const char *basepath, const char *basefile, const char *sufx, 183*cda252a4SMatthew Dillon const char *comp, const char *decomp) 184*cda252a4SMatthew Dillon { 185*cda252a4SMatthew Dillon char *buf; 186*cda252a4SMatthew Dillon 187*cda252a4SMatthew Dillon asprintf(&buf, "%s < %s/%s.txz | %s > %s/%s%s", 188*cda252a4SMatthew Dillon decomp, basepath, basefile, comp, basepath, basefile, sufx); 189*cda252a4SMatthew Dillon if (system(buf) != 0) { 190*cda252a4SMatthew Dillon dfatal("command failed: %s", buf); 191*cda252a4SMatthew Dillon } 192*cda252a4SMatthew Dillon free(buf); 1938e25f19bSMatthew Dillon } 1948e25f19bSMatthew Dillon 1958e25f19bSMatthew Dillon void 1968e25f19bSMatthew Dillon DoUpgradePkgs(pkg_t *pkgs __unused, int ask __unused) 1978e25f19bSMatthew Dillon { 1988e25f19bSMatthew Dillon dfatal("Not Implemented"); 1998e25f19bSMatthew Dillon } 2008e25f19bSMatthew Dillon 2018e25f19bSMatthew Dillon void 2021645cafeSMatthew Dillon PurgeDistfiles(pkg_t *pkgs) 2038e25f19bSMatthew Dillon { 2041645cafeSMatthew Dillon pinfo_t *list; 2051645cafeSMatthew Dillon pinfo_t *item; 2061645cafeSMatthew Dillon pinfo_t **list_tail; 2071645cafeSMatthew Dillon pinfo_t **ary; 2081645cafeSMatthew Dillon char *dstr; 2091645cafeSMatthew Dillon char *buf; 2101645cafeSMatthew Dillon int count; 2111645cafeSMatthew Dillon int delcount; 2121645cafeSMatthew Dillon int i; 2131645cafeSMatthew Dillon 2141645cafeSMatthew Dillon printf("Scanning distfiles... "); 2151645cafeSMatthew Dillon fflush(stdout); 2161645cafeSMatthew Dillon count = 0; 2171645cafeSMatthew Dillon list = NULL; 2181645cafeSMatthew Dillon list_tail = &list; 2191645cafeSMatthew Dillon scanit(DistFilesPath, NULL, &count, &list_tail); 2201645cafeSMatthew Dillon printf("Checking %d distfiles\n", count); 2211645cafeSMatthew Dillon fflush(stdout); 2221645cafeSMatthew Dillon 2231645cafeSMatthew Dillon ary = calloc(count, sizeof(pinfo_t *)); 2241645cafeSMatthew Dillon for (i = 0; i < count; ++i) { 2251645cafeSMatthew Dillon ary[i] = list; 2261645cafeSMatthew Dillon list = list->next; 2271645cafeSMatthew Dillon } 2281645cafeSMatthew Dillon ddassert(list == NULL); 2291645cafeSMatthew Dillon qsort(ary, count, sizeof(pinfo_t *), pinfocmp); 2301645cafeSMatthew Dillon 2311645cafeSMatthew Dillon for (; pkgs; pkgs = pkgs->bnext) { 2321645cafeSMatthew Dillon if (pkgs->distfiles == NULL || pkgs->distfiles[0] == 0) 2331645cafeSMatthew Dillon continue; 2341645cafeSMatthew Dillon ddprintf(0, "distfiles %s\n", pkgs->distfiles); 2351645cafeSMatthew Dillon dstr = strtok(pkgs->distfiles, " \t"); 2361645cafeSMatthew Dillon while (dstr) { 2371645cafeSMatthew Dillon for (;;) { 2384ea2ee4dSMatthew Dillon if (pkgs->distsubdir) { 2391645cafeSMatthew Dillon asprintf(&buf, "%s/%s", 2401645cafeSMatthew Dillon pkgs->distsubdir, dstr); 2411645cafeSMatthew Dillon item = pinfofind(ary, count, buf); 2421645cafeSMatthew Dillon ddprintf(0, "TEST %s %p\n", buf, item); 2431645cafeSMatthew Dillon free(buf); 2441645cafeSMatthew Dillon buf = NULL; 2451645cafeSMatthew Dillon } else { 2461645cafeSMatthew Dillon item = pinfofind(ary, count, dstr); 2471645cafeSMatthew Dillon ddprintf(0, "TEST %s %p\n", dstr, item); 2481645cafeSMatthew Dillon } 2491645cafeSMatthew Dillon if (item) { 2501645cafeSMatthew Dillon item->foundit = 1; 2511645cafeSMatthew Dillon break; 2521645cafeSMatthew Dillon } 2531645cafeSMatthew Dillon if (strrchr(dstr, ':') == NULL) 2541645cafeSMatthew Dillon break; 2551645cafeSMatthew Dillon *strrchr(dstr, ':') = 0; 2561645cafeSMatthew Dillon } 2571645cafeSMatthew Dillon dstr = strtok(NULL, " \t"); 2581645cafeSMatthew Dillon } 2591645cafeSMatthew Dillon } 2601645cafeSMatthew Dillon 2611645cafeSMatthew Dillon delcount = 0; 2621645cafeSMatthew Dillon for (i = 0; i < count; ++i) { 2631645cafeSMatthew Dillon item = ary[i]; 2641645cafeSMatthew Dillon if (item->foundit == 0) { 2651645cafeSMatthew Dillon ++delcount; 2661645cafeSMatthew Dillon } 2671645cafeSMatthew Dillon } 2681645cafeSMatthew Dillon if (askyn("Delete %d of %d items? ", delcount, count)) { 2691645cafeSMatthew Dillon printf("Deleting %d/%d obsolete source distfiles\n", 2701645cafeSMatthew Dillon delcount, count); 2711645cafeSMatthew Dillon for (i = 0; i < count; ++i) { 2721645cafeSMatthew Dillon item = ary[i]; 2731645cafeSMatthew Dillon if (item->foundit == 0) { 2741645cafeSMatthew Dillon asprintf(&buf, "%s/%s", 2751645cafeSMatthew Dillon DistFilesPath, item->spath); 2761645cafeSMatthew Dillon if (remove(buf) < 0) 2771645cafeSMatthew Dillon printf("Cannot delete %s\n", buf); 2781645cafeSMatthew Dillon free(buf); 2791645cafeSMatthew Dillon } 2801645cafeSMatthew Dillon } 2811645cafeSMatthew Dillon } 2821645cafeSMatthew Dillon 2831645cafeSMatthew Dillon 2841645cafeSMatthew Dillon free(ary); 2858e25f19bSMatthew Dillon } 2868e25f19bSMatthew Dillon 2878e25f19bSMatthew Dillon void 288f4094b20SMatthew Dillon RemovePackages(pkg_t *list) 2898e25f19bSMatthew Dillon { 290f4094b20SMatthew Dillon pkg_t *scan; 291f4094b20SMatthew Dillon char *path; 292f4094b20SMatthew Dillon 293f4094b20SMatthew Dillon for (scan = list; scan; scan = scan->bnext) { 294f4094b20SMatthew Dillon if ((scan->flags & PKGF_MANUALSEL) == 0) 295f4094b20SMatthew Dillon continue; 296f4094b20SMatthew Dillon if (scan->pkgfile) { 297f4094b20SMatthew Dillon scan->flags &= ~PKGF_PACKAGED; 298f4094b20SMatthew Dillon scan->pkgfile_size = 0; 299f4094b20SMatthew Dillon asprintf(&path, "%s/%s", RepositoryPath, scan->pkgfile); 300f4094b20SMatthew Dillon if (remove(path) == 0) 301f4094b20SMatthew Dillon printf("Removed: %s\n", path); 302f4094b20SMatthew Dillon free(path); 303f4094b20SMatthew Dillon } 304f4094b20SMatthew Dillon if (scan->pkgfile == NULL || 305f4094b20SMatthew Dillon (scan->flags & (PKGF_DUMMY | PKGF_META))) { 306f4094b20SMatthew Dillon removePackagesMetaRecurse(scan); 307f4094b20SMatthew Dillon } 308f4094b20SMatthew Dillon } 309f4094b20SMatthew Dillon } 310f4094b20SMatthew Dillon 311f4094b20SMatthew Dillon static void 312f4094b20SMatthew Dillon removePackagesMetaRecurse(pkg_t *pkg) 313f4094b20SMatthew Dillon { 314f4094b20SMatthew Dillon pkglink_t *link; 315f4094b20SMatthew Dillon pkg_t *scan; 316f4094b20SMatthew Dillon char *path; 317f4094b20SMatthew Dillon 318f4094b20SMatthew Dillon PKGLIST_FOREACH(link, &pkg->idepon_list) { 319f4094b20SMatthew Dillon scan = link->pkg; 320f4094b20SMatthew Dillon if (scan == NULL) 321f4094b20SMatthew Dillon continue; 322f4094b20SMatthew Dillon if (scan->pkgfile == NULL || 323f4094b20SMatthew Dillon (scan->flags & (PKGF_DUMMY | PKGF_META))) { 324f4094b20SMatthew Dillon removePackagesMetaRecurse(scan); 325f4094b20SMatthew Dillon continue; 326f4094b20SMatthew Dillon } 327f4094b20SMatthew Dillon scan->flags &= ~PKGF_PACKAGED; 328f4094b20SMatthew Dillon scan->pkgfile_size = 0; 329f4094b20SMatthew Dillon 330f4094b20SMatthew Dillon asprintf(&path, "%s/%s", RepositoryPath, scan->pkgfile); 331f4094b20SMatthew Dillon if (remove(path) == 0) 332f4094b20SMatthew Dillon printf("Removed: %s\n", path); 333f4094b20SMatthew Dillon free(path); 334f4094b20SMatthew Dillon } 3358e25f19bSMatthew Dillon } 3361645cafeSMatthew Dillon 3371645cafeSMatthew Dillon static int 3381645cafeSMatthew Dillon pinfocmp(const void *s1, const void *s2) 3391645cafeSMatthew Dillon { 3401645cafeSMatthew Dillon const pinfo_t *item1 = *(const pinfo_t *const*)s1; 3411645cafeSMatthew Dillon const pinfo_t *item2 = *(const pinfo_t *const*)s2; 3421645cafeSMatthew Dillon 3431645cafeSMatthew Dillon return (strcmp(item1->spath, item2->spath)); 3441645cafeSMatthew Dillon } 3451645cafeSMatthew Dillon 3461645cafeSMatthew Dillon pinfo_t * 3471645cafeSMatthew Dillon pinfofind(pinfo_t **ary, int count, char *spath) 3481645cafeSMatthew Dillon { 3491645cafeSMatthew Dillon pinfo_t *item; 3501645cafeSMatthew Dillon int res; 3511645cafeSMatthew Dillon int b; 3521645cafeSMatthew Dillon int e; 3531645cafeSMatthew Dillon int m; 3541645cafeSMatthew Dillon 3551645cafeSMatthew Dillon b = 0; 3561645cafeSMatthew Dillon e = count; 3571645cafeSMatthew Dillon while (b != e) { 3581645cafeSMatthew Dillon m = b + (e - b) / 2; 3591645cafeSMatthew Dillon item = ary[m]; 3601645cafeSMatthew Dillon res = strcmp(spath, item->spath); 3611645cafeSMatthew Dillon if (res == 0) 3621645cafeSMatthew Dillon return item; 3631645cafeSMatthew Dillon if (res < 0) { 3641645cafeSMatthew Dillon e = m; 3651645cafeSMatthew Dillon } else { 3661645cafeSMatthew Dillon b = m + 1; 3671645cafeSMatthew Dillon } 3681645cafeSMatthew Dillon } 3691645cafeSMatthew Dillon return NULL; 3701645cafeSMatthew Dillon } 3711645cafeSMatthew Dillon 3721645cafeSMatthew Dillon void 3731645cafeSMatthew Dillon scanit(const char *path, const char *subpath, 3741645cafeSMatthew Dillon int *countp, pinfo_t ***list_tailp) 3751645cafeSMatthew Dillon { 3761645cafeSMatthew Dillon struct dirent *den; 3771645cafeSMatthew Dillon pinfo_t *item; 3781645cafeSMatthew Dillon char *npath; 3791645cafeSMatthew Dillon char *spath; 3801645cafeSMatthew Dillon DIR *dir; 3811645cafeSMatthew Dillon struct stat st; 3821645cafeSMatthew Dillon 3831645cafeSMatthew Dillon if ((dir = opendir(path)) != NULL) { 3841645cafeSMatthew Dillon while ((den = readdir(dir)) != NULL) { 3851645cafeSMatthew Dillon if (den->d_namlen == 1 && den->d_name[0] == '.') 3861645cafeSMatthew Dillon continue; 3871645cafeSMatthew Dillon if (den->d_namlen == 2 && den->d_name[0] == '.' && 3881645cafeSMatthew Dillon den->d_name[1] == '.') 3891645cafeSMatthew Dillon continue; 3901645cafeSMatthew Dillon asprintf(&npath, "%s/%s", path, den->d_name); 3911645cafeSMatthew Dillon if (lstat(npath, &st) < 0) { 3921645cafeSMatthew Dillon free(npath); 3931645cafeSMatthew Dillon continue; 3941645cafeSMatthew Dillon } 3951645cafeSMatthew Dillon if (S_ISDIR(st.st_mode)) { 3961645cafeSMatthew Dillon if (subpath) { 3971645cafeSMatthew Dillon asprintf(&spath, "%s/%s", 3981645cafeSMatthew Dillon subpath, den->d_name); 3991645cafeSMatthew Dillon scanit(npath, spath, 4001645cafeSMatthew Dillon countp, list_tailp); 4011645cafeSMatthew Dillon free(spath); 4021645cafeSMatthew Dillon } else { 4031645cafeSMatthew Dillon scanit(npath, den->d_name, 4041645cafeSMatthew Dillon countp, list_tailp); 4051645cafeSMatthew Dillon } 4061645cafeSMatthew Dillon } else if (S_ISREG(st.st_mode)) { 4071645cafeSMatthew Dillon item = calloc(1, sizeof(*item)); 4081645cafeSMatthew Dillon if (subpath) { 4091645cafeSMatthew Dillon asprintf(&item->spath, "%s/%s", 4101645cafeSMatthew Dillon subpath, den->d_name); 4111645cafeSMatthew Dillon } else { 4121645cafeSMatthew Dillon item->spath = strdup(den->d_name); 4131645cafeSMatthew Dillon } 4141645cafeSMatthew Dillon **list_tailp = item; 4151645cafeSMatthew Dillon *list_tailp = &item->next; 4161645cafeSMatthew Dillon ++*countp; 4171645cafeSMatthew Dillon ddprintf(0, "scan %s\n", item->spath); 4181645cafeSMatthew Dillon } 4196a3a20b1SMatthew Dillon free(npath); 4201645cafeSMatthew Dillon } 4211645cafeSMatthew Dillon closedir(dir); 4221645cafeSMatthew Dillon } 4231645cafeSMatthew Dillon } 424f7f25838SMatthew Dillon 425f7f25838SMatthew Dillon /* 426f7f25838SMatthew Dillon * This removes any .new files left over in the repo. These can wind 427f7f25838SMatthew Dillon * being left around when dsynth is killed. 428f7f25838SMatthew Dillon */ 429f7f25838SMatthew Dillon static void 430f7f25838SMatthew Dillon scandeletenew(const char *path) 431f7f25838SMatthew Dillon { 432f7f25838SMatthew Dillon struct dirent *den; 433f7f25838SMatthew Dillon const char *ptr; 434f7f25838SMatthew Dillon DIR *dir; 435f7f25838SMatthew Dillon char *buf; 436f7f25838SMatthew Dillon 437f7f25838SMatthew Dillon if ((dir = opendir(path)) == NULL) 438f7f25838SMatthew Dillon dfatal_errno("Cannot scan directory %s", path); 439f7f25838SMatthew Dillon while ((den = readdir(dir)) != NULL) { 440f7f25838SMatthew Dillon if ((ptr = strrchr(den->d_name, '.')) != NULL && 441f7f25838SMatthew Dillon strcmp(ptr, ".new") == 0) { 442f7f25838SMatthew Dillon asprintf(&buf, "%s/%s", path, den->d_name); 443f7f25838SMatthew Dillon if (remove(buf) < 0) 444f7f25838SMatthew Dillon dfatal_errno("remove: Garbage %s\n", buf); 445f7f25838SMatthew Dillon printf("Deleted Garbage %s\n", buf); 446f7f25838SMatthew Dillon free(buf); 447f7f25838SMatthew Dillon } 448f7f25838SMatthew Dillon } 449f7f25838SMatthew Dillon closedir(dir); 450f7f25838SMatthew Dillon } 451066daf2aSMatthew Dillon 452066daf2aSMatthew Dillon static void 453066daf2aSMatthew Dillon rebuildTerminateSignal(int signo __unused) 454066daf2aSMatthew Dillon { 455066daf2aSMatthew Dillon if (RebuildRemovePath) 456066daf2aSMatthew Dillon remove(RebuildRemovePath); 457066daf2aSMatthew Dillon exit(1); 458066daf2aSMatthew Dillon 459066daf2aSMatthew Dillon } 460