]> git.ozlabs.org Git - ccan/blobdiff - tools/namespacize.c
tools/namespacize: use unlink_noerr.
[ccan] / tools / namespacize.c
index f1a9374e8fee1e363a549db84d324a81ecf132bc..ae20e3d66c136e83b4675526f9d76c34647d8b41 100644 (file)
@@ -13,7 +13,9 @@
 #include "ccan/take/take.h"
 #include "ccan/rbuf/rbuf.h"
 #include "ccan/tal/path/path.h"
+#include "ccan/tal/grab_file/grab_file.h"
 #include "ccan/err/err.h"
+#include "ccan/noerr/noerr.h"
 #include "tools.h"
 
 static bool verbose = false;
@@ -28,15 +30,6 @@ static int indent = 0;
 #define verbose_indent() (indent += 2)
 #define verbose_unindent() (indent -= 2)
 
-static int unlink_no_errno(const char *filename)
-{
-       int ret = 0, serrno = errno;
-       if (unlink(filename) < 0)
-               ret = errno;
-       errno = serrno;
-       return ret;
-}
-
 static char **get_dir(const char *dir)
 {
        DIR *d;
@@ -256,9 +249,10 @@ static void analyze_headers(const char *dir, struct replace **repl)
        char *hdr, *contents;
 
        /* Get hold of header, assume that's it. */
-       hdr = tal_fmt(dir, "%s/%s.h", dir, path_basename(dir, dir));
+       hdr = tal_fmt(dir, "%s.h",
+                     path_join(NULL, dir, take(path_basename(NULL, dir))));
 
-       contents = tal_grab_file(dir, hdr, NULL);
+       contents = grab_file(dir, hdr);
        if (!contents)
                err(1, "Reading %s", hdr);
 
@@ -275,7 +269,7 @@ static void analyze_headers(const char *dir, struct replace **repl)
 
 static void write_replacement_file(const char *dir, struct replace **repl)
 {
-       char *replname = tal_fmt(dir, "%s/.namespacize", dir);
+       char *replname = path_join(dir, dir, ".namespacize");
        int fd;
        struct replace *r;
 
@@ -290,7 +284,7 @@ static void write_replacement_file(const char *dir, struct replace **repl)
        for (r = *repl; r; r = r->next) {
                if (write(fd,r->string,strlen(r->string)) != strlen(r->string)
                    || write(fd, "\n", 1) != 1) {
-                       unlink_no_errno(replname);
+                       unlink_noerr(replname);
                        if (errno == 0)
                                errx(1, "Short write to %s: disk full?",
                                     replname);
@@ -333,7 +327,7 @@ static const char *rewrite_file(const char *filename,
        int fd;
 
        verbose("Rewriting %s\n", filename);
-       file = tal_grab_file(filename, filename, NULL);
+       file = grab_file(filename, filename);
        if (!file)
                err(1, "Reading file %s", filename);
 
@@ -415,10 +409,7 @@ static void convert_dir(const char *dir)
        struct adjusted *adj = NULL;
 
        /* Remove any ugly trailing slashes. */
-       name = tal_strdup(NULL, dir);
-       while (strends(name, "/"))
-               name[strlen(name)-1] = '\0';
-
+       name = path_canon(NULL, dir);
        analyze_headers(name, &replace);
        write_replacement_file(name, &replace);
        setup_adjust_files(name, replace, &adj);
@@ -430,10 +421,10 @@ static void convert_dir(const char *dir)
 static struct replace *read_replacement_file(const char *depdir)
 {
        struct replace *repl = NULL;
-       char *replname = tal_fmt(depdir, "%s/.namespacize", depdir);
+       char *replname = path_join(depdir, depdir, ".namespacize");
        char *file, **line;
 
-       file = tal_grab_file(replname, replname, NULL);
+       file = grab_file(replname, replname);
        if (!file) {
                if (errno != ENOENT)
                        err(1, "Opening %s", replname);
@@ -459,7 +450,7 @@ static void adjust_dir(const char *dir)
                struct adjusted *adj = NULL;
                struct replace *repl;
 
-               depdir = tal_fmt(parent, "%s/%s", parent, *deps);
+               depdir = path_join(parent, parent, *deps);
                repl = read_replacement_file(depdir);
                if (repl) {
                        verbose("%s has been namespacized\n", depdir);
@@ -488,7 +479,7 @@ static void adjust_dependents(const char *dir)
                if (path_basename(*file, *file)[0] == '.')
                        continue;
 
-               info = tal_fmt(*file, "%s/_info", *file);
+               info = path_join(*file, *file, "_info");
                if (access(info, R_OK) != 0)
                        continue;