• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_tmpdir(), 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 				return;
61 			}
62 			startdir = curdir;
63 		}
64 		ret = snprintf(dataroot, PATH_MAX, "%s/datafiles", startdir);
65 	}
66 
67 	if (ret < 0 || ret >= PATH_MAX)
68 		tst_brkm(TBROK, NULL, "tst_dataroot snprintf: %d", ret);
69 }
70 
tst_dataroot(void)71 const char *tst_dataroot(void)
72 {
73 	if (dataroot[0] == 0) {
74 		pthread_mutex_lock(&tmutex);
75 		if (dataroot[0] == 0)
76 			tst_dataroot_init();
77 		pthread_mutex_unlock(&tmutex);
78 	}
79 	return dataroot;
80 }
81 
file_copy(const char * file,const int lineno,void (* cleanup_fn)(void),const char * path,const char * filename,const char * dest)82 static int file_copy(const char *file, const int lineno,
83                      void (*cleanup_fn)(void), const char *path,
84 		     const char *filename, const char *dest)
85 {
86 	size_t len = strlen(path) + strlen(filename) + 2;
87 	char buf[len];
88 
89 	snprintf(buf, sizeof(buf), "%s/%s", path, filename);
90 
91 	/* check if file exists */
92 	if (access(buf, R_OK))
93 		return 0;
94 
95 	safe_cp(file, lineno, cleanup_fn, buf, dest);
96 
97 	return 1;
98 }
99 
tst_resource_copy(const char * file,const int lineno,void (* cleanup_fn)(void),const char * filename,const char * dest)100 void tst_resource_copy(const char *file, const int lineno,
101                        void (*cleanup_fn)(void),
102 		       const char *filename, const char *dest)
103 {
104 	if (!tst_tmpdir_created()) {
105 		tst_brkm_(file, lineno, TBROK, cleanup_fn,
106 			"Temporary directory doesn't exist");
107 		return;
108 	}
109 
110 	if (dest == NULL)
111 		dest = ".";
112 
113 	const char *ltproot = getenv("LTPROOT");
114 	const char *dataroot = tst_dataroot();
115 
116 	/* look for data files in $LTP_DATAROOT, $LTPROOT/testcases/bin
117 	 * and $CWD */
118 	if (file_copy(file, lineno, cleanup_fn, dataroot, filename, dest))
119 		return;
120 
121 	if (ltproot != NULL) {
122 		char buf[strlen(ltproot) + 64];
123 
124 		snprintf(buf, sizeof(buf), "%s/testcases/bin", ltproot);
125 
126 		if (file_copy(file, lineno, cleanup_fn, buf, filename, dest))
127 			return;
128 	}
129 
130 	/* try directory test started in as last resort */
131 	const char *startwd = tst_get_startwd();
132 	if (file_copy(file, lineno, cleanup_fn, startwd, filename, dest))
133 		return;
134 
135 	tst_brkm_(file, lineno, TBROK, cleanup_fn,
136 		"Failed to copy resource '%s'", filename);
137 }
138