-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
629 lines (624 loc) · 19.2 KB
/
Copy pathmain.c
File metadata and controls
629 lines (624 loc) · 19.2 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <openssl/sha.h>
#include <zlib.h>
#include <dirent.h>
#include "gitflex.h"
struct tree_entry {
char mode[8]; // File mode (e.g., "100644" for regular file)
char name[256]; // Entry name
char hash[41]; // SHA-1 hash (40 hex chars + null terminator)
};
//int compare_entries(const void *a, const void *b) {
// return strcmp(((struct tree_entry *)a)->name, ((struct tree_entry *)b)->name);
//}
//
//void runInit() {
// struct stat st;
//
// if (stat(".gitflex", &st) == 0) {
// printf("Error: .gitflex already initialized.\n");
// return;
// }
//
// if (mkdir(".gitflex", 0777) == -1) {
// perror("Error creating .gitflex");
// return;
// }
//
// mkdir(".gitflex/objects", 0777);
// mkdir(".gitflex/refs", 0777);
// mkdir(".gitflex/refs/heads", 0777);
//
// FILE *headFile = fopen(".gitflex/HEAD", "w");
// if (headFile) {
// fprintf(headFile, "ref: refs/heads/main\n");
// fclose(headFile);
// } else {
// perror("Error creating .gitflex/HEAD");
// }
//
// printf("Initialized empty GitFlex repository in .gitflex/\n");
//}
//
//void runHashObject(const char *fileName) {
// if(fileName == NULL)
// {
// printf("Provide a file name.\n");
// return;
// }
//
// struct stat st;
//
// if (stat(fileName, &st) == -1) {
// printf("Error: file doesn\'t exist.\n");
// return;
// }
//
// int fileSize = st.st_size;
// if (fileSize == 0) {
// printf("Error: file is empty.\n");
// return;
// }
//
// FILE *file = fopen(fileName, "rb");
// if (file) {
// char *buffer = malloc(fileSize);
// if (buffer == NULL) {
// perror("Error allocating memory");
// fclose(file);
// return;
// }
// fread(buffer, sizeof(char), fileSize, file);
// fclose(file);
//
// unsigned char hash[SHA_DIGEST_LENGTH];
// SHA1((unsigned char *)buffer, fileSize, hash);
// unsigned char hexHash[SHA_DIGEST_LENGTH * 2 + 1];
// for (int i = 0; i < SHA_DIGEST_LENGTH; i++) {
// sprintf((char*)hexHash + (i * 2), "%02x", hash[i]);
// }
// hexHash[SHA_DIGEST_LENGTH * 2] = '\0';
//
//// printf("%s", hexHash);
//
// char dirPath[256];
// snprintf(dirPath, sizeof(dirPath), ".gitflex/objects/%c%c/", hexHash[0], hexHash[1]);
// if (mkdir(dirPath, 0777) == -1) {
//// perror("Error creating directory");
// } else {
// printf("Directory %s created successfully\n", dirPath);
// }
//
// char filePath[256];
// snprintf(filePath, sizeof(filePath), ".gitflex/objects/%c%c/%s", hexHash[0], hexHash[1], hexHash + 2);
//
//
// char header[64];
// snprintf(header, sizeof(header), "blob %d", fileSize);
// size_t headerLen = strlen(header) + 1;
// char *blob = malloc(headerLen + fileSize);
// if (!blob) {
// perror("Error allocating memory");
// return;
// }
// memcpy(blob, header, headerLen);
// memcpy(blob + headerLen, buffer, fileSize);
//
//
// uLong compressedSize = compressBound(headerLen + fileSize);
// char *compressedBlob = malloc(compressedSize);
// if (!compressedBlob) {
// perror("Error allocating memory for compression");
// free(blob);
// return;
// }
// if (compress((Bytef *)compressedBlob, &compressedSize, (Bytef *)blob, headerLen + fileSize) != Z_OK) {
// perror("Error compressing blob");
// free(blob);
// free(compressedBlob);
// return;
// }
//
//
// FILE *outFile = fopen(filePath, "wb");
// if (outFile) {
// fwrite(compressedBlob, sizeof(unsigned char), compressedSize, outFile);
// fclose(outFile);
// printf("File %s created successfully\n", filePath);
// } else {
// perror("Error creating file");
// }
//
// free(buffer);
// free(blob);
// free(compressedBlob);
// } else {
// perror("Error reding file.\n");
// }
//
//
//}
//
//void runCatFile(const char *hash) {
// if(hash == NULL)
// {
// printf("Provide a hash.\n");
// return;
// }
//
// struct stat st;
//
// char dirPath[256];
// snprintf(dirPath, sizeof(dirPath), ".gitflex/objects/%c%c/%s", hash[0], hash[1], hash+2);
// if (stat(dirPath, &st) == -1) {
// perror("Error finding directory");
// }
//
// FILE *file = fopen(dirPath, "rb");
// int fileSize = st.st_size;
// if (file) {
// char *buffer = malloc(fileSize);
// if (buffer == NULL) {
// perror("Error allocating memory");
// fclose(file);
// return;
// }
// fread(buffer, sizeof(char), fileSize, file);
// fclose(file);
//
//
//// uLong ucompSize = strlen(buffer)+1;
// uLong ucompSize = fileSize * 10;
// uLong compSize = compressBound(ucompSize);
// char *outBuffer = malloc(compSize);
//
// if (outBuffer == NULL) {
// perror("Error allocating memory for decompressed content");
// free(buffer);
// return;
// }
//
// int ret = uncompress((Bytef *)outBuffer, &ucompSize, (Bytef *)buffer, compSize);
// if (ret != Z_OK) {
// printf("Error decompressing data.\n");
// free(buffer);
// free(outBuffer);
// return;
// }
// char *content = outBuffer;
// if (strncmp(content, "blob ", 5) != 0) {
// content += 5;
// int contentSize = 0;
// while (*content >= '0' && *content <= '9') {
// contentSize = contentSize * 10 + (*content - '0');
// content++;
// }
//
// if (*content == '\0') {
// content++;
// } else {
// printf("Error: Expected null byte after size.\n");
// free(buffer);
// free(outBuffer);
// return;
// }
//
// printf("Content size: %d bytes\n", contentSize);
// printf("Actual content: %s\n", content);
// }
// else {
// printf("Error: Invalid object format.\n");
// }
//
// free(buffer);
// free(outBuffer);
// }
//}
//
///*
//char* runWriteTree(const char* dir) {
// DIR *dirp;
// struct dirent *dp;
// struct stat st;
// struct tree_entry *entries = NULL;
// int entry_count = 0;
// int entries_capacity = 10;
//
// entries = malloc(entries_capacity * sizeof(struct tree_entry));
// if (!entries) {
// perror("Failed to allocate memory for tree entries");
// return NULL;
// }
//
// if ((dirp = opendir(dir)) == NULL) {
// fprintf(stderr, "Couldn't open %s: ", dir);
// perror("");
// return NULL;
// }
//
// while ((dp = readdir(dirp)) != NULL) {
// if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0 || strcmp(dp->d_name, ".gitflex") == 0)
// continue;
//
// char path[512];
// snprintf(path, sizeof(path), "%s/%s", dir, dp->d_name);
//
// if (stat(path, &st) != 0)
// {
// perror("stat failed");
// continue;
// }
//
// if (entry_count >= entries_capacity) {
// entries_capacity *= 2;
// struct tree_entry *new_entries = realloc(entries,
// entries_capacity * sizeof(struct tree_entry));
// if (!new_entries) {
// perror("Failed to resize entries array");
// free(entries);
// closedir(dirp);
// return NULL;
// }
// entries = new_entries;
// }
//
// if (S_ISDIR(st.st_mode)) {
// printf("%s is a directory\n", path);
//
// char *subdir_hash = runWriteTree(path);
// if (subdir_hash) {
// // Add tree entry
// strcpy(entries[entry_count].mode, "40000");
// strcpy(entries[entry_count].name, dp->d_name);
// strcpy(entries[entry_count].hash, subdir_hash);
// entry_count++;
// free(subdir_hash);
// }
//
// } else if (S_ISREG(st.st_mode)) {
// printf("%s is a file\n", path);
//
// // Hash the file
// char *file_hash = hashTree(path);
// if (file_hash) {
// // Set the file mode based on permissions
// if (st.st_mode & S_IXUSR) {
// strcpy(entries[entry_count].mode, "100755"); // Executable file
// } else {
// strcpy(entries[entry_count].mode, "100644"); // Regular file
// }
// strcpy(entries[entry_count].name, dp->d_name);
// strcpy(entries[entry_count].hash, file_hash);
// entry_count++;
// free(file_hash); // Free the allocated hash
// }
// }
//
// }
// closedir(dirp);
// if (entry_count == 0) {
// free(entries);
// return NULL;
// }
//
// // Sort entries by name (Git requirement)
// qsort(entries, entry_count, sizeof(struct tree_entry), compare_entries);
//
// // Create and store the tree object
// char *tree_hash = hashTree(entries, entry_count);
//
// // Clean up
// free(entries);
//
// // Return the hash of the created tree
// return tree_hash;
//}
//*/
//
//char* getHashFromFile(const char *path) {
// struct stat st;
//
// if (stat(path, &st) == -1) {
// printf("Error: file doesn't exist.\n");
// return NULL;
// }
//
// int fileSize = st.st_size;
// if (fileSize == 0) {
// printf("Error: file is empty.\n");
// return NULL;
// }
//
// FILE *file = fopen(path, "rb");
// if (!file) {
// perror("Error reading file");
// return NULL;
// }
//
// char *buffer = malloc(fileSize);
// if (buffer == NULL) {
// perror("Error allocating memory");
// fclose(file);
// return NULL;
// }
// fread(buffer, sizeof(char), fileSize, file);
// fclose(file);
//
// unsigned char hash[SHA_DIGEST_LENGTH];
// SHA1((unsigned char *)buffer, fileSize, hash);
//
// char *hexHash = malloc(SHA_DIGEST_LENGTH * 2 + 1);
// if (hexHash == NULL) {
// perror("Error allocating memory for hash");
// free(buffer);
// return NULL;
// }
//
// for (int i = 0; i < SHA_DIGEST_LENGTH; i++) {
// sprintf((char*)hexHash + (i * 2), "%02x", hash[i]);
// }
// hexHash[SHA_DIGEST_LENGTH * 2] = '\0';
//
// // Also store the object using your runHashObject function
// runHashObject(path);
//
// free(buffer);
// return hexHash;
//}
//
//char* runWriteTree(const char* dir) {
// DIR *dirp;
// struct dirent *dp;
// struct stat st;
//
// // Create an array to store entries
// struct tree_entry *entries = malloc(100 * sizeof(struct tree_entry)); // Start with space for 100 entries
// if (!entries) {
// perror("Failed to allocate memory for tree entries");
// return NULL;
// }
// int entry_count = 0;
//
// if ((dirp = opendir(dir)) == NULL) {
// fprintf(stderr, "Couldn't open %s: ", dir);
// perror("");
// free(entries);
// return NULL;
// }
//
// while ((dp = readdir(dirp)) != NULL) {
// // Skip . and .. directories
// if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)
// continue;
//
// // Skip .gitflex directory
// if (strcmp(dp->d_name, ".gitflex") == 0)
// continue;
//
// char path[512];
// snprintf(path, sizeof(path), "%s/%s", dir, dp->d_name);
//
// if (stat(path, &st) == 0) {
// if (S_ISDIR(st.st_mode)) {
// printf("%s is a directory\n", path);
//
// // Recursively process subdirectory and get its tree hash
// char *subdir_hash = runWriteTree(path);
// if (subdir_hash) {
// // Store directory entry
// strcpy(entries[entry_count].mode, "40000");
// strcpy(entries[entry_count].name, dp->d_name);
// strcpy(entries[entry_count].hash, subdir_hash);
// entry_count++;
// free(subdir_hash);
// }
// } else if (S_ISREG(st.st_mode)) {
// printf("%s is a file\n", path);
//
// // Get file hash and store the blob
// char *file_hash = getHashFromFile(path);
// if (file_hash) {
// // Determine file mode (executable or regular)
// if (st.st_mode & S_IXUSR) {
// strcpy(entries[entry_count].mode, "100755"); // Executable
// } else {
// strcpy(entries[entry_count].mode, "100644"); // Regular file
// }
// strcpy(entries[entry_count].name, dp->d_name);
// strcpy(entries[entry_count].hash, file_hash);
// entry_count++;
// free(file_hash);
// }
// }
// } else {
// perror("stat failed");
// }
// }
// closedir(dirp);
//
// // If no entries, return NULL
// if (entry_count == 0) {
// free(entries);
// return NULL;
// }
//
// // Sort entries by name (Git requirement)
// qsort(entries, entry_count, sizeof(struct tree_entry), compare_entries);
//
// // Now build the tree content
// // First calculate the size
// size_t total_size = 0;
// for (int i = 0; i < entry_count; i++) {
// // Format: [mode] [space] [name] [null byte] [20-byte SHA-1 binary hash]
// total_size += strlen(entries[i].mode) + 1 + strlen(entries[i].name) + 1 + 20;
// }
//
// // Allocate memory for tree content
// unsigned char *content = malloc(total_size);
// if (!content) {
// perror("Failed to allocate memory for tree content");
// free(entries);
// return NULL;
// }
//
// // Build tree content
// unsigned char *p = content;
// for (int i = 0; i < entry_count; i++) {
// // Add mode and space
// size_t mode_len = strlen(entries[i].mode);
// memcpy(p, entries[i].mode, mode_len);
// p += mode_len;
// *p++ = ' ';
//
// // Add name and null byte
// size_t name_len = strlen(entries[i].name);
// memcpy(p, entries[i].name, name_len);
// p += name_len;
// *p++ = '\0';
//
// // Convert hash from hex to binary and add
// unsigned char binary_hash[20];
// for (int j = 0; j < 20; j++) {
// sscanf(&entries[i].hash[j*2], "%2hhx", &binary_hash[j]);
// }
// memcpy(p, binary_hash, 20);
// p += 20;
// }
//
// // Create header for tree object (like your blob header)
// char header[64];
// snprintf(header, sizeof(header), "tree %zu", total_size);
// size_t headerLen = strlen(header) + 1; // +1 for null byte
//
// // Create the full object with header + content
// unsigned char *tree_obj = malloc(headerLen + total_size);
// if (!tree_obj) {
// perror("Failed to allocate memory for tree object");
// free(content);
// free(entries);
// return NULL;
// }
// memcpy(tree_obj, header, headerLen);
// memcpy(tree_obj + headerLen, content, total_size);
//
// // Hash the tree object
// unsigned char hash[SHA_DIGEST_LENGTH];
// SHA1(tree_obj, headerLen + total_size, hash);
//
// // Convert hash to hex
// char *hexHash = malloc(SHA_DIGEST_LENGTH * 2 + 1);
// if (!hexHash) {
// perror("Failed to allocate memory for hash");
// free(content);
// free(tree_obj);
// free(entries);
// return NULL;
// }
//
// for (int i = 0; i < SHA_DIGEST_LENGTH; i++) {
// sprintf((char*)hexHash + (i * 2), "%02x", hash[i]);
// }
// hexHash[SHA_DIGEST_LENGTH * 2] = '\0';
//
// // Create directory for the object
// char dirPath[256];
// snprintf(dirPath, sizeof(dirPath), ".gitflex/objects/%c%c/", hexHash[0], hexHash[1]);
// if (mkdir(dirPath, 0777) == -1 /*&& errno != EEXIST*/) {
//// perror("Error creating directoryy");
// } else {
// printf("Directory %s created successfully\n", dirPath);
// }
//
// // Create file path
// char filePath[256];
// snprintf(filePath, sizeof(filePath), ".gitflex/objects/%c%c/%s", hexHash[0], hexHash[1], hexHash + 2);
//
// // Compress the tree object
// uLong compressedSize = compressBound(headerLen + total_size);
// unsigned char *compressedTree = malloc(compressedSize);
// if (!compressedTree) {
// perror("Error allocating memory for compression");
// free(content);
// free(tree_obj);
// free(hexHash);
// free(entries);
// return NULL;
// }
//
// if (compress((Bytef *)compressedTree, &compressedSize, (Bytef *)tree_obj, headerLen + total_size) != Z_OK) {
// perror("Error compressing tree");
// free(content);
// free(tree_obj);
// free(compressedTree);
// free(hexHash);
// free(entries);
// return NULL;
// }
//
// // Write compressed tree object to file
// FILE *outFile = fopen(filePath, "wb");
// if (outFile) {
// fwrite(compressedTree, sizeof(unsigned char), compressedSize, outFile);
// fclose(outFile);
// printf("Tree object stored: %s\n", hexHash);
// } else {
// perror("Error creating file");
// free(content);
// free(tree_obj);
// free(compressedTree);
// free(hexHash);
// free(entries);
// return NULL;
// }
//
// // Clean up
// free(content);
// free(tree_obj);
// free(compressedTree);
// free(entries);
//
// return hexHash;
//}
int main(int argc, char* argv[]) {
if (argc > 1) {
char* command = argv[1];
if (!strcmp(command, "init")) {
runInit();
}
else if (!strcmp(command, "hash-object")) {
if (argc > 2) {
runHashObject(argv[2]);
} else {
printf("Error: Please provide a file name.\n");
}
}
else if (!strcmp(command, "cat-file")) {
if (argc > 2) {
runCatFile(argv[2]);
} else {
printf("Error: Please provide a hash.\n");
}
}
else if (!strcmp(command, "write-tree")) {
char *tree_hash = runWriteTree(".");
if (tree_hash) {
printf("%s\n", tree_hash);
free(tree_hash);
}
}
else {
printf("Error: Unknown command '%s'\n", command);
}
} else {
printf("Usage: gitflex <command> [args]\n");
printf("Commands:\n");
printf(" init Initialize a new repository\n");
printf(" hash-object <file> Hash a file\n");
printf(" cat-file <hash> Show contents of a file\n");
printf(" write-tree Write a tree object\n");
}
return 0;
}