Skip to content
Snippets Groups Projects
Commit 3d792a88 authored by Michal 'vorner' Vaner's avatar Michal 'vorner' Vaner
Browse files

Reading and writing the cache

parent 03fe0227
No related branches found
No related tags found
No related merge requests found
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <openssl/sha.h>
const char *read_line() {
size_t buf_size = 10;
......@@ -31,8 +38,47 @@ void dump_lines(const char *const *lines) {
fputs(*lines ++, stdout);
}
uint8_t last[SHA_DIGEST_LENGTH] = {0};
int main(int argc, const char *argv[]) {
if (!argv[1]) {
fprintf(stderr, "Need to know the cache file\n");
return 1;
}
int cache = open(argv[1], O_RDONLY);
if (cache == -1) {
if (errno != ENOENT) {
perror("Can't open the cache:");
return 1;
}
} else {
ssize_t result = read(cache, last, SHA_DIGEST_LENGTH);
if (result == -1) {
perror("Can't read the cache:");
return 1;
}
if (result != SHA_DIGEST_LENGTH) {
fputs("Corrupt cache\n", stderr);
return 1;
}
close(cache);
}
const char *const *lines = read_lines();
dump_lines(lines);
cache = open(argv[1], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
if (cache == -1) {
perror("Can't open cache for write:");
return 1;
}
ssize_t result = write(cache, last, SHA_DIGEST_LENGTH);
if (result == -1) {
perror("Can't write the cache:");
return 1;
}
if (result != SHA_DIGEST_LENGTH) {
fputs("Short write to cache\n", stderr);
return 1;
}
close(cache);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment