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