aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-make
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2017-01-30 01:53:38 +0100
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2017-01-30 01:53:38 +0100
commit8ab79c1e2773540f0188ba33da01fb7fb7a4c7f6 (patch)
treec6203471bb270b6aa2981e2c016431486626d514 /src/blogc-make
parent306e29bcfa7d963fb4841829227a1e56360bc75d (diff)
downloadblogc-8ab79c1e2773540f0188ba33da01fb7fb7a4c7f6.tar.gz
blogc-8ab79c1e2773540f0188ba33da01fb7fb7a4c7f6.tar.bz2
blogc-8ab79c1e2773540f0188ba33da01fb7fb7a4c7f6.zip
make: improved clean rule
Diffstat (limited to 'src/blogc-make')
-rw-r--r--src/blogc-make/ctx.c6
-rw-r--r--src/blogc-make/exec-native.c86
-rw-r--r--src/blogc-make/exec-native.h1
-rw-r--r--src/blogc-make/rules.c4
4 files changed, 61 insertions, 36 deletions
diff --git a/src/blogc-make/ctx.c b/src/blogc-make/ctx.c
index 2a4426d..39f1bf4 100644
--- a/src/blogc-make/ctx.c
+++ b/src/blogc-make/ctx.c
@@ -102,12 +102,10 @@ bm_ctx_new(const char *settings_file, bc_error_t **err)
const char *output_dir = bc_trie_lookup(settings->settings, "output_dir");
if (output_dir[0] == '/') {
- rv->output_dir = realpath(output_dir, NULL);
+ rv->output_dir = bc_strdup(output_dir);
}
else {
- char *tmp = bc_strdup_printf("%s/%s", rv->root_dir, output_dir);
- rv->output_dir = realpath(tmp, NULL);
- free(tmp);
+ rv->output_dir = bc_strdup_printf("%s/%s", rv->root_dir, output_dir);
}
const char *template_dir = bc_trie_lookup(settings->settings,
diff --git a/src/blogc-make/exec-native.c b/src/blogc-make/exec-native.c
index 3181677..8c49593 100644
--- a/src/blogc-make/exec-native.c
+++ b/src/blogc-make/exec-native.c
@@ -16,6 +16,7 @@
#include <unistd.h>
#include <libgen.h>
#include <errno.h>
+#include "../common/error.h"
#include "../common/file.h"
#include "../common/utils.h"
#include "exec-native.h"
@@ -87,6 +88,42 @@ bm_exec_native_cp(bm_filectx_t *source, bm_filectx_t *dest, bool verbose)
}
+bool
+bm_exec_empty_dir(const char *dir, bc_error_t **err)
+{
+ DIR *d = opendir(dir);
+ if (d == NULL) {
+ if (errno == ENOENT) {
+ return true;
+ }
+ if (err != NULL) {
+ *err = bc_error_new_printf(0, "failed to open directory (%s): %s\n",
+ dir, strerror(errno));
+ }
+ return true;
+ }
+
+ struct dirent *e;
+ size_t count = 0;
+ while (NULL != (e = readdir(d))) {
+ if ((0 == strcmp(e->d_name, ".")) || (0 == strcmp(e->d_name, "..")))
+ continue;
+ count++;
+ break;
+ }
+
+ if (0 != closedir(d)) {
+ if (err != NULL) {
+ *err = bc_error_new_printf(0, "failed to close directory (%s): %s\n",
+ dir, strerror(errno));
+ }
+ return true;
+ }
+
+ return count == 0;
+}
+
+
int
bm_exec_native_rm(const char *output_dir, bm_filectx_t *dest, bool verbose)
{
@@ -110,53 +147,38 @@ bm_exec_native_rm(const char *output_dir, bm_filectx_t *dest, bool verbose)
char *dir_short = dirname(short_path);
char *dir = dirname(path);
- while ((0 != strcmp(dir_short, ".")) && (0 != strcmp(dir, output_dir))) {
- DIR *d = opendir(dir);
- if (d == NULL) {
- fprintf(stderr, "error: failed to open directory (%s): %s\n",
- dir, strerror(errno));
+ bc_error_t *err = NULL;
+
+ while ((0 != strcmp(dir_short, ".")) && (0 != strcmp(dir_short, "/"))) {
+ bool empty = bm_exec_empty_dir(dir, &err);
+ if (err != NULL) {
+ fprintf(stderr, "blogc-make: error: %s\n", err->msg);
+ bc_error_free(err);
rv = 3;
break;
}
-
- struct dirent *e;
- size_t count = 0;
- while (NULL != (e = readdir(d))) {
- if ((0 == strcmp(e->d_name, ".")) || (0 == strcmp(e->d_name, "..")))
- continue;
- count++;
+ if (!empty) {
break;
}
-
- if (0 != closedir(d)) {
- fprintf(stderr, "error: failed to close directory (%s): %s\n",
+ if (verbose) {
+ printf("Removing directory '%s'\n", dir);
+ fflush(stdout);
+ }
+ if (0 != rmdir(dir)) {
+ fprintf(stderr,
+ "blogc-make: error: failed to remove directory(%s): %s\n",
dir, strerror(errno));
rv = 3;
break;
}
-
- if (count == 0) {
- if (verbose) {
- printf("Removing directory '%s'\n", dir);
- fflush(stdout);
- }
- if (0 != rmdir(dir)) {
- fprintf(stderr, "error: failed to remove directory(%s): %s\n",
- dir, strerror(errno));
- rv = 3;
- break;
- }
+ if (0 == strcmp(dir, output_dir)) {
+ break;
}
dir_short = dirname(dir_short);
dir = dirname(dir);
}
- // try to remove output dir
- // this is done on a best-effort basis, if we can't remove it, it probably
- // have files, so we don't really want to remove it.
- rmdir(output_dir);
-
free(short_path);
free(path);
diff --git a/src/blogc-make/exec-native.h b/src/blogc-make/exec-native.h
index f812238..d835144 100644
--- a/src/blogc-make/exec-native.h
+++ b/src/blogc-make/exec-native.h
@@ -13,6 +13,7 @@
#include "ctx.h"
int bm_exec_native_cp(bm_filectx_t *source, bm_filectx_t *dest, bool verbose);
+bool bm_exec_empty_dir(const char *dir, bc_error_t **err);
int bm_exec_native_rm(const char *output_dir, bm_filectx_t *dest, bool verbose);
#endif /* _MAKE_EXEC_NATIVE_H */
diff --git a/src/blogc-make/rules.c b/src/blogc-make/rules.c
index b2416f0..711809a 100644
--- a/src/blogc-make/rules.c
+++ b/src/blogc-make/rules.c
@@ -540,6 +540,10 @@ clean_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bool verbose)
}
}
+ if (!bm_exec_empty_dir(ctx->output_dir, NULL)) {
+ fprintf(stderr, "blogc-make: warning: output directory is not empty!\n");
+ }
+
return rv;
}