-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
executable file
·74 lines (64 loc) · 2.51 KB
/
util.h
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
/**
* This file contains utility functions to use in the homework. Do not modify!
*/
#ifndef _BEARGIT_UTIL_H_
#define _BEARGIT_UTIL_H_
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>
#include <limits.h>
#include <openssl/sha.h>
int fake_print(char* fmt, ...);
int fake_fprint(FILE* stream, char* fmt, ...);
int is_sane_path(const char* path);
/* In testing mode (initialized with -DTESTING fed to gcc and done automatically
* when you run make beargit-unittest), we need to replace printf and fprintf
* with "fakes" that redirect your output to files, which you can read from
* in your testing code.
*/
#ifdef TESTING
#define printf fake_print
#define fprintf fake_fprint
#endif
static const char* path = "";
static const char* dirname = "";
static const char* filename = "";
static const char* src = "";
static const char* dst = "";
#define PRINT_FILENAME(fn) \
if (strlen(fn) > 0) \
fprintf(stderr, " * %s = %s\n", # fn, fn);
#define ASSERT_ERROR_MESSAGE(fn, msg) \
if (!(fn)) { \
fprintf(stderr, "\n *** AN ERROR OCCURRED IN %s ***\n\n", __func__); \
fprintf(stderr, " This should not happen. Most likely, you tried to access\n"); \
fprintf(stderr, " or manipulate a file that doesn't exist, or create one\n"); \
fprintf(stderr, " that already does. You should first try to delete the\n"); \
fprintf(stderr, " .beargit directory and start over; maybe a previous run\n"); \
fprintf(stderr, " corrupted it. If not, you need to fix the problem in\n"); \
fprintf(stderr, " your program -- the following error message might give\n"); \
fprintf(stderr, " more information about what went wrong:\n\n"); \
fprintf(stderr, " Error in %s: %s\n\n", __func__, msg); \
fprintf(stderr, " Non-empty variables in scope:\n"); \
PRINT_FILENAME(path) \
PRINT_FILENAME(dirname) \
PRINT_FILENAME(filename) \
PRINT_FILENAME(src) \
PRINT_FILENAME(dst) \
fprintf(stderr, "\n"); \
exit(1); \
}
void fs_mkdir(const char* dirname);
void fs_rm(const char* filename);
void fs_force_rm_beargit_dir();
void fs_mv(const char* src, const char* dst);
void fs_cp(const char* src, const char* dst);
void write_string_to_file(const char* filename, const char* str);
void read_string_from_file(const char* filename, char* str, int size);
int fs_check_dir_exists(const char* dirname);
#define SHA_HEX_BYTES (SHA_DIGEST_LENGTH * 2)
void cryptohash(const char* str, char dst[SHA_HEX_BYTES + 1]);
#endif // _BEARGIT_UTIL_H_