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