int main(int ac, char **av){
struct zpm zpm;
int rv;
- int status;
+ int status = 0;
int required = 0;
int fail = 0;
char *pkgstr;
char *rootdir = 0;
char *db = "/var/lib/zpm/local.db";
- char *script = "/var/tmp/zpm-script";
- char *output = "/var/tmp/zpm-script.out";
+ char *script = 0;
+ char *output = 0;
char *phase = 0;
- char *envvar = 0;
- int quiet = 0;
- int scriptishash = 0;
+ int quiet = 0, verbose = 0;
+ int allowempty = 0, notealways = 0, makenote = 0, showoutput = 0;
+ int scriptishash = 0, notenever = 0, noteifwritable = 0;
+ size_t outsize;
int mode = RUN;
if (getenv("ZPMDB")) {
/* ZPM_PACKAGE_FILE ? */
rootdir = getenv("ZPM_ROOT_DIR");
-
- envvar = getenv("ZPM_SCRIPT_FILE");
- if (envvar) {
- script = envvar;
- }
-
- envvar = getenv("ZPM_SCRIPT_OUTPUT");
- if (envvar) {
- output = envvar;
- }
+ script = getenv("ZPM_SCRIPT_FILE");
+ output = getenv("ZPM_SCRIPT_OUTPUT");
/* run, set, show, hash */
/* set -S, if -H set the hash, output hash, unless quiet
* show: zpm-script -l [-a] [-o outfile] <pkgid>
* show hash: zpm-script -lh <pkgid>
*/
- while ((opt = getopt(ac, av, "f:p:rslRFho:S:q")) != -1) {
+ while ((opt = getopt(ac, av, "f:p:rslRFho:S:qv")) != -1) {
switch (opt) {
case 'f': db = optarg; break;
case 'p': phase = optarg; break;
case 'l': mode = LIST; break;
case 'R': rootdir = optarg; break;
case 'F': required = 1; break;
-
+ case 'a': notealways = 1; break;
+ case 'n': notenever = 1; break;
+ case 'N': noteifwritable = 1; break;
+ case 'e': allowempty = 1; break;
case 'h': scriptishash = 1; break;
case 'o': output = optarg; break;
case 'S': script = optarg; break;
case 'q': quiet = 1; break;
+ case 'v': verbose = 1; break;
default:
usage();
if (!zpm_script_hash(&zpm, pkgid, phase, hash)) {
fail = SOFT;
+ status = EXIT_FAILURE;
goto cleanup;
}
if (!setdir(rootdir)) {
fail = HARD;
+ status = EXIT_FAILURE;
goto cleanup;
}
if (!zpm_extract(&zpm, hash, script, 0700)) {
fprintf(stderr, "unable to extract script");
fail = HARD;
+ status = EXIT_FAILURE;
zpm_note_add(&zpm, pkgid, NULL, hash, "unable to extract %s script", phase);
goto cleanup;
}
rv = run(script, args, output, &status);
- if (rv) {
- fprintf(stderr, "package %s script failed with code %d\n",
- pkgid, rv);
-
- zpm_import(&zpm, output, 0, hash);
- zpm_note_add(&zpm, pkgid, NULL, hash, "package %s script failed with code %d", phase, rv);
+ status = WEXITSTATUS(status);
+ if (status != 0) {
fail = HARD;
}
- /* TODO log output */
+ if (zpm_readonly(&zpm) && noteifwritable) {
+ notenever = 1;
+ }
+
+ if (output && !notenever) {
+ struct stat outstat;
+ stat(output, &outstat);
+ outsize = outstat.st_size;
+ if (notealways || (outsize && status && (outsize || allowempty))) {
+ makenote = 1;
+ }
+ }
+
+ if (makenote) {
+ char note[1024];
+
+ zpm_import(&zpm, output, 0, hash);
+
+ if (status) {
+ sprintf(note, "%.64s script failed with code %d",
+ phase, status);
+
+ fail = HARD;
+ } else {
+ sprintf(note, "%.64s script succeeded", phase);
+ }
+ zpm_note_add(&zpm, pkgid, NULL, hash, note);
+ }
+
+ if (output && showoutput) {
+ char buf[4096];
+ int out = open(output, O_RDONLY);
+ ssize_t bytes;
+ if (out == -1) {
+ perror("can't open output file");
+ } else {
+ while ((bytes = read(out, buf, sizeof buf)) > 0) {
+ write(1, buf, bytes);
+ }
+ if (bytes == -1) {
+ perror("output file read error");
+ }
+ }
+ if (out != -1) {
+ close(out);
+ }
+ }
+
if (script) {
unlink(script);
}
free(pkgid);
zpm_close(&zpm);
- return (fail == HARD || (required && fail)) ? EXIT_FAILURE : 0;
+ return (fail == HARD || (required && fail)) ? status : 0;
}