Cyril Hrubis | 69c2ab0 | 2012-12-12 17:22:29 +0100 | [diff] [blame] | 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 | |
Jan Stancek | f423b1a | 2014-10-02 14:18:20 +0200 | [diff] [blame] | 24 | #include <pthread.h> |
Cyril Hrubis | 69c2ab0 | 2012-12-12 17:22:29 +0100 | [diff] [blame] | 25 | #include "tst_resource.h" |
Cyril Hrubis | ed69cd5 | 2013-06-24 17:51:00 +0200 | [diff] [blame] | 26 | #include "ltp_priv.h" |
Cyril Hrubis | 69c2ab0 | 2012-12-12 17:22:29 +0100 | [diff] [blame] | 27 | |
Jan Stancek | f423b1a | 2014-10-02 14:18:20 +0200 | [diff] [blame] | 28 | #ifndef PATH_MAX |
| 29 | #ifdef MAXPATHLEN |
| 30 | #define PATH_MAX MAXPATHLEN |
| 31 | #else |
| 32 | #define PATH_MAX 1024 |
| 33 | #endif |
| 34 | #endif |
| 35 | |
| 36 | static pthread_mutex_t tmutex = PTHREAD_MUTEX_INITIALIZER; |
| 37 | static char dataroot[PATH_MAX]; |
| 38 | extern char *TCID; |
| 39 | |
| 40 | static 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 | |
| 68 | const 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 Hrubis | 69c2ab0 | 2012-12-12 17:22:29 +0100 | [diff] [blame] | 79 | static 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 Hrubis | 69c2ab0 | 2012-12-12 17:22:29 +0100 | [diff] [blame] | 97 | void 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 Stancek | f423b1a | 2014-10-02 14:18:20 +0200 | [diff] [blame] | 111 | 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 Hrubis | 69c2ab0 | 2012-12-12 17:22:29 +0100 | [diff] [blame] | 117 | |
| 118 | if (ltproot != NULL) { |
Cyril Hrubis | 69c2ab0 | 2012-12-12 17:22:29 +0100 | [diff] [blame] | 119 | char buf[strlen(ltproot) + 64]; |
Cyril Hrubis | 69c2ab0 | 2012-12-12 17:22:29 +0100 | [diff] [blame] | 120 | |
| 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 Stancek | f423b1a | 2014-10-02 14:18:20 +0200 | [diff] [blame] | 127 | /* try directory test started in as last resort */ |
Cyril Hrubis | 69c2ab0 | 2012-12-12 17:22:29 +0100 | [diff] [blame] | 128 | const char *startwd = tst_get_startwd(); |
Cyril Hrubis | 69c2ab0 | 2012-12-12 17:22:29 +0100 | [diff] [blame] | 129 | 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 | } |