From 504da13ea8572883eb798f2a536b02b3794eaa11 Mon Sep 17 00:00:00 2001 From: onefang Date: Fri, 8 Oct 2021 00:24:09 +1000 Subject: Add UNGITAR. --- src/sledjchisl/sledjchisl.c | 148 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 146 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/sledjchisl/sledjchisl.c b/src/sledjchisl/sledjchisl.c index b74e419..ed84ada 100644 --- a/src/sledjchisl/sledjchisl.c +++ b/src/sledjchisl/sledjchisl.c @@ -39,6 +39,12 @@ config SLEDJCHISL gitar Welcome.shini gitar "Welcome sim" Build a gitAR. + ungitar -m "Joan Smith" + ungitar Welcome + ungitar Welcome.ini + ungitar Welcome.shini + ungitar "Welcome sim" + Unpack a gitAR into separate IAR / OAR files. restart restart Welcome restart Welcome.ini @@ -162,7 +168,8 @@ typedef enum STATUS = 4, BUILD = 5, TEST = 6, - UPDATE = 7, + UNGITAR = 7, + UPDATE = 8, STOP = 9 } modes; @@ -177,8 +184,9 @@ char *modeStrings[] = "status", "build", "test", + "ungitAR", "update", - "dontDoThis" +// "dontDoThis" "stop" }; @@ -2448,6 +2456,137 @@ gitARend: free(name); } +void ungitar(simData *simd, char *sim, int count, int window, int panes, int pane, int m, int member, char *last) +{ +/* Extact all the I/OAR files from a gitAR file. + + Gotta do it the hard way, OpenSim expects the "control file" to be first file in the archive. I guess that's archive.xml, which would normally end up last. + + Gonna see errors like - + [FORMS]: Error sending request to http://127.0.0.1:9135/estate: Error: ConnectFailure (Connection refused). Request: METHOD=update_covenant&CovenantID=00000000-0000-0000-0000-000000000000&EstateID=102 + Real OARs do that to, so not my fault. + + Results in slightly larger I/OARs, as we store the directory names separately. Can't see any options to not do that. + Also, we name them .oar.tgz, to NOT hide the fact they are simple tarballs, while still tagging them as I/OARs. +*/ + char *name = xstrdup(sim); + + if (FLAG(m)) + { + if (member) + { + free(name); + name = xmprintf("%s_%s", sim, last); + } + else + { + free(name); + return; + } + } + + char type = FLAG(m) ? 'I' : 'O'; + char *dir = xmprintf("%s/temp_%c_%s", scBackup, type, name); + char bar[] = {'_', '-'}; + int b; + for (b = 0; b < 2; b++) + { + char *gar = xmprintf("%s%cgit%cAR", name, bar[b], type), *gtr = xmprintf("%s/%s.tar.xz", scBackup, gar); + + if (qfile_exist(dir)) + { + E("Cleaning up the mess!"); + if (shellMeFail("mv %s/*.%car %s", dir, tolower(type), scBackup )) E("Failed cleaning up the mess!"); + } + + // Either unpack the old gitAR, or bail. + qfile_mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP, true); + if (qfile_exist(gtr)) + { + I("Unpacking %s", gtr); + if (shellMeFail("cd %s; ionice -c3 nice -n 19 tar -xf %s", dir, gtr)) E("Failed to unpack %s!", gtr); + if ('-' == bar[b]) + { + char *t = xmprintf("%s/%s_git%cAR", dir, name, type); + // Changed from _ to -, but deal with old archives. + if (qfile_exist(t)) + { + V("Moving %s -> %s/%s", t, dir, gar); + if (shellMeFail("mv %s %s/%s", t, dir, gar)) E("Failed to move %s!", t); + } + free(t); + } + } + else + { + E("No such gitAR file - %s", gtr); + goto ungitARend; + } + + // Loop backwards through the commits, saving them as ordinary OAR files. + // The backup script should put only the file name in the commit message. + char *cmd = xmprintf("cd %s/%s; git shortlog | tail -n 2 | head -n 1 | cut -c 7- ", dir, gar); + char *prev = xmprintf(""); + + while (true) + { + char *out = qsyscmd(cmd); + + if (NULL != out) + { + out[strlen(out) - 1] = '\0'; + if ('\0' == out[0]) + { + E("Command returned nothing - %s", cmd); + free(out); + break; + } + D("gitAR archive - |%s|", out); + if ((strcmp("Initial commit", out) == 0) || (strcmp(prev, out) == 0)) + { + D("No more commits."); + free(out); + break; + } + free(prev); + prev = xmprintf("%s/%s.tgz", scBackup, out); + if (!qfile_exist(prev)) + { + I("Extracting %s", prev); + free(prev); + prev = xmprintf("%s/%s", dir, out); + // OpenSim insists on the archive.xml file being the very first in the tarball. + if (shellMeFail("cd %s/%s; ionice -c3 nice -n 19 tar -c archive.xml -f %s", dir, gar, prev)) E("Failed to tar %s!", prev); + if (shellMeFail("cd %s/%s; ionice -c3 nice -n 19 tar -r -f %s --exclude='.git*' --exclude='archive.xml' * ", + dir, gar, prev)) E("Failed to tar %s!", prev); + // Using gzip instead of something that compresses better, coz OpenSim only knows how to deal with gzipped files. + if (shellMeFail("cd %s/%s; ionice -c3 nice -n 19 gzip -S .tgz %s", dir, gar, prev)) E("Failed to gzip %s!", prev); + if (shellMeFail("cd %s/%s; mv %s.tgz ../..", dir, gar, prev)) E("Failed to rename %s!", prev); + } + else + W("NOT extracting %s, it already exists.", prev); + free(prev); + prev = out; + if (shellMeFail("cd %s/%s; git reset --hard HEAD~1 2>/dev/null >/dev/null", dir, gar)) E("Failed to git head %s/%s!", dir, gar); + } + else + { + E("Command failed - %s", cmd); + break; + } + } + +ungitARend: + if (shellMeFail("rm -fr %s", dir)) E("Failed to rm!"); + free(prev); + free(gtr); + free(gar); + } + free(dir); + free(name); +} + + // Forward declare this. my_ulonglong dbCount(char *table, char *where); void doSimsThing(simData *simd, char *sim, char *type, int count, int window, int panes, int pane) @@ -2587,6 +2726,9 @@ byTab has the short name as the key, simData as the value. break; } + case UNGITAR : // "ungitAR -m avatar name" "ungitAR sim name" + { + ungitar(simd, sim, count, window, panes, pane, FLAG(m), member, last); break; } @@ -8338,6 +8480,8 @@ void sledjchisl_main(void) currentMode = BUILD; else if (strcmp(toys.optargs[0], "test") == 0) currentMode = TEST; + else if (strcmp(toys.optargs[0], "ungitar") == 0) + currentMode = UNGITAR; else if (strcmp(toys.optargs[0], "update") == 0) currentMode = UPDATE; else if (strcmp(toys.optargs[0], "stop") == 0) -- cgit v1.1