From 1681b9ad0021e0f72bd2f2a4e59e51b9bcdded02 Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Fri, 25 Jun 2021 21:27:53 -0400 Subject: Started work on `create_info()`. --- git.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/git.c b/git.c index bd4fb4b..04c088f 100644 --- a/git.c +++ b/git.c @@ -68,6 +68,53 @@ pull_request *get_pull_request(int id, const char *root) { } +int create_info(pull_request *pr, const char *pr_dir) { + int len = strlen(pr_dir) + strlen("/info"); + char *info = calloc(len+1, sizeof(char)); + char *info_buf; + FILE *fp; + + /* Append /info to the PR directory. */ + sprintf(info, "%s/info", pr_dir); + + /* TODO: Write the rest of the code. */ + + /* Is there already an info file? */ + if (access(info, F_OK) == 0) { + long size = 0; + char *buf = read_file(info, &size); + + log(LOG_INFO, "Info file already exists."); + + /* Did we read the file? */ + if (buf != NULL) { + /* Are the contents of the info file the same? */ + if (strcmp(info_buf, buf) == 0) { + log(LOG_INFO, "New info file is the same as the existing one."); + free(info); + free(info_buf); + free(buf); + return 2; + } + free(buf); + } else { + log(LOG_ERR, "Couldn't open existing info file."); + return 0; + } + } + + /* Open the info file. */ + fp = fopen(pr_dir, "w"); + + /* Did we fail to open the file? */ + if (fp == NULL) { + log(LOG_ERR, "Failed to open info file."); + return 0; + } + + return 1; +} + int create_pull_request_dir(pull_request *pr, int id, const char *root) { int ret = id; struct stat st; -- cgit v1.2.3-13-gbd6f