From 46d80b1dc49652ac7100a0d1647f5c3b571680aa Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 1 Jan 2023 15:18:48 -0600 Subject: [PATCH] Yet more whitespace cleanup. --- toys/pending/git.c | 356 +++++++++++++++++++++++---------------------- 1 file changed, 183 insertions(+), 173 deletions(-) diff --git a/toys/pending/git.c b/toys/pending/git.c index 7c0872c7..86390b92 100644 --- a/toys/pending/git.c +++ b/toys/pending/git.c @@ -74,13 +74,14 @@ GLOBALS( struct IndexV2 *i; ) -struct IndexV2 { //git inxed format v2 https://github.com/git/git/blob/master/Documentation/technical/pack-format.txt#L241 +//git inxed format v2 https://github.com/git/git/blob/master/Documentation/technical/pack-format.txt#L241 +struct IndexV2 { char header[8]; uint32_t fot[256]; char (*sha1)[20]; uint32_t *crc; uint32_t *offset; - long long *offset64;//not supported yet + long long *offset64; //not supported yet char packsha1[20]; char idxsha1[20]; }; @@ -88,16 +89,17 @@ struct IndexV2 { //git inxed format v2 https://github.com/git/git/blob/master/Do static void read_index(struct IndexV2 *i) { FILE *fpi; - i=malloc(sizeof(i)); - //i->fot={ 0 }; - i->sha1=malloc(20*sizeof(char)); - i->crc=malloc(sizeof(uint32_t)); - i->offset=malloc(sizeof(uint32_t)); - i->offset64=malloc(sizeof(long long)); + + i = malloc(sizeof(i)); + //i->fot = { 0 }; + i->sha1 = malloc(20*sizeof(char)); + i->crc = malloc(sizeof(uint32_t)); + i->offset = malloc(sizeof(uint32_t)); + i->offset64 = malloc(sizeof(long long)); //TODO: not used yet as index is not persisted yet if (access(".git/object/pack/temp.idx", F_OK)==0) { //persistance needed for other git commands (not clone) - fpi=fopen(".git/object/pack/temp.idx", "rb"); + fpi = fopen(".git/object/pack/temp.idx", "rb"); printf("read header\n"); fread(i->header, sizeof(i->header), 1, fpi); printf("Header: %s..Read fot\n", i->header); @@ -121,28 +123,30 @@ static char *l; //for saving the insertion position int cmp (const void *i, void *j) { - l=j; //inject inseration position in compare to binary search - //printf("Compare %p %p %d\n",i,j,strncmp(i,j,20)); - return strncmp(i,j,20); + l = j; //inject inseration position in compare to binary search + //printf("Compare %p %p %d\n", i, j, strncmp(i, j, 20)); + + return strncmp(i, j, 20); } //inspired by musl bsearch -long bsearchpos(const void *k, const void *a,size_t h, size_t w) +long bsearchpos(const void *k, const void *a, size_t h, size_t w) { long l = 0, m = 0, r = 0; + if (!h) return 0; - //printf("Array: %p Key:%p\n",a,k); - while(h>0){ - m=l+(h/2); - //m=(l+h)/2; - //printf("l: %ld m:%ld h:%ld\n",l,m,h); - r=strncmp(k,a+(m*w),20); - //printf("r: %ld\n",r); - if(!r||h==1)break;//match on search or position for insert - if(r<0){h/=2;}else{l=m;h-=h/2;} - //if(r<0){h=m-1;}else{l=m+1;} + //printf("Array: %p Key:%p\n", a, k); + while (h>0) { + m = l+(h/2); + //m = (l+h)/2; + //printf("l: %ld m:%ld h:%ld\n", l, m, h); + r = strncmp(k, a+(m*w), 20); + //printf("r: %ld\n", r); + if (!r||h==1) break; //match on search or position for insert + if (r<0) { h /= 2; } else { l = m; h -= h/2; } + //if (r<0) { h = m-1; } else { l = m+1; } } - //printf("Return m: %ld r:%ld \n",m,r); + //printf("Return m: %ld r:%ld \n", m, r); //For inserts check if insert is bigger obj at identified position return m += (r>0) ? 1 : 0; @@ -150,22 +154,22 @@ long bsearchpos(const void *k, const void *a,size_t h, size_t w) long get_index(struct IndexV2 *i, char *h) { - long pos=bsearchpos(h, i->sha1[0], i->fot[255], 20); //, + long pos = bsearchpos(h, i->sha1[0], i->fot[255], 20); //, - //(int(*)(const void*,const void*)) cmp);//TODO: Should be placed by bsearchpos() below; cmp and *l to be removed too - // for (int j=0;j<20;j++) printf("%02x",h[j]); + //(int(*)(const void*, const void*)) cmp); //TODO: Should be placed by bsearchpos() below; cmp and *l to be removed too + // for (int j = 0; j<20; j++) printf("%02x", h[j]); // printf("\n"); //if (pos == NULL){ - // for (int h=0;hfot[255];h++){ - // printf("%d: ",h); - // for (int j=0;j<20;j++) printf("%02x",i->sha1[h][j]); + // for (int h = 0; hfot[255]; h++){ + // printf("%d: ", h); + // for (int j = 0; j<20; j++) printf("%02x", i->sha1[h][j]); // printf("\n"); // } //} - // printf("index pointer: %ld\n",pos); - // printf("fot[255]: %d\n",i->fot[255]); - // printf("sha1[0] pointer: %p\n",i->sha1[0]); - // printf("offset index : %ld\n",pos); + // printf("index pointer: %ld\n", pos); + // printf("fot[255]: %d\n", i->fot[255]); + // printf("sha1[0] pointer: %p\n", i->sha1[0]); + // printf("offset index : %ld\n", pos); // return i->offset[(pos-i->sha1[0])/20]; return i->offset[pos]; @@ -182,22 +186,23 @@ uint64_t unpack(FILE *fpp, int *type, long int *offset) uint8_t data; printf("Start unpack\n"); - fseek(fpp,*offset,SEEK_SET); + fseek(fpp, *offset, SEEK_SET); printf("Offset set to: %ld\n", *offset); fread(&data, 1, 1, fpp); printf("Data: %d\n", data); - *type=((data & 0x70)>>4); - printf("Type: %d\n",*type); + *type = ((data & 0x70)>>4); + printf("Type: %d\n", *type); length |= (uint64_t)(data & 0x0F); //(*offset)++; - while((data & 0x80) && fread(&data, 1, 1, fpp)!=-1) + while ((data & 0x80) && fread(&data, 1, 1, fpp)!=-1) { length |= (uint64_t)(data & 0x7F) << bitshift; bitshift += 7; // (*offset)++; - //printf("Offset set to: %ld\n",*offset); + //printf("Offset set to: %ld\n", *offset); } - //printf("Offset set to: %ld\n",*offset); + //printf("Offset set to: %ld\n", *offset); printf("Length: %ld\n", length); + return length; } @@ -209,7 +214,7 @@ uint64_t unpack(FILE *fpp, int *type, long int *offset) int inf(FILE *source, char *dest) //modified signature to ease use { int ret; - char *position=dest; + char *position = dest; unsigned have; z_stream strm; unsigned char in[CHUNK]; @@ -220,8 +225,7 @@ int inf(FILE *source, char *dest) //modified signature to ease use strm.avail_in = 0; strm.next_in = Z_NULL; ret = inflateInit(&strm); - if (ret != Z_OK) - return ret; + if (ret != Z_OK) return ret; // decompress until deflate stream ends or end of file do { @@ -229,6 +233,7 @@ int inf(FILE *source, char *dest) //modified signature to ease use strm.avail_in = fread(in, 1, CHUNK, source); if (ferror(source)) { (void)inflateEnd(&strm); + return Z_ERRNO; } if (strm.avail_in == 0) @@ -250,12 +255,13 @@ int inf(FILE *source, char *dest) //modified signature to ease use case Z_DATA_ERROR: case Z_MEM_ERROR: (void)inflateEnd(&strm); + return ret; } have = CHUNK - strm.avail_out; - memcpy(position,out,have);//added to original - position+=have;//added to original + memcpy(position, out, have); //added to original + position += have; //added to original //if (fwrite(out, 1, have, dest) != have || ferror(dest)) { // (void)inflateEnd(&strm); // return Z_ERRNO; @@ -263,86 +269,88 @@ int inf(FILE *source, char *dest) //modified signature to ease use } while (strm.avail_out == 0); // done when inflate() says it's done } while (ret != Z_STREAM_END); - fseek(source,ftell(source)-strm.avail_in,SEEK_SET);//modified from zpipe.c to set FP to end of zlib object + // modified from zpipe.c to set FP to end of zlib object + fseek(source, ftell(source)-strm.avail_in, SEEK_SET); // clean up and return (void)inflateEnd(&strm); + return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR; } //https://github.com/git/git/blob/master/Documentation/gitformat-pack.txt#L72 -long set_object(struct IndexV2 *idx,int type, char *o, uint32_t count, +long set_object(struct IndexV2 *idx, int type, char *o, uint32_t count, uint32_t ofs) { // 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 - long pos=0; + char *c, *p = "", *h = (char*)xmalloc(sizeof(char)*20); //composition,prefix,hash + long pos = 0; printf("Alloc... "); if (h == NULL) error_exit("Hash malloc failed in set_object"); switch(type) { - case 1:p=xmprintf("commit %d",count);break;//count is used as o can contain \0 in the string - case 2: p=xmprintf("tree %d",count); break; - case 3: p=xmprintf("blob %d",count); break; - case 4: p=xmprintf("tag %d",count); break; + case 1: p = xmprintf("commit %d", count); break; //count is used as o can contain \0 in the string + case 2: p = xmprintf("tree %d", count); break; + case 3: p = xmprintf("blob %d", count); break; + case 4: p = xmprintf("tag %d", count); break; 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 - //printf("Enriched Object: %s %ld\n",c,strlen(p)+count+2); - h = SHA1(c,strlen(p)+count+1,h); //ToDo: borrowed from OpenSSL to not to pipe or refactor SHA1SUM in toybox + memcpy(c, p, strlen(p)+1); //Robs null terminator embedding + memcpy(c+strlen(p)+1, o, count+1); //Robs null terminator embedding + //printf("Enriched Object: %s %ld\n", c, strlen(p)+count+2); + 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"); - //printf("\nidx->fot[255]=%d\n",idx->fot[255]); - //printf("idx->sha1[fot[h[0]]]=%d\n",sizeof(idx->sha1)); + //printf("\nidx->fot[255]=%d\n", idx->fot[255]); + //printf("idx->sha1[fot[h[0]]]=%d\n", sizeof(idx->sha1)); //TODO:Array Insert broken //if (idx->fot[255]>1) //{ - // printf("Bsearch result: %p\n",bsearch(h, idx->sha1[0], idx->fot[255], 20, - // (int(*)(const void * ,const void *)) cmp));//find insertation position - // printf("Inseration position pointer %p\n",l); - // pos=(long)(((l-idx->sha1[0])/20)+((strncmp(h,l,20)<0)?0:1));//ugly ins pos hack - // printf("Bigger one %ld\n",(long)(((l-idx->sha1[0])/20)+((strncmp(h,l,20)<0)?0:1))); + // printf("Bsearch result: %p\n", bsearch(h, idx->sha1[0], idx->fot[255], 20, + // (int(*)(const void *, const void *)) cmp)); //find insertation position + // printf("Inseration position pointer %p\n", l); + // pos = (long)(((l-idx->sha1[0])/20)+((strncmp(h, l, 20)<0) ? 0 : 1)); //ugly ins pos hack + // printf("Bigger one %ld\n", (long)(((l-idx->sha1[0])/20)+((strncmp(h, l, 20)<0) ? 0 : 1))); //} else { // printf("Smaller two\n"); - // if (idx->fot[255]==0) - // { - // pos=0; - // }else{ - // pos=(strncmp(h,idx->sha1[0],20)<0)?0:1; + // if (idx->fot[255]==0) { + // pos = 0; + // } else { + // pos = (strncmp(h, idx->sha1[0], 20)<0) ? 0 : 1; // } // } - //printf("Binary search position: %ld, %p %p\n",pos,idx->sha1[0],l); - //l=NULL; - for (int j = 0; j<20; j++) printf("%02x",h[j]); //find insert position - pos = bsearchpos(h,idx->sha1[0],idx->fot[255],20); - printf("\n..Insert pos %ld\n",pos); + //printf("Binary search position: %ld, %p %p\n", pos, idx->sha1[0], l); + //l = NULL; + for (int j = 0; j<20; j++) printf("%02x", h[j]); //find insert position + pos = bsearchpos(h, idx->sha1[0], idx->fot[255], 20); + printf("\n..Insert pos %ld\n", pos); printf("..Preloop\n"); - for (int i = h[0]; i<=255; i++) - idx->fot[i]+=1; //adjust of fanout table https://github.com/git/git/blob/master/Documentation/technical/pack-format.txt#L179 + + // adjust of fanout table https://github.com/git/git/blob/master/Documentation/technical/pack-format.txt#L179 + for (int i = h[0]; i<=255; i++) idx->fot[i] += 1; printf("Post loop\n"); - printf("Resize sha1 array..idx->fot[255]%d\n",idx->fot[255]); //Memory management for insert TODO:Could be also a single malloc at gitfetch based on the nbr of objects in pack + printf("Resize sha1 array..idx->fot[255]%d\n", idx->fot[255]); //Memory management for insert TODO:Could be also a single malloc at gitfetch based on the nbr of objects in pack //Did not fix the TODO yet, because set_object could be reused for other command im mem mgmt is here - idx->sha1 = realloc(idx->sha1,(idx->fot[255]+1)*20*sizeof(char)); - printf("Mem copy sha1 array..sizeof(idx->sha1)%ld\n",sizeof(idx->sha1)); - memmove(&idx->sha1[pos+1],&idx->sha1[pos],(idx->fot[255]-pos)*20*sizeof(char)); + idx->sha1 = realloc(idx->sha1, (idx->fot[255]+1)*20*sizeof(char)); + printf("Mem copy sha1 array..sizeof(idx->sha1)%ld\n", sizeof(idx->sha1)); + memmove(&idx->sha1[pos+1], &idx->sha1[pos], (idx->fot[255]-pos)*20*sizeof(char)); printf("Resize offset\n"); idx->offset = realloc(idx->offset, (idx->fot[255]+1)*sizeof(uint32_t)); printf("Mem copy offset\n"); memmove(&idx->offset[pos+1], &idx->offset[pos], sizeof(uint32_t)*(idx->fot[255]-pos)); printf("Set offset value\n"); - memcpy(&idx->sha1[pos], h, 20);//insert SHA1 - idx->offset[pos] = ofs;//insert offset of SHA1 + memcpy(&idx->sha1[pos], h, 20); //insert SHA1 + idx->offset[pos] = ofs; //insert offset of SHA1 //ToDo: id->crc[idx->fot[h[0]]]=; printf("Write object\n"); - // printf("SetGet %d %ld\n :",idx->offset[pos],get_index(idx,h)); + // printf("SetGet %d %ld\n :", idx->offset[pos], get_index(idx, h)); //writeObject to local pack; - // for (int h=0;hfot[255];h++){ - // for (int j=0;j<20;j++) printf("%02x",idx->sha1[h][j]); + // for (int h = 0; hfot[255]; h++) { + // for (int j = 0; j<20; j++) printf("%02x", idx->sha1[h][j]); // printf("\n"); // } free(h); @@ -354,12 +362,13 @@ long set_object(struct IndexV2 *idx,int type, char *o, uint32_t count, static void gitinit(char *name) { //For git clone actually only refs and object/pack needed - if (mkdir(name,0755)!=0){ - mkdir(xmprintf("%s%s", name, "/.git"), 0755);//I create the other for a git compliant folder structure + if (mkdir(name, 0755)!=0){ + //I create the other for a git compliant folder structure + mkdir(xmprintf("%s%s", name, "/.git"), 0755); mkdir(xmprintf("%s%s", name, "/.git/objects"), 0755); mkdir(xmprintf("%s%s", name, "/.git/objects/pack"), 0755); mkdir(xmprintf("%s%s", name, "/.git/branches"), 0755); - mkdir(xmprintf("%s%s", name, "/.git/hooks"), 0755);//hook files skipped as implementations does not support hooks + mkdir(xmprintf("%s%s", name, "/.git/hooks"), 0755); //hook files skipped as implementations does not support hooks mkdir(xmprintf("%s%s", name, "/.git/info"), 0755); mkdir(xmprintf("%s%s", name, "/.git/objects/info"), 0755); mkdir(xmprintf("%s%s", name, "/.git/refs"), 0755); @@ -375,7 +384,7 @@ static void gitinit(char *name) static void gitremote(char *url) { if (access(".git/config", F_OK)!=0) { - FILE *fp = fopen(".git/config","wb"); + FILE *fp = fopen(".git/config", "wb"); fwrite("[core]\n", 1, 7, fp); fwrite("\trepositoryformatversion = 0\n", 1, 29, fp); @@ -383,36 +392,38 @@ static void gitremote(char *url) fwrite("\tbare = false\n", 1, 14, fp); fwrite("\tlogallrefupdates = true\n", 1, 25, fp); fwrite("\tsymlinks = false\n", 1, 18, fp); - fwrite("\tignorecase = true\n", 1 ,19, fp); - fwrite("[remote \"origin\"]\n", 1 ,18, fp); - fwrite(xmprintf("\turl = %s/refs\n",TT.url), 1, strlen(TT.url)+13, fp); + fwrite("\tignorecase = true\n", 1, 19, fp); + fwrite("[remote \"origin\"]\n", 1, 18, fp); + fwrite(xmprintf("\turl = %s/refs\n", TT.url), 1, strlen(TT.url)+13, fp); fwrite("\tfetch = +ref/heads/*:refs/remotes/origin/*\n", 1, 44, fp); fclose(fp); } } -//this is most likely still buggy and create a late observable heap overflow larger deltafied repos -char* resolve_delta(char *s, char *d, long dsize, uint32_t *count) -{ //https://stackoverflow.com/a/14303988 - long pos=0, bitshift=0; +// this is most likely still buggy and create a late observable heap overflow larger deltafied repos +// https://stackoverflow.com/a/14303988 +char *resolve_delta(char *s, char *d, long dsize, uint32_t *count) +{ + long pos = 0, bitshift = 0; printf("Original Source: \n"); - //for (int k=0;k<*count;k++){printf("%c",s[k]);} + //for (int k = 0; k<*count; k++) { printf("%c", s[k]); } //printf("\n"); //printf("Delta:\n"); - //for (int k=0;kfot[255]; h++){ @@ -512,7 +523,7 @@ char* unpack_object(FILE *fpp, struct IndexV2 *i, long offset, uint32_t *count, free(h); // recursion due to https://github.com/git/git/blob/master/Documentation/technical/pack-format.txt#L83 - return resolve_delta(source,object,dcount,count); + return resolve_delta(source, object, dcount, count); } else { printf("Type Else:\n"); inf(fpp, object); @@ -533,9 +544,9 @@ char *txtoh(char *p) if (h == NULL) error_exit("h malloc failed in unpack_object"); - for(int c=0; c<20; c++) { - // printf("c: %d\n",c); - sscanf(&p[2*c],"%2hhx",&(h[c])); + for (int c = 0; c<20; c++) { + // printf("c: %d\n", c); + sscanf(&p[2*c], "%2hhx", &(h[c])); } // printf("txtoh end"); h[20] = '\0'; @@ -553,38 +564,38 @@ void write_children(char *hash, char *path, FILE *fpp) { uint32_t count; //printf("process hash: "); - // for (int j=0; j<20; j++) printf("%02x", hash[j]); + // for (int j = 0; j<20; j++) printf("%02x", hash[j]); // printf("\n"); printf("seek index\n"); offset= get_index(TT.i, hash); - printf("Found index: %ld\n",offset); - //fseek(fpp,offset,SEEK_SET); + printf("Found index: %ld\n", offset); + //fseek(fpp, offset, SEEK_SET); printf("read object\n"); - // size_t size=unpack(fpp,&type,&offset); - // printf("Size: %ld \n",size); - // printf("Size2: %ld \n",size); + // size_t size = unpack(fpp, &type, &offset); + // printf("Size: %ld \n", size); + // printf("Size2: %ld \n", size); object = unpack_object(fpp, TT.i, offset, &count, &type); printf("%s\n", object); printf("Type %d\n", type); - if(type==1) { //at commit object + if (type==1) { //at commit object memcpy(hash, &object[5], 40); write_children(txtoh(hash), path, fpp); - } else if(type==2) { //at tree object https://stackoverflow.com/a/21599232 - char *hs=0; - int pos=0; + } else if (type==2) { //at tree object https://stackoverflow.com/a/21599232 + char *hs = 0; + int pos = 0; - printf("process folder %s\n",path); + printf("process folder %s\n", path); while (pos0) ? xmprintf("%s/%s", path, object+pos+7) : object+pos+7; - printf("prepare file %s\n",name); + 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; @@ -599,20 +610,20 @@ void write_children(char *hash, char *path, FILE *fpp) { } printf("**EXIT WHILE**\n"); } else { //at blob/file object - printf("process file %s\n",path); + printf("process file %s\n", path); fc = fopen(path, "w"); printf("process opened \n"); - fputs(object,fc);//TODO:Not sure if length might be an issue here + fputs(object, fc); //TODO:Not sure if length might be an issue here printf("process file written\n"); fclose(fc); } free(object); - printf("Child: %s done\n",path); + printf("Child: %s done\n", path); } static void gitfetch(void) { - //size_t l=0; + //size_t l = 0; printf("refs\n"); pid_t pid; @@ -620,14 +631,14 @@ static void gitfetch(void) // 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", + if ((pid = fork())==0) + execv("toybox", (char *[]){"toybox", "wget", "-O", ".git/refs/temp.refs", "https://github.com/landley/toybox/info/refs?service=git-upload-pack", (char*)0}); perror("execv\n"); - char h[]="8cf1722f0fde510ea81d13b31bde1e48917a0306"; + char h[] = "8cf1722f0fde510ea81d13b31bde1e48917a0306"; //TODO: Replace static testing hash and uncomment the following line if rare delta resolve /?heap overflow? bug was found -// char h[]="52fb04274b3491fdfe91b2e5acc23dc3f3064a86"; +// char h[] = "52fb04274b3491fdfe91b2e5acc23dc3f3064a86"; // FILE *fpr; // fpr = fopen(".git/ref/temp.refs", "r"); // fseek(); @@ -638,8 +649,8 @@ static void gitfetch(void) // strcpy(h, &h[4], 4); // h[40] = '\0'; printf("pack\n"); - //if ((pid=fork())==0) execv("toybox",(char *[]){"toybox","wget","-O",".git/objects/pack/temp.pack","-p","$'0032want 52fb04274b3491fdfe91b2e5acc23dc3f3064a86\n00000009done\n'","https://github.com/landley/toybox/git-upload-pack",(char*)0});//TODO: does not skip 0008NAK printf("init\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"); + //if ((pid = fork())==0) execv("toybox", (char *[]){"toybox", "wget", "-O", ".git/objects/pack/temp.pack", "-p", "$'0032want 52fb04274b3491fdfe91b2e5acc23dc3f3064a86\n00000009done\n'", "https://github.com/landley/toybox/git-upload-pack", (char*)0}); //TODO: does not skip 0008NAK printf("init\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"); FILE *fpp; printf("openpack\n"); @@ -647,17 +658,17 @@ static void gitfetch(void) printf("read index\n"); read_index(TT.i); //init index with out reading printf("init\n"); - uint32_t ocount=0, count; + uint32_t ocount = 0, count; int type; char *object; printf("skip header\n"); - long int offset=12+8; //8byte from the wget post response are skipped too - fseek(fpp,8+8,SEEK_SET); //header check skipped https://github.com/git/git/blob/master/Documentation/technical/pack-format.txt#L14 + long int offset = 12+8; //8byte from the wget post response are skipped too + fseek(fpp, 8+8, SEEK_SET); //header check skipped https://github.com/git/git/blob/master/Documentation/technical/pack-format.txt#L14 printf("read count\n"); fread(&ocount, 4, 1, fpp); ocount = ntohl(ocount); //https://github.com/git/git/blob/master/Documentation/technical/pack-format.txt#L21 printf("Count: %d ..Loop pack\n", ocount); - for (int j=0; jfot[255]; h++){ -// printf("%d: ",h); -// for (int j=0; j<20; j++) printf("%02x", TT.i->sha1[h][j]); +// for (int h = 0; hfot[255]; h++){ +// printf("%d: ", h); +// for (int j = 0; j<20; j++) printf("%02x", TT.i->sha1[h][j]); // printf("\n"); // } @@ -688,36 +699,35 @@ 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 + //fh = fopen(xmprintf(".git/ref/heads/%s", name ? "master" : name), "r"); //TODO: Checkout master as in ref/heads printf("Read head\n"); - //hf[fread(&hf,40,1,fh)]='\0'; + //hf[fread(&hf, 40, 1, fh)] = '\0'; //fclose(fh); printf("Close heads and read pack\n"); - fpp=fopen(".git/objects/pack/temp.pack","r"); + fpp = fopen(".git/objects/pack/temp.pack", "r"); printf("set signature\n"); - char *p="52fb04274b3491fdfe91b2e5acc23dc3f3064a86";//static hashes for testing toybox 0.0.1"; - //char *p="c555a0ca46e75097596274bf5e634127015aa144";//static hashes for testing 0.0.2"; - //char *p="4307a7b07cec4ad8cbab47a29ba941f8cb041812";//static hashes for testing 0.0.3"; - //char *p="3632d5d8fe05d14da983e37c7cd34db0769e6238";//static hashes for testing 0.0.4"; - //char *p="8cf1722f0fde510ea81d13b31bde1e48917a0306";//3604ba4f42c3d83e2b14f6d0f423a33a3a8706c3"; + char *p = "52fb04274b3491fdfe91b2e5acc23dc3f3064a86"; //static hashes for testing toybox 0.0.1"; + //char *p = "c555a0ca46e75097596274bf5e634127015aa144"; //static hashes for testing 0.0.2"; + //char *p = "4307a7b07cec4ad8cbab47a29ba941f8cb041812"; //static hashes for testing 0.0.3"; + //char *p = "3632d5d8fe05d14da983e37c7cd34db0769e6238"; //static hashes for testing 0.0.4"; + //char *p = "8cf1722f0fde510ea81d13b31bde1e48917a0306"; //3604ba4f42c3d83e2b14f6d0f423a33a3a8706c3"; printf("enter tree root\n"); - write_children(txtoh(p),"",fpp); + write_children(txtoh(p), "", fpp); fclose(fpp); } void gitclone_main(void) { TT.url = xstrdup(toys.optargs[0]); - if(strend(TT.url,".git")) TT.url[strlen(TT.url)-4]='\0'; - TT.name = strrchr(TT.url,'/')+1; + if (strend(TT.url, ".git")) TT.url[strlen(TT.url)-4] = '\0'; + TT.name = strrchr(TT.url, '/')+1; gitinit(TT.name); chdir(TT.name); - TT.i=malloc(sizeof(struct IndexV2)); + TT.i = malloc(sizeof(struct IndexV2)); gitremote(TT.url); gitfetch(); gitcheckout("master"); chdir(".."); - return; } #define FOR_gitinit @@ -733,7 +743,7 @@ void gitinit_main(void) void gitremote_main(void) { TT.url = xstrdup(toys.optargs[0]); - if(strend(TT.url,".git")) TT.url[strlen(TT.url)-4]='\0'; + if (strend(TT.url, ".git")) TT.url[strlen(TT.url)-4] = '\0'; gitremote(TT.url); } -- 2.39.2