2005-04-21 16:53:53 +02:00
|
|
|
/* Utility routines for Minix tests.
|
|
|
|
* This is designed to be #includ'ed near the top of test programs. It is
|
|
|
|
* self-contained except for MAX_ERRORS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
2010-08-02 13:16:32 +02:00
|
|
|
#include <sys/statvfs.h>
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
int common_test_nr = -1, errct = 0, subtest;
|
|
|
|
|
2013-01-25 02:00:10 +01:00
|
|
|
#define e(errn) e_f(__FILE__, __LINE__, (errn))
|
|
|
|
|
2012-03-24 16:16:34 +01:00
|
|
|
void cleanup(void);
|
|
|
|
int does_fs_truncate(void);
|
2013-01-25 02:00:10 +01:00
|
|
|
void e_f(char *file, int lineno, int n);
|
2012-03-24 16:16:34 +01:00
|
|
|
int name_max(char *path);
|
|
|
|
void quit(void);
|
|
|
|
void rm_rf_dir(int test_nr);
|
|
|
|
void rm_rf_ppdir(int test_nr);
|
|
|
|
void start(int test_nr);
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
void start(test_nr)
|
|
|
|
int test_nr;
|
|
|
|
{
|
|
|
|
char buf[64];
|
2011-12-16 15:45:53 +01:00
|
|
|
int i;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
common_test_nr = test_nr;
|
|
|
|
printf("Test %2d ", test_nr);
|
|
|
|
fflush(stdout); /* since stdout is probably line buffered */
|
|
|
|
sync();
|
|
|
|
rm_rf_dir(test_nr);
|
|
|
|
sprintf(buf, "mkdir DIR_%02d", test_nr);
|
|
|
|
if (system(buf) != 0) {
|
|
|
|
e(666);
|
|
|
|
quit();
|
|
|
|
}
|
|
|
|
sprintf(buf, "DIR_%02d", test_nr);
|
|
|
|
if (chdir(buf) != 0) {
|
|
|
|
e(6666);
|
|
|
|
quit();
|
|
|
|
}
|
2011-12-16 15:45:53 +01:00
|
|
|
|
|
|
|
for (i = 3; i < OPEN_MAX; ++i) {
|
|
|
|
/* Close all files except stdin, stdout, and stderr */
|
|
|
|
(void) close(i);
|
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
|
2010-08-02 13:16:32 +02:00
|
|
|
int does_fs_truncate(void)
|
|
|
|
{
|
|
|
|
struct statvfs stvfs;
|
|
|
|
int does_truncate = 0;
|
|
|
|
char cwd[PATH_MAX]; /* Storage for path to current working dir */
|
|
|
|
|
|
|
|
if (realpath(".", cwd) == NULL) e(7777); /* Get current working dir */
|
|
|
|
if (statvfs(cwd, &stvfs) != 0) e(7778); /* Get FS information */
|
|
|
|
/* Depending on how an FS handles too long file names, we have to adjust our
|
|
|
|
* error checking. If an FS does not truncate file names, it should generate
|
|
|
|
* an ENAMETOOLONG error when we provide too long a file name.
|
|
|
|
*/
|
|
|
|
if (!(stvfs.f_flag & ST_NOTRUNC)) does_truncate = 1;
|
|
|
|
|
|
|
|
return(does_truncate);
|
|
|
|
}
|
|
|
|
|
2011-08-26 15:16:16 +02:00
|
|
|
int name_max(char *path)
|
|
|
|
{
|
|
|
|
struct statvfs stvfs;
|
|
|
|
|
|
|
|
if (statvfs(path, &stvfs) != 0) e(7779);
|
|
|
|
return(stvfs.f_namemax);
|
|
|
|
}
|
|
|
|
|
2010-08-02 13:16:32 +02:00
|
|
|
|
2005-04-21 16:53:53 +02:00
|
|
|
void rm_rf_dir(test_nr)
|
|
|
|
int test_nr;
|
|
|
|
{
|
|
|
|
char buf[128];
|
|
|
|
|
|
|
|
sprintf(buf, "rm -rf DIR_%02d >/dev/null 2>&1", test_nr);
|
|
|
|
if (system(buf) != 0) printf("Warning: system(\"%s\") failed\n", buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rm_rf_ppdir(test_nr)
|
|
|
|
int test_nr;
|
|
|
|
{
|
|
|
|
/* Attempt to remove everything in the test directory (== the current dir). */
|
|
|
|
|
|
|
|
char buf[128];
|
|
|
|
|
|
|
|
sprintf(buf, "chmod 777 ../DIR_%02d/* ../DIR_%02d/*/* >/dev/null 2>&1",
|
|
|
|
test_nr, test_nr);
|
|
|
|
(void) system(buf);
|
|
|
|
sprintf(buf, "rm -rf ../DIR_%02d >/dev/null 2>&1", test_nr);
|
|
|
|
if (system(buf) != 0) printf("Warning: system(\"%s\") failed\n", buf);
|
|
|
|
}
|
|
|
|
|
2013-01-25 02:00:10 +01:00
|
|
|
void e_f(char *file, int line, int n)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-08-02 13:16:32 +02:00
|
|
|
int err_number;
|
|
|
|
err_number = errno; /* Store before printf can clobber it */
|
2005-04-21 16:53:53 +02:00
|
|
|
if (errct == 0) printf("\n"); /* finish header */
|
2013-01-25 02:00:10 +01:00
|
|
|
printf("%s:%d: Subtest %d, error %d, errno %d: %s\n",
|
|
|
|
file, line, subtest, n, errno, strerror(errno));
|
2012-12-07 10:49:34 +01:00
|
|
|
if (++errct > MAX_ERROR) {
|
2005-04-21 16:53:53 +02:00
|
|
|
printf("Too many errors; test aborted\n");
|
|
|
|
cleanup();
|
|
|
|
exit(1);
|
|
|
|
}
|
2010-08-02 13:16:32 +02:00
|
|
|
errno = err_number;
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cleanup()
|
|
|
|
{
|
|
|
|
if (chdir("..") == 0 && common_test_nr != -1) rm_rf_dir(common_test_nr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void quit()
|
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
if (errct == 0) {
|
|
|
|
printf("ok\n");
|
|
|
|
exit(0);
|
|
|
|
} else {
|
|
|
|
printf("%d errors\n", errct);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|