summaryrefslogtreecommitdiffstats
path: root/kernel/tests/lib/tst_resource.c
blob: 0b9b381f12a9296c505120ab327a3571f1f23333 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (C) 2012 Cyril Hrubis chrubis@suse.cz
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Further, this software is distributed without any warranty that it is
 * free of the rightful claim of any third person regarding infringement
 * or the like.  Any license provided herein, whether implied or
 * otherwise, applies only to this software file.  Patent licenses, if
 * any, provided herein do not apply to combinations of this program with
 * other software, or any other product whatsoever.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <pthread.h>
#include "test.h"
#include "old_resource.h"
#include "ltp_priv.h"

#ifndef PATH_MAX
#ifdef MAXPATHLEN
#define PATH_MAX	MAXPATHLEN
#else
#define PATH_MAX	1024
#endif
#endif

static pthread_mutex_t tmutex = PTHREAD_MUTEX_INITIALIZER;
static char dataroot[PATH_MAX];
extern char *TCID;

static void tst_dataroot_init(void)
{
	const char *ltproot = getenv("LTPROOT");
	char curdir[PATH_MAX];
	const char *startdir;
	int ret;

	/* 1. if LTPROOT is set, use $LTPROOT/testcases/data/$TCID
	 * 2. else if startwd is set by tst_tmpdir(), use $STARWD/datafiles
	 * 3. else use $CWD/datafiles */
	if (ltproot) {
		ret = snprintf(dataroot, PATH_MAX, "%s/testcases/data/%s",
			ltproot, TCID);
	} else {
		startdir = tst_get_startwd();
		if (startdir[0] == 0) {
			if (getcwd(curdir, PATH_MAX) == NULL) {
				tst_brkm(TBROK | TERRNO, NULL,
					"tst_dataroot getcwd");
				return;
			}
			startdir = curdir;
		}
		ret = snprintf(dataroot, PATH_MAX, "%s/datafiles", startdir);
	}

	if (ret < 0 || ret >= PATH_MAX)
		tst_brkm(TBROK, NULL, "tst_dataroot snprintf: %d", ret);
}

const char *tst_dataroot(void)
{
	if (dataroot[0] == 0) {
		pthread_mutex_lock(&tmutex);
		if (dataroot[0] == 0)
			tst_dataroot_init();
		pthread_mutex_unlock(&tmutex);
	}
	return dataroot;
}

static int file_copy(const char *file, const int lineno,
                     void (*cleanup_fn)(void), const char *path,
		     const char *filename, const char *dest)
{
	size_t len = strlen(path) + strlen(filename) + 2;
	char buf[len];

	snprintf(buf, sizeof(buf), "%s/%s", path, filename);

	/* check if file exists */
	if (access(buf, R_OK))
		return 0;

	safe_cp(file, lineno, cleanup_fn, buf, dest);

	return 1;
}

void tst_resource_copy(const char *file, const int lineno,
                       void (*cleanup_fn)(void),
		       const char *filename, const char *dest)
{
	if (!tst_tmpdir_created()) {
		tst_brkm(TBROK, cleanup_fn,
		         "Temporary directory doesn't exist at %s:%d",
		         file, lineno);
		return;
	}

	if (dest == NULL)
		dest = ".";

	const char *ltproot = getenv("LTPROOT");
	const char *dataroot = tst_dataroot();

	/* look for data files in $LTP_DATAROOT, $LTPROOT/testcases/bin
	 * and $CWD */
	if (file_copy(file, lineno, cleanup_fn, dataroot, filename, dest))
		return;

	if (ltproot != NULL) {
		char buf[strlen(ltproot) + 64];
		
		snprintf(buf, sizeof(buf), "%s/testcases/bin", ltproot);
		
		if (file_copy(file, lineno, cleanup_fn, buf, filename, dest))
			return;
	}

	/* try directory test started in as last resort */
	const char *startwd = tst_get_startwd();
	if (file_copy(file, lineno, cleanup_fn, startwd, filename, dest))
		return;

	tst_brkm(TBROK, cleanup_fn, "Failed to copy resource '%s' at %s:%d",
	         filename, file, lineno);
}