diff options
author | EuAndreh <eu@euandre.org> | 2021-08-23 07:41:55 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2021-08-23 07:41:55 -0300 |
commit | 35cead0b83da84c037bcf79ce112cb5e59f56b10 (patch) | |
tree | c897348af5bf2cad5e122840ba95a198386eff5c /src | |
parent | TODOs.md: Extend description of #task-4e40832e-78cf-fc21-cbf9-2fe00fd3828d (diff) | |
download | gistatic-35cead0b83da84c037bcf79ce112cb5e59f56b10.tar.gz gistatic-35cead0b83da84c037bcf79ce112cb5e59f56b10.tar.xz |
src/: Move logerr* functions to src/logerr.{c,h}; forward tarballs_fd to src/tar.c
Diffstat (limited to '')
-rw-r--r-- | src/lib.c | 73 | ||||
-rw-r--r-- | src/logerr.c | 64 | ||||
-rw-r--r-- | src/logerr.h | 31 | ||||
-rw-r--r-- | src/tar.c | 84 | ||||
-rw-r--r-- | src/tar.h | 7 |
5 files changed, 220 insertions, 39 deletions
@@ -1,4 +1,5 @@ #include "config.h" +#include "logerr.h" #include "tar.h" #include "gistatic.h" @@ -296,16 +297,12 @@ static const char *const PROJECT_HOMEPAGE_LINK = "<a href=\"https://euandreh.xyz/" PROGNAME "\">" PROGNAME "</a>."; -static void logerr(const char *const s, const char *const msg, int lineno) { - fprintf( - stderr, - "%s:%s:%d: %s: %s\n", - PROGNAME, - __FILE__, - lineno, - s, - msg - ); +static void logerr( + const char *const s, + const char *const msg, + const int lineno +) { + logerr_file(s, msg, __FILE__, lineno); } static void logerrs( @@ -313,19 +310,9 @@ static void logerrs( const char *const mid, const char *const post, const char *const msg, - int lineno + const int lineno ) { - fprintf( - stderr, - "%s:%s:%d: %s%s%s: %s\n", - PROGNAME, - __FILE__, - lineno, - pre, - mid, - post, - msg - ); + logerrs_file(pre, mid, post, msg, __FILE__, lineno); } static void logerrl( @@ -333,21 +320,12 @@ static void logerrl( const size_t mid, const char *const post, const char *const msg, - int lineno + const int lineno ) { - fprintf( - stderr, - "%s:%s:%d: %s%ld%s: %s\n", - PROGNAME, - __FILE__, - lineno, - pre, - mid, - post, - msg - ); + logerrl_file(pre, mid, post, msg, __FILE__, lineno); } + static const char *_(int msg_id) { if (!catalog_descriptor || catalog_descriptor == (nl_catd)-1) { return MSGS[msg_id]; @@ -1960,12 +1938,15 @@ cleanup: static int repo_tarballs_refs_each( struct git_repository *const repo, struct git_reference *const ref, + const char *const tarballs_dir, const char *const project_name ) { int ret = 0; char *out = NULL; struct git_commit *commit = NULL; struct git_tree *tree = NULL; + char *tar_path = NULL; + FILE *tar_fd = NULL; const bool is_tag = git_reference_is_tag(ref); const bool is_branch = git_reference_is_branch(ref); @@ -2026,12 +2007,31 @@ static int repo_tarballs_refs_each( goto cleanup; } - if (tarball_write_from_directory(out)) { + tar_path = strsjoin((const char *const []) + { tarballs_dir, "/", project_name, "-", name, ".tar", NULL }); + if (!tar_path) { + ret = -1; + goto cleanup; + } + + if (!(tar_fd = fopen(tar_path, "w"))) { + logerrs("fopen(\"", tar_path, "\")", strerror(errno), __LINE__); + ret = -1; + goto cleanup; + } + + if (tarball_write_from_directory(tar_fd, out)) { ret = -1; goto cleanup; } cleanup: + if (tar_fd && fclose(tar_fd)) { + logerrs("fclose(\"", tar_path, "\")", strerror(errno), + __LINE__); + ret = -1; + } + free(tar_path); git_tree_free(tree); git_commit_free(commit); free(out); @@ -2073,7 +2073,8 @@ static int repo_tarballs_write( struct git_reference *ref; while (!(e = git_reference_next(&ref, ref_iter))) { - e = repo_tarballs_refs_each(repo, ref, project_name); + e = repo_tarballs_refs_each(repo, ref, + tarballs_dir, project_name); git_reference_free(ref); if (e == -1) { ret = -1; diff --git a/src/logerr.c b/src/logerr.c new file mode 100644 index 0000000..ba27b35 --- /dev/null +++ b/src/logerr.c @@ -0,0 +1,64 @@ +#include "config.h" +#include "logerr.h" + +#include <stdio.h> + + +void logerr_file( + const char *const s, + const char *const msg, + const char *const file, + const int lineno +) { + fprintf( + stderr, + "%s:%s:%d: %s: %s\n", + PROGNAME, + file, + lineno, + s, + msg + ); +} + +void logerrs_file( + const char *const pre, + const char *const mid, + const char *const post, + const char *const msg, + const char *const file, + const int lineno +) { + fprintf( + stderr, + "%s:%s:%d: %s%s%s: %s\n", + PROGNAME, + file, + lineno, + pre, + mid, + post, + msg + ); +} + +void logerrl_file( + const char *const pre, + const size_t mid, + const char *const post, + const char *const msg, + const char *const file, + int lineno +) { + fprintf( + stderr, + "%s:%s:%d: %s%ld%s: %s\n", + PROGNAME, + file, + lineno, + pre, + mid, + post, + msg + ); +} diff --git a/src/logerr.h b/src/logerr.h new file mode 100644 index 0000000..08ad682 --- /dev/null +++ b/src/logerr.h @@ -0,0 +1,31 @@ +#ifndef GISTATIC_LOGERR_H +#define GISTATIC_LOGERR_H + +#include <stddef.h> + +void logerr_file( + const char *const s, + const char *const msg, + const char *const file, + const int lineno +); + +void logerrs_file( + const char *const pre, + const char *const mid, + const char *const post, + const char *const msg, + const char *const file, + const int lineno +); + +void logerrl_file( + const char *const pre, + const size_t mid, + const char *const post, + const char *const msg, + const char *const file, + const int lineno +); + +#endif @@ -1,5 +1,26 @@ #include "config.h" #include "tar.h" +#include "logerr.h" + +#include <stdlib.h> +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdbool.h> + +/* + +About the ustar format: +- name[100] can only contain 100 bytes, while PATH_MAX's minimum value is 256 + for `_POSIX_PATH_MAX`, and 1024 for _XOPEN_PATH_MAX. Wait! Actually there is + a "prefix" field, which is probably what I'm looking for; +- characters are encoded in ISO/IEC 646:1991 [0] instead of UTF-8, even though + it requires the file names to be conformant to the POSIX filename + restrictions; + +[0]: https://en.wikipedia.org/wiki/ISO/IEC_646 + +*/ /* * Implementation of the "ustar archive tape" (tar) format, conformant to the @@ -8,9 +29,68 @@ * [0]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html#tag_20_92_13_06 */ -int tarball_write_from_directory(const char *const directory_path) { + +static void logerr( + const char *const s, + const char *const msg, + const int lineno +) { + logerr_file(s, msg, __FILE__, lineno); +} + +static void logerrs( + const char *const pre, + const char *const mid, + const char *const post, + const char *const msg, + const int lineno +) { + logerrs_file(pre, mid, post, msg, __FILE__, lineno); +} + +static void logerrl( + const char *const pre, + const size_t mid, + const char *const post, + const char *const msg, + const int lineno +) { + logerrl_file(pre, mid, post, msg, __FILE__, lineno); +} + + +struct TarEntry { + // What size the spec says? + unsigned int begin; +}; + +// static int tar_write(FILE *const fd, + +int tarball_write_from_directory( + FILE *const fd, + const char *const directory_path +) { + int ret = 0; + struct TarEntry *entry = NULL; (void)directory_path; - return 0; + (void)fd; + + if (!(entry = malloc(sizeof(struct TarEntry)))) { + logerrl("malloc(", sizeof(struct TarEntry), ")", + strerror(errno), __LINE__); + ret = -1; + goto cleanup; + } + + if (false) { + logerrs("", "", "", "", 0); + logerr("", "", 0); + } + + +cleanup: + free(entry); + return ret; } #ifdef TEST @@ -1,7 +1,12 @@ #ifndef GISTATIC_TAR_H #define GISTATIC_TAR_H -int tarball_write_from_directory(const char *const directory_path); +#include <stdio.h> + +int tarball_write_from_directory( + FILE *const fd, + const char *const directory_path +); #ifdef TEST void unit_tests_tar(void); |