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 | |
| 24 | #include "tst_resource.h" |
Cyril Hrubis | ed69cd5 | 2013-06-24 17:51:00 +0200 | [diff] [blame] | 25 | #include "ltp_priv.h" |
Cyril Hrubis | 69c2ab0 | 2012-12-12 17:22:29 +0100 | [diff] [blame] | 26 | |
| 27 | static int file_copy(const char *file, const int lineno, |
| 28 | void (*cleanup_fn)(void), const char *path, |
| 29 | const char *filename, const char *dest) |
| 30 | { |
| 31 | size_t len = strlen(path) + strlen(filename) + 2; |
| 32 | char buf[len]; |
| 33 | |
| 34 | snprintf(buf, sizeof(buf), "%s/%s", path, filename); |
| 35 | |
| 36 | /* check if file exists */ |
| 37 | if (access(buf, R_OK)) |
| 38 | return 0; |
| 39 | |
| 40 | safe_cp(file, lineno, cleanup_fn, buf, dest); |
| 41 | |
| 42 | return 1; |
| 43 | } |
| 44 | |
Cyril Hrubis | 69c2ab0 | 2012-12-12 17:22:29 +0100 | [diff] [blame] | 45 | void tst_resource_copy(const char *file, const int lineno, |
| 46 | void (*cleanup_fn)(void), |
| 47 | const char *filename, const char *dest) |
| 48 | { |
| 49 | if (!tst_tmpdir_created()) { |
| 50 | tst_brkm(TBROK, cleanup_fn, |
| 51 | "Temporary directory doesn't exist at %s:%d", |
| 52 | file, lineno); |
| 53 | } |
| 54 | |
| 55 | if (dest == NULL) |
| 56 | dest = "."; |
| 57 | |
| 58 | const char *ltproot = getenv("LTPROOT"); |
| 59 | |
| 60 | if (ltproot != NULL) { |
| 61 | /* the data are either in testcases/data or testcases/bin */ |
| 62 | char buf[strlen(ltproot) + 64]; |
| 63 | |
| 64 | snprintf(buf, sizeof(buf), "%s/testcases/data", ltproot); |
| 65 | |
| 66 | if (file_copy(file, lineno, cleanup_fn, buf, filename, dest)) |
| 67 | return; |
| 68 | |
| 69 | snprintf(buf, sizeof(buf), "%s/testcases/bin", ltproot); |
| 70 | |
| 71 | if (file_copy(file, lineno, cleanup_fn, buf, filename, dest)) |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | const char *startwd = tst_get_startwd(); |
| 76 | |
| 77 | /* try directory test started int first */ |
| 78 | if (file_copy(file, lineno, cleanup_fn, startwd, filename, dest)) |
| 79 | return; |
| 80 | |
| 81 | tst_brkm(TBROK, cleanup_fn, "Failed to copy resource '%s' at %s:%d", |
| 82 | filename, file, lineno); |
| 83 | } |