-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_core.c
More file actions
4533 lines (3885 loc) · 136 KB
/
git_core.c
File metadata and controls
4533 lines (3885 loc) · 136 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* git_core.c - Git client core for NeXTSTEP 3.3
*
* Pure logic layer: no printf, no stdin, no GUI calls.
* Returns data via structs; reports progress via callback.
*
* Build: cc -O -c git_core.c
*
* (c) 2026 ARNLTony & Claude. MIT License.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/dir.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
/* Crypto Ancienne TLS library */
#include "cryanc.c"
/* Our header */
#include "git_core.h"
/* --- NeXTSTEP compatibility --- */
/*
* snprintf shim for NeXTSTEP 3.3 (GCC 2.5.8 lacks snprintf).
* Uses vsprintf into a large temp buffer, then copies at most
* size-1 chars + NUL into dst. Returns the full formatted length.
*
* The temp buffer is 2048 bytes, which covers all our use cases
* (result.message is 512, api_path is 1024, pattern is 256).
*/
#include <stdarg.h>
#define SNPRINTF_TMPBUF 2048
int safe_snprintf(char *buf, int size, char *fmt, ...)
{
va_list ap;
char tmp[SNPRINTF_TMPBUF];
int len;
if (size <= 0) return 0;
va_start(ap, fmt);
len = vsprintf(tmp, fmt, ap);
va_end(ap);
if (len < 0) {
buf[0] = '\0';
return 0;
}
if (len >= size) {
strncpy(buf, tmp, size - 1);
buf[size - 1] = '\0';
} else {
strcpy(buf, tmp);
}
return len;
}
/*
* Path safety check: reject paths containing ".." or starting with "/".
* Returns 1 if safe, 0 if unsafe (potential path traversal).
*/
static int path_is_safe(path)
char *path;
{
if (!path || !path[0]) return 0;
/* Reject absolute paths */
if (path[0] == '/') return 0;
/* Reject paths containing ".." component */
if (strstr(path, "..") != NULL) return 0;
return 1;
}
/* NeXTSTEP 3.3 lacks S_ISDIR/S_ISREG macros */
#ifndef S_ISDIR
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif
#ifndef S_ISREG
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#endif
/* --- Configuration --- */
#define API_HOST "api.github.com"
#define API_PORT 443
/* --- TLS helpers (from gh.c) --- */
int https_send_pending(sockfd, context)
int sockfd;
struct TLSContext *context;
{
unsigned int out_buffer_len = 0;
unsigned int out_buffer_index = 0;
int send_res = 0;
const unsigned char *out_buffer;
out_buffer = tls_get_write_buffer(context, &out_buffer_len);
while (out_buffer && out_buffer_len > 0) {
int res = send(sockfd, (char *)&out_buffer[out_buffer_index],
out_buffer_len, 0);
if (res <= 0) {
send_res = res;
break;
}
out_buffer_len -= res;
out_buffer_index += res;
}
tls_buffer_clear(context);
return send_res;
}
int validate_certificate(context, certificate_chain, len)
struct TLSContext *context;
struct TLSCertificate **certificate_chain;
int len;
{
return no_error;
}
/* --- JSON helpers (from gh.c) --- */
char *json_find_string(json, key, out_len)
char *json;
char *key;
int *out_len;
{
char pattern[256];
char *p, *start, *search;
safe_snprintf(pattern, sizeof(pattern), "\"%s\"", key);
search = json;
while (1) {
p = strstr(search, pattern);
if (!p) return NULL;
p += strlen(pattern);
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
p++;
if (*p != ':') {
search = p;
continue;
}
p++;
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
p++;
if (*p != '"') {
search = p;
continue;
}
p++;
start = p;
while (*p && !(*p == '"' && *(p-1) != '\\'))
p++;
*out_len = p - start;
return start;
}
}
long json_find_number(json, key)
char *json;
char *key;
{
char pattern[256];
char *p, *search;
long val;
safe_snprintf(pattern, sizeof(pattern), "\"%s\"", key);
search = json;
while (1) {
p = strstr(search, pattern);
if (!p) return -1;
p += strlen(pattern);
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
p++;
if (*p != ':') {
search = p;
continue;
}
p++;
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
p++;
if (*p == '-' || (*p >= '0' && *p <= '9')) {
val = atol(p);
return val;
}
search = p;
}
}
int json_find_bool(json, key)
char *json;
char *key;
{
char pattern[256];
char *p, *search;
safe_snprintf(pattern, sizeof(pattern), "\"%s\"", key);
search = json;
while (1) {
p = strstr(search, pattern);
if (!p) return -1;
p += strlen(pattern);
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
p++;
if (*p != ':') {
search = p;
continue;
}
p++;
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
p++;
if (strncmp(p, "true", 4) == 0) return 1;
if (strncmp(p, "false", 5) == 0) return 0;
search = p;
}
}
char *json_array_first(json, end)
char *json;
char **end;
{
char *p;
int depth;
p = json;
while (*p && *p != '[') p++;
if (!*p) return NULL;
p++;
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
p++;
if (*p == ']') return NULL;
if (*p != '{') return NULL;
depth = 0;
*end = p;
while (**end) {
if (**end == '{') depth++;
else if (**end == '}') {
depth--;
if (depth == 0) {
(*end)++;
return p;
}
} else if (**end == '"') {
(*end)++;
while (**end && !(**end == '"' && *(*end - 1) != '\\'))
(*end)++;
}
(*end)++;
}
return NULL;
}
char *json_array_next(pos, end)
char *pos;
char **end;
{
char *p;
int depth;
p = *end;
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
p++;
if (*p != ',') return NULL;
p++;
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
p++;
if (*p != '{') return NULL;
depth = 0;
*end = p;
while (**end) {
if (**end == '{') depth++;
else if (**end == '}') {
depth--;
if (depth == 0) {
(*end)++;
return p;
}
} else if (**end == '"') {
(*end)++;
while (**end && !(**end == '"' && *(*end - 1) != '\\'))
(*end)++;
}
(*end)++;
}
return NULL;
}
void json_unescape(src, src_len, dst, dst_size)
char *src;
int src_len;
char *dst;
int dst_size;
{
int i, j;
j = 0;
for (i = 0; i < src_len && j < dst_size - 1; i++) {
if (src[i] == '\\' && i + 1 < src_len) {
i++;
switch (src[i]) {
case 'n': dst[j++] = '\n'; break;
case 't': dst[j++] = '\t'; break;
case '"': dst[j++] = '"'; break;
case '\\': dst[j++] = '\\'; break;
case '/': dst[j++] = '/'; break;
default: dst[j++] = src[i]; break;
}
} else {
dst[j++] = src[i];
}
}
dst[j] = '\0';
}
void json_escape(src, dst, dst_size)
char *src;
char *dst;
int dst_size;
{
int i, j;
j = 0;
for (i = 0; src[i] && j < dst_size - 2; i++) {
switch (src[i]) {
case '"': dst[j++] = '\\'; dst[j++] = '"'; break;
case '\\': dst[j++] = '\\'; dst[j++] = '\\'; break;
case '\n': dst[j++] = '\\'; dst[j++] = 'n'; break;
case '\t': dst[j++] = '\\'; dst[j++] = 't'; break;
case '\r': dst[j++] = '\\'; dst[j++] = 'r'; break;
default:
if ((unsigned char)src[i] < 0x20) {
/* skip other control characters */
} else {
dst[j++] = src[i];
}
break;
}
}
dst[j] = '\0';
}
/* --- Base64 decoder (from gh.c, renamed) --- */
static char b64_table[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static int b64_val(c)
int c;
{
char *p;
if (c == '=') return 0;
p = strchr(b64_table, c);
if (!p) return -1;
return p - b64_table;
}
int gh_base64_decode(src, src_len, dst, dst_size)
char *src;
int src_len;
char *dst;
int dst_size;
{
int i, j, a, b, c, d;
j = 0;
i = 0;
while (i < src_len && j < dst_size - 1) {
/* skip whitespace and newlines */
while (i < src_len && (src[i] == '\n' || src[i] == '\r' ||
src[i] == ' ' || src[i] == '\t' || src[i] == '\\'))
{
if (src[i] == '\\' && i + 1 < src_len && src[i+1] == 'n') {
i += 2;
} else {
i++;
}
}
if (i + 3 >= src_len) break;
a = b64_val(src[i]);
b = b64_val(src[i+1]);
c = b64_val(src[i+2]);
d = b64_val(src[i+3]);
if (a < 0 || b < 0 || c < 0 || d < 0) { i += 4; continue; }
if (j < dst_size - 1) dst[j++] = (a << 2) | (b >> 4);
if (src[i+2] != '=' && j < dst_size - 1) dst[j++] = ((b & 0x0F) << 4) | (c >> 2);
if (src[i+3] != '=' && j < dst_size - 1) dst[j++] = ((c & 0x03) << 6) | d;
i += 4;
}
dst[j] = '\0';
return j;
}
/* --- Base64 encoder (NEW, for push) --- */
int gh_base64_encode(src, src_len, dst, dst_size)
char *src;
int src_len;
char *dst;
int dst_size;
{
int i, j;
unsigned char a, b, c;
j = 0;
for (i = 0; i < src_len && j < dst_size - 4; i += 3) {
a = (unsigned char)src[i];
b = (i + 1 < src_len) ? (unsigned char)src[i+1] : 0;
c = (i + 2 < src_len) ? (unsigned char)src[i+2] : 0;
dst[j++] = b64_table[(a >> 2) & 0x3F];
dst[j++] = b64_table[((a & 0x03) << 4) | ((b >> 4) & 0x0F)];
if (i + 1 < src_len) {
dst[j++] = b64_table[((b & 0x0F) << 2) | ((c >> 6) & 0x03)];
} else {
dst[j++] = '=';
}
if (i + 2 < src_len) {
dst[j++] = b64_table[c & 0x3F];
} else {
dst[j++] = '=';
}
}
dst[j] = '\0';
return j;
}
/* --- GitHub API request engine --- */
/*
* Make a GitHub API request. Returns HTTP status code (e.g. 200, 404),
* or a negative GIT_ERR_* code on connection failure.
* No printf/fprintf — pure logic.
*/
int gh_api_request(token, method, path, post_body, response, response_size)
char *token;
char *method;
char *path;
char *post_body;
char *response;
int response_size;
{
int sockfd;
struct sockaddr_in serv_addr;
struct hostent *server;
struct TLSContext *context;
char *http_request;
int req_len, body_len;
char *resp_data;
int resp_len;
int read_size;
int sent;
char *body_start;
char *status_line;
int status_code;
unsigned char tls_buf[GIT_HTTP_BUF];
response[0] = '\0';
body_len = post_body ? strlen(post_body) : 0;
/* Build HTTP request */
http_request = (char *)malloc(body_len + 2048);
if (!http_request) {
strcpy(response, "{\"message\":\"Out of memory\"}");
return GIT_ERR_MEMORY;
}
if (post_body) {
sprintf(http_request,
"%s %s HTTP/1.0\r\n"
"Host: %s\r\n"
"Authorization: Bearer %s\r\n"
"User-Agent: nextstep-git/1.0\r\n"
"Accept: application/vnd.github+json\r\n"
"Content-Type: application/json\r\n"
"Content-Length: %d\r\n"
"Connection: close\r\n"
"\r\n"
"%s",
method, path, API_HOST, token, body_len, post_body);
} else {
sprintf(http_request,
"%s %s HTTP/1.0\r\n"
"Host: %s\r\n"
"Authorization: Bearer %s\r\n"
"User-Agent: nextstep-git/1.0\r\n"
"Accept: application/vnd.github+json\r\n"
"Connection: close\r\n"
"\r\n",
method, path, API_HOST, token);
}
req_len = strlen(http_request);
/* Resolve hostname */
server = gethostbyname(API_HOST);
if (!server) {
free(http_request);
strcpy(response, "{\"message\":\"DNS lookup failed\"}");
return GIT_ERR_NETWORK;
}
/* Create socket */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
free(http_request);
strcpy(response, "{\"message\":\"Socket creation failed\"}");
return GIT_ERR_NETWORK;
}
memset((char *)&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
memcpy((char *)&serv_addr.sin_addr.s_addr,
(char *)server->h_addr, server->h_length);
serv_addr.sin_port = htons(API_PORT);
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
free(http_request);
close(sockfd);
strcpy(response, "{\"message\":\"Connection failed\"}");
return GIT_ERR_NETWORK;
}
/* TLS handshake */
context = tls_create_context(0, TLS_V12);
if (!context || !tls_sni_set(context, API_HOST)) {
free(http_request);
close(sockfd);
strcpy(response, "{\"message\":\"TLS setup failed\"}");
return GIT_ERR_NETWORK;
}
tls_client_connect(context);
https_send_pending(sockfd, context);
/* Complete handshake and send request */
sent = 0;
resp_data = (char *)malloc(response_size);
if (!resp_data) {
free(http_request);
tls_destroy_context(context);
close(sockfd);
strcpy(response, "{\"message\":\"Out of memory\"}");
return GIT_ERR_MEMORY;
}
resp_len = 0;
while (1) {
read_size = recv(sockfd, (char *)tls_buf, sizeof(tls_buf), 0);
if (read_size <= 0) break;
tls_consume_stream(context, tls_buf, read_size,
validate_certificate);
https_send_pending(sockfd, context);
if (!tls_established(context))
continue;
if (!sent) {
/* Send in chunks to respect TLS record size limits */
int offset = 0;
int chunk;
while (offset < req_len) {
chunk = req_len - offset;
if (chunk > 8192) chunk = 8192;
tls_write(context, (unsigned char *)http_request + offset, chunk);
https_send_pending(sockfd, context);
offset += chunk;
}
sent = 1;
}
while ((read_size = tls_read(context, tls_buf, sizeof(tls_buf) - 1)) > 0) {
if (resp_len + read_size < response_size - 1) {
memcpy(resp_data + resp_len, tls_buf, read_size);
resp_len += read_size;
}
}
}
resp_data[resp_len] = '\0';
free(http_request);
tls_destroy_context(context);
close(sockfd);
/* Extract HTTP status code */
status_code = 0;
status_line = strstr(resp_data, "HTTP/");
if (status_line) {
char *sp = strchr(status_line, ' ');
if (sp) status_code = atoi(sp + 1);
}
/* Skip HTTP headers, copy body to response */
body_start = strstr(resp_data, "\r\n\r\n");
if (body_start) {
body_start += 4;
strncpy(response, body_start, response_size - 1);
response[response_size - 1] = '\0';
} else {
strncpy(response, resp_data, response_size - 1);
response[response_size - 1] = '\0';
}
free(resp_data);
return status_code;
}
/* --- File utilities --- */
/*
* Read entire file into malloc'd buffer. Returns NULL on error.
* Sets *size_out to file size if non-NULL.
*/
char *git_read_file(filepath, size_out)
char *filepath;
long *size_out;
{
int fd;
struct stat st;
char *buf;
long n, total;
if (size_out) *size_out = 0;
if (stat(filepath, &st) < 0) return NULL;
if (!S_ISREG(st.st_mode)) return NULL;
fd = open(filepath, O_RDONLY);
if (fd < 0) return NULL;
buf = (char *)malloc(st.st_size + 1);
if (!buf) {
close(fd);
return NULL;
}
total = 0;
while (total < st.st_size) {
n = read(fd, buf + total, st.st_size - total);
if (n <= 0) break;
total += n;
}
close(fd);
buf[total] = '\0';
if (size_out) *size_out = total;
return buf;
}
/*
* Write data to file. Creates/overwrites. Returns 0 on success, -1 on error.
*/
int git_write_file(filepath, data, size)
char *filepath;
char *data;
long size;
{
int fd;
long n, total;
fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0) return -1;
total = 0;
while (total < size) {
n = write(fd, data + total, size - total);
if (n <= 0) {
close(fd);
return -1;
}
total += n;
}
close(fd);
return 0;
}
int git_is_directory(path)
char *path;
{
struct stat st;
if (stat(path, &st) < 0) return 0;
return S_ISDIR(st.st_mode) ? 1 : 0;
}
int git_is_file(path)
char *path;
{
struct stat st;
if (stat(path, &st) < 0) return 0;
return S_ISREG(st.st_mode) ? 1 : 0;
}
/*
* Create directory and all parents (like mkdir -p).
* Returns 0 on success, -1 on error.
*/
int git_mkdir_p(path)
char *path;
{
char tmp[GIT_MAX_PATH];
char *p;
int len;
strncpy(tmp, path, sizeof(tmp) - 1);
tmp[sizeof(tmp) - 1] = '\0';
len = strlen(tmp);
/* remove trailing slash */
if (len > 0 && tmp[len - 1] == '/')
tmp[len - 1] = '\0';
for (p = tmp + 1; *p; p++) {
if (*p == '/') {
*p = '\0';
if (!git_is_directory(tmp)) {
if (mkdir(tmp, 0755) < 0) return -1;
}
*p = '/';
}
}
if (!git_is_directory(tmp)) {
if (mkdir(tmp, 0755) < 0) return -1;
}
return 0;
}
/*
* Recursively scan directory for files.
* Skips entries starting with '.' (hidden files/dirs).
* Paths are stored relative to the initial dirpath.
* Returns number of files found.
*/
static int scan_dir_recursive(basepath, relpath, paths, max_entries, count)
char *basepath;
char *relpath;
char paths[][GIT_MAX_PATH];
int max_entries;
int count;
{
DIR *dir;
struct direct *entry;
char fullpath[GIT_MAX_PATH];
char newrel[GIT_MAX_PATH];
struct stat st;
if (relpath[0]) {
safe_snprintf(fullpath, sizeof(fullpath), "%s/%s", basepath, relpath);
} else {
strncpy(fullpath, basepath, sizeof(fullpath) - 1);
fullpath[sizeof(fullpath) - 1] = '\0';
}
dir = opendir(fullpath);
if (!dir) return count;
while ((entry = readdir(dir)) != NULL) {
if (count >= max_entries) break;
/* skip . and .. and hidden files */
if (entry->d_name[0] == '.') continue;
/* build full path for stat */
if (relpath[0]) {
safe_snprintf(newrel, sizeof(newrel), "%s/%s", relpath, entry->d_name);
} else {
strncpy(newrel, entry->d_name, sizeof(newrel) - 1);
newrel[sizeof(newrel) - 1] = '\0';
}
safe_snprintf(fullpath, sizeof(fullpath), "%s/%s", basepath, newrel);
if (stat(fullpath, &st) < 0) continue;
if (S_ISDIR(st.st_mode)) {
/* recurse into subdirectory */
count = scan_dir_recursive(basepath, newrel, paths,
max_entries, count);
} else if (S_ISREG(st.st_mode)) {
strncpy(paths[count], newrel, GIT_MAX_PATH - 1);
paths[count][GIT_MAX_PATH - 1] = '\0';
count++;
}
}
closedir(dir);
return count;
}
int git_scan_directory(dirpath, paths, max_entries)
char *dirpath;
char paths[][GIT_MAX_PATH];
int max_entries;
{
return scan_dir_recursive(dirpath, "", paths, max_entries, 0);
}
/* --- Simple SHA1-like hash for file content comparison --- */
/* We use CRC32 as a quick content hash since real SHA1 is heavy */
static unsigned long crc32_table[256];
static int crc32_inited = 0;
static void crc32_init()
{
unsigned long c;
int n, k;
for (n = 0; n < 256; n++) {
c = (unsigned long)n;
for (k = 0; k < 8; k++) {
if (c & 1)
c = 0xEDB88320L ^ (c >> 1);
else
c = c >> 1;
}
crc32_table[n] = c;
}
crc32_inited = 1;
}
static unsigned long crc32_calc(data, len)
char *data;
long len;
{
unsigned long crc;
long i;
if (!crc32_inited) crc32_init();
crc = 0xFFFFFFFFL;
for (i = 0; i < len; i++) {
crc = crc32_table[(crc ^ (unsigned char)data[i]) & 0xFF] ^ (crc >> 8);
}
return crc ^ 0xFFFFFFFFL;
}
/* Format CRC32 as 8-char hex, pad to look sha-ish */
static void content_hash(data, len, out)
char *data;
long len;
char *out;
{
unsigned long crc;
char hex[] = "0123456789abcdef";
int i;
crc = crc32_calc(data, len);
/* produce 8-char hex from CRC32, pad with zeros to fill GIT_MAX_SHA-1 chars */
for (i = 0; i < 8; i++) {
out[7 - i] = hex[crc & 0x0F];
crc >>= 4;
}
/* pad remainder with zeros to make it look like a SHA */
for (i = 8; i < 40; i++) {
out[i] = '0';
}
out[40] = '\0';
}
/* --- State persistence --- */
/*
* State file format (.nextstep_git):
* Line 1: owner
* Line 2: repo
* Line 3: branch
* Line 4: head_sha
* Line 5: tree_sha
* Line 6: file_count
* Lines 7+: path\tsha\tstatus\tsize (one per file)
*/
GitResult git_load_state(r, out)
GitRepo *r;
GitFileList *out;
{
GitResult result;
char statepath[GIT_MAX_PATH];
char *data;
long fsize;
char *line, *next;
int line_num, i;
int file_count;
char *tab1, *tab2, *tab3;
result.code = GIT_OK;
result.message[0] = '\0';
result.files_affected = 0;
out->count = 0;
safe_snprintf(statepath, sizeof(statepath), "%s/%s", r->local_path, GIT_STATE_FILE);
data = git_read_file(statepath, &fsize);
if (!data) {
result.code = GIT_ERR_STATE;
safe_snprintf(result.message, sizeof(result.message), "No state file found (not a cloned repo?)");
return result;
}
line_num = 0;
file_count = 0;
line = data;
while (line && *line) {
/* find end of line */
next = strchr(line, '\n');
if (next) {
*next = '\0';
next++;
}
/* strip CR */
{
int ll = strlen(line);
if (ll > 0 && line[ll-1] == '\r')
line[ll-1] = '\0';
}
line_num++;
if (line_num == 1) {
strncpy(r->owner, line, GIT_MAX_OWNER - 1);
} else if (line_num == 2) {
strncpy(r->repo, line, GIT_MAX_REPO - 1);
} else if (line_num == 3) {
strncpy(r->branch, line, GIT_MAX_BRANCH - 1);
} else if (line_num == 4) {
strncpy(r->head_sha, line, GIT_MAX_SHA - 1);
} else if (line_num == 5) {
strncpy(r->tree_sha, line, GIT_MAX_SHA - 1);
} else if (line_num == 6) {
file_count = atoi(line);
} else {
/* parse file entry: path\tsha\tstatus\tsize */
i = out->count;
if (i >= GIT_MAX_FILES) break;
tab1 = strchr(line, '\t');
if (!tab1) goto next_line;
*tab1 = '\0';
tab1++;
tab2 = strchr(tab1, '\t');
if (!tab2) goto next_line;
*tab2 = '\0';
tab2++;
tab3 = strchr(tab2, '\t');
if (!tab3) goto next_line;
*tab3 = '\0';
tab3++;