add patch to use Debian paths for resources
Sascha Steinbiss
4 years ago
0 | --- a/src/gth/bssm_param.c | |
1 | +++ b/src/gth/bssm_param.c | |
2 | @@ -29,6 +29,7 @@ | |
3 | #include "gth/gthprobdef.h" | |
4 | #include "gth/gthspeciestab.h" | |
5 | #include "gth/showbool.h" | |
6 | +#include "libgenomethreader/findfile.h" | |
7 | ||
8 | /* | |
9 | This is a collection of functions associated with | |
10 | @@ -451,42 +452,7 @@ | |
11 | if (gt_file_exists(filename)) | |
12 | gt_str_append_cstr(path, filename); | |
13 | else { | |
14 | - if (strchr(filename, GT_PATH_SEPARATOR)) { | |
15 | - gt_error_set(err, "filename \"%s\" contains illegal symbol '%c': the " | |
16 | - "path list specified by environment variable \"%s\" " | |
17 | - "cannot be searched for it", filename, | |
18 | - GT_PATH_SEPARATOR, BSSMENVNAME); | |
19 | - had_err = -1; | |
20 | - } | |
21 | - /* check for file path in environment variable */ | |
22 | - if (!had_err) | |
23 | - had_err = gt_file_find_in_env(path, filename, BSSMENVNAME, err); | |
24 | - if (!had_err && !gt_str_length(path)) { | |
25 | - gt_error_set(err, "file \"%s\" not found in directory list specified " | |
26 | - "by environment variable %s", filename, BSSMENVNAME); | |
27 | - had_err = -1; | |
28 | - } | |
29 | - if (!had_err) { | |
30 | - /* path found -> append filename */ | |
31 | - gt_str_append_char(path, GT_PATH_SEPARATOR); | |
32 | - gt_str_append_cstr(path, filename); | |
33 | - } | |
34 | - else { | |
35 | - /* check for file path relative to binary */ | |
36 | - int new_err = gt_file_find_exec_in_path(path, gt_error_get_progname(err), | |
37 | - NULL); | |
38 | - if (!new_err) { | |
39 | - gt_assert(gt_str_length(path)); | |
40 | - gt_str_append_char(path, GT_PATH_SEPARATOR); | |
41 | - gt_str_append_cstr(path, "bssm"); | |
42 | - gt_str_append_char(path, GT_PATH_SEPARATOR); | |
43 | - gt_str_append_cstr(path, filename); | |
44 | - if (gt_file_exists(gt_str_get(path))) { | |
45 | - gt_error_unset(err); | |
46 | - had_err = 0; | |
47 | - } | |
48 | - } | |
49 | - } | |
50 | + had_err = gth_find_file(filename, BSSMENVNAME, "bssm", path, err); | |
51 | } | |
52 | ||
53 | if (!had_err) { | |
54 | --- a/src/gth/input.c | |
55 | +++ b/src/gth/input.c | |
56 | @@ -30,6 +30,7 @@ | |
57 | #include "gth/input.h" | |
58 | #include "gth/md5_cache.h" | |
59 | #include "gth/parse_options.h" | |
60 | +#include "libgenomethreader/findfile.h" | |
61 | ||
62 | #define GTHFORWARD 1 /* run program to match forward */ | |
63 | #define GTHREVERSE (1 << 1) /* run program to match reverse */ | |
64 | @@ -653,45 +654,14 @@ | |
65 | { | |
66 | GtStr *path = gt_str_new(); | |
67 | int had_err = 0; | |
68 | + | |
69 | if (gt_file_exists(scorematrixfile)) { | |
70 | gt_str_set(path, scorematrixfile); | |
71 | return path; | |
72 | } | |
73 | - if (strchr(scorematrixfile, GT_PATH_SEPARATOR)) { | |
74 | - gt_error_set(err, "filename \"%s\" contains illegal symbol '%c': the path " | |
75 | - "list specified by environment variable \"%s\" cannot be " | |
76 | - "searched for it", scorematrixfile, GT_PATH_SEPARATOR, | |
77 | - GTHDATAENVNAME); | |
78 | - had_err = -1; | |
79 | - } | |
80 | - if (!had_err) | |
81 | - had_err = gt_file_find_in_env(path, scorematrixfile, GTHDATAENVNAME, err); | |
82 | - if (!had_err && !gt_str_length(path)) { | |
83 | - gt_error_set(err, "file \"%s\" not found in directory list specified by " | |
84 | - "environment variable %s", scorematrixfile, GTHDATAENVNAME); | |
85 | - had_err = -1; | |
86 | - } | |
87 | - if (!had_err) { | |
88 | - gt_assert(gt_str_length(path)); | |
89 | - /* path found -> append score matrix file name */ | |
90 | - gt_str_append_char(path, GT_PATH_SEPARATOR); | |
91 | - gt_str_append_cstr(path, scorematrixfile); | |
92 | - } | |
93 | - else { | |
94 | - /* check for file relative to binary */ | |
95 | - int new_err = gt_file_find_exec_in_path(path, gt_error_get_progname(err), | |
96 | - NULL); | |
97 | - if (!new_err) { | |
98 | - gt_str_append_char(path, GT_PATH_SEPARATOR); | |
99 | - gt_str_append_cstr(path, GTHDATADIRNAME); | |
100 | - gt_str_append_char(path, GT_PATH_SEPARATOR); | |
101 | - gt_str_append_cstr(path, scorematrixfile); | |
102 | - if (gt_file_exists(gt_str_get(path))) { | |
103 | - gt_error_unset(err); | |
104 | - had_err = 0; | |
105 | - } | |
106 | - } | |
107 | - } | |
108 | + | |
109 | + had_err = gth_find_file(scorematrixfile, GTHDATAENVNAME, GTHDATADIRNAME, path, | |
110 | + err); | |
111 | if (had_err) { | |
112 | gt_str_delete(path); | |
113 | return NULL; | |
114 | --- /dev/null | |
115 | +++ b/src/libgenomethreader/findfile.c | |
116 | @@ -0,0 +1,74 @@ | |
117 | +/* | |
118 | + Copyright (c) 2020 Sascha Steinbiss <sascha@steinbiss.name> | |
119 | + | |
120 | + Permission to use, copy, modify, and distribute this software for any | |
121 | + purpose with or without fee is hereby granted, provided that the above | |
122 | + copyright notice and this permission notice appear in all copies. | |
123 | + | |
124 | + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
125 | + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
126 | + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
127 | + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
128 | + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
129 | + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
130 | + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
131 | +*/ | |
132 | + | |
133 | +#include "findfile.h" | |
134 | +#include "core/fileutils_api.h" | |
135 | +#include "core/compat_api.h" | |
136 | + | |
137 | +int gth_find_file(const char *filename, const char *envname, | |
138 | + const char *dirname, GtStr *out, GtError *err) | |
139 | +{ | |
140 | + int had_err = 0; | |
141 | + const char **defaultpath; | |
142 | + static const char *defaultpaths[] = { | |
143 | + "/usr/share/genomethreader", | |
144 | + "/usr/local/share/genomethreader", | |
145 | + "/usr/lib/genomethreader", | |
146 | + "/usr/local/lib/genomethreader", | |
147 | + NULL | |
148 | + }; | |
149 | + | |
150 | + gt_str_reset(out); | |
151 | + /* check in directory given by envname */ | |
152 | + if (getenv(envname)) { | |
153 | + gt_str_append_cstr(out, getenv(envname)); | |
154 | + gt_str_append_char(out, GT_PATH_SEPARATOR); | |
155 | + gt_str_append_cstr(out, filename); | |
156 | + if (gt_file_exists(gt_str_get(out))) { | |
157 | + return 0; | |
158 | + } | |
159 | + } | |
160 | + | |
161 | + /* check for file relative to binary */ | |
162 | + gt_str_reset(out); | |
163 | + had_err = gt_file_find_exec_in_path(out, gt_error_get_progname(err), NULL); | |
164 | + if (!had_err) { | |
165 | + gt_str_append_char(out, GT_PATH_SEPARATOR); | |
166 | + gt_str_append_cstr(out, dirname); | |
167 | + gt_str_append_char(out, GT_PATH_SEPARATOR); | |
168 | + gt_str_append_cstr(out, filename); | |
169 | + if (gt_file_exists(gt_str_get(out))) { | |
170 | + return 0; | |
171 | + } | |
172 | + } | |
173 | + | |
174 | + /* check for file in set of default paths */ | |
175 | + for (defaultpath = defaultpaths; *defaultpath; defaultpath++) { | |
176 | + gt_str_reset(out); | |
177 | + gt_str_append_cstr(out, *defaultpath); | |
178 | + gt_str_append_char(out, GT_PATH_SEPARATOR); | |
179 | + gt_str_append_cstr(out, dirname); | |
180 | + gt_str_append_char(out, GT_PATH_SEPARATOR); | |
181 | + gt_str_append_cstr(out, filename); | |
182 | + if (gt_file_exists(gt_str_get(out))) { | |
183 | + return 0; | |
184 | + } | |
185 | + } | |
186 | + | |
187 | + gt_error_set(err, "could not find file '%s' in any of the search paths", | |
188 | + filename); | |
189 | + return -1; | |
190 | +} | |
191 | --- /dev/null | |
192 | +++ b/src/libgenomethreader/findfile.h | |
193 | @@ -0,0 +1,26 @@ | |
194 | +/* | |
195 | + Copyright (c) 2020 Sascha Steinbiss <sascha@steinbiss.name> | |
196 | + | |
197 | + Permission to use, copy, modify, and distribute this software for any | |
198 | + purpose with or without fee is hereby granted, provided that the above | |
199 | + copyright notice and this permission notice appear in all copies. | |
200 | + | |
201 | + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
202 | + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
203 | + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
204 | + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
205 | + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
206 | + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
207 | + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
208 | +*/ | |
209 | + | |
210 | +#ifndef GTH_FINDFILE_H | |
211 | +#define GTH_FINDFILE_H | |
212 | + | |
213 | +#include "core/str_api.h" | |
214 | +#include "core/error_api.h" | |
215 | + | |
216 | +int gth_find_file(const char *filename, const char *envname, | |
217 | + const char *dirname, GtStr *out, GtError *err); | |
218 | + | |
219 | +#endif | |
220 | --- a/src/libgenomethreader/gthmkvtree.c | |
221 | +++ b/src/libgenomethreader/gthmkvtree.c | |
222 | @@ -6,6 +6,7 @@ | |
223 | #include "core/ma_api.h" | |
224 | #include "gth/gthdef.h" | |
225 | #include "gth/gthoutput.h" | |
226 | +#include "libgenomethreader/findfile.h" | |
227 | #include "libgenomethreader/gthmkvtree.h" | |
228 | #include "types.h" | |
229 | #include "virtualdef.h" | |
230 | @@ -52,39 +53,14 @@ | |
231 | { | |
232 | int rval, had_err = 0; | |
233 | gt_error_check(err); | |
234 | - if (!getenv(GTHDATAENVNAME)) { | |
235 | - gt_error_set(err, "$%s not defined. Please set correctly", GTHDATAENVNAME); | |
236 | - had_err = -1; | |
237 | - } | |
238 | + | |
239 | + GtStr *path = gt_str_new(); | |
240 | + had_err = gth_find_file(mapping, GTHDATAENVNAME, GTHDATADIRNAME, path, err); | |
241 | if (!had_err) { | |
242 | - rval = snprintf(smapfile, PATH_MAX+1, "%s%c%s", getenv(GTHDATAENVNAME), | |
243 | - GT_PATH_SEPARATOR, mapping); | |
244 | - gt_assert(rval < PATH_MAX + 1); | |
245 | - if (!gt_file_exists(smapfile)) { | |
246 | - gt_error_set(err, "cannot open smap file '%s'", smapfile); | |
247 | - had_err = -1; | |
248 | - } | |
249 | - } | |
250 | - if (had_err) { | |
251 | - /* check for file relative to binary */ | |
252 | - GtStr *path; | |
253 | - int new_err; | |
254 | - path = gt_str_new(); | |
255 | - new_err = gt_file_find_exec_in_path(path, gt_error_get_progname(err), NULL); | |
256 | - if (!new_err) { | |
257 | - gt_str_append_char(path, GT_PATH_SEPARATOR); | |
258 | - gt_str_append_cstr(path, GTHDATADIRNAME); | |
259 | - gt_str_append_char(path, GT_PATH_SEPARATOR); | |
260 | - gt_str_append_cstr(path, mapping); | |
261 | - if (gt_file_exists(gt_str_get(path))) { | |
262 | - rval = snprintf(smapfile, PATH_MAX+1, "%s", gt_str_get(path)); | |
263 | - gt_assert(rval < PATH_MAX + 1); | |
264 | - gt_error_unset(err); | |
265 | - had_err = 0; | |
266 | - } | |
267 | - } | |
268 | - gt_str_delete(path); | |
269 | + rval = snprintf(smapfile, PATH_MAX+1, "%s", gt_str_get(path)); | |
270 | + gt_assert(rval < PATH_MAX + 1); | |
271 | } | |
272 | + gt_str_delete(path); | |
273 | return had_err; | |
274 | } | |
275 | ||
276 | --- a/src/libgenomethreader/gthpre.c | |
277 | +++ b/src/libgenomethreader/gthpre.c | |
278 | @@ -11,6 +11,7 @@ | |
279 | #include "gth/gthdef.h" | |
280 | #include "gth/gthoutput.h" | |
281 | #include "gth/input.h" | |
282 | +#include "libgenomethreader/findfile.h" | |
283 | #include "libgenomethreader/gthmkvtree.h" | |
284 | #include "libgenomethreader/gthpolyafunc.h" | |
285 | #include "libgenomethreader/gthpre.h" | |
286 | @@ -301,49 +302,22 @@ | |
287 | ||
288 | /* create masked file if necessary */ | |
289 | if (createmaskedfile) { | |
290 | - sprintf(dnafilename, "%s.%s", POLYA_FASTAFILENAME, DNASUFFIX); | |
291 | - /* check if polyatail.dna exists */ | |
292 | - fp = scanpathsforfile(GTHDATAENVNAME, dnafilename); /* XXX */ | |
293 | - if (!fp) { | |
294 | - gt_error_set(err, "file \"%s\" not found in $%s. Set correctly?", | |
295 | - dnafilename, GTHDATAENVNAME); | |
296 | - had_err = -1; | |
297 | - } | |
298 | - if (!had_err /* XXX */ && DELETEFILEHANDLE(fp)) { | |
299 | - fprintf(stderr,"%s\n", messagespace()); | |
300 | - exit(EXIT_FAILURE); | |
301 | - } | |
302 | - | |
303 | - /* call vmatch and save masked file */ | |
304 | - if (!had_err && !getenv(GTHDATAENVNAME)) { | |
305 | - gt_error_set(err, "$%s not defined. Please set correctly", | |
306 | - GTHDATAENVNAME); | |
307 | - had_err = -1; | |
308 | - } | |
309 | + int rval = 0; | |
310 | + GtStr *path = gt_str_new(); | |
311 | ||
312 | + sprintf(dnafilename, "%s.%s", POLYA_FASTAFILENAME, DNASUFFIX); | |
313 | + had_err = gth_find_file(dnafilename, GTHDATAENVNAME, GTHDATADIRNAME, path, | |
314 | + err); | |
315 | if (!had_err) { | |
316 | - sprintf(dnafilename, "%s%c%s", getenv(GTHDATAENVNAME), GT_PATH_SEPARATOR, | |
317 | - POLYA_FASTAFILENAME); | |
318 | - } | |
319 | - else { | |
320 | - /* check for file relative to binary */ | |
321 | - GtStr *path; | |
322 | - int new_err; | |
323 | - path = gt_str_new(); | |
324 | - new_err = gt_file_find_exec_in_path(path, gt_error_get_progname(err), | |
325 | - NULL); | |
326 | - if (!new_err) { | |
327 | - gt_str_append_char(path, GT_PATH_SEPARATOR); | |
328 | - gt_str_append_cstr(path, GTHDATADIRNAME); | |
329 | - if (gt_file_exists_and_is_dir(gt_str_get(path))) { | |
330 | - sprintf(dnafilename, "%s%c%s", gt_str_get(path), GT_PATH_SEPARATOR, | |
331 | - POLYA_FASTAFILENAME); | |
332 | - gt_error_unset(err); | |
333 | - had_err = 0; | |
334 | - } | |
335 | - } | |
336 | + rval = snprintf(dnafilename, PATH_MAX+1, "%s", gt_str_get(path)); | |
337 | + gt_assert(rval < PATH_MAX + 1); | |
338 | + /* clip off DNASUFFIX */ | |
339 | + gt_assert(strlen(dnafilename) > 5); | |
340 | + dnafilename[strlen(dnafilename)-4] = '\0'; | |
341 | } | |
342 | + gt_str_delete(path); | |
343 | ||
344 | + /* call vmatch and save masked file */ | |
345 | if (!had_err) { | |
346 | /* open file pointer for masked reference file */ | |
347 | fp = gt_fa_xfopen(maskedfilename, "w"); |