From 43d153c00bb067276d3cda141ad62de27cb4971d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yaroslav=20de=20la=20Pe=C3=B1a=20Smirnov?= Date: Thu, 26 Jan 2023 22:43:29 +0300 Subject: Cleanup & fix new album images not being rendered * Cleaned up some code and formatted with clang-format * Fix the result of what can only be explained as me having a brain-fart^1 leading me to write returns where they shouldn't be hence the image-walk loop returning early when new images were added to an album. * Also cleaned up and fixed a bug in roscha; more in roscha's own repo. 1: https://www.youtube.com/watch?v=UN7SBXJj2pc --- .clang-format | 90 ++++++++++++++++++++++++++++++++++ Makefile | 2 +- README.md | 4 -- include/components.h | 4 +- include/config.h | 16 +++--- include/fs.h | 14 +++--- include/log.h | 12 ++--- include/render.h | 8 +-- include/site.h | 2 +- roscha | 2 +- src/components.c | 22 ++++----- src/config.c | 80 ++++++++++++++++++------------ src/fs.c | 46 ++++++++--------- src/log.c | 2 +- src/render.c | 47 +++++++++--------- src/revela.c | 12 ++--- src/site.c | 136 ++++++++++++++++++++++++++++----------------------- 17 files changed, 306 insertions(+), 193 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..53912e6 --- /dev/null +++ b/.clang-format @@ -0,0 +1,90 @@ +--- +AlignAfterOpenBracket: Align +AlignArrayOfStructures: None +AlignConsecutiveAssignments: Consecutive +AlignConsecutiveBitFields: Consecutive +AlignConsecutiveDeclarations: Consecutive +AlignConsecutiveMacros: Consecutive +AlignEscapedNewlines: Left +AlignOperands: AlignAfterOperator +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortEnumsOnASingleLine: false +AllowShortBlocksOnASingleLine: Empty +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: WithoutElse +AllowShortLoopsOnASingleLine: false +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakAfterReturnType: AllDefinitions +BinPackArguments: true +BinPackParameters: true +BitFieldColonSpacing: Both +BreakBeforeBraces: Linux +BreakBeforeBinaryOperators: NonAssignment +BreakBeforeTernaryOperators: true +BreakStringLiterals: true + +# Actually should be 80, but clang-format is brain-dead and makes array +# initializers less readable by mangling the line breaks. +ColumnLimit: 0 + +DeriveLineEnding: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false + +ForEachMacros: + - 'vector_foreach' + - 'hmap_iter_foreach' + +IncludeBlocks: Preserve +IndentCaseLabels: false +IndentCaseBlocks: false +IndentGotoLabels: false +IndentPPDirectives: None +IndentExternBlock: NoIndent +IndentWidth: 4 +IndentWrappedFunctionNames: false +InsertTrailingCommas: None +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: false +MaxEmptyLinesToKeep: 1 + +# Taken from git's rules +PenaltyBreakAssignment: 10 +PenaltyBreakBeforeFirstCallParameter: 30 +PenaltyBreakComment: 10 +PenaltyBreakFirstLessLess: 0 +PenaltyBreakString: 10 +PenaltyExcessCharacter: 100 +PenaltyReturnTypeOnItsOwnLine: 60 + +PointerAlignment: Right +ReflowComments: true +SortIncludes: Never +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeParens: ControlStatements +SpaceAroundPointerQualifiers: Default +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 2 +SpacesInConditionalStatement: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParentheses: false +SpacesInSquareBrackets: false +SpaceBeforeSquareBrackets: false +Standard: Auto +TabWidth: 4 +UseCRLF: false +UseTab: AlignWithSpaces + diff --git a/Makefile b/Makefile index b91f3d2..96ce393 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC?=gcc XFLAGS=-D_XOPEN_SOURCE=500 -D_POSIX_C_SOURCE=200809L -CFLAGS?=-std=c11 -O2 -Wall $(XFLAGS) +CFLAGS?=-std=c11 -O2 -flto -Wall $(XFLAGS) LIBS:=-lexif LIBS+=$(shell pkg-config --cflags --libs GraphicsMagickWand) diff --git a/README.md b/README.md index bb10952..d5384e6 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,3 @@ system, or read the contents in `docs/` in the source. * Add exif tags to template hashmap. * Better test coverage? (if I am not too lazy). * Document templates. -* Improve performance? by generating html in a separate thread, e.g. send the - jobs to a queue in a separate thread in order to render at the same time as - images are converted with GraphicsMagick. Not sure if it would actually - improve performance or be worth the added complexity. diff --git a/include/components.h b/include/components.h index 88023cb..293ff17 100644 --- a/include/components.h +++ b/include/components.h @@ -45,7 +45,7 @@ struct image { struct roscha_object *map; /* hashmap with values to be passed to the thumbs vector */ struct roscha_object *thumb; - /* + /* * Whether this image was modified since the last time the gallery was * generated. */ @@ -100,7 +100,7 @@ int image_cmp(const void *a, const void *b); void image_destroy(struct image *); struct album *album_new(struct album_config *, struct site *, - const char *src, const char *rsrc, const struct stat *); + const char *src, const char *rsrc, const struct stat *); int album_cmp(const void *a, const void *b); diff --git a/include/config.h b/include/config.h index 085eb5e..3ba1cf7 100644 --- a/include/config.h +++ b/include/config.h @@ -4,21 +4,21 @@ #include #include -#define SITE_CONF "site.ini" +#define SITE_CONF "site.ini" #define ALBUM_CONF "album.ini" struct image_config { - bool strip; + bool strip; uint8_t quality; - size_t max_width; - size_t max_height; - bool smart_resize; - double blur; + size_t max_width; + size_t max_height; + bool smart_resize; + double blur; }; struct site_config { - char *title; - char *base_url; + char *title; + char *base_url; struct image_config images; struct image_config thumbnails; }; diff --git a/include/fs.h b/include/fs.h index 71c566b..6d405a4 100644 --- a/include/fs.h +++ b/include/fs.h @@ -14,12 +14,12 @@ enum nmkdir_res { NMKDIR_CREATED, }; -/* +/* * Returns a pointer to where the basename of the file is inside path. */ const char *rbasename(const char *path); -/* +/* * Makes a new directory if it doesn't exist. If there were errors returns * false, otherwise returns true. */ @@ -49,23 +49,23 @@ const char *delext(const char *restrict basename, char *restrict dest, size_t n) */ int file_is_uptodate(const char *path, const struct timespec *srcmtim); -/* +/* * Sets access and modification times to the time passed. */ void setdatetime(const char *path, const struct timespec *mtim); bool rmentry(const char *path, bool dry); -/* +/* * Recursively deletes extaneous files from directory, keeping files in the * preserved hmap. Returns -1 on error, number of deleted entries on success. * The number is not the total number of files on all subdirectories, but only * the number of files/dirs deleted from the directory pointed by path. */ ssize_t rmextra(const char *path, struct hmap *preserved, preremove_fn, - void *data, bool dry); + void *data, bool dry); -/* +/* * Copies file(s) truncating and overwritting the file(s) in the destination * path if they exist and are not a directory, or creating them if they don't * exist. If srcpath is a directory, copies all files within that directory @@ -73,6 +73,6 @@ ssize_t rmextra(const char *path, struct hmap *preserved, preremove_fn, * timestamps do not match. */ bool filesync(const char *restrict srcpath, const char *restrict dstpath, - struct hmap *preserved, bool dry); + struct hmap *preserved, bool dry); #endif diff --git a/include/log.h b/include/log.h index 2be35ee..c4a2430 100644 --- a/include/log.h +++ b/include/log.h @@ -29,27 +29,27 @@ void log_set_verbosity(enum log_level); void log_printf(enum log_level, const char *restrict fmt, ...); #ifdef DEBUG -#define log_print(v, fmt, ...) \ +#define log_print(v, fmt, ...) \ log_printf(v, "[%s] %s:%d " fmt, log_level_names[v], \ - __FILE__, __LINE__, ##__VA_ARGS__) + __FILE__, __LINE__, ##__VA_ARGS__) #else #define log_print(v, fmt, ...) \ log_printf(v, fmt, ##__VA_ARGS__) #endif #ifdef DEBUG -#define log_printl(v, fmt, ...) \ +#define log_printl(v, fmt, ...) \ log_printf(v, "[%s] %s:%d " fmt "\n", log_level_names[v], \ - __FILE__, __LINE__, ##__VA_ARGS__) + __FILE__, __LINE__, ##__VA_ARGS__) #else #define log_printl(v, fmt, ...) \ log_printf(v, fmt "\n", ##__VA_ARGS__) #endif #ifdef DEBUG -#define log_printl_errno(v, fmt, ...) \ +#define log_printl_errno(v, fmt, ...) \ log_printf(v, "[%s] %s:%d " fmt ": %s\n", log_level_names[v], \ - __FILE__, __LINE__, ##__VA_ARGS__, strerror(errno)) + __FILE__, __LINE__, ##__VA_ARGS__, strerror(errno)) #else #define log_printl_errno(v, fmt, ...) \ log_printf(v, fmt ": %s\n", ##__VA_ARGS__, strerror(errno)) diff --git a/include/render.h b/include/render.h index 24c931a..72c3cd2 100644 --- a/include/render.h +++ b/include/render.h @@ -24,7 +24,7 @@ struct index_template { struct roscha_object *years; }; -/* +/* * Variables for the album template and album elements in the albums vector of * the index template. */ @@ -77,15 +77,15 @@ struct render { bool render_make_index(struct render *, const char *path); bool render_make_album(struct render *r, const char *path, - const struct album *album); + const struct album *album); bool render_make_image(struct render *r, const char *path, - const struct image *image); + const struct image *image); bool render_set_album_vars(struct render *, struct album *); bool render_init(struct render *, const char *path, struct site_config *, - struct vector *albums); + struct vector *albums); void render_deinit(struct render *); diff --git a/include/site.h b/include/site.h index 0f45667..3980ea0 100644 --- a/include/site.h +++ b/include/site.h @@ -22,7 +22,7 @@ struct site { char *root_dir; char *output_dir; char *content_dir; - /* + /* * Indicates how many characters after the full root dir path of the input * directory the relative path of the albums + the content dir start. See * site_init() diff --git a/roscha b/roscha index 4665a62..49c2589 160000 --- a/roscha +++ b/roscha @@ -1 +1 @@ -Subproject commit 4665a620775da64ec7280762979a9fc6fa37c0bc +Subproject commit 49c2589427e0f81bea68ccba1a95c6890e10538d diff --git a/src/components.c b/src/components.c index 4d2afbd..08ab556 100644 --- a/src/components.c +++ b/src/components.c @@ -67,8 +67,8 @@ slugify(const char *path, const char *base_url, char **url) } static void -image_date_from_stat(struct image *image, const struct stat *pstat, - struct tm *date) +image_date_from_stat(struct image *image, const struct stat *pstat, + struct tm *date) { image->tstamp = pstat->st_mtim.tv_sec; localtime_r(&image->tstamp, date); @@ -93,15 +93,15 @@ image_set_date(struct image *image, const struct stat *pstat) } ExifEntry *entry = exif_content_get_entry( - image->exif_data->ifd[EXIF_IFD_EXIF], EXIF_TAG_DATE_TIME_ORIGINAL); + image->exif_data->ifd[EXIF_IFD_EXIF], EXIF_TAG_DATE_TIME_ORIGINAL); if (entry == NULL) { entry = exif_content_get_entry( image->exif_data->ifd[EXIF_IFD_EXIF], EXIF_TAG_DATE_TIME_DIGITIZED); if (entry == NULL) { log_printl(LOG_DEBUG, "No date exif tags present in %s", - image->source); + image->source); log_printl(LOG_DEBUG, "Using date from stat for file %s", - image->source); + image->source); image_date_from_stat(image, pstat, &date); goto out; } @@ -148,7 +148,7 @@ image_new(char *src, const struct stat *pstat, struct album *album) if ((image->ext = delext(image->basename, noext, NAME_MAX + 1)) == NULL) { log_printl(LOG_FATAL, "Can't read %s, file name too long", - image->basename); + image->basename); free(image); return NULL; } @@ -156,13 +156,12 @@ image_new(char *src, const struct stat *pstat, struct album *album) size_t relstart = album->slug - album->url; image->url = joinpath(album->url, noext); image->url_image = joinpath(image->url, image->basename); - image->url_thumb = malloc(strlen(image->url) + strlen(THUMB_SUFFIX) + - strlen(image->basename) + 2); + image->url_thumb = malloc(strlen(image->url) + strlen(THUMB_SUFFIX) + strlen(image->basename) + 2); image->dst = image->url + relstart; image->dst_image = image->url_image + relstart; image->dst_thumb = image->url_thumb + relstart; sprintf(image->url_thumb, "%s/%s" THUMB_SUFFIX "%s", image->url, - noext, image->ext); + noext, image->ext); image->exif_data = exif_data_new_from_file(image->source); image->modtime = pstat->st_mtim; @@ -201,7 +200,7 @@ image_destroy(struct image *image) struct album * album_new(struct album_config *conf, struct site *site, const char *src, - const char *rsrc, const struct stat *dstat) + const char *rsrc, const struct stat *dstat) { struct album *album = calloc(1, sizeof *album); if (album == NULL) { @@ -227,7 +226,6 @@ album_cmp(const void *va, const void *vb) struct album *a = *(struct album **)va, *b = *(struct album **)vb; /* Compare in descending order */ return (a->tstamp < b->tstamp) - (a->tstamp > b->tstamp); - } void @@ -257,7 +255,7 @@ album_destroy(struct album *album) } size_t i; struct image *img; - vector_foreach(album->images, i, img) { + vector_foreach (album->images, i, img) { image_destroy(img); } vector_free(album->images); diff --git a/src/config.c b/src/config.c index a570104..7c1d173 100644 --- a/src/config.c +++ b/src/config.c @@ -28,17 +28,21 @@ enum kv_handler_result { static int site_config_images_keyvalue_handler(struct parcini_line *parsed, - struct image_config *iconfig) + struct image_config *iconfig) { int res = CONFIG_KEY_BADKEY; if (!strcmp(parsed->key, "strip")) { res = parcini_value_handle(&parsed->value, PARCINI_VALUE_BOOLEAN, - &iconfig->strip) ? CONFIG_KEY_OK : CONFIG_KEY_BADVALUE; + &iconfig->strip) + ? CONFIG_KEY_OK + : CONFIG_KEY_BADVALUE; } if (!strcmp(parsed->key, "quality")) { long int temp; res = parcini_value_handle(&parsed->value, PARCINI_VALUE_INTEGER, - &temp) ? CONFIG_KEY_OK : CONFIG_KEY_BADVALUE; + &temp) + ? CONFIG_KEY_OK + : CONFIG_KEY_BADVALUE; if (res == CONFIG_KEY_OK) { if (temp > 100 || temp < 0) { res = CONFIG_KEY_BADVALUE; @@ -50,7 +54,9 @@ site_config_images_keyvalue_handler(struct parcini_line *parsed, if (!strcmp(parsed->key, "max_width")) { long int temp; res = parcini_value_handle(&parsed->value, PARCINI_VALUE_INTEGER, - &temp) ? CONFIG_KEY_OK : CONFIG_KEY_BADVALUE; + &temp) + ? CONFIG_KEY_OK + : CONFIG_KEY_BADVALUE; if (res == CONFIG_KEY_OK) { if (temp < 1) { res = CONFIG_KEY_BADVALUE; @@ -62,7 +68,9 @@ site_config_images_keyvalue_handler(struct parcini_line *parsed, if (!strcmp(parsed->key, "max_height")) { long int temp; res = parcini_value_handle(&parsed->value, PARCINI_VALUE_INTEGER, - &temp) ? CONFIG_KEY_OK : CONFIG_KEY_BADVALUE; + &temp) + ? CONFIG_KEY_OK + : CONFIG_KEY_BADVALUE; if (res == CONFIG_KEY_OK) { if (temp < 1) { res = CONFIG_KEY_BADVALUE; @@ -73,17 +81,21 @@ site_config_images_keyvalue_handler(struct parcini_line *parsed, } if (!strcmp(parsed->key, "smart_resize")) { res = parcini_value_handle(&parsed->value, PARCINI_VALUE_BOOLEAN, - &iconfig->smart_resize) ? CONFIG_KEY_OK : CONFIG_KEY_BADVALUE; + &iconfig->smart_resize) + ? CONFIG_KEY_OK + : CONFIG_KEY_BADVALUE; } if (!strcmp(parsed->key, "blur")) { long int temp; res = parcini_value_handle(&parsed->value, PARCINI_VALUE_INTEGER, - &temp) ? CONFIG_KEY_OK : CONFIG_KEY_BADVALUE; + &temp) + ? CONFIG_KEY_OK + : CONFIG_KEY_BADVALUE; if (res == CONFIG_KEY_OK) { if (temp < -100 || temp > 100) { res = CONFIG_KEY_BADVALUE; } else { - iconfig->blur = (double)temp/100; + iconfig->blur = (double)temp / 100; } } } @@ -102,7 +114,7 @@ site_config_keyvalue_handler(struct parcini_line *parsed, void *dst) subconf = site_config_images_keyvalue_handler(parsed, &config->images); } else if (!strcmp(parsed->section, "thumbnails")) { subconf = site_config_images_keyvalue_handler(parsed, - &config->thumbnails); + &config->thumbnails); } switch (subconf) { case CONFIG_KEY_OK: @@ -117,14 +129,16 @@ site_config_keyvalue_handler(struct parcini_line *parsed, void *dst) if (MATCHSK("", "title", parsed)) { free(config->title); return parcini_value_handle(&parsed->value, PARCINI_VALUE_STRING, - &config->title)? - KV_HANDLER_OK : KV_HANDLER_BADVALUE; + &config->title) + ? KV_HANDLER_OK + : KV_HANDLER_BADVALUE; } if (MATCHSK("", "base_url", parsed)) { free(config->base_url); return parcini_value_handle(&parsed->value, PARCINI_VALUE_STRING, - &config->base_url)? - KV_HANDLER_OK : KV_HANDLER_BADVALUE; + &config->base_url) + ? KV_HANDLER_OK + : KV_HANDLER_BADVALUE; } out: @@ -138,26 +152,29 @@ album_config_keyvalue_handler(struct parcini_line *parsed, void *dst) if (MATCHSK("", "title", parsed)) { free(config->title); return parcini_value_handle(&parsed->value, PARCINI_VALUE_STRING, - &config->title) ? - KV_HANDLER_OK : KV_HANDLER_BADVALUE; + &config->title) + ? KV_HANDLER_OK + : KV_HANDLER_BADVALUE; } if (MATCHSK("", "desc", parsed)) { free(config->desc); return parcini_value_handle(&parsed->value, PARCINI_VALUE_STRING, - &config->desc) ? - KV_HANDLER_OK : KV_HANDLER_BADVALUE; + &config->desc) + ? KV_HANDLER_OK + : KV_HANDLER_BADVALUE; } return KV_HANDLER_NOMATCH; } -static bool ini_handler(const char *fpath, ini_keyvalue_handler_fn kvhandler, - void *dst) +static bool +ini_handler(const char *fpath, ini_keyvalue_handler_fn kvhandler, + void *dst) { struct parcini_line parsed; enum parcini_result res; parcini_t *parser = parcini_from_file(fpath); - if (parser == NULL){ + if (parser == NULL) { log_printl_errno(LOG_FATAL, "Couldn't open %s", fpath); return false; } @@ -165,18 +182,19 @@ static bool ini_handler(const char *fpath, ini_keyvalue_handler_fn kvhandler, bool ok = true; enum kv_handler_result hres; while ((res = parcini_parse_next_line(parser, &parsed)) != PARCINI_EOF - && ok) { + && ok) { switch (res) { case PARCINI_KEYVALUE: if ((hres = kvhandler(&parsed, dst)) == KV_HANDLER_BADVALUE) { log_printl(LOG_ERROR, "Error parsing %s:%ld, bad value for key" - "%s", fpath, parsed.lineno, parsed.key); + "%s", + fpath, parsed.lineno, parsed.key); ok = false; - } else if(hres == KV_HANDLER_NOMATCH) { - log_printl(LOG_ERROR, - "Warning: key '%s' in section '%s' is not a valid " - "section-key combination for %s, ignoring.", - parsed.key, parsed.section, fpath); + } else if (hres == KV_HANDLER_NOMATCH) { + log_printl(LOG_ERROR, + "Warning: key '%s' in section '%s' is not a valid " + "section-key combination for %s, ignoring.", + parsed.key, parsed.section, fpath); } continue; @@ -205,8 +223,8 @@ bool site_config_read_ini(const char *wdir, struct site_config *config) { if (wdir == NULL) { - return ini_handler(SITE_CONF, site_config_keyvalue_handler, - config); + return ini_handler(SITE_CONF, site_config_keyvalue_handler, + config); } char *fpath = joinpath(wdir, SITE_CONF); bool ok = ini_handler(fpath, site_config_keyvalue_handler, config); @@ -226,7 +244,7 @@ site_config_init(void) { struct site_config *config = calloc(1, sizeof *config); if (config != NULL) { - config->images = (struct image_config) { + config->images = (struct image_config){ .strip = true, .quality = 80, .max_width = 3000, @@ -234,7 +252,7 @@ site_config_init(void) .smart_resize = true, .blur = 0, }; - config->thumbnails = (struct image_config) { + config->thumbnails = (struct image_config){ .strip = true, .quality = 60, .max_width = 400, diff --git a/src/fs.c b/src/fs.c index 3d2c4da..d1bcff0 100644 --- a/src/fs.c +++ b/src/fs.c @@ -23,7 +23,7 @@ #define BUFSIZE 8192 -#define CONTENTDIR "content" +#define CONTENTDIR "content" #define DEFAULTALBUM "unorganized" /* @@ -73,7 +73,7 @@ nmkdir(const char *path, struct stat *dstat, bool dry) return NMKDIR_NOOP; } - if(mkdir(path, 0755) < 0) { + if (mkdir(path, 0755) < 0) { if (errno == EEXIST) { if (stat(path, dstat)) { log_printl_errno(LOG_FATAL, "Can't read %s", path); @@ -107,7 +107,7 @@ isimage(const char *fname) { char *ext = strrchr(fname, '.'); if (!ext || *(ext + 1) == '\0') return false; - + ext++; for (size_t i = 0; img_extensions[i] != NULL; i++) { if (!strcasecmp(ext, img_extensions[i])) return true; @@ -120,7 +120,7 @@ const char * delext(const char *restrict basename, char *restrict buf, size_t n) { const char *ext = strrchr(basename, '.'); - size_t i = ext - basename; + size_t i = ext - basename; if (i + 1 > n) return NULL; memcpy(buf, basename, i); buf[i] = '\0'; @@ -149,8 +149,8 @@ void setdatetime(const char *path, const struct timespec *mtim) { struct timespec tms[] = { - { .tv_sec = mtim->tv_sec, .tv_nsec = mtim->tv_nsec }, - { .tv_sec = mtim->tv_sec, .tv_nsec = mtim->tv_nsec }, + {.tv_sec = mtim->tv_sec, .tv_nsec = mtim->tv_nsec}, + {.tv_sec = mtim->tv_sec, .tv_nsec = mtim->tv_nsec}, }; if (utimensat(AT_FDCWD, path, tms, 0) == -1) { log_printl_errno(LOG_ERROR, "Warning: couldn't set times of %s", path); @@ -167,7 +167,7 @@ rmentry(const char *path, bool dry) } if (S_ISDIR(st.st_mode)) { - DIR *dir = opendir(path); + DIR *dir = opendir(path); struct dirent *ent; while ((ent = readdir(dir))) { if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) { @@ -201,10 +201,10 @@ error: ssize_t rmextra(const char *path, struct hmap *preserved, preremove_fn cb, - void *data, bool dry) + void *data, bool dry) { ssize_t removed = 0; - DIR *dir = opendir(path); + DIR *dir = opendir(path); if (dir == NULL) { return dry ? 0 : -1; } @@ -235,12 +235,12 @@ rmextra(const char *path, struct hmap *preserved, preremove_fn cb, bool filesync(const char *restrict srcpath, const char *restrict dstpath, - struct hmap *preserved, bool dry) + struct hmap *preserved, bool dry) { - int fdsrc; - struct stat stsrc; - struct vector *own = NULL; - bool cleanup = false; + int fdsrc; + struct stat stsrc; + struct vector *own = NULL; + bool cleanup = false; fdsrc = open(srcpath, O_RDONLY); if (fdsrc < 0) { @@ -256,14 +256,14 @@ filesync(const char *restrict srcpath, const char *restrict dstpath, if (mkdir(dstpath, 0755)) { if (errno != EEXIST) { log_printl_errno(LOG_ERROR, "Couldn't create directory %s", - dstpath); + dstpath); goto dir_error; } if (stat(dstpath, &stsrc)) { log_printl_errno(LOG_ERROR, "Couldn't stat %s", dstpath); goto dir_error; } - if (!S_ISDIR(stsrc.st_mode)){ + if (!S_ISDIR(stsrc.st_mode)) { log_printl(LOG_ERROR, "%s is not a directory", dstpath); errno = ENOTDIR; goto dir_error; @@ -343,20 +343,20 @@ filesync(const char *restrict srcpath, const char *restrict dstpath, #ifdef __linux__ ssize_t nwrote = sendfile(fddst, fdsrc, NULL, stsrc.st_size); if (nwrote != stsrc.st_size) { - log_printl_errno(LOG_ERROR, "Failed to copy %s (wrote %lu/%lu bytes)", - dstpath, nwrote, stsrc.st_size); + log_printl_errno(LOG_ERROR, "Failed to copy %s (wrote %lu/%lu bytes)", + dstpath, nwrote, stsrc.st_size); goto copy_error; } #else - char buf[BUFSIZE]; + char buf[BUFSIZE]; ssize_t nread; while ((nread = read(fdsrc, buf, BUFSIZE)) > 0) { ssize_t nwrote = write(fddst, buf, nread); if (nread != nwrote) { log_printl_errno(LOG_ERROR, "Failed to copy %s " - "(buffer wrote %lu/%lu bytes)", - dstpath, nwrote, nread); + "(buffer wrote %lu/%lu bytes)", + dstpath, nwrote, nread); goto copy_error; } } @@ -367,8 +367,8 @@ filesync(const char *restrict srcpath, const char *restrict dstpath, #endif struct timespec tms[] = { - { .tv_sec = stsrc.st_mtim.tv_sec, .tv_nsec = stsrc.st_mtim.tv_nsec }, - { .tv_sec = stsrc.st_mtim.tv_sec, .tv_nsec = stsrc.st_mtim.tv_nsec }, + {.tv_sec = stsrc.st_mtim.tv_sec, .tv_nsec = stsrc.st_mtim.tv_nsec}, + {.tv_sec = stsrc.st_mtim.tv_sec, .tv_nsec = stsrc.st_mtim.tv_nsec}, }; futimens(fddst, tms); diff --git a/src/log.c b/src/log.c index 4ff617f..281c507 100644 --- a/src/log.c +++ b/src/log.c @@ -17,7 +17,7 @@ void log_printf(enum log_level lvl, const char *restrict fmt, ...) { if (lvl > log_verbosity) return; - FILE *out = lvl < LOG_INFO ? stderr : stdout; + FILE *out = lvl < LOG_INFO ? stderr : stdout; va_list args; va_start(args, fmt); vfprintf(out, fmt, args); diff --git a/src/render.c b/src/render.c index 8909570..851c06d 100644 --- a/src/render.c +++ b/src/render.c @@ -10,27 +10,29 @@ static void images_walk(struct vector *images) { - size_t i; + size_t i; struct image *image; - size_t last = images->len - 1; + size_t last = images->len - 1; - vector_foreach(images, i, image) { - roscha_hmap_set_new(image->map, "source", (slice_whole(image->url_image))); + vector_foreach (images, i, image) { + roscha_hmap_set_new(image->map, "source", + (slice_whole(image->url_image))); roscha_hmap_set_new(image->map, "date", (slice_whole(image->datestr))); char *url; if (i > 0) { struct image *prev = images->values[i - 1]; - url = prev->url; + url = prev->url; roscha_hmap_set_new(image->map, "prev", (slice_whole(url))); } if (i < last) { struct image *next = images->values[i + 1]; - url = next->url; + url = next->url; roscha_hmap_set_new(image->map, "next", (slice_whole(url))); } roscha_hmap_set_new(image->thumb, "link", (slice_whole(image->url))); - roscha_hmap_set_new(image->thumb, "source", (slice_whole(image->url_thumb))); + roscha_hmap_set_new(image->thumb, "source", + (slice_whole(image->url_thumb))); roscha_vector_push(image->album->thumbs, image->thumb); } @@ -38,9 +40,9 @@ images_walk(struct vector *images) static inline void years_push_new_year(struct roscha_object *years, char *yearstr, - struct roscha_object **year, struct roscha_object **albums) + struct roscha_object **year, struct roscha_object **albums) { - *year = roscha_object_new(hmap_new_with_cap(8)); + *year = roscha_object_new(hmap_new_with_cap(8)); *albums = roscha_object_new(vector_new_with_cap(8)); roscha_hmap_set_new((*year), "name", (slice_whole(yearstr))); roscha_hmap_set(*year, "albums", *albums); @@ -57,7 +59,7 @@ years_push_album(struct roscha_object *years, struct album *album) if (years->vector->len == 0) { years_push_new_year(years, album->year, &years, &albums); } else { - year = years->vector->values[years->vector->len - 1]; + year = years->vector->values[years->vector->len - 1]; struct roscha_object *yearval = roscha_hmap_get(year, "name"); if (strcmp(yearval->slice.str, album->year)) { years_push_new_year(years, album->year, &year, &albums); @@ -71,14 +73,13 @@ years_push_album(struct roscha_object *years, struct album *album) bool render_set_album_vars(struct render *r, struct album *album) { - if (album->config->title) { roscha_hmap_set_new(album->map, "title", - (slice_whole(album->config->title))); + (slice_whole(album->config->title))); } if (album->config->desc) { roscha_hmap_set_new(album->map, "desc", - (slice_whole(album->config->desc))); + (slice_whole(album->config->desc))); } roscha_hmap_set_new(album->map, "link", (slice_whole(album->url))); roscha_hmap_set_new(album->map, "date", (slice_whole(album->datestr))); @@ -103,10 +104,10 @@ render_set_album_vars(struct render *r, struct album *album) static bool render(struct roscha_env *env, const char *tmpl, const char *opath) { - bool ok = true; - sds output = roscha_env_render(env, tmpl); + bool ok = true; + sds output = roscha_env_render(env, tmpl); size_t outlen = strlen(output); - FILE *f = fopen(opath, "w"); + FILE *f = fopen(opath, "w"); if (fwrite(output, 1, outlen, f) != outlen) { ok = false; log_printl_errno(LOG_FATAL, "Can't write %s", opath); @@ -182,9 +183,9 @@ done: bool render_init(struct render *r, const char *root, struct site_config *conf, - struct vector *albums) + struct vector *albums) { - char *tmplpath = joinpath(root, TEMPLATESDIR); + char *tmplpath = joinpath(root, TEMPLATESDIR); struct stat tstat; if (stat(tmplpath, &tstat) == -1) { if (errno == ENOENT) { @@ -200,22 +201,20 @@ render_init(struct render *r, const char *root, struct site_config *conf, if (r->dry_run) goto cleanup; - r->env = roscha_env_new(); - bool ok = roscha_env_load_dir(r->env, tmplpath); + r->env = roscha_env_new(); + bool ok = roscha_env_load_dir(r->env, tmplpath); if (!ok) { struct vector *errors = roscha_env_check_errors(r->env); log_printl(LOG_FATAL, "Couldn't initialize template engine: %s", - errors->values[0]); + errors->values[0]); return false; } - r->years = roscha_object_new(vector_new_with_cap(8)); + r->years = roscha_object_new(vector_new_with_cap(8)); r->albums = roscha_object_new(vector_new_with_cap(64)); roscha_hmap_set_new(r->env->vars, "title", (slice_whole(conf->title))); roscha_hmap_set_new(r->env->vars, "index", (slice_whole(conf->base_url))); - //bstree_inorder_walk(albums->root, albums_walk, (void *)r); - cleanup: free(tmplpath); return true; diff --git a/src/revela.c b/src/revela.c index f41d271..aca19c5 100644 --- a/src/revela.c +++ b/src/revela.c @@ -13,9 +13,8 @@ #define VERSION "0.1.2" #endif -static const char *usage = - "Usage: %s [OPTIONS] -o \n" - "For more information consult `man 1 revela`\n"; +static const char *usage = "Usage: %s [OPTIONS] -o \n" + "For more information consult `man 1 revela`\n"; static struct site site = {0}; static enum log_level loglvl = LOG_DETAIL; @@ -47,7 +46,7 @@ parse_arguments(int argc, char *argv[]) printf(usage, cmd); exit(0); case 'V': - printf("revela "VERSION"\n"); + printf("revela " VERSION "\n"); exit(0); default: bad_arguments(cmd); @@ -69,8 +68,9 @@ main(int argc, char *argv[]) log_set_verbosity(loglvl); #endif - int ret = site_init(&site) && site_load(&site) && site_build(&site) - ? EXIT_SUCCESS : EXIT_FAILURE; + int ret = site_init(&site) && site_load(&site) && site_build(&site) + ? EXIT_SUCCESS + : EXIT_FAILURE; if (site.dry_run) { log_printl(LOG_INFO, "==== [DRY RUN] ===="); diff --git a/src/site.c b/src/site.c index 0b76a0d..59cd669 100644 --- a/src/site.c +++ b/src/site.c @@ -27,9 +27,9 @@ static const char *album_meta = ".revela"; static bool prerm_imagedir(const char *path, void *data) { - struct stat st; + struct stat st; struct album *album = data; - char htmlpath[PATH_MAX]; + char htmlpath[PATH_MAX]; if (stat(path, &st)) { log_printl_errno(LOG_ERROR, "Couldn't stat %s", path); @@ -37,8 +37,8 @@ prerm_imagedir(const char *path, void *data) } if (S_ISDIR(st.st_mode)) { struct image *prev, *next = NULL; - size_t i; - vector_foreach(album->images, i, prev) { + size_t i; + vector_foreach (album->images, i, prev) { if (prev->tstamp > st.st_mtim.tv_sec) { prev = next; break; @@ -72,23 +72,24 @@ static bool wand_passfail(MagickWand *wand, MagickPassFail status) { if (status != MagickPass) { - char *desc; + char *desc; ExceptionType severity; desc = MagickGetException(wand, &severity); log_printl(LOG_FATAL, "GraphicsMagick error: %.1024s severity: %d\n", - desc, severity); + desc, severity); return false; } return true; } -#define TRYWAND(w, f) if (!wand_passfail(w, f)) goto magick_fail +#define TRYWAND(w, f) \ + if (!wand_passfail(w, f)) goto magick_fail static bool optimize_image(MagickWand *wand, const char *dst, - const struct image_config *conf, - const struct timespec *srcmtim, bool dry) + const struct image_config *conf, const struct timespec *srcmtim, + bool dry) { log_printl(LOG_DETAIL, "Converting %s", dst); if (dry) goto out; @@ -98,8 +99,7 @@ optimize_image(MagickWand *wand, const char *dst, TRYWAND(wand, MagickStripImage(wand)); } TRYWAND(wand, MagickSetCompressionQuality(wand, conf->quality)); - unsigned long x = MagickGetImageWidth(wand), - y = MagickGetImageHeight(wand); + unsigned long x = MagickGetImageWidth(wand), y = MagickGetImageHeight(wand); /* Resize only if the image is actually bigger. No point in making small * images bigger. */ if (x > nx || y > ny) { @@ -111,8 +111,8 @@ optimize_image(MagickWand *wand, const char *dst, nx = ny * ratio; } } - TRYWAND(wand, MagickResizeImage(wand, nx, ny, GaussianFilter, - conf->blur)); + TRYWAND(wand, + MagickResizeImage(wand, nx, ny, GaussianFilter, conf->blur)); } TRYWAND(wand, MagickWriteImage(wand, dst)); setdatetime(dst, srcmtim); @@ -126,18 +126,18 @@ magick_fail: static bool images_walk(struct site *site, struct vector *images) { - size_t i; + size_t i; struct image *image; - - vector_foreach(images, i, image) { - struct stat dstat; - struct timespec ddate = { .tv_sec = image->tstamp, .tv_nsec = 0 }; - int imgupdate, thumbupdate; - char htmlpath[PATH_MAX]; - const char *base = rbasename(image->dst); - log_printl(LOG_DEBUG, "Image: %s, datetime %s", image->basename, - image->datestr); + vector_foreach (images, i, image) { + struct stat dstat; + struct timespec ddate = {.tv_sec = image->tstamp, .tv_nsec = 0}; + int imgupdate, thumbupdate; + char htmlpath[PATH_MAX]; + const char *base = rbasename(image->dst); + + log_printl(LOG_DEBUG, "Image: %s, datetime %s", image->basename, + image->datestr); if (!nmkdir(image->dst, &dstat, site->dry_run)) return false; @@ -148,12 +148,16 @@ images_walk(struct site *site, struct vector *images) if (!site->dry_run && (!imgupdate || !thumbupdate)) { TRYWAND(site->wand, MagickReadImage(site->wand, image->source)); } - if (!imgupdate && !optimize_image(site->wand, image->dst_image, - &site->config->images, &image->modtime, site->dry_run)) { + if (!imgupdate + && !optimize_image(site->wand, image->dst_image, + &site->config->images, &image->modtime, + site->dry_run)) { goto magick_fail; } - if (!thumbupdate && !optimize_image(site->wand, image->dst_thumb, - &site->config->thumbnails, &image->modtime, site->dry_run)) { + if (!thumbupdate + && !optimize_image(site->wand, image->dst_thumb, + &site->config->thumbnails, &image->modtime, + site->dry_run)) { goto magick_fail; } if (!site->dry_run && (!imgupdate || !thumbupdate)) { @@ -194,21 +198,24 @@ images_walk(struct site *site, struct vector *images) struct image *next = images->values[i + 1]; if (access(next->dst, F_OK) != 0) { image->album->images_updated++; - return render_make_image(&site->render, htmlpath, image); + if (!render_make_image(&site->render, htmlpath, image)) { + return false; + } } - } - if (i > 0) { - struct image *prev = images->values[i - 1]; + } else if (i > 0) { + struct image *prev = images->values[i - 1]; if (prev->modified) { image->album->images_updated++; - return render_make_image(&site->render, htmlpath, image); + if (!render_make_image(&site->render, htmlpath, image)) { + return false; + } } } - success: +success: if (!site->dry_run) setdatetime(image->dst, &ddate); continue; - magick_fail: +magick_fail: return false; } return true; @@ -217,12 +224,12 @@ images_walk(struct site *site, struct vector *images) static bool albums_walk(struct site *site) { - size_t i; + size_t i; struct album *album; - vector_foreach(site->albums, i, album) { + vector_foreach (site->albums, i, album) { struct stat dstat; - char pathbuf[PATH_MAX]; + char pathbuf[PATH_MAX]; switch (nmkdir(album->slug, &dstat, site->dry_run)) { case NMKDIR_ERROR: return false; @@ -251,7 +258,8 @@ albums_walk(struct site *site) if (!render_set_album_vars(&site->render, album)) return false; } - log_printl(LOG_DEBUG, "Album: %s, datetime %s", album->slug, album->datestr); + log_printl(LOG_DEBUG, "Album: %s, datetime %s", album->slug, + album->datestr); if (!images_walk(site, album->images)) { return false; } @@ -259,10 +267,11 @@ albums_walk(struct site *site) hmap_set(album->preserved, index_html, (char *)index_html); hmap_set(album->preserved, album_meta, (char *)album_meta); ssize_t deleted = rmextra(album->slug, album->preserved, prerm_imagedir, - album, site->dry_run); + album, site->dry_run); if (deleted < 0) { - log_printl_errno(LOG_ERROR, - "Something happened while deleting extraneous files"); + log_printl_errno( + LOG_ERROR, + "Something happened while deleting extraneous files"); } else { album->images_updated += deleted; } @@ -287,16 +296,16 @@ albums_walk(struct site *site) static bool traverse(struct site *site, const char *path, struct stat *dstat) { - bool ok = true; + bool ok = true; DIR *dir = opendir(path); if (!dir) { log_printl_errno(LOG_FATAL, "Can't open directory %s", path); return false; } - struct dirent *ent; + struct dirent *ent; struct album_config *album_conf = calloc(1, sizeof *album_conf); - struct album *album = album_new(album_conf, site, path, - path + site->rel_content_dir, dstat); + struct album *album = + album_new(album_conf, site, path, path + site->rel_content_dir, dstat); if (album == NULL) { closedir(dir); return false; @@ -307,7 +316,7 @@ traverse(struct site *site, const char *path, struct stat *dstat) } struct stat fstats; - char *subpath = joinpath(path, ent->d_name); + char *subpath = joinpath(path, ent->d_name); if (stat(subpath, &fstats)) { log_printl_errno(LOG_FATAL, "Can't read %s", subpath); ok = false; @@ -332,11 +341,11 @@ traverse(struct site *site, const char *path, struct stat *dstat) } free(subpath); } - + if (album->images->len != 0) { album_set_year(album); - qsort(album->images->values, - album->images->len, sizeof(void *), image_cmp); + qsort(album->images->values, album->images->len, sizeof(void *), + image_cmp); vector_push(site->albums, album); closedir(dir); return true; @@ -352,15 +361,15 @@ bool site_build(struct site *site) { struct stat dstat; - char staticp[PATH_MAX]; - char startwd[PATH_MAX]; + char staticp[PATH_MAX]; + char startwd[PATH_MAX]; getcwd(startwd, PATH_MAX); if (!nmkdir(site->output_dir, &dstat, false)) return false; if (chdir(site->output_dir)) { log_printl_errno(LOG_FATAL, "Can't change to directory %s", - site->output_dir); + site->output_dir); return false; } @@ -378,12 +387,14 @@ site_build(struct site *site) return false; } if (rmextra(site->output_dir, site->album_dirs, NULL, NULL, - site->dry_run) < 0) { - log_printl_errno(LOG_ERROR, - "Something happened while deleting extraneous files"); + site->dry_run) + < 0) { + log_printl_errno( + LOG_ERROR, + "Something happened while deleting extraneous files"); } } else if (!filesync(staticp, site->output_dir, site->album_dirs, - site->dry_run)) { + site->dry_run)) { log_printl(LOG_FATAL, "Can't copy static files"); return false; } @@ -404,7 +415,8 @@ site_load(struct site *site) if (!traverse(site, site->content_dir, &cstat)) return false; qsort(site->albums->values, site->albums->len, sizeof(void *), album_cmp); - return render_init(&site->render, site->root_dir, site->config, site->albums); + return render_init(&site->render, site->root_dir, site->config, + site->albums); } bool @@ -422,11 +434,11 @@ site_init(struct site *site) } } - site->content_dir = joinpath(site->root_dir, CONTENTDIR); + site->content_dir = joinpath(site->root_dir, CONTENTDIR); site->rel_content_dir = strlen(site->root_dir) + 1; InitializeMagick(NULL); - site->wand = NewMagickWand(); - site->album_dirs = hmap_new(); + site->wand = NewMagickWand(); + site->album_dirs = hmap_new(); site->render.dry_run = site->dry_run; roscha_init(); @@ -438,9 +450,9 @@ void site_deinit(struct site *site) { if (site->albums) { - size_t i; + size_t i; struct album *a; - vector_foreach(site->albums, i, a) { + vector_foreach (site->albums, i, a) { album_destroy(a); } vector_free(site->albums); -- cgit v1.2.3