commit 8d6b85d960219e6416dcfa61a13ed392bd54d268
parent 98b16df79a33caf1ba0771f234e739fc68b093ae
Author: Mohamed Aslan <maslan@sce.carleton.ca>
Date: Sun, 5 Oct 2014 19:20:29 -0600
fix the 'make_tmpdir()' issue in cmd-diff.c
* make_tmpdir() is now called only once.
* named pipes are now named after the object IDs and not the object names.
* each named pipe is created in a separate directory.
Diffstat:
M | cmd-diff.c | | | 61 | +++++++++++++++++++++++++++++++------------------------------ |
1 file changed, 31 insertions(+), 30 deletions(-)
diff --git a/cmd-diff.c b/cmd-diff.c
@@ -35,18 +35,11 @@ static char *
make_tmpdir()
{
char *name = NULL;
- char *old, *new;
name = strdup("/tmp/baseline.XXXXXX");
if (mkdtemp(name) == NULL) {
- errx(EXIT_FAILURE, "error, failed to create a temporary directory.");
+ errx(EXIT_FAILURE, "error, failed to create a temporary directory (%s).", name);
}
- asprintf(&old, "%s/old", name);
- asprintf(&new, "%s/new", name);
- if ((mkdir(old, S_IRWXU) < 0) || (mkdir(new, S_IRWXU) < 0))
- errx(EXIT_FAILURE, "error, failed to write to temporary directory.");
- free(old);
- free(new);
return name;
}
@@ -140,7 +133,7 @@ ext_diff_proc(struct session *s, struct dirent *ent, char *fifo)
static void
ext_diff(struct session *s, const char *tmpdir, struct dirent *ent1, struct dirent *ent2, const char *path)
{
- char *dir;
+ char *dir1 = NULL, *dir2 = NULL;
char *label1, *label2;
char *fifo1 = NULL, *fifo2 = NULL;
pid_t pid1, pid2;
@@ -151,18 +144,22 @@ ext_diff(struct session *s, const char *tmpdir, struct dirent *ent1, struct dire
return;
if (ent1 != NULL) {
- asprintf(&dir, "%s/old", tmpdir);
- fifo1 = make_fifo(dir, ent1->name);
- free(dir);
+ asprintf(&dir1, "%s/XXXXXXX", tmpdir);
+ if (mkdtemp(dir1) == NULL)
+ errx(EXIT_FAILURE, "error, failed to create a temporary directory (%s).", dir1);
+ fifo1 = make_fifo(dir1, ent1->id);
+ //free(dir1);
if (strlen(path) > 0)
asprintf(&label1, "%s/%s", path, ent1->name);
else
asprintf(&label1, "%s", ent1->name);
}
if(ent2 != NULL) {
- asprintf(&dir, "%s/new", tmpdir);
- fifo2 = make_fifo(dir, ent2->name);
- free(dir);
+ asprintf(&dir2, "%s/XXXXXXX", tmpdir);
+ if (mkdtemp(dir2) == NULL)
+ errx(EXIT_FAILURE, "error, failed to create a temporary directory (%s).", dir2);
+ fifo2 = make_fifo(dir2, ent2->id);
+ //free(dir);
if (strlen(path) > 0)
asprintf(&label2, "%s/%s", path, ent2->name);
else
@@ -200,29 +197,33 @@ ext_diff(struct session *s, const char *tmpdir, struct dirent *ent1, struct dire
}
waitpid(pid1, NULL, 0);
}
+ remove(dir1);
+ remove(dir2);
+ free(dir1);
+ free(dir2);
free(fifo1);
free(fifo2);
}
static void
-diff_r(struct session *s, struct dir *d1, struct dir *d2, const char *p)
+diff_r(struct session *s, struct dir *d1, struct dir *d2, const char *p, const char *tmpdir)
{
- char *tmpdir = NULL;
char *pnext = NULL;
struct dir *child1, *child2;
struct dirent *ent1 = NULL, *ent2 = NULL;
- tmpdir = make_tmpdir();
-
+ if (d1 == NULL && d2 == NULL)
+ return;
+ if (p == NULL || tmpdir == NULL)
+ return;
if (d1 != NULL)
ent1 = d1->children;
if (d2 != NULL)
ent2 = d2->children;
- if (p == NULL)
- return;
if (*p == '/')
p++;
+
while (1) {
if (ent1 == NULL && ent2 == NULL)
break;
@@ -234,7 +235,7 @@ diff_r(struct session *s, struct dir *d1, struct dir *d2, const char *p)
asprintf(&pnext, "%s/%s", p, ent2->name);
child2 = baseline_dir_new();
s->db_ops->select_dir(s->db_ctx, ent2->id, child2);
- diff_r(s, NULL, child2, pnext);
+ diff_r(s, NULL, child2, pnext, tmpdir);
baseline_dir_free(child2);
free(pnext);
}
@@ -250,7 +251,7 @@ diff_r(struct session *s, struct dir *d1, struct dir *d2, const char *p)
asprintf(&pnext, "%s/%s", p, ent1->name);
child1 = baseline_dir_new();
s->db_ops->select_dir(s->db_ctx, ent1->id, child1);
- diff_r(s, child1, NULL, pnext);
+ diff_r(s, child1, NULL, pnext, tmpdir);
baseline_dir_free(child1);
free(pnext);
}
@@ -270,7 +271,7 @@ diff_r(struct session *s, struct dir *d1, struct dir *d2, const char *p)
asprintf(&pnext, "%s/%s", p, ent2->name);
child2 = baseline_dir_new();
s->db_ops->select_dir(s->db_ctx, ent2->id, child2);
- diff_r(s, NULL, child2, pnext);
+ diff_r(s, NULL, child2, pnext, tmpdir);
baseline_dir_free(child2);
free(pnext);
}
@@ -279,7 +280,7 @@ diff_r(struct session *s, struct dir *d1, struct dir *d2, const char *p)
asprintf(&pnext, "%s/%s", p, ent1->name);
child1 = baseline_dir_new();
s->db_ops->select_dir(s->db_ctx, ent1->id, child1);
- diff_r(s, child1, NULL, pnext);
+ diff_r(s, child1, NULL, pnext, tmpdir);
baseline_dir_free(child1);
free(pnext);
/* add new file */
@@ -291,7 +292,7 @@ diff_r(struct session *s, struct dir *d1, struct dir *d2, const char *p)
child2 = baseline_dir_new();
s->db_ops->select_dir(s->db_ctx, ent1->id, child1);
s->db_ops->select_dir(s->db_ctx, ent2->id, child2);
- diff_r(s, child1, child2, pnext);
+ diff_r(s, child1, child2, pnext, tmpdir);
baseline_dir_free(child1);
baseline_dir_free(child2);
free(pnext);
@@ -310,7 +311,7 @@ diff_r(struct session *s, struct dir *d1, struct dir *d2, const char *p)
asprintf(&pnext, "%s/%s", p, ent1->name);
child1 = baseline_dir_new();
s->db_ops->select_dir(s->db_ctx, ent1->id, child1);
- diff_r(s, child1, NULL, pnext);
+ diff_r(s, child1, NULL, pnext, tmpdir);
baseline_dir_free(child1);
free(pnext);
}
@@ -326,7 +327,7 @@ diff_r(struct session *s, struct dir *d1, struct dir *d2, const char *p)
asprintf(&pnext, "%s/%s", p, ent2->name);
child2 = baseline_dir_new();
s->db_ops->select_dir(s->db_ctx, ent2->id, child2);
- diff_r(s, NULL, child2, pnext);
+ diff_r(s, NULL, child2, pnext, tmpdir);
baseline_dir_free(child2);
free(pnext);
}
@@ -354,7 +355,7 @@ cmd_diff(int argc, char **argv)
baseline_session_begin(&s, 0);
- //tmpdir = make_tmpdir();
+ tmpdir = make_tmpdir();
/*
s.db_ops->branch_get_head(s.db_ctx, s.branch, &head);
@@ -374,7 +375,7 @@ cmd_diff(int argc, char **argv)
dir_new = baseline_dir_new();
s.db_ops->select_dir(s.db_ctx, comm_new->dir, dir_new);
- diff_r(&s, dir_old, dir_new, "");
+ diff_r(&s, dir_old, dir_new, "", tmpdir);
baseline_dir_free(dir_old);
baseline_dir_free(dir_new);