blob: ffe59f202563173e45ba1ebe237f34cb024e51d6 [file] [log] [blame]
Cyril Hrubis69c2ab02012-12-12 17:22:29 +01001/*
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
Jan Stancekf423b1a2014-10-02 14:18:20 +020024#include <pthread.h>
Cyril Hrubis69c2ab02012-12-12 17:22:29 +010025#include "tst_resource.h"
Cyril Hrubised69cd52013-06-24 17:51:00 +020026#include "ltp_priv.h"
Cyril Hrubis69c2ab02012-12-12 17:22:29 +010027
Jan Stancekf423b1a2014-10-02 14:18:20 +020028#ifndef PATH_MAX
29#ifdef MAXPATHLEN
30#define PATH_MAX MAXPATHLEN
31#else
32#define PATH_MAX 1024
33#endif
34#endif
35
36static pthread_mutex_t tmutex = PTHREAD_MUTEX_INITIALIZER;
37static char dataroot[PATH_MAX];
38extern char *TCID;
39
40static void tst_dataroot_init(void)
41{
42 const char *ltproot = getenv("LTPROOT");
43 char curdir[PATH_MAX];
44 const char *startdir;
45 int ret;
46
47 /* 1. if LTPROOT is set, use $LTPROOT/testcases/data/$TCID
48 * 2. else if startwd is set by tst_tmdir(), use $STARWD/datafiles
49 * 3. else use $CWD/datafiles */
50 if (ltproot) {
51 ret = snprintf(dataroot, PATH_MAX, "%s/testcases/data/%s",
52 ltproot, TCID);
53 } else {
54 startdir = tst_get_startwd();
55 if (startdir[0] == 0) {
56 if (getcwd(curdir, PATH_MAX) == NULL)
57 tst_brkm(TBROK | TERRNO, NULL,
58 "tst_dataroot getcwd");
59 startdir = curdir;
60 }
61 ret = snprintf(dataroot, PATH_MAX, "%s/datafiles", startdir);
62 }
63
64 if (ret < 0 || ret >= PATH_MAX)
65 tst_brkm(TBROK, NULL, "tst_dataroot snprintf: %d", ret);
66}
67
68const char *tst_dataroot(void)
69{
70 if (dataroot[0] == 0) {
71 pthread_mutex_lock(&tmutex);
72 if (dataroot[0] == 0)
73 tst_dataroot_init();
74 pthread_mutex_unlock(&tmutex);
75 }
76 return dataroot;
77}
78
Cyril Hrubis69c2ab02012-12-12 17:22:29 +010079static int file_copy(const char *file, const int lineno,
80 void (*cleanup_fn)(void), const char *path,
81 const char *filename, const char *dest)
82{
83 size_t len = strlen(path) + strlen(filename) + 2;
84 char buf[len];
85
86 snprintf(buf, sizeof(buf), "%s/%s", path, filename);
87
88 /* check if file exists */
89 if (access(buf, R_OK))
90 return 0;
91
92 safe_cp(file, lineno, cleanup_fn, buf, dest);
93
94 return 1;
95}
96
Cyril Hrubis69c2ab02012-12-12 17:22:29 +010097void tst_resource_copy(const char *file, const int lineno,
98 void (*cleanup_fn)(void),
99 const char *filename, const char *dest)
100{
101 if (!tst_tmpdir_created()) {
102 tst_brkm(TBROK, cleanup_fn,
103 "Temporary directory doesn't exist at %s:%d",
104 file, lineno);
105 }
106
107 if (dest == NULL)
108 dest = ".";
109
110 const char *ltproot = getenv("LTPROOT");
Jan Stancekf423b1a2014-10-02 14:18:20 +0200111 const char *dataroot = tst_dataroot();
112
113 /* look for data files in $LTP_DATAROOT, $LTPROOT/testcases/bin
114 * and $CWD */
115 if (file_copy(file, lineno, cleanup_fn, dataroot, filename, dest))
116 return;
Cyril Hrubis69c2ab02012-12-12 17:22:29 +0100117
118 if (ltproot != NULL) {
Cyril Hrubis69c2ab02012-12-12 17:22:29 +0100119 char buf[strlen(ltproot) + 64];
Cyril Hrubis69c2ab02012-12-12 17:22:29 +0100120
121 snprintf(buf, sizeof(buf), "%s/testcases/bin", ltproot);
122
123 if (file_copy(file, lineno, cleanup_fn, buf, filename, dest))
124 return;
125 }
126
Jan Stancekf423b1a2014-10-02 14:18:20 +0200127 /* try directory test started in as last resort */
Cyril Hrubis69c2ab02012-12-12 17:22:29 +0100128 const char *startwd = tst_get_startwd();
Cyril Hrubis69c2ab02012-12-12 17:22:29 +0100129 if (file_copy(file, lineno, cleanup_fn, startwd, filename, dest))
130 return;
131
132 tst_brkm(TBROK, cleanup_fn, "Failed to copy resource '%s' at %s:%d",
133 filename, file, lineno);
134}