-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblob.c
More file actions
38 lines (32 loc) · 1.04 KB
/
blob.c
File metadata and controls
38 lines (32 loc) · 1.04 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
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "blob.h"
#include "index.h"
#include "object.h"
#include "path.h"
void write_blob(char *filename) {
/* Open file to hash */
int fd;
fd = open(filename, O_RDONLY);
struct stat buf;
fstat(fd, &buf);
char *data = (char *) malloc((size_t) buf.st_size + 15);
sprintf(data, "blob %lu", (unsigned long) buf.st_size);
int left_part_len = strlen(data) + 1;
read(fd, data + left_part_len, buf.st_size);
int full_len = left_part_len + buf.st_size;
char *hash = hash_object(data, full_len);
char *path = get_repo_troll_dir();
path = (char *) realloc(path, strlen(path) + 49); // Path + "objects/" + 40 digit hash + null terminator
strcat(path, "objects/");
strncat(path, hash, 40);
/* Create object file */
int objectfd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0644);
write(objectfd, data, full_len);
/* Add new object to index */
add_to_index(hash, filename);
}