commit ec33c2e787adbe8b080123c3fb4c30d1169df320
parent 111c160a73dae7ab000c9d652432eda929802b5f
Author: Josuah Demangeon <mail@josuah.net>
Date: Sat, 12 Jan 2019 11:09:46 +0100
apply patches
Diffstat:
M | fs.c | | | 110 | ++++++++++++++++++++++++++++++++++++++++++++----------------------------------- |
M | fs.h | | | 3 | +++ |
M | pack-build.c | | | 180 | +++++++++++++++++++++++++++++++++++++++++-------------------------------------- |
M | pack-dep.c | | | 2 | +- |
M | stralloc.c | | | 6 | ------ |
M | stralloc.h | | | 5 | +++-- |
6 files changed, 162 insertions(+), 144 deletions(-)
diff --git a/fs.c b/fs.c
@@ -10,6 +10,30 @@
#include "stralloc.h"
int
+fs_isdir(char *path)
+{
+ struct stat st;
+ if (stat(path, &st) == -1) return 0;
+ return S_ISDIR(st.st_mode);
+}
+
+int
+fs_islink(char *path)
+{
+ struct stat st;
+ if (stat(path, &st) == -1) return 0;
+ return S_ISLNK(st.st_mode);
+}
+
+int
+fs_isfile(char *path)
+{
+ struct stat st;
+ if (stat(path, &st) == -1) return 0;
+ return S_ISREG(st.st_mode);
+}
+
+int
fs_mkdirr(char *dir, unsigned int mode)
{
char *d = dir + (*dir == '/');
@@ -17,7 +41,6 @@ fs_mkdirr(char *dir, unsigned int mode)
while (d[n] != '\0') {
d += n = str_chr(d, '/');
-
*d = '\0';
if (mkdir(dir, mode) == -1 && errno != EEXIST) return 0;
*d = '/';
@@ -26,6 +49,28 @@ fs_mkdirr(char *dir, unsigned int mode)
return 1;
}
+int
+fs_copy(char const *from, char const *to, unsigned int mode)
+{
+ char buf[1024 * 8];
+ int fd_from = -1;
+ int fd_to = -1;
+ int r = -1;
+ int w = 1; /* for empty files */
+
+ fd_from = open_read(from); if (fd_from == -1) goto err;
+ fd_to = open_trunc(to); if (fd_to == -1) goto err;
+ while (1) {
+ r = read(fd_from, buf, sizeof buf); if (r <= 0) break;
+ w = write(fd_to, buf, r); if (w < r) break;
+ }
+ if (chmod(to, mode) == -1) goto err;
+
+err: close(fd_from);
+ close(fd_to);
+ return r == 0 && w > 0;
+}
+
static int
fs_rmdirr_recurse(stralloc *path)
{
@@ -36,7 +81,7 @@ fs_rmdirr_recurse(stralloc *path)
dp = opendir(path->x); if (dp == NULL) return 0;
- if (!stralloc_append(path, '/')) return 0;
+ if (!stralloc_cats(path, "/")) return 0;
n = path->n;
errno = 0;
@@ -46,8 +91,7 @@ fs_rmdirr_recurse(stralloc *path)
path->n = n;
if (!stralloc_cats(path, de->d_name)) break;
- if (!stralloc_append(path, '\0')) break;
-
+ if (!stralloc_0(path)) break;
if (lstat(path->x, &st) == -1) break;
if (S_ISDIR(st.st_mode)) {
@@ -68,38 +112,13 @@ fs_rmdirr(char const *dir)
stralloc path = STRALLOC_ZERO;
int ret = 0;
- if (!stralloc_copys(&path, dir)) goto err;
-
+ if (!stralloc_cats(&path, dir)) goto err;
ret = fs_rmdirr_recurse(&path);
err: stralloc_free(&path);
return ret;
}
-int
-fs_copy(char const *from, char const *to, unsigned int mode)
-{
- char buf[1024 * 8];
- int fd_from = -1;
- int fd_to = -1;
- int r = -1;
- int w = -1;
-
- fd_from = open_read(from); if (fd_from == -1) goto err;
- fd_to = open_trunc(to); if (fd_to == -1) goto err;
-
- if (chmod(to, mode) == -1) goto err;
-
- while (1) {
- r = read(fd_from, buf, sizeof buf); if (r <= 0) break;
- w = write(fd_to, buf, r); if (w < r) break;
- }
-
-err: close(fd_from);
- close(fd_to);
- return r == 0 && w == 0;
-}
-
static int
fs_copyr_recurse(stralloc *from, stralloc *to, int action)
{
@@ -110,32 +129,29 @@ fs_copyr_recurse(stralloc *from, stralloc *to, int action)
size_t to_n = to->n;
size_t from_n = from->n;
- dp = opendir(from->x); if (!dp) return 0;
-
- if (!stralloc_append(from, '/')) return 0;
- if (!stralloc_append(to, '/')) return 0;
+ if (!stralloc_cats(from, "/")) return 0;
+ if (!stralloc_cats(to, "/")) return 0;
from_n = from->n;
to_n = to->n;
+ dp = opendir(from->x); if (!dp) return 0;
+
errno = 0;
while ((de = readdir(dp))) {
if (str_equal(de->d_name, ".")) continue;
if (str_equal(de->d_name, "..")) continue;
- from->n = from_n;
- if (!stralloc_cats(from, de->d_name)) break;
- if (!stralloc_append(from, '\0')) break;
-
to->n = to_n;
if (!stralloc_cats(to, de->d_name)) break;
- if (!stralloc_append(to, '\0')) break;
+ if (!stralloc_0(to)) break;
+ from->n = from_n;
+ if (!stralloc_cats(from, de->d_name)) break;
+ if (!stralloc_0(from)) break;
if (lstat(from->x, &st) == -1) break;
- if (lstat(to->x, &st) == -1) break;
if (S_ISDIR(st.st_mode)) {
- if (lstat(to->x, &st) == 0) continue;
- if (mkdir(to->x, st.st_mode) == -1) break;
+ if (mkdir(to->x, st.st_mode) == -1 && errno != EEXIST) break;
if (!fs_copyr_recurse(from, to, action)) break;
} else if (S_ISREG(st.st_mode)) {
switch (action) {
@@ -163,9 +179,8 @@ fs_copyr(char const *from, char const *to)
stralloc sa_to = STRALLOC_ZERO;
int ret = 0;
- if (!stralloc_copys(&sa_from, from)) goto err;
- if (!stralloc_copys(&sa_to, to)) goto err;
-
+ if (!stralloc_cats(&sa_from, from)) goto err;
+ if (!stralloc_cats(&sa_to, to)) goto err;
ret = fs_copyr_recurse(&sa_from, &sa_to, 0);
err: stralloc_free(&sa_from);
@@ -180,9 +195,8 @@ fs_copyrl(char const *from, char const *to)
stralloc sa_to = STRALLOC_ZERO;
int ret = 0;
- if (!stralloc_copys(&sa_from, from)) goto err;
- if (!stralloc_copys(&sa_to, to)) goto err;
-
+ if (!stralloc_cats(&sa_from, from)) goto err;
+ if (!stralloc_cats(&sa_to, to)) goto err;
ret = fs_copyr_recurse(&sa_from, &sa_to, 1);
err: stralloc_free(&sa_from);
diff --git a/fs.h b/fs.h
@@ -4,6 +4,9 @@
int fs_copy(char const *, char const *, unsigned int);
int fs_copyr(char const *, char const *);
int fs_copyrl(char const *, char const *);
+int fs_isdir(char *);
+int fs_isfile(char *);
+int fs_islink(char *);
int fs_mkdirr(char *, unsigned int);
int fs_rmdirr(char const *);
diff --git a/pack-build.c b/pack-build.c
@@ -1,4 +1,3 @@
-#include <sys/stat.h>
#include <sys/types.h>
#include <ctype.h>
#include <sys/dirent.h>
@@ -10,62 +9,20 @@
#include "forkexec.h"
#include "fs.h"
#include "log.h"
+#include "open.h"
#include "pack.h"
#include "str.h"
-
-static int
-pack_data(pack const *p, char const *def, char const *src)
-{
- stralloc sa_def = STRALLOC_ZERO;
- stralloc sa_src = STRALLOC_ZERO;
- struct stat sb;
- size_t n;
- int ret = 0;
-
- stralloc_init(&sa_src);
- if (!stralloc_copys(&sa_src, src)) goto err;
- if (!stralloc_append(&sa_src, '/')) goto err;
- if (!stralloc_cats(&sa_src, p->name)) goto err;
- if (!stralloc_append(&sa_src, '/')) goto err;
- if (!stralloc_cats(&sa_src, p->ver)) goto err;
- if (!stralloc_append(&sa_src, '\0')) goto err;
-
- stralloc_init(&sa_def);
- if (!stralloc_copys(&sa_def, def)) goto err;
- if (!stralloc_append(&sa_def, '/')) goto err;
- if (!stralloc_cats(&sa_def, p->name)) goto err;
- if (!stralloc_append(&sa_def, '/')) goto err;
- n = sa_def.n;
- if (!stralloc_cats(&sa_def, "default/data")) goto err;
- if (!stralloc_append(&sa_def, '\0')) goto err;
-
- if (stat(sa_def.x, &sb) == 0)
- if (!fs_copyr(sa_def.x, sa_src.x)) goto err;
-
- sa_def.n = n;
- if (!stralloc_cats(&sa_def, p->ver)) goto err;
- if (!stralloc_cats(&sa_def, "/data")) goto err;
- if (!stralloc_append(&sa_def, '\0')) goto err;
-
- if (stat(sa_def.x, &sb) == 0)
- if (!fs_copyr(sa_def.x, sa_src.x)) goto err;
-
- ret = 1;
-
-err: stralloc_free(&sa_def);
- stralloc_free(&sa_src);
- return ret;
-}
+#include "stralloc.h"
static int
pack_envset(char const *name, char const *path)
{
char buf[NAME_MAX];
- int i;
+ int len;
- for (i = 0; name[i] != '\0'; ++i)
- buf[i] = isalnum(name[i]) ? name[i] : '_';
- buf[i] = '\0';
+ for (len = 0; name[len] != '\0'; ++len)
+ buf[len] = isalnum(name[len]) ? name[len] : '_';
+ buf[len] = '\0';
return env_set(buf, path);
}
@@ -82,13 +39,13 @@ pack_env(pack *p, const char *def, const char *out)
int ret = 0;
stralloc_init(&path);
- if (!stralloc_copys(&path, out)) goto err;
- if (!stralloc_append(&path, '/')) goto err;
+ if (!stralloc_cats(&path, out)) goto err;
+ if (!stralloc_cats(&path, "/")) goto err;
n = path.n;
if (!stralloc_cats(&path, p->name)) goto err;
- if (!stralloc_append(&path, '/')) goto err;
+ if (!stralloc_cats(&path, "/")) goto err;
if (!stralloc_cats(&path, p->ver)) goto err;
- if (!stralloc_append(&path, '\0')) goto err;
+ if (!stralloc_0(&path)) goto err;
pack_envset("PREFIX", path.x);
errno = 0;
@@ -100,16 +57,16 @@ pack_env(pack *p, const char *def, const char *out)
while (stralloc_zero(&line), buffer_getline(&b, &line)) {
pack new;
- stralloc_append(&line, '\0');
+ stralloc_0(&line);
m = pack_scan(&new, line.x); if (m == 0) goto err;
if (line.x[m] != '\n' && line.x[m] != '\0') goto err;
if (!pack_version(&new, def)) goto err;
path.n = n;
if (!stralloc_cats(&path, new.name)) goto err;
- if (!stralloc_append(&path, '/')) goto err;
+ if (!stralloc_cats(&path, "/")) goto err;
if (!stralloc_cats(&path, new.ver)) goto err;
- if (!stralloc_append(&path, '\0')) goto err;
+ if (!stralloc_0(&path)) goto err;
pack_envset(new.name, path.x);
}
@@ -120,8 +77,44 @@ err: stralloc_free(&path);
return ret;
}
-/* CWD must be set to the source directory */
-int
+/*
+ * CWD must be set to the source directory
+ */
+static int
+pack_data(pack const *p, char const *def)
+{
+ stralloc sa = STRALLOC_ZERO;
+ size_t n;
+ int ret = 0;
+
+ stralloc_init(&sa);
+ if (!stralloc_cats(&sa, def)) goto err;
+ if (!stralloc_cats(&sa, "/")) goto err;
+ if (!stralloc_cats(&sa, p->name)) goto err;
+ if (!stralloc_cats(&sa, "/")) goto err;
+ n = sa.n;
+ if (!stralloc_cats(&sa, "default/data")) goto err;
+ if (!stralloc_0(&sa)) goto err;
+
+ if (fs_isdir(sa.x)) if (!fs_copyr(sa.x, ".")) goto err;
+
+ sa.n = n;
+ if (!stralloc_cats(&sa, p->ver)) goto err;
+ if (!stralloc_cats(&sa, "/data")) goto err;
+ if (!stralloc_0(&sa)) goto err;
+
+ if (fs_isdir(sa.x)) if (!fs_copyr(sa.x, ".")) goto err;
+
+ ret = 1;
+
+err: stralloc_free(&sa);
+ return ret;
+}
+
+/*
+ * CWD must be set to the source directory
+ */
+static int
pack_build(pack const *p, char const *def)
{
stralloc path;
@@ -129,21 +122,21 @@ pack_build(pack const *p, char const *def)
size_t n;
stralloc_init(&path);
- if (!stralloc_copys(&path, def)) goto err;
- if (!stralloc_append(&path, '/')) goto err;
+ if (!stralloc_cats(&path, def)) goto err;
+ if (!stralloc_cats(&path, "/")) goto err;
if (!stralloc_cats(&path, p->name)) goto err;
- if (!stralloc_append(&path, '/')) goto err;
+ if (!stralloc_cats(&path, "/")) goto err;
n = path.n;
if (!stralloc_cats(&path, p->ver)) goto err;
if (!stralloc_cats(&path, "/build")) goto err;
- if (!stralloc_append(&path, '\0')) goto err;
+ if (!stralloc_0(&path)) goto err;
execv(path.x, argv);
if (!(errno == ENOTDIR || errno == ENOENT)) goto err;
path.n = n;
if (!stralloc_cats(&path, "default/build")) goto err;
- if (!stralloc_append(&path, '\0')) goto err;
+ if (!stralloc_0(&path)) goto err;
execv(path.x, argv);
if (!(errno == ENOTDIR || errno == ENOENT)) goto err;
@@ -152,19 +145,19 @@ err: stralloc_free(&path);
return 0;
}
-int
+static int
pack_chdir(pack const *p, char const *src)
{
stralloc path;
int ret = 0;
stralloc_init(&path);
- if (!stralloc_copys(&path, src)) goto err;
- if (!stralloc_append(&path, '/')) goto err;
+ if (!stralloc_cats(&path, src)) goto err;
+ if (!stralloc_cats(&path, "/")) goto err;
if (!stralloc_cats(&path, p->name)) goto err;
- if (!stralloc_append(&path, '/')) goto err;
+ if (!stralloc_cats(&path, "/")) goto err;
if (!stralloc_cats(&path, p->ver)) goto err;
- if (!stralloc_append(&path, '\0')) goto err;
+ if (!stralloc_0(&path)) goto err;
if (chdir(path.x) == -1) goto err;
@@ -174,11 +167,13 @@ err: stralloc_free(&path);
return ret;
}
-/* CWD must be set to the source directory */
-int
+/*
+ * CWD must be set to the source directory
+ */
+static int
pack_patch(pack const *p, char *def)
{
- char const *argv[] = { "patch", "-N1", NULL, NULL };
+ char *const argv[] = { "patch", "-N", NULL };
char const *d[2];
stralloc path;
size_t n;
@@ -186,23 +181,27 @@ pack_patch(pack const *p, char *def)
DIR *dp = NULL;
struct dirent *de;
int i;
+ int fd = -1;
int ret = 0;
+ write(1, "E\n", 2);
stralloc_init(&path);
- if (!stralloc_copys(&path, def)) goto err;
- if (!stralloc_append(&path, '/')) goto err;
+ if (!stralloc_cats(&path, def)) goto err;
+ if (!stralloc_cats(&path, "/")) goto err;
if (!stralloc_cats(&path, p->name)) goto err;
- if (!stralloc_append(&path, '/')) goto err;
+ if (!stralloc_cats(&path, "/")) goto err;
n = path.n;
+ dprintf(1, "%p\n", path.x);
+
d[0] = "default";
d[1] = p->ver;
for (i = 0; i < 2; ++i) {
path.n = n;
if (!stralloc_cats(&path, d[i])) goto err;
- if (!stralloc_cats(&path, "/patch")) goto err;
+ if (!stralloc_cats(&path, "/patch/")) goto err;
m = path.n;
- if (!stralloc_append(&path, '\0')) goto err;
+ if (!stralloc_0(&path)) goto err;
dp = opendir(path.x);
if (dp == NULL) {
if (errno == ENOTDIR || errno == ENOENT) continue;
@@ -215,15 +214,22 @@ pack_patch(pack const *p, char *def)
path.n = m;
if (!stralloc_cats(&path, de->d_name)) goto err;
- if (!stralloc_append(&path, '\0')) goto err;
- argv[2] = path.x;
+ if (!stralloc_0(&path)) goto err;
+ write(1, path.x, path.n);
+ fd = open_read(path.x); if (fd == -1) goto err;
+ if (dup2(fd, 0) == -1) goto err;
if (forkexec_wait(argv) != 0) goto err;
+ close(fd);
}
closedir(dp);
}
+ dprintf(1, "%p\n", path.x);
ret = 1;
-err: stralloc_free(&path);
+
+err: write(1, "E\n", 2);
+ stralloc_free(&path);
+ close(fd);
closedir(dp);
return 0;
}
@@ -237,7 +243,6 @@ main(int argc, char **argv)
char *src;
char *out;
pack p;
- struct stat st;
size_t n;
if (argc != 2) log_usage(*argv, "package[/version]");
@@ -246,10 +251,8 @@ main(int argc, char **argv)
src = env_get("PACK_SRC"); if (!src) src = PACK_SRC;
out = env_get("PACK_OUT"); if (!out) out = PACK_OUT;
- if (stat(def, &st) == -1) log_diesys1(101, def);
- if (!S_ISDIR(st.st_mode)) errno = ENOTDIR, log_diesys1(101, def);
- if (stat(src, &st) == -1) log_diesys1(101, src);
- if (!S_ISDIR(st.st_mode)) errno = ENOTDIR, log_diesys1(101, src);
+ if (!fs_isdir(def)) errno = ENOTDIR, log_diesys1(101, def);
+ if (!fs_isdir(src)) errno = ENOTDIR, log_diesys1(101, src);
++argv;
@@ -260,15 +263,18 @@ main(int argc, char **argv)
if (!pack_version(&p, def))
log_diesys2(100, "could not read the version of ", p.name);
- if (!pack_data(&p, def, src))
- log_diesys6(100, "copying ", p.name, "'s data from ", def, " to ", src);
-
if (!pack_env(&p, def, out))
log_diesys2(100, "could not set environment variables for ", *argv);
if (!pack_chdir(&p, src))
log_diesys1(100, "could not change directory to the source directory");
+ if (!pack_data(&p, def))
+ log_diesys6(100, "copying ", p.name, "'s data from ", def, " to ", src);
+
+ if (!pack_patch(&p, def))
+ log_diesys3(100, "patching ", p.name, "'s source");
+
if (!pack_build(&p, def))
log_diesys2(100, "could not execute build script of ", p.name);
diff --git a/pack-dep.c b/pack-dep.c
@@ -45,7 +45,7 @@ pack_dep(pack const *p, genalloc *packs, char const *def)
pack new;
size_t n;
- stralloc_append(&line, '\0');
+ stralloc_0(&line);
n = pack_scan(&new, line.x); if (n == 0) goto err;
if (line.x[n] != '\n' && line.x[n] != '\0') goto err;
if (!pack_version(&new, def)) goto err;
diff --git a/stralloc.c b/stralloc.c
@@ -51,12 +51,6 @@ stralloc_append(stralloc *sa, char c)
}
void
-stralloc_zero(stralloc *sa)
-{
- sa->n = 0;
-}
-
-void
stralloc_init(stralloc *sa)
{
sa->x = 0;
diff --git a/stralloc.h b/stralloc.h
@@ -12,6 +12,7 @@ typedef struct stralloc {
#define STRALLOC_ZERO { 0, 0, 0 }
+#define stralloc_0(sa) stralloc_catb((sa), "", 1)
#define stralloc_cat(sa1, sa2) stralloc_catb((sa1), (sa2)->x, (sa2)->n)
#define stralloc_catint(sa, i) stralloc_catlong0((sa), (i), 0)
#define stralloc_catint0(sa, i, n) stralloc_catlong0((sa), (i), (n))
@@ -20,9 +21,10 @@ typedef struct stralloc {
#define stralloc_catuint(sa, i) stralloc_catulong0((sa), (i), 0)
#define stralloc_catuint0(sa, i, n) stralloc_catulong0((sa), (i), (n))
#define stralloc_catulong(sa, l) stralloc_catulong0((sa), (l), 0)
-#define stralloc_copys(sa, buf) stralloc_copyb((sa), (buf), str_len(buf))
#define stralloc_copy(sa1, sa2) stralloc_copyb((sa1), (sa2)->x, (sa2)->n)
+#define stralloc_copys(sa, buf) stralloc_copyb((sa), (buf), str_len(buf))
#define stralloc_readyplus(sa, len) stralloc_ready((sa), (sa)->n + (len))
+#define stralloc_zero(sa) ((sa)->n = 0)
int stralloc_append(stralloc *, char);
int stralloc_catb(stralloc *, const char *, size_t);
@@ -33,6 +35,5 @@ int stralloc_ready(stralloc *, size_t);
int stralloc_starts(stralloc *, const char *);
void stralloc_free(stralloc *);
void stralloc_init(stralloc *);
-void stralloc_zero(stralloc *);
#endif