18e25f19bSMatthew Dillon /* 2*6ec1f604SMatthew Dillon * Copyright (c) 2019-2020 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" 38*6ec1f604SMatthew Dillon #include <openssl/md5.h> 398e25f19bSMatthew Dillon 401645cafeSMatthew Dillon typedef struct pinfo { 411645cafeSMatthew Dillon struct pinfo *next; 421645cafeSMatthew Dillon char *spath; 431645cafeSMatthew Dillon int foundit; 44*6ec1f604SMatthew Dillon int inlocks; 451645cafeSMatthew Dillon } pinfo_t; 461645cafeSMatthew Dillon 47f4094b20SMatthew Dillon static void removePackagesMetaRecurse(pkg_t *pkg); 481645cafeSMatthew Dillon static int pinfocmp(const void *s1, const void *s2); 491645cafeSMatthew Dillon static void scanit(const char *path, const char *subpath, 50*6ec1f604SMatthew Dillon int *countp, pinfo_t ***list_tailp, 51*6ec1f604SMatthew Dillon int inlocks); 521645cafeSMatthew Dillon pinfo_t *pinfofind(pinfo_t **ary, int count, char *spath); 53066daf2aSMatthew Dillon static void childRebuildRepo(bulk_t *bulk); 54f7f25838SMatthew Dillon static void scandeletenew(const char *path); 551645cafeSMatthew Dillon 56066daf2aSMatthew Dillon static void rebuildTerminateSignal(int signo); 57*6ec1f604SMatthew Dillon static char *md5lkfile(char *rpath, int which); 58*6ec1f604SMatthew Dillon static int lkdircount(char *buf); 59066daf2aSMatthew Dillon 60066daf2aSMatthew Dillon static char *RebuildRemovePath; 61066daf2aSMatthew Dillon 628e25f19bSMatthew Dillon void 631645cafeSMatthew Dillon DoRebuildRepo(int ask) 648e25f19bSMatthew Dillon { 65066daf2aSMatthew Dillon bulk_t *bulk; 66066daf2aSMatthew Dillon FILE *fp; 67066daf2aSMatthew Dillon int fd; 68066daf2aSMatthew Dillon char tpath[256]; 6968dc2eeaSMatthew Dillon const char *sufx; 701645cafeSMatthew Dillon 711645cafeSMatthew Dillon if (ask) { 721645cafeSMatthew Dillon if (askyn("Rebuild the repository? ") == 0) 731645cafeSMatthew Dillon return; 741645cafeSMatthew Dillon } 75f7f25838SMatthew Dillon 76f7f25838SMatthew Dillon /* 77f7f25838SMatthew Dillon * Scan the repository for temporary .new files and delete them. 78f7f25838SMatthew Dillon */ 79f7f25838SMatthew Dillon scandeletenew(RepositoryPath); 80f7f25838SMatthew Dillon 81f7f25838SMatthew Dillon /* 82066daf2aSMatthew Dillon * Generate temporary file 83f7f25838SMatthew Dillon */ 84066daf2aSMatthew Dillon snprintf(tpath, sizeof(tpath), "/tmp/meta.XXXXXXXX.conf"); 85066daf2aSMatthew Dillon 86066daf2aSMatthew Dillon signal(SIGTERM, rebuildTerminateSignal); 87066daf2aSMatthew Dillon signal(SIGINT, rebuildTerminateSignal); 88066daf2aSMatthew Dillon signal(SIGHUP, rebuildTerminateSignal); 89066daf2aSMatthew Dillon 90066daf2aSMatthew Dillon RebuildRemovePath = tpath; 91066daf2aSMatthew Dillon 92483dbac9SMatthew Dillon sufx = UsePkgSufx; 93066daf2aSMatthew Dillon fd = mkostemps(tpath, 5, 0); 94066daf2aSMatthew Dillon if (fd < 0) 95066daf2aSMatthew Dillon dfatal_errno("Cannot create %s", tpath); 96066daf2aSMatthew Dillon fp = fdopen(fd, "w"); 97066daf2aSMatthew Dillon fprintf(fp, "version = 1;\n"); 9868dc2eeaSMatthew Dillon fprintf(fp, "packing_format = \"%s\";\n", sufx + 1); 99066daf2aSMatthew Dillon fclose(fp); 100066daf2aSMatthew Dillon 101066daf2aSMatthew Dillon /* 102066daf2aSMatthew Dillon * Run the operation under our bulk infrastructure to 103066daf2aSMatthew Dillon * get the correct environment. 104066daf2aSMatthew Dillon */ 105066daf2aSMatthew Dillon initbulk(childRebuildRepo, 1); 106066daf2aSMatthew Dillon queuebulk(tpath, NULL, NULL, NULL); 107066daf2aSMatthew Dillon bulk = getbulk(); 108066daf2aSMatthew Dillon 109066daf2aSMatthew Dillon if (bulk->r1) 1101645cafeSMatthew Dillon printf("Rebuild succeeded\n"); 111066daf2aSMatthew Dillon else 112066daf2aSMatthew Dillon printf("Rebuild failed\n"); 113066daf2aSMatthew Dillon donebulk(); 114066daf2aSMatthew Dillon 115066daf2aSMatthew Dillon remove(tpath); 116066daf2aSMatthew Dillon } 117066daf2aSMatthew Dillon 118066daf2aSMatthew Dillon static void 1197f2422cfSMatthew Dillon repackage(const char *basepath, const char *basefile, 1207f2422cfSMatthew Dillon const char *decomp_suffix, const char *comp_suffix, 1217f2422cfSMatthew Dillon const char *decomp, const char *comp); 122cda252a4SMatthew Dillon 123cda252a4SMatthew Dillon static void 124066daf2aSMatthew Dillon childRebuildRepo(bulk_t *bulk) 125066daf2aSMatthew Dillon { 126066daf2aSMatthew Dillon FILE *fp; 127066daf2aSMatthew Dillon char *ptr; 128066daf2aSMatthew Dillon size_t len; 129066daf2aSMatthew Dillon pid_t pid; 130066daf2aSMatthew Dillon const char *cav[MAXCAC]; 131066daf2aSMatthew Dillon int cac; 1327f2422cfSMatthew Dillon int repackage_mode = 0; 133066daf2aSMatthew Dillon 134066daf2aSMatthew Dillon cac = 0; 135066daf2aSMatthew Dillon cav[cac++] = PKG_BINARY; 136066daf2aSMatthew Dillon cav[cac++] = "repo"; 137066daf2aSMatthew Dillon cav[cac++] = "-m"; 138066daf2aSMatthew Dillon cav[cac++] = bulk->s1; 139066daf2aSMatthew Dillon cav[cac++] = "-o"; 140066daf2aSMatthew Dillon cav[cac++] = PackagesPath; 141cda252a4SMatthew Dillon 142cda252a4SMatthew Dillon /* 143cda252a4SMatthew Dillon * The yaml needs to generate paths relative to PackagePath 144cda252a4SMatthew Dillon */ 145cda252a4SMatthew Dillon if (strncmp(PackagesPath, RepositoryPath, strlen(PackagesPath)) == 0) 146cda252a4SMatthew Dillon cav[cac++] = PackagesPath; 147cda252a4SMatthew Dillon else 148066daf2aSMatthew Dillon cav[cac++] = RepositoryPath; 149066daf2aSMatthew Dillon 150cda252a4SMatthew Dillon printf("pkg repo -m %s -o %s %s\n", bulk->s1, cav[cac-2], cav[cac-1]); 151066daf2aSMatthew Dillon 15268dc2eeaSMatthew Dillon fp = dexec_open(cav, cac, &pid, NULL, 1, 0); 153066daf2aSMatthew Dillon while ((ptr = fgetln(fp, &len)) != NULL) 154066daf2aSMatthew Dillon fwrite(ptr, 1, len, stdout); 15596cc8f92SMatthew Dillon if (dexec_close(fp, pid) == 0) 156066daf2aSMatthew Dillon bulk->r1 = strdup(""); 15796cc8f92SMatthew Dillon 15896cc8f92SMatthew Dillon /* 15996cc8f92SMatthew Dillon * Check package version. Pkg version 1.12 and later generates 16096cc8f92SMatthew Dillon * the proper repo compression format. Prior to that version 16196cc8f92SMatthew Dillon * the repo directive always generated .txz files. 16296cc8f92SMatthew Dillon */ 16396cc8f92SMatthew Dillon cac = 0; 16496cc8f92SMatthew Dillon cav[cac++] = PKG_BINARY; 16596cc8f92SMatthew Dillon cav[cac++] = "-v"; 16696cc8f92SMatthew Dillon fp = dexec_open(cav, cac, &pid, NULL, 1, 0); 16796cc8f92SMatthew Dillon if ((ptr = fgetln(fp, &len)) != NULL && len > 0) { 16896cc8f92SMatthew Dillon int v1; 16996cc8f92SMatthew Dillon int v2; 17096cc8f92SMatthew Dillon 17196cc8f92SMatthew Dillon ptr[len-1] = 0; 17296cc8f92SMatthew Dillon if (sscanf(ptr, "%d.%d", &v1, &v2) == 2) { 17396cc8f92SMatthew Dillon if (v1 > 1 || (v1 == 1 && v2 >= 12)) 1747f2422cfSMatthew Dillon repackage_mode = 1; 1751645cafeSMatthew Dillon } 17696cc8f92SMatthew Dillon } 17796cc8f92SMatthew Dillon dexec_close(fp, pid); 178cda252a4SMatthew Dillon 179cda252a4SMatthew Dillon /* 180cda252a4SMatthew Dillon * Repackage the .txz files created by pkg repo if necessary 181cda252a4SMatthew Dillon */ 1827f2422cfSMatthew Dillon if (repackage_mode == 0 && strcmp(UsePkgSufx, ".txz") != 0) { 183cda252a4SMatthew Dillon const char *comp; 184cda252a4SMatthew Dillon const char *decomp; 185cda252a4SMatthew Dillon 1867f2422cfSMatthew Dillon printf("pkg repo - recompressing digests and packagesite\n"); 18796cc8f92SMatthew Dillon 188483dbac9SMatthew Dillon if (strcmp(UsePkgSufx, ".tar") == 0) { 189cda252a4SMatthew Dillon decomp = "unxz"; 190cda252a4SMatthew Dillon comp = "cat"; 191483dbac9SMatthew Dillon } else if (strcmp(UsePkgSufx, ".tgz") == 0) { 192cda252a4SMatthew Dillon decomp = "unxz"; 193cda252a4SMatthew Dillon comp = "gzip"; 194483dbac9SMatthew Dillon } else if (strcmp(UsePkgSufx, ".tbz") == 0) { 195cda252a4SMatthew Dillon decomp = "unxz"; 196cda252a4SMatthew Dillon comp = "bzip"; 197cda252a4SMatthew Dillon } else { 1987f2422cfSMatthew Dillon dfatal("recompressing as %s not supported", 1997f2422cfSMatthew Dillon UsePkgSufx); 200cda252a4SMatthew Dillon decomp = "unxz"; 201cda252a4SMatthew Dillon comp = "cat"; 202cda252a4SMatthew Dillon } 2037f2422cfSMatthew Dillon repackage(PackagesPath, "digests", 2047f2422cfSMatthew Dillon ".txz", UsePkgSufx, 2057f2422cfSMatthew Dillon decomp, comp); 2067f2422cfSMatthew Dillon repackage(PackagesPath, "packagesite", 2077f2422cfSMatthew Dillon ".txz", UsePkgSufx, 2087f2422cfSMatthew Dillon decomp, comp); 2097f2422cfSMatthew Dillon } else if (repackage_mode == 1 && strcmp(UsePkgSufx, ".txz") != 0) { 2107f2422cfSMatthew Dillon const char *comp; 2117f2422cfSMatthew Dillon const char *decomp; 2127f2422cfSMatthew Dillon 2137f2422cfSMatthew Dillon printf("pkg repo - recompressing meta\n"); 2147f2422cfSMatthew Dillon 2157f2422cfSMatthew Dillon if (strcmp(UsePkgSufx, ".tar") == 0) { 2167f2422cfSMatthew Dillon decomp = "cat"; 2177f2422cfSMatthew Dillon comp = "xz"; 2187f2422cfSMatthew Dillon } else if (strcmp(UsePkgSufx, ".tgz") == 0) { 2197f2422cfSMatthew Dillon decomp = "gunzip"; 2207f2422cfSMatthew Dillon comp = "xz"; 2217f2422cfSMatthew Dillon } else if (strcmp(UsePkgSufx, ".tbz") == 0) { 2227f2422cfSMatthew Dillon decomp = "bunzip2"; 2237f2422cfSMatthew Dillon comp = "xz"; 2247f2422cfSMatthew Dillon } else { 2257f2422cfSMatthew Dillon dfatal("recompressing from %s not supported", 2267f2422cfSMatthew Dillon UsePkgSufx); 2277f2422cfSMatthew Dillon decomp = "cat"; 2287f2422cfSMatthew Dillon comp = "cat"; 2297f2422cfSMatthew Dillon } 2307f2422cfSMatthew Dillon repackage(PackagesPath, "meta", 2317f2422cfSMatthew Dillon UsePkgSufx, ".txz", 2327f2422cfSMatthew Dillon decomp, comp); 233cda252a4SMatthew Dillon } 234cda252a4SMatthew Dillon } 235cda252a4SMatthew Dillon 236cda252a4SMatthew Dillon static 237cda252a4SMatthew Dillon void 2387f2422cfSMatthew Dillon repackage(const char *basepath, const char *basefile, 2397f2422cfSMatthew Dillon const char *decomp_suffix, const char *comp_suffix, 2407f2422cfSMatthew Dillon const char *decomp, const char *comp) 241cda252a4SMatthew Dillon { 242cda252a4SMatthew Dillon char *buf; 243cda252a4SMatthew Dillon 2447f2422cfSMatthew Dillon asprintf(&buf, "%s < %s/%s%s | %s > %s/%s%s", 2457f2422cfSMatthew Dillon decomp, basepath, basefile, decomp_suffix, 2467f2422cfSMatthew Dillon comp, basepath, basefile, comp_suffix); 247cda252a4SMatthew Dillon if (system(buf) != 0) { 248cda252a4SMatthew Dillon dfatal("command failed: %s", buf); 249cda252a4SMatthew Dillon } 250cda252a4SMatthew Dillon free(buf); 2518e25f19bSMatthew Dillon } 2528e25f19bSMatthew Dillon 2538e25f19bSMatthew Dillon void 2548e25f19bSMatthew Dillon DoUpgradePkgs(pkg_t *pkgs __unused, int ask __unused) 2558e25f19bSMatthew Dillon { 2568e25f19bSMatthew Dillon dfatal("Not Implemented"); 2578e25f19bSMatthew Dillon } 2588e25f19bSMatthew Dillon 2598e25f19bSMatthew Dillon void 2601645cafeSMatthew Dillon PurgeDistfiles(pkg_t *pkgs) 2618e25f19bSMatthew Dillon { 2621645cafeSMatthew Dillon pinfo_t *list; 2631645cafeSMatthew Dillon pinfo_t *item; 2641645cafeSMatthew Dillon pinfo_t **list_tail; 2651645cafeSMatthew Dillon pinfo_t **ary; 2661645cafeSMatthew Dillon char *dstr; 2671645cafeSMatthew Dillon char *buf; 2681645cafeSMatthew Dillon int count; 2691645cafeSMatthew Dillon int delcount; 2701645cafeSMatthew Dillon int i; 2711645cafeSMatthew Dillon 2721645cafeSMatthew Dillon printf("Scanning distfiles... "); 2731645cafeSMatthew Dillon fflush(stdout); 2741645cafeSMatthew Dillon count = 0; 2751645cafeSMatthew Dillon list = NULL; 2761645cafeSMatthew Dillon list_tail = &list; 277*6ec1f604SMatthew Dillon scanit(DistFilesPath, NULL, &count, &list_tail, 0); 2781645cafeSMatthew Dillon printf("Checking %d distfiles\n", count); 2791645cafeSMatthew Dillon fflush(stdout); 2801645cafeSMatthew Dillon 2811645cafeSMatthew Dillon ary = calloc(count, sizeof(pinfo_t *)); 2821645cafeSMatthew Dillon for (i = 0; i < count; ++i) { 2831645cafeSMatthew Dillon ary[i] = list; 2841645cafeSMatthew Dillon list = list->next; 2851645cafeSMatthew Dillon } 2861645cafeSMatthew Dillon ddassert(list == NULL); 2871645cafeSMatthew Dillon qsort(ary, count, sizeof(pinfo_t *), pinfocmp); 2881645cafeSMatthew Dillon 2891645cafeSMatthew Dillon for (; pkgs; pkgs = pkgs->bnext) { 2901645cafeSMatthew Dillon if (pkgs->distfiles == NULL || pkgs->distfiles[0] == 0) 2911645cafeSMatthew Dillon continue; 2921645cafeSMatthew Dillon ddprintf(0, "distfiles %s\n", pkgs->distfiles); 2931645cafeSMatthew Dillon dstr = strtok(pkgs->distfiles, " \t"); 2941645cafeSMatthew Dillon while (dstr) { 2951645cafeSMatthew Dillon for (;;) { 296*6ec1f604SMatthew Dillon /* 297*6ec1f604SMatthew Dillon * Look for distfile 298*6ec1f604SMatthew Dillon */ 2994ea2ee4dSMatthew Dillon if (pkgs->distsubdir) { 3001645cafeSMatthew Dillon asprintf(&buf, "%s/%s", 3011645cafeSMatthew Dillon pkgs->distsubdir, dstr); 302*6ec1f604SMatthew Dillon } else { 303*6ec1f604SMatthew Dillon buf = dstr; 304*6ec1f604SMatthew Dillon } 3051645cafeSMatthew Dillon item = pinfofind(ary, count, buf); 306*6ec1f604SMatthew Dillon if (item) 307*6ec1f604SMatthew Dillon item->foundit = 1; 308*6ec1f604SMatthew Dillon if (item && item->inlocks == 0) { 309*6ec1f604SMatthew Dillon /* 310*6ec1f604SMatthew Dillon * Look for the lock file 311*6ec1f604SMatthew Dillon */ 312*6ec1f604SMatthew Dillon int scount; 313*6ec1f604SMatthew Dillon 314*6ec1f604SMatthew Dillon scount = lkdircount(buf); 315*6ec1f604SMatthew Dillon 316*6ec1f604SMatthew Dillon for (i = 0; i <= scount; ++i) { 317*6ec1f604SMatthew Dillon item = pinfofind(ary, count, 318*6ec1f604SMatthew Dillon md5lkfile(buf, i)); 319*6ec1f604SMatthew Dillon if (item) 320*6ec1f604SMatthew Dillon item->foundit = 1; 321*6ec1f604SMatthew Dillon } 322*6ec1f604SMatthew Dillon } 323*6ec1f604SMatthew Dillon 324*6ec1f604SMatthew Dillon /* 325*6ec1f604SMatthew Dillon * Cleanup and iterate 326*6ec1f604SMatthew Dillon */ 327*6ec1f604SMatthew Dillon if (buf != dstr) { 3281645cafeSMatthew Dillon free(buf); 3291645cafeSMatthew Dillon buf = NULL; 3301645cafeSMatthew Dillon } 3311645cafeSMatthew Dillon if (strrchr(dstr, ':') == NULL) 3321645cafeSMatthew Dillon break; 3331645cafeSMatthew Dillon *strrchr(dstr, ':') = 0; 3341645cafeSMatthew Dillon } 3351645cafeSMatthew Dillon dstr = strtok(NULL, " \t"); 3361645cafeSMatthew Dillon } 3371645cafeSMatthew Dillon } 3381645cafeSMatthew Dillon 3391645cafeSMatthew Dillon delcount = 0; 3401645cafeSMatthew Dillon for (i = 0; i < count; ++i) { 3411645cafeSMatthew Dillon item = ary[i]; 3421645cafeSMatthew Dillon if (item->foundit == 0) { 3431645cafeSMatthew Dillon ++delcount; 3441645cafeSMatthew Dillon } 3451645cafeSMatthew Dillon } 34661e823a7SMatthew Dillon if (delcount == 0) { 34761e823a7SMatthew Dillon printf("No obsolete source files out of %d found\n", count); 34861e823a7SMatthew Dillon } else if (askyn("Delete %d of %d items? ", delcount, count)) { 3491645cafeSMatthew Dillon printf("Deleting %d/%d obsolete source distfiles\n", 3501645cafeSMatthew Dillon delcount, count); 3511645cafeSMatthew Dillon for (i = 0; i < count; ++i) { 3521645cafeSMatthew Dillon item = ary[i]; 3531645cafeSMatthew Dillon if (item->foundit == 0) { 3541645cafeSMatthew Dillon asprintf(&buf, "%s/%s", 3551645cafeSMatthew Dillon DistFilesPath, item->spath); 3561645cafeSMatthew Dillon if (remove(buf) < 0) 3571645cafeSMatthew Dillon printf("Cannot delete %s\n", buf); 358*6ec1f604SMatthew Dillon else 359*6ec1f604SMatthew Dillon printf("Deleted %s\n", item->spath); 3601645cafeSMatthew Dillon free(buf); 3611645cafeSMatthew Dillon } 3621645cafeSMatthew Dillon } 3631645cafeSMatthew Dillon } 3641645cafeSMatthew Dillon 3651645cafeSMatthew Dillon 3661645cafeSMatthew Dillon free(ary); 3678e25f19bSMatthew Dillon } 3688e25f19bSMatthew Dillon 3698e25f19bSMatthew Dillon void 370f4094b20SMatthew Dillon RemovePackages(pkg_t *list) 3718e25f19bSMatthew Dillon { 372f4094b20SMatthew Dillon pkg_t *scan; 373f4094b20SMatthew Dillon char *path; 374f4094b20SMatthew Dillon 375f4094b20SMatthew Dillon for (scan = list; scan; scan = scan->bnext) { 376f4094b20SMatthew Dillon if ((scan->flags & PKGF_MANUALSEL) == 0) 377f4094b20SMatthew Dillon continue; 378f4094b20SMatthew Dillon if (scan->pkgfile) { 379f4094b20SMatthew Dillon scan->flags &= ~PKGF_PACKAGED; 380f4094b20SMatthew Dillon scan->pkgfile_size = 0; 381f4094b20SMatthew Dillon asprintf(&path, "%s/%s", RepositoryPath, scan->pkgfile); 382f4094b20SMatthew Dillon if (remove(path) == 0) 383f4094b20SMatthew Dillon printf("Removed: %s\n", path); 384f4094b20SMatthew Dillon free(path); 385f4094b20SMatthew Dillon } 386f4094b20SMatthew Dillon if (scan->pkgfile == NULL || 387f4094b20SMatthew Dillon (scan->flags & (PKGF_DUMMY | PKGF_META))) { 388f4094b20SMatthew Dillon removePackagesMetaRecurse(scan); 389f4094b20SMatthew Dillon } 390f4094b20SMatthew Dillon } 391f4094b20SMatthew Dillon } 392f4094b20SMatthew Dillon 393f4094b20SMatthew Dillon static void 394f4094b20SMatthew Dillon removePackagesMetaRecurse(pkg_t *pkg) 395f4094b20SMatthew Dillon { 396f4094b20SMatthew Dillon pkglink_t *link; 397f4094b20SMatthew Dillon pkg_t *scan; 398f4094b20SMatthew Dillon char *path; 399f4094b20SMatthew Dillon 400f4094b20SMatthew Dillon PKGLIST_FOREACH(link, &pkg->idepon_list) { 401f4094b20SMatthew Dillon scan = link->pkg; 402f4094b20SMatthew Dillon if (scan == NULL) 403f4094b20SMatthew Dillon continue; 404f4094b20SMatthew Dillon if (scan->pkgfile == NULL || 405f4094b20SMatthew Dillon (scan->flags & (PKGF_DUMMY | PKGF_META))) { 406f4094b20SMatthew Dillon removePackagesMetaRecurse(scan); 407f4094b20SMatthew Dillon continue; 408f4094b20SMatthew Dillon } 409f4094b20SMatthew Dillon scan->flags &= ~PKGF_PACKAGED; 410f4094b20SMatthew Dillon scan->pkgfile_size = 0; 411f4094b20SMatthew Dillon 412f4094b20SMatthew Dillon asprintf(&path, "%s/%s", RepositoryPath, scan->pkgfile); 413f4094b20SMatthew Dillon if (remove(path) == 0) 414f4094b20SMatthew Dillon printf("Removed: %s\n", path); 415f4094b20SMatthew Dillon free(path); 416f4094b20SMatthew Dillon } 4178e25f19bSMatthew Dillon } 4181645cafeSMatthew Dillon 4191645cafeSMatthew Dillon static int 4201645cafeSMatthew Dillon pinfocmp(const void *s1, const void *s2) 4211645cafeSMatthew Dillon { 4221645cafeSMatthew Dillon const pinfo_t *item1 = *(const pinfo_t *const*)s1; 4231645cafeSMatthew Dillon const pinfo_t *item2 = *(const pinfo_t *const*)s2; 4241645cafeSMatthew Dillon 4251645cafeSMatthew Dillon return (strcmp(item1->spath, item2->spath)); 4261645cafeSMatthew Dillon } 4271645cafeSMatthew Dillon 4281645cafeSMatthew Dillon pinfo_t * 4291645cafeSMatthew Dillon pinfofind(pinfo_t **ary, int count, char *spath) 4301645cafeSMatthew Dillon { 4311645cafeSMatthew Dillon pinfo_t *item; 4321645cafeSMatthew Dillon int res; 4331645cafeSMatthew Dillon int b; 4341645cafeSMatthew Dillon int e; 4351645cafeSMatthew Dillon int m; 4361645cafeSMatthew Dillon 4371645cafeSMatthew Dillon b = 0; 4381645cafeSMatthew Dillon e = count; 4391645cafeSMatthew Dillon while (b != e) { 4401645cafeSMatthew Dillon m = b + (e - b) / 2; 4411645cafeSMatthew Dillon item = ary[m]; 4421645cafeSMatthew Dillon res = strcmp(spath, item->spath); 4431645cafeSMatthew Dillon if (res == 0) 4441645cafeSMatthew Dillon return item; 4451645cafeSMatthew Dillon if (res < 0) { 4461645cafeSMatthew Dillon e = m; 4471645cafeSMatthew Dillon } else { 4481645cafeSMatthew Dillon b = m + 1; 4491645cafeSMatthew Dillon } 4501645cafeSMatthew Dillon } 4511645cafeSMatthew Dillon return NULL; 4521645cafeSMatthew Dillon } 4531645cafeSMatthew Dillon 4541645cafeSMatthew Dillon void 4551645cafeSMatthew Dillon scanit(const char *path, const char *subpath, 456*6ec1f604SMatthew Dillon int *countp, pinfo_t ***list_tailp, 457*6ec1f604SMatthew Dillon int inlocks) 4581645cafeSMatthew Dillon { 4591645cafeSMatthew Dillon struct dirent *den; 4601645cafeSMatthew Dillon pinfo_t *item; 4611645cafeSMatthew Dillon char *npath; 4621645cafeSMatthew Dillon char *spath; 4631645cafeSMatthew Dillon DIR *dir; 4641645cafeSMatthew Dillon struct stat st; 4651645cafeSMatthew Dillon 4661645cafeSMatthew Dillon if ((dir = opendir(path)) != NULL) { 4671645cafeSMatthew Dillon while ((den = readdir(dir)) != NULL) { 4681645cafeSMatthew Dillon if (den->d_namlen == 1 && den->d_name[0] == '.') 4691645cafeSMatthew Dillon continue; 4701645cafeSMatthew Dillon if (den->d_namlen == 2 && den->d_name[0] == '.' && 4711645cafeSMatthew Dillon den->d_name[1] == '.') 4721645cafeSMatthew Dillon continue; 4731645cafeSMatthew Dillon asprintf(&npath, "%s/%s", path, den->d_name); 4741645cafeSMatthew Dillon if (lstat(npath, &st) < 0) { 4751645cafeSMatthew Dillon free(npath); 4761645cafeSMatthew Dillon continue; 4771645cafeSMatthew Dillon } 4781645cafeSMatthew Dillon if (S_ISDIR(st.st_mode)) { 479*6ec1f604SMatthew Dillon int sublocks; 480*6ec1f604SMatthew Dillon 481*6ec1f604SMatthew Dillon sublocks = 482*6ec1f604SMatthew Dillon (strcmp(den->d_name, ".locks") == 0); 483*6ec1f604SMatthew Dillon 4841645cafeSMatthew Dillon if (subpath) { 4851645cafeSMatthew Dillon asprintf(&spath, "%s/%s", 4861645cafeSMatthew Dillon subpath, den->d_name); 487*6ec1f604SMatthew Dillon scanit(npath, spath, countp, 488*6ec1f604SMatthew Dillon list_tailp, sublocks); 4891645cafeSMatthew Dillon free(spath); 4901645cafeSMatthew Dillon } else { 491*6ec1f604SMatthew Dillon scanit(npath, den->d_name, countp, 492*6ec1f604SMatthew Dillon list_tailp, sublocks); 4931645cafeSMatthew Dillon } 4941645cafeSMatthew Dillon } else if (S_ISREG(st.st_mode)) { 4951645cafeSMatthew Dillon item = calloc(1, sizeof(*item)); 4961645cafeSMatthew Dillon if (subpath) { 4971645cafeSMatthew Dillon asprintf(&item->spath, "%s/%s", 4981645cafeSMatthew Dillon subpath, den->d_name); 4991645cafeSMatthew Dillon } else { 5001645cafeSMatthew Dillon item->spath = strdup(den->d_name); 5011645cafeSMatthew Dillon } 502*6ec1f604SMatthew Dillon item->inlocks = inlocks; 503*6ec1f604SMatthew Dillon 5041645cafeSMatthew Dillon **list_tailp = item; 5051645cafeSMatthew Dillon *list_tailp = &item->next; 5061645cafeSMatthew Dillon ++*countp; 5071645cafeSMatthew Dillon ddprintf(0, "scan %s\n", item->spath); 5081645cafeSMatthew Dillon } 5096a3a20b1SMatthew Dillon free(npath); 5101645cafeSMatthew Dillon } 5111645cafeSMatthew Dillon closedir(dir); 5121645cafeSMatthew Dillon } 5131645cafeSMatthew Dillon } 514f7f25838SMatthew Dillon 515f7f25838SMatthew Dillon /* 516f7f25838SMatthew Dillon * This removes any .new files left over in the repo. These can wind 517f7f25838SMatthew Dillon * being left around when dsynth is killed. 518f7f25838SMatthew Dillon */ 519f7f25838SMatthew Dillon static void 520f7f25838SMatthew Dillon scandeletenew(const char *path) 521f7f25838SMatthew Dillon { 522f7f25838SMatthew Dillon struct dirent *den; 523f7f25838SMatthew Dillon const char *ptr; 524f7f25838SMatthew Dillon DIR *dir; 525f7f25838SMatthew Dillon char *buf; 526f7f25838SMatthew Dillon 527f7f25838SMatthew Dillon if ((dir = opendir(path)) == NULL) 528f7f25838SMatthew Dillon dfatal_errno("Cannot scan directory %s", path); 529f7f25838SMatthew Dillon while ((den = readdir(dir)) != NULL) { 530f7f25838SMatthew Dillon if ((ptr = strrchr(den->d_name, '.')) != NULL && 531f7f25838SMatthew Dillon strcmp(ptr, ".new") == 0) { 532f7f25838SMatthew Dillon asprintf(&buf, "%s/%s", path, den->d_name); 533f7f25838SMatthew Dillon if (remove(buf) < 0) 534f7f25838SMatthew Dillon dfatal_errno("remove: Garbage %s\n", buf); 535f7f25838SMatthew Dillon printf("Deleted Garbage %s\n", buf); 536f7f25838SMatthew Dillon free(buf); 537f7f25838SMatthew Dillon } 538f7f25838SMatthew Dillon } 539f7f25838SMatthew Dillon closedir(dir); 540f7f25838SMatthew Dillon } 541066daf2aSMatthew Dillon 542066daf2aSMatthew Dillon static void 543066daf2aSMatthew Dillon rebuildTerminateSignal(int signo __unused) 544066daf2aSMatthew Dillon { 545066daf2aSMatthew Dillon if (RebuildRemovePath) 546066daf2aSMatthew Dillon remove(RebuildRemovePath); 547066daf2aSMatthew Dillon exit(1); 548066daf2aSMatthew Dillon 549066daf2aSMatthew Dillon } 550*6ec1f604SMatthew Dillon 551*6ec1f604SMatthew Dillon /* 552*6ec1f604SMatthew Dillon * There will be a .locks sub-directory in /usr/distfiles and also 553*6ec1f604SMatthew Dillon * in each sub-directory underneath it containing the MD5 sums for 554*6ec1f604SMatthew Dillon * the files in that subdirectory. 555*6ec1f604SMatthew Dillon * 556*6ec1f604SMatthew Dillon * This is a bit of a mess. Sometimes the .locks/ for a subdirectory 557*6ec1f604SMatthew Dillon * are in parentdir/.locks and not parentdir/subdir/.locks. The invocation 558*6ec1f604SMatthew Dillon * of do-fetch can be a bit messy so we look for a .locks subdir everywhere. 559*6ec1f604SMatthew Dillon * 560*6ec1f604SMatthew Dillon * The /usr/dports/Mk/Scripts/do-fetch.sh script uses 'echo blah | md5', 561*6ec1f604SMatthew Dillon * so we have to add a newline to the buffer being md5'd. 562*6ec1f604SMatthew Dillon * 563*6ec1f604SMatthew Dillon * The pass-in rpath is relative to the distfiles base. 564*6ec1f604SMatthew Dillon */ 565*6ec1f604SMatthew Dillon static char * 566*6ec1f604SMatthew Dillon md5lkfile(char *rpath, int which_slash) 567*6ec1f604SMatthew Dillon { 568*6ec1f604SMatthew Dillon static char mstr[128]; 569*6ec1f604SMatthew Dillon static char lkfile[128]; 570*6ec1f604SMatthew Dillon uint8_t digest[MD5_DIGEST_LENGTH]; 571*6ec1f604SMatthew Dillon int bplen; 572*6ec1f604SMatthew Dillon int i; 573*6ec1f604SMatthew Dillon 574*6ec1f604SMatthew Dillon bplen = 0; 575*6ec1f604SMatthew Dillon for (i = 0; i < which_slash; ++i) { 576*6ec1f604SMatthew Dillon while (rpath[bplen] && rpath[bplen] != '/') 577*6ec1f604SMatthew Dillon ++bplen; 578*6ec1f604SMatthew Dillon if (rpath[bplen]) 579*6ec1f604SMatthew Dillon ++bplen; 580*6ec1f604SMatthew Dillon } 581*6ec1f604SMatthew Dillon snprintf(mstr, sizeof(mstr), "%s\n", rpath + bplen); 582*6ec1f604SMatthew Dillon MD5(mstr, strlen(mstr), digest); 583*6ec1f604SMatthew Dillon 584*6ec1f604SMatthew Dillon snprintf(lkfile, sizeof(lkfile), 585*6ec1f604SMatthew Dillon "%*.*s.locks/" 586*6ec1f604SMatthew Dillon "%02x%02x%02x%02x%02x%02x%02x%02x" 587*6ec1f604SMatthew Dillon "%02x%02x%02x%02x%02x%02x%02x%02x" 588*6ec1f604SMatthew Dillon ".lk", 589*6ec1f604SMatthew Dillon bplen, bplen, rpath, 590*6ec1f604SMatthew Dillon digest[0], digest[1], digest[2], digest[3], 591*6ec1f604SMatthew Dillon digest[4], digest[5], digest[6], digest[7], 592*6ec1f604SMatthew Dillon digest[8], digest[9], digest[10], digest[11], 593*6ec1f604SMatthew Dillon digest[12], digest[13], digest[14], digest[15]); 594*6ec1f604SMatthew Dillon 595*6ec1f604SMatthew Dillon return lkfile; 596*6ec1f604SMatthew Dillon } 597*6ec1f604SMatthew Dillon 598*6ec1f604SMatthew Dillon static int 599*6ec1f604SMatthew Dillon lkdircount(char *buf) 600*6ec1f604SMatthew Dillon { 601*6ec1f604SMatthew Dillon int i; 602*6ec1f604SMatthew Dillon int n; 603*6ec1f604SMatthew Dillon 604*6ec1f604SMatthew Dillon n = 0; 605*6ec1f604SMatthew Dillon for (i = 0; buf[i]; ++i) { 606*6ec1f604SMatthew Dillon if (buf[i] == '/') 607*6ec1f604SMatthew Dillon ++n; 608*6ec1f604SMatthew Dillon } 609*6ec1f604SMatthew Dillon return n; 610*6ec1f604SMatthew Dillon } 611