librazor/test-lua.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Feb 09 20:45:27 2012 +0000 (2012-02-09)
changeset 418 33b825d3128d
parent 376 d15a16347c77
child 424 8cbc438cc298
permissions -rw-r--r--
Add transaction barriers
These allow packages to be installed and removed which have scripts
that depend on each other when atomic transactions are involved.
Note that yum supports pre, but not other requires flags. post will
need similar support to the post scripts themselves pulling in the
requires flags from the rpms. Likewise preun and postun will need
similar handling to those scrips since the requires flags will need
to be stored in the razor database.
ali@352
     1
/*
ali@352
     2
 * Copyright (C) 2009  J. Ali Harlow <ali@juiblex.co.uk>
ali@352
     3
 *
ali@352
     4
 * This program is free software; you can redistribute it and/or modify
ali@352
     5
 * it under the terms of the GNU General Public License as published by
ali@352
     6
 * the Free Software Foundation; either version 2 of the License, or
ali@352
     7
 * (at your option) any later version.
ali@352
     8
 *
ali@352
     9
 * This program is distributed in the hope that it will be useful,
ali@352
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ali@352
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ali@352
    12
 * GNU General Public License for more details.
ali@352
    13
 *
ali@352
    14
 * You should have received a copy of the GNU General Public License along
ali@352
    15
 * with this program; if not, write to the Free Software Foundation, Inc.,
ali@352
    16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ali@352
    17
 */
ali@352
    18
ali@352
    19
#include "config.h"
ali@352
    20
ali@352
    21
#include <stdlib.h>
ali@352
    22
#include <stdio.h>
ali@352
    23
#include <string.h>
ali@378
    24
#include <errno.h>
ali@352
    25
#include <sys/types.h>
ali@352
    26
#include <sys/stat.h>
ali@352
    27
#include <dirent.h>
ali@352
    28
#include <unistd.h>
ali@352
    29
#include <math.h>
ali@352
    30
#include "razor-internal.h"
ali@352
    31
ali@352
    32
/*
ali@352
    33
 * This kludge is to work around an apparent bug in Fedora 10's lua package
ali@352
    34
 * (it appears to require libm but not to include a DT_NEEDED).
ali@352
    35
 */
ali@352
    36
ali@352
    37
void (*kludge)() = (void (*)())sin;
ali@352
    38
ali@352
    39
static void recursive_remove(const char *directory)
ali@352
    40
{
ali@352
    41
	DIR *dp;
ali@352
    42
	struct dirent *dirp;
ali@352
    43
	char *buf;
ali@352
    44
ali@352
    45
	dp = opendir(directory);
ali@352
    46
	while((dirp = readdir(dp))) {
ali@352
    47
		if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
ali@352
    48
			buf = malloc(strlen(directory) + strlen(dirp->d_name)
ali@352
    49
				     + 2);
ali@352
    50
			sprintf(buf, "%s/%s", directory, dirp->d_name);
ali@352
    51
			if (remove(buf) < 0)
ali@352
    52
				recursive_remove(buf);
ali@352
    53
			free(buf);
ali@352
    54
		}
ali@352
    55
	}
ali@352
    56
ali@352
    57
	rmdir(directory);
ali@352
    58
}
ali@352
    59
ali@352
    60
int main(int argc, char *argv[])
ali@352
    61
{
ali@352
    62
	char root[] = "/tmp/razor.XXXXXX";
ali@352
    63
	int r;
ali@352
    64
	void *script;
ali@352
    65
	size_t len;
ali@378
    66
	char *s, *test_file, *srcdir;
ali@352
    67
	FILE *fp;
ali@352
    68
ali@352
    69
	if (argc > 2) {
ali@352
    70
		fprintf(stderr, "usage: %s [TESTS-FILE]\n", argv[0]);
ali@352
    71
		exit(1);
ali@352
    72
	}
ali@352
    73
	if (argc == 2)
ali@352
    74
		test_file = argv[1];
ali@352
    75
	else
ali@352
    76
		test_file = "test.lua";
ali@352
    77
ali@352
    78
	if (!mkdtemp(root)) {
ali@352
    79
		perror(root);
ali@352
    80
		exit(1);
ali@352
    81
	}
ali@352
    82
ali@352
    83
	s = malloc(strlen(root) + strlen("/testfile") + 1);
ali@352
    84
	strcpy(s, root);
ali@352
    85
	strcat(s, "/testfile");
ali@352
    86
	fp = fopen(s, "w");
ali@352
    87
	if (!fp) {
ali@352
    88
		perror(s);
ali@352
    89
		exit(1);
ali@352
    90
	}
ali@352
    91
	fprintf(fp, "#!" LUA_BINARY "\n"
ali@352
    92
	  "print('Abracadabra!')\n");
ali@352
    93
	fchmod(fileno(fp), S_IRUSR | S_IWUSR | S_IXUSR);
ali@352
    94
	fclose(fp);
ali@352
    95
	free(s);
ali@352
    96
ali@352
    97
	script = razor_file_get_contents(test_file, &len);
ali@378
    98
	if (!script) {
ali@378
    99
		srcdir = getenv("srcdir");
ali@378
   100
		if (srcdir && errno == ENOENT && *test_file != '/') {
ali@378
   101
			s = malloc(strlen(srcdir) + strlen(test_file) + 2);
ali@378
   102
			strcpy(s, srcdir);
ali@378
   103
			strcat(s, "/");
ali@378
   104
			strcat(s, test_file);
ali@378
   105
			script = razor_file_get_contents(s, &len);
ali@378
   106
			if (!script) {
ali@378
   107
				perror(s);
ali@378
   108
				exit(1);
ali@378
   109
			}
ali@378
   110
			free(s);
ali@378
   111
		} else {
ali@378
   112
			perror(test_file);
ali@378
   113
			exit(1);
ali@378
   114
		}
ali@378
   115
	}
ali@376
   116
	r = run_lua_script(root, test_file, script, len, -1);
ali@352
   117
	razor_file_free_contents(script, len);
ali@352
   118
ali@352
   119
	recursive_remove(root);
ali@352
   120
	exit(r ? 1 : 0);
ali@352
   121
}