1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (C) 2012 Cyril Hrubis chrubis@suse.cz
3f08c3bdfSopenharmony_ci *
4f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify it
5f08c3bdfSopenharmony_ci * under the terms of version 2 of the GNU General Public License as
6f08c3bdfSopenharmony_ci * published by the Free Software Foundation.
7f08c3bdfSopenharmony_ci *
8f08c3bdfSopenharmony_ci * This program is distributed in the hope that it would be useful, but
9f08c3bdfSopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
10f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * Further, this software is distributed without any warranty that it is
13f08c3bdfSopenharmony_ci * free of the rightful claim of any third person regarding infringement
14f08c3bdfSopenharmony_ci * or the like.  Any license provided herein, whether implied or
15f08c3bdfSopenharmony_ci * otherwise, applies only to this software file.  Patent licenses, if
16f08c3bdfSopenharmony_ci * any, provided herein do not apply to combinations of this program with
17f08c3bdfSopenharmony_ci * other software, or any other product whatsoever.
18f08c3bdfSopenharmony_ci *
19f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License along
20f08c3bdfSopenharmony_ci * with this program; if not, write the Free Software Foundation, Inc.,
21f08c3bdfSopenharmony_ci * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22f08c3bdfSopenharmony_ci */
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci#include <pthread.h>
25f08c3bdfSopenharmony_ci#include "test.h"
26f08c3bdfSopenharmony_ci#include "old_resource.h"
27f08c3bdfSopenharmony_ci#include "ltp_priv.h"
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_ci#ifndef PATH_MAX
30f08c3bdfSopenharmony_ci#ifdef MAXPATHLEN
31f08c3bdfSopenharmony_ci#define PATH_MAX	MAXPATHLEN
32f08c3bdfSopenharmony_ci#else
33f08c3bdfSopenharmony_ci#define PATH_MAX	1024
34f08c3bdfSopenharmony_ci#endif
35f08c3bdfSopenharmony_ci#endif
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_cistatic pthread_mutex_t tmutex = PTHREAD_MUTEX_INITIALIZER;
38f08c3bdfSopenharmony_cistatic char dataroot[PATH_MAX];
39f08c3bdfSopenharmony_ciextern char *TCID;
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_cistatic void tst_dataroot_init(void)
42f08c3bdfSopenharmony_ci{
43f08c3bdfSopenharmony_ci	const char *ltproot = getenv("LTPROOT");
44f08c3bdfSopenharmony_ci	char curdir[PATH_MAX];
45f08c3bdfSopenharmony_ci	const char *startdir;
46f08c3bdfSopenharmony_ci	int ret;
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	/* 1. if LTPROOT is set, use $LTPROOT/testcases/data/$TCID
49f08c3bdfSopenharmony_ci	 * 2. else if startwd is set by tst_tmpdir(), use $STARWD/datafiles
50f08c3bdfSopenharmony_ci	 * 3. else use $CWD/datafiles */
51f08c3bdfSopenharmony_ci	if (ltproot) {
52f08c3bdfSopenharmony_ci		ret = snprintf(dataroot, PATH_MAX, "%s/testcases/data/%s",
53f08c3bdfSopenharmony_ci			ltproot, TCID);
54f08c3bdfSopenharmony_ci	} else {
55f08c3bdfSopenharmony_ci		startdir = tst_get_startwd();
56f08c3bdfSopenharmony_ci		if (startdir[0] == 0) {
57f08c3bdfSopenharmony_ci			if (getcwd(curdir, PATH_MAX) == NULL) {
58f08c3bdfSopenharmony_ci				tst_brkm(TBROK | TERRNO, NULL,
59f08c3bdfSopenharmony_ci					"tst_dataroot getcwd");
60f08c3bdfSopenharmony_ci				return;
61f08c3bdfSopenharmony_ci			}
62f08c3bdfSopenharmony_ci			startdir = curdir;
63f08c3bdfSopenharmony_ci		}
64f08c3bdfSopenharmony_ci		ret = snprintf(dataroot, PATH_MAX, "%s/datafiles", startdir);
65f08c3bdfSopenharmony_ci	}
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	if (ret < 0 || ret >= PATH_MAX)
68f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "tst_dataroot snprintf: %d", ret);
69f08c3bdfSopenharmony_ci}
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_ciconst char *tst_dataroot(void)
72f08c3bdfSopenharmony_ci{
73f08c3bdfSopenharmony_ci	if (dataroot[0] == 0) {
74f08c3bdfSopenharmony_ci		pthread_mutex_lock(&tmutex);
75f08c3bdfSopenharmony_ci		if (dataroot[0] == 0)
76f08c3bdfSopenharmony_ci			tst_dataroot_init();
77f08c3bdfSopenharmony_ci		pthread_mutex_unlock(&tmutex);
78f08c3bdfSopenharmony_ci	}
79f08c3bdfSopenharmony_ci	return dataroot;
80f08c3bdfSopenharmony_ci}
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_cistatic int file_copy(const char *file, const int lineno,
83f08c3bdfSopenharmony_ci                     void (*cleanup_fn)(void), const char *path,
84f08c3bdfSopenharmony_ci		     const char *filename, const char *dest)
85f08c3bdfSopenharmony_ci{
86f08c3bdfSopenharmony_ci	size_t len = strlen(path) + strlen(filename) + 2;
87f08c3bdfSopenharmony_ci	char buf[len];
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_ci	snprintf(buf, sizeof(buf), "%s/%s", path, filename);
90f08c3bdfSopenharmony_ci
91f08c3bdfSopenharmony_ci	/* check if file exists */
92f08c3bdfSopenharmony_ci	if (access(buf, R_OK))
93f08c3bdfSopenharmony_ci		return 0;
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_ci	safe_cp(file, lineno, cleanup_fn, buf, dest);
96f08c3bdfSopenharmony_ci
97f08c3bdfSopenharmony_ci	return 1;
98f08c3bdfSopenharmony_ci}
99f08c3bdfSopenharmony_ci
100f08c3bdfSopenharmony_civoid tst_resource_copy(const char *file, const int lineno,
101f08c3bdfSopenharmony_ci                       void (*cleanup_fn)(void),
102f08c3bdfSopenharmony_ci		       const char *filename, const char *dest)
103f08c3bdfSopenharmony_ci{
104f08c3bdfSopenharmony_ci	if (!tst_tmpdir_created()) {
105f08c3bdfSopenharmony_ci		tst_brkm_(file, lineno, TBROK, cleanup_fn,
106f08c3bdfSopenharmony_ci			"Temporary directory doesn't exist");
107f08c3bdfSopenharmony_ci		return;
108f08c3bdfSopenharmony_ci	}
109f08c3bdfSopenharmony_ci
110f08c3bdfSopenharmony_ci	if (dest == NULL)
111f08c3bdfSopenharmony_ci		dest = ".";
112f08c3bdfSopenharmony_ci
113f08c3bdfSopenharmony_ci	const char *ltproot = getenv("LTPROOT");
114f08c3bdfSopenharmony_ci	const char *dataroot = tst_dataroot();
115f08c3bdfSopenharmony_ci
116f08c3bdfSopenharmony_ci	/* look for data files in $LTP_DATAROOT, $LTPROOT/testcases/bin
117f08c3bdfSopenharmony_ci	 * and $CWD */
118f08c3bdfSopenharmony_ci	if (file_copy(file, lineno, cleanup_fn, dataroot, filename, dest))
119f08c3bdfSopenharmony_ci		return;
120f08c3bdfSopenharmony_ci
121f08c3bdfSopenharmony_ci	if (ltproot != NULL) {
122f08c3bdfSopenharmony_ci		char buf[strlen(ltproot) + 64];
123f08c3bdfSopenharmony_ci
124f08c3bdfSopenharmony_ci		snprintf(buf, sizeof(buf), "%s/testcases/bin", ltproot);
125f08c3bdfSopenharmony_ci
126f08c3bdfSopenharmony_ci		if (file_copy(file, lineno, cleanup_fn, buf, filename, dest))
127f08c3bdfSopenharmony_ci			return;
128f08c3bdfSopenharmony_ci	}
129f08c3bdfSopenharmony_ci
130f08c3bdfSopenharmony_ci	/* try directory test started in as last resort */
131f08c3bdfSopenharmony_ci	const char *startwd = tst_get_startwd();
132f08c3bdfSopenharmony_ci	if (file_copy(file, lineno, cleanup_fn, startwd, filename, dest))
133f08c3bdfSopenharmony_ci		return;
134f08c3bdfSopenharmony_ci
135f08c3bdfSopenharmony_ci	tst_brkm_(file, lineno, TBROK, cleanup_fn,
136f08c3bdfSopenharmony_ci		"Failed to copy resource '%s'", filename);
137f08c3bdfSopenharmony_ci}
138