4 Copyright (c) 2000-2001 MEDICAL RESEARCH COUNCIL
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 1. Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
17 3. Neither the name of the MEDICAL RESEARCH COUNCIL, THE LABORATORY OF
18 MOLECULAR BIOLOGY nor the names of its contributors may be used to endorse or
19 promote products derived from this software without specific prior written
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 Copyright (c) 2008, 2009, 2013, 2014 Genome Research Ltd.
36 Author: James Bonfield <jkb@sanger.ac.uk>
38 Redistribution and use in source and binary forms, with or without
39 modification, are permitted provided that the following conditions are met:
41 1. Redistributions of source code must retain the above copyright notice,
42 this list of conditions and the following disclaimer.
44 2. Redistributions in binary form must reproduce the above copyright notice,
45 this list of conditions and the following disclaimer in the documentation
46 and/or other materials provided with the distribution.
48 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
49 Institute nor the names of its contributors may be used to endorse or promote
50 products derived from this software without specific prior written permission.
52 THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS IS" AND
53 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
54 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
55 DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH LTD OR CONTRIBUTORS BE LIABLE
56 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
58 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
59 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
60 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
71 #include <sys/types.h>
75 # define PATH_MAX 1024
78 #include "cram/open_trace_file.h"
79 #include "cram/misc.h"
80 #include "htslib/hfile.h"
83 * Tokenises the search path splitting on colons (unix) or semicolons
85 * We also explicitly add a "./" to the end of the search path
87 * Returns: A new search path with items separated by nul chars. Two nul
88 * chars in a row represent the end of the tokenised path.
89 * Returns NULL for a failure.
91 * The returned data has been malloced. It is up to the caller to free this
94 char *tokenise_search_path(char *searchpath
) {
107 newsearch
= (char *)malloc((len
= strlen(searchpath
))+5);
111 for (i
= 0, j
= 0; i
< len
; i
++) {
112 /* "::" => ":". Used for escaping colons in http://foo */
113 if (i
< len
-1 && searchpath
[i
] == ':' && searchpath
[i
+1] == ':') {
114 newsearch
[j
++] = ':';
119 /* Handle http:// and ftp:// too without :: */
120 if (path_sep
== ':') {
121 if ((i
== 0 || (i
> 0 && searchpath
[i
-1] == ':')) &&
122 (!strncmp(&searchpath
[i
], "http:", 5) ||
123 !strncmp(&searchpath
[i
], "ftp:", 4) ||
124 !strncmp(&searchpath
[i
], "|http:", 6) ||
125 !strncmp(&searchpath
[i
], "|ftp:", 5) ||
126 !strncmp(&searchpath
[i
], "URL=http:", 9) ||
127 !strncmp(&searchpath
[i
], "URL=ftp:", 8))) {
129 newsearch
[j
++] = searchpath
[i
];
130 } while (i
<len
&& searchpath
[i
++] != ':');
131 if (searchpath
[i
] == ':')
133 if (searchpath
[i
]=='/')
134 newsearch
[j
++] = searchpath
[i
++];
135 if (searchpath
[i
]=='/')
136 newsearch
[j
++] = searchpath
[i
++];
137 // Look for host:port
139 newsearch
[j
++] = searchpath
[i
++];
140 } while (i
<len
&& searchpath
[i
] != ':' && searchpath
[i
] != '/');
141 newsearch
[j
++] = searchpath
[i
++];
142 if (searchpath
[i
] == ':')
147 if (searchpath
[i
] == path_sep
) {
148 /* Skip blank path components */
149 if (j
&& newsearch
[j
-1] != 0)
152 newsearch
[j
++] = searchpath
[i
];
158 newsearch
[j
++] = '.';
159 newsearch
[j
++] = '/';
166 mFILE
*find_file_url(char *file
, char *url
) {
169 int maxlen
= 8190 - strlen(file
), len
;
172 /* Expand %s for the trace name */
173 for (cp
= buf
; *url
&& cp
- buf
< maxlen
; url
++) {
174 if (*url
== '%' && *(url
+1) == 's') {
176 cp
+= strlen(strcpy(cp
, file
));
183 if (!(hf
= hopen(buf
, "r")))
186 if (NULL
== (mf
= mfcreate(NULL
, 0)))
188 while ((len
= hread(hf
, buf
, 8192)) > 0) {
189 if (mfwrite(buf
, len
, 1, mf
) <= 0) {
195 if (hclose(hf
) < 0 || len
< 0) {
205 * Takes a dirname possibly including % rules and appends the filename
208 * Returns expanded pathname or NULL for malloc failure.
210 static char *expand_path(char *file
, char *dirname
) {
211 size_t len
= strlen(dirname
);
212 size_t lenf
= strlen(file
);
215 path
= malloc(len
+lenf
+2); // worst expansion DIR/FILE
219 if (dirname
[len
-1] == '/')
222 /* Special case for "./" or absolute filenames */
223 if (*file
== '/' || (len
==1 && *dirname
== '.')) {
224 sprintf(path
, "%s", file
);
226 /* Handle %[0-9]*s expansions, if required */
227 char *path_end
= path
;
229 while ((cp
= strchr(dirname
, '%'))) {
231 long l
= strtol(cp
+1, &endp
, 10);
233 strncpy(path_end
, dirname
, (endp
+1)-dirname
);
234 path_end
+= (endp
+1)-dirname
;
239 strncpy(path_end
, dirname
, cp
-dirname
);
240 path_end
+= cp
-dirname
;
242 strncpy(path_end
, file
, l
);
243 path_end
+= MIN(strlen(file
), l
);
244 file
+= MIN(strlen(file
), l
);
246 strcpy(path_end
, file
);
247 path_end
+= strlen(file
);
248 file
+= strlen(file
);
250 len
-= (endp
+1) - dirname
;
253 strncpy(path_end
, dirname
, len
);
254 path_end
+= MIN(strlen(dirname
), len
);
258 strcpy(path_end
, file
);
262 //fprintf(stderr, "*PATH=\"%s\"\n", path);
267 * Searches for file in the directory 'dirname'. If it finds it, it opens
268 * it. This also searches for compressed versions of the file in dirname
271 * Returns mFILE pointer if found
274 static mFILE
*find_file_dir(char *file
, char *dirname
) {
278 path
= expand_path(file
, dirname
);
281 mf
= mfopen(path
, "rbm");
288 * ------------------------------------------------------------------------
289 * Public functions below.
293 * Opens a trace file named 'file'. This is initially looked for as a
294 * pathname relative to a file named "relative_to". This may (for
295 * example) be the name of an experiment file referencing the trace
296 * file. In this case by passing relative_to as the experiment file
297 * filename the trace file will be picked up in the same directory as
298 * the experiment file. Relative_to may be supplied as NULL.
300 * 'file' is looked for at relative_to, then the current directory, and then
301 * all of the locations listed in 'path' (which is a colon separated list).
302 * If 'path' is NULL it uses the RAWDATA environment variable instead.
304 * Returns a mFILE pointer when found.
307 mFILE
*open_path_mfile(char *file
, char *path
, char *relative_to
) {
314 path
= getenv("RAWDATA");
315 if (NULL
== (newsearch
= tokenise_search_path(path
)))
319 * Step through the search path testing out each component.
320 * We now look through each path element treating some prefixes as
321 * special, otherwise we treat the element as a directory.
323 for (ele
= newsearch
; *ele
; ele
+= strlen(ele
)+1) {
327 * '|' prefixing a path component indicates that we do not
328 * wish to perform the compression extension searching in that
331 * NB: this has been removed from the htslib implementation.
339 if (0 == strncmp(ele2
, "URL=", 4)) {
340 if ((fp
= find_file_url(file
, ele2
+4))) {
344 } else if (!strncmp(ele2
, "http:", 5) ||
345 !strncmp(ele2
, "ftp:", 4)) {
346 if ((fp
= find_file_url(file
, ele2
))) {
350 } else if ((fp
= find_file_dir(file
, ele2
))) {
358 /* Look in the same location as the incoming 'relative_to' filename */
361 char relative_path
[PATH_MAX
+1];
362 strcpy(relative_path
, relative_to
);
363 if ((cp
= strrchr(relative_path
, '/')))
365 if ((fp
= find_file_dir(file
, relative_path
)))
374 * As per open_path_mfile, but searching only for local filenames.
375 * This is useful as we may avoid doing a full mfopen and loading
376 * the entire file into memory.
378 * Returns the expanded pathname if found.
381 char *find_path(char *file
, char *path
) {
384 char *outpath
= NULL
;
388 path
= getenv("RAWDATA");
389 if (NULL
== (newsearch
= tokenise_search_path(path
)))
392 for (ele
= newsearch
; *ele
; ele
+= strlen(ele
)+1) {
393 char *ele2
= (*ele
== '|') ? ele
+1 : ele
;
395 if (!strncmp(ele2
, "URL=", 4) ||
396 !strncmp(ele2
, "http:", 5) ||
397 !strncmp(ele2
, "ftp:", 4)) {
400 outpath
= expand_path(file
, ele2
);
401 if (is_file(outpath
)) {