-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_object.c
More file actions
59 lines (45 loc) · 1.18 KB
/
hash_object.c
File metadata and controls
59 lines (45 loc) · 1.18 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>
#include "hash_object.h"
#include "utils.h"
#define OBJECTS_DIR ".git_gud/objects"
void hash_object(const char *filename) {
unsigned char hash[SHA_DIGEST_LENGTH];
hash_file(filename, hash);
char hash_str[SHA_DIGEST_LENGTH * 2 + 1];
hash_to_string(hash, hash_str);
char path[1024];
snprintf(path, sizeof(path), "%s/%s", OBJECTS_DIR, hash_str);
if (create_dir_if_not_exists(OBJECTS_DIR) == -1) {
perror("mkdir");
exit(EXIT_FAILURE);
}
FILE *out = fopen(path, "wb");
if (!out) {
perror("fopen");
exit(EXIT_FAILURE);
}
FILE *file = fopen(filename, "rb");
if (!file) {
perror("fopen");
exit(EXIT_FAILURE);
}
fseek(file, 0, SEEK_END);
long size = ftell(file);
rewind(file);
unsigned char *buffer = malloc(size);
if (!buffer) {
perror("malloc");
fclose(file);
fclose(out);
exit(EXIT_FAILURE);
}
fread(buffer, 1, size, file);
fclose(file);
fwrite(buffer, 1, size, out);
fclose(out);
free(buffer);
printf("%s\n", hash_str);
}