From 9a2fb3151d779c753dff6fce07e58afc5cc63621 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 11 Jan 2023 17:26:38 -0600 Subject: [PATCH] More cleanup. --- toys/pending/git.c | 69 +++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/toys/pending/git.c b/toys/pending/git.c index f8f7a9f4..63e8dcc4 100644 --- a/toys/pending/git.c +++ b/toys/pending/git.c @@ -89,8 +89,8 @@ static void read_index(struct IndexV2 *i) { FILE *fpi; - i = malloc(sizeof(i)); - i->sha1 = malloc(20*sizeof(char)); + i = xmalloc(sizeof(i)); + i->sha1 = malloc(20); i->crc = malloc(sizeof(unsigned)); i->offset = malloc(sizeof(unsigned)); i->offset64 = malloc(sizeof(long long)); @@ -246,7 +246,7 @@ long set_object(struct IndexV2 *idx, int type, char *o, unsigned count, // TODO: Too many allocs in here 1) to concat the search string for hashing // 2) to insert into the array (can be reduce to a single malloc in fetch as // the pack header contains the number of objects in pack - char *c, *p = "", *h = (char*)xmalloc(sizeof(char)*20); //composition,prefix,hash + char *c, *p = "", *h = xmalloc(20); //composition,prefix,hash long pos = 0; printf("Alloc... "); @@ -259,10 +259,9 @@ long set_object(struct IndexV2 *idx, int type, char *o, unsigned count, case 6: printf("REF_DELTA"); break; //not expected in fetch packs as fetch packs are self-containing case 7: printf("OBJ_DELTA\n"); break; } - c = (char*)xmalloc(strlen(p)+count+2); //Robs null terminator embedding - if (c == NULL) error_exit("c malloc failed in set_object"); - memcpy(c, p, strlen(p)+1); //Robs null terminator embedding - memcpy(c+strlen(p)+1, o, count+1); //Robs null terminator embedding + c = xmalloc(strlen(p)+count+2); + memcpy(c, p, strlen(p)+1); + memcpy(c+strlen(p)+1, o, count+1); h = SHA1(c, strlen(p)+count+1, h); //ToDo: borrowed from OpenSSL to not to pipe or refactor SHA1SUM in toybox printf("..Binary search\n"); for (int j = 0; j<20; j++) printf("%02x", h[j]); //find insert position @@ -357,12 +356,12 @@ char *resolve_delta(char *s, char *d, long dsize, unsigned *count) *count |= (unsigned long long)(d[pos++]& 0x7F) << bitshift; printf("Target Count %d:\n", *count); - char *t = malloc(sizeof(char)*(*count+1)); - if (t == NULL) error_exit("t malloc failed in resolve_delta"); + char *t = xmalloc(*count+1); *count = 0; while (pos0) ? xmprintf("%s/%s", path, object+pos+7) : object+pos+7; + name = *path ? xmprintf("%s/%s", path, hs+7) : hs+7; printf("prepare file %s\n", name); } else { //tree object reference is a folder // concat folder name - name = (strlen(path)>0) ? xmprintf("%s/%s", path, object+pos+6) : object+pos+6; + name = *path ? xmprintf("%s/%s", path, hs+6) : hs+6; printf("create folder %s\n", name); mkdir(name, 0755); //TODO: umask } + hs += strlen(hs)+1; memcpy(hash, hs, 20); write_children(hash, name, fpp); pos = hs-object+20; @@ -520,33 +519,32 @@ void write_children(char *hash, char *path, FILE *fpp) { static void gitfetch(void) { printf("refs\n"); - pid_t pid; // TODO:I use herein after two temp files for fetch which git does not offer // to 1) avoid a rewrite and 2) messing up the repo files while testing // TODO: Refactor wget into lib - if ((pid = fork())==0) - execv("toybox", (char *[]){"toybox", "wget", "-O", ".git/refs/temp.refs", + xrun((char *[]){"wget", "-O", ".git/refs/temp.refs", "https://github.com/landley/toybox/info/refs?service=git-upload-pack", - (char*)0}); - perror("execv\n"); + 0}); //char h[] = "8cf1722f0fde510ea81d13b31bde1e48917a0306"; //TODO: Replace static testing hash and uncomment the following line if rare delta resolve /?heap overflow? bug was found FILE *fpr = fopen(".git/ref/temp.refs", "r"); - char *h;size_t l =0; + char *h; + size_t l = 0; + getline(&h,&l,fpr); getline(&h,&l,fpr); getline(&h,&l,fpr); getline(&h,&l,fpr); fclose(fpr); strcpy(h,&h[4]); - h[40]='\0'; + h[40] = 0; printf("Master HEAD hash: %s\n",h); //TODO: Persist hash to /refs/master/HEAD printf("pack\n"); - if ((pid = fork())==0) execv("toybox", (char *[]){"toybox", "wget", "-O", ".git/objects/pack/temp.pack", "-p", xmprintf("$'0032want %s\n00000009done\n'", h), "https://github.com/landley/toybox/git-upload-pack", (char*)0}); //TODO: does not skip 0008NAK printf("init\n"); - perror("execv\n"); + xrun((char *[]){"toybox", "wget", "-O", ".git/objects/pack/temp.pack", "-p", xmprintf("$'0032want %s\n00000009done\n'", h), "https://github.com/landley/toybox/git-upload-pack", 0}); + //TODO: does not skip 0008NAK printf("init\n"); FILE *fpp; printf("openpack\n"); fpp = fopen(".git/objects/pack/temp.pack", "r"); @@ -589,6 +587,7 @@ static void gitcheckout(char *name) FILE *fpp; //FILE *fh; + printf("Find branch for checkout\n"); //fh = fopen(xmprintf(".git/ref/heads/%s", name ? "master" : name), "r"); //TODO: Checkout master as in ref/heads printf("Read head\n"); -- 2.39.2