aboutsummaryrefslogtreecommitdiff
path: root/src/site.c
diff options
context:
space:
mode:
authorYaroslav de la Peña Smirnov <yps@yaroslavps.com>2021-11-28 04:37:59 +0300
committerYaroslav de la Peña Smirnov <yps@yaroslavps.com>2021-11-28 04:37:59 +0300
commit7b0eaa806f2cfc84e4c26f8f608e1d4e4843ea05 (patch)
treefd7ca9abad667e43c75aa09580dd145448ff72a5 /src/site.c
parente2a71b0366aa80a7cf131fdbde6012671493d364 (diff)
downloadrevela-7b0eaa806f2cfc84e4c26f8f608e1d4e4843ea05.tar.gz
revela-7b0eaa806f2cfc84e4c26f8f608e1d4e4843ea05.zip
Clean out old files/dirs
Delete extraneous files/images/albums that are no longer present in the source directory.
Diffstat (limited to 'src/site.c')
-rw-r--r--src/site.c59
1 files changed, 47 insertions, 12 deletions
diff --git a/src/site.c b/src/site.c
index 5fa40c4..6fe8d85 100644
--- a/src/site.c
+++ b/src/site.c
@@ -9,12 +9,15 @@
#include "fs.h"
#include "log.h"
#include "hashmap.h"
+#include "render.h"
/* TODO: Probably shouldn't use PATH_MAX, but i'll leave it for now */
/* TODO: handle error cases for paths that are too long */
#define THUMB_SUFFIX "_thumb"
+static const char *index_html = "index.html";
+
static bool
wand_passfail(MagickWand *wand, MagickPassFail status)
{
@@ -80,6 +83,8 @@ images_walk(struct bstnode *node, void *data)
struct site *site = data;
struct image *image = node->value;
struct stat dstat;
+ char htmlpath[PATH_MAX];
+ const char *base = rbasename(image->dst);
log_printl(LOG_DEBUG, "Image: %s, datetime %s", image->basename,
image->datestr);
@@ -101,8 +106,8 @@ images_walk(struct bstnode *node, void *data)
MagickRemoveImage(site->wand);
}
- char htmlpath[PATH_MAX];
- joinpathb(htmlpath, image->dst, "index.html");
+ joinpathb(htmlpath, image->dst, index_html);
+ hashmap_insert(image->album->preserved, base, (char *)base);
return render_make_image(&site->render, htmlpath, image);
magick_fail:
return false;
@@ -116,12 +121,27 @@ albums_walk(struct bstnode *node, void *data)
struct stat dstat;
if (!nmkdir(album->slug, &dstat, site->dry_run)) return false;
+ if (!site->dry_run) {
+ hashmap_insert(site->album_dirs, album->slug, (char *)album->slug);
+ if (!render_set_album_vars(&site->render, album)) return false;
+ }
+
char htmlpath[PATH_MAX];
- joinpathb(htmlpath, album->slug, "index.html");
+ joinpathb(htmlpath, album->slug, index_html);
if (!render_make_album(&site->render, htmlpath, album)) return false;
log_printl(LOG_DEBUG, "Album: %s, datetime %s", album->slug, album->datestr);
- return bstree_inorder_walk(album->images->root, images_walk, site);
+ if (!bstree_inorder_walk(album->images->root, images_walk, site)) {
+ return false;
+ }
+
+ hashmap_insert(album->preserved, index_html, (char *)index_html);
+ if (rmextra(album->slug, album->preserved) < 0) {
+ log_printl_errno(LOG_ERROR,
+ "Something happened while deleting extraneous files");
+ }
+
+ return true;
}
/*
@@ -201,24 +221,35 @@ site_build(struct site *site)
if (!nmkdir(site->output_dir, &dstat, false)) return false;
- joinpathb(staticp, site->root_dir, STATICDIR);
- if (!filesync(staticp, site->output_dir) && errno != ENOENT) {
- log_printl_errno(LOG_FATAL, "Can't copy static files");
- return false;
- }
-
if (chdir(site->output_dir)) {
log_printl_errno(LOG_FATAL, "Can't change to directory %s",
site->output_dir);
return false;
}
- if (!render_make_index(&site->render, "index.html")) return false;
-
if (!bstree_inorder_walk(site->albums->root, albums_walk, (void *)site)) {
return false;
}
+ if (!render_make_index(&site->render, index_html)) return false;
+ hashmap_insert(site->album_dirs, index_html, (char *)index_html);
+
+ joinpathb(staticp, site->root_dir, STATICDIR);
+ if (stat(staticp, &dstat)) {
+ if (errno != ENOENT) {
+ log_printl_errno(LOG_FATAL, "Couldn't read static dir");
+ return false;
+ }
+ if (rmextra(site->output_dir, site->album_dirs) < 0) {
+ log_printl_errno(LOG_ERROR,
+ "Something happened while deleting extraneous files");
+ }
+ } else if (!site->dry_run
+ && !filesync(staticp, site->output_dir, site->album_dirs)) {
+ log_printl(LOG_FATAL, "Can't copy static files");
+ return false;
+ }
+
chdir(site->root_dir);
return true;
}
@@ -256,6 +287,9 @@ site_init(struct site *site)
site->rel_content_dir = strlen(site->root_dir) + 1;
InitializeMagick(NULL);
site->wand = NewMagickWand();
+ if (!site->dry_run) {
+ site->album_dirs = hashmap_new();
+ }
site->render.dry_run = site->dry_run;
return true;
@@ -273,6 +307,7 @@ site_deinit(struct site *site)
DestroyMagick();
}
if (!site->dry_run) {
+ hashmap_free(site->album_dirs);
render_deinit(&site->render);
}
}