#include <ccan/read_write_all/read_write_all.h>
#include <ccan/noerr/noerr.h>
#include <ccan/time/time.h>
+#include <ccan/tal/path/path.h>
+#include <ccan/tal/grab_file/grab_file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
/* Ten minutes. */
const unsigned int default_timeout_ms = 10 * 60 * 1000;
-char *tal_basename(const void *ctx, const char *dir)
-{
- const char *p = strrchr(dir, '/');
-
- if (!p)
- return tal_strdup(ctx, dir);
- return tal_strdup(ctx, p+1);
-}
-
-char *tal_dirname(const void *ctx, const char *dir)
-{
- const char *p = strrchr(dir, '/');
-
- if (!p)
- return tal_strdup(ctx, ".");
- return tal_strndup(ctx, dir, p - dir);
-}
-
-char *tal_getcwd(const void *ctx)
-{
- unsigned int len;
- char *cwd;
-
- /* *This* is why people hate C. */
- len = 32;
- cwd = tal_arr(ctx, char, len);
- while (!getcwd(cwd, len)) {
- if (errno != ERANGE) {
- tal_free(cwd);
- return NULL;
- }
- tal_resize(&cwd, len *= 2);
- }
- return cwd;
-}
-
static void killme(int sig)
{
kill(-getpid(), SIGKILL);
int p[2];
struct rbuf in;
int status, ms;
- struct timespec start;
+ struct timeabs start;
*ok = false;
if (pipe(p) != 0)
signal(SIGALRM, killme);
itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0;
- itim.it_value = timespec_to_timeval(time_from_msec(*timeout_ms));
+ itim.it_value = timespec_to_timeval(time_from_msec(*timeout_ms).ts);
setitimer(ITIMER_REAL, &itim, NULL);
status = system(cmd);
if (waitpid(pid, &status, 0) != pid)
err(1, "Failed to wait for child");
- ms = time_to_msec(time_sub(time_now(), start));
+ ms = time_to_msec(time_between(time_now(), start));
if (ms > *timeout_ms)
*timeout_ms = 0;
else
char *temp_file(const void *ctx, const char *extension, const char *srcname)
{
- unsigned baselen;
- char *f, *suffix = tal_strdup(ctx, "");
+ char *f, *base, *suffix;
struct stat st;
unsigned int count = 0;
- srcname = tal_basename(ctx, srcname);
- if (strrchr(srcname, '.'))
- baselen = strrchr(srcname, '.') - srcname;
- else
- baselen = strlen(srcname);
+ base = path_join(ctx, temp_dir(), take(path_basename(ctx, srcname)));
+ /* Trim extension. */
+ base[path_ext_off(base)] = '\0';
+ suffix = tal_strdup(ctx, extension);
do {
- f = tal_fmt(ctx, "%s/%.*s%s%s",
- temp_dir(), baselen, srcname, suffix, extension);
- tal_free(suffix);
- suffix = tal_fmt(ctx, "-%u", ++count);
+ f = tal_strcat(ctx, base, suffix);
+ suffix = tal_fmt(base, "-%u%s", ++count, extension);
} while (lstat(f, &st) == 0);
if (tools_verbose)
printf("Creating file %s\n", f);
- tal_free(suffix);
+ tal_free(base);
return f;
}
bool move_file(const char *oldname, const char *newname)
{
char *contents;
- size_t size;
int fd;
bool ret;
}
/* Try copy and delete: not atomic! */
- contents = tal_grab_file(NULL, oldname, &size);
+ contents = grab_file(NULL, oldname);
if (!contents) {
if (tools_verbose)
printf("read failed: %s\n", strerror(errno));
goto free;
}
- ret = write_all(fd, contents, size);
+ ret = write_all(fd, contents, tal_count(contents)-1);
if (close(fd) != 0)
ret = false;
tal_resize((char **)&p, size);
return p;
}
-
-void *tal_grab_file(const void *ctx, const char *filename, size_t *size)
-{
- struct rbuf rbuf;
- char *buf = tal_arr(ctx, char, 0);
-
- if (!rbuf_open(&rbuf, filename, buf, 0))
- return tal_free(buf);
-
- if (!rbuf_fill_all(&rbuf, do_tal_realloc) && errno)
- rbuf.buf = tal_free(rbuf.buf);
- else {
- rbuf.buf[rbuf.len] = '\0';
- if (size)
- *size = rbuf.len;
- }
- close(rbuf.fd);
-
- return rbuf.buf;
-}