]> wimlib.net Git - wimlib/blob - src/paths.c
wimlib-imagex: Allow specifying LZMS compression
[wimlib] / src / paths.c
1 /*
2  * paths.c - Path manipulation routines
3  */
4
5 /*
6  * Copyright (C) 2012, 2013 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
10  * wimlib is free software; you can redistribute it and/or modify it under the
11  * terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option)
13  * any later version.
14  *
15  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17  * A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with wimlib; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "wimlib.h"
29 #include "wimlib/paths.h"
30 #include "wimlib/util.h"
31
32 #include <string.h>
33
34 /* Like the basename() function, but does not modify @path; it just returns a
35  * pointer to it.  This assumes the path separator is the
36  * OS_PREFERRED_PATH_SEPARATOR.  */
37 const tchar *
38 path_basename(const tchar *path)
39 {
40         return path_basename_with_len(path, tstrlen(path));
41 }
42
43 /* Like path_basename(), but take an explicit string length.  */
44 const tchar *
45 path_basename_with_len(const tchar *path, size_t len)
46 {
47         const tchar *p = &path[len];
48
49         do {
50                 if (p == path)
51                         return &path[len];
52         } while (*--p == OS_PREFERRED_PATH_SEPARATOR);
53
54         do {
55                 if (p == path)
56                         return &path[0];
57         } while (*--p != OS_PREFERRED_PATH_SEPARATOR);
58
59         return ++p;
60 }
61
62
63 /* Returns a pointer to the part of @path following the first colon in the last
64  * path component, or NULL if the last path component does not contain a colon
65  * or has no characters following the first colon.  */
66 const tchar *
67 path_stream_name(const tchar *path)
68 {
69         const tchar *base = path_basename(path);
70         const tchar *stream_name = tstrchr(base, T(':'));
71         if (stream_name == NULL || *(stream_name + 1) == T('\0'))
72                 return NULL;
73         else
74                 return stream_name + 1;
75 }
76
77 /* Duplicate a path; return empty string for NULL input.  */
78 tchar *
79 canonicalize_fs_path(const tchar *fs_path)
80 {
81         if (fs_path == NULL)
82                 fs_path = T("");
83         return TSTRDUP(fs_path);
84 }
85
86 /*
87  * canonicalize_wim_path() - Given a user-provided path to a file within a WIM
88  * image, translate it into a "canonical" path.
89  *
90  * To do this, translate all supported path separators (is_any_path_separator())
91  * into the WIM_PATH_SEPARATOR, and strip any leading and trailing slashes.  The
92  * returned string is allocated.  Note that there still may be consecutive path
93  * separators within the string.  Furthermore, the string may be empty, which
94  * indicates the root dentry of the WIM image.
95  */
96 tchar *
97 canonicalize_wim_path(const tchar *wim_path)
98 {
99         tchar *canonical_path;
100         tchar *p;
101
102         if (wim_path == NULL) {
103                 wim_path = T("");
104         } else {
105                 /* Strip leading path separators.  */
106                 while (is_any_path_separator(*wim_path))
107                         wim_path++;
108         }
109
110         canonical_path = TSTRDUP(wim_path);
111         if (canonical_path == NULL)
112                 return NULL;
113
114         /* Translate all path separators to WIM_PATH_SEPARATOR.  */
115         for (p = canonical_path; *p; p++)
116                 if (is_any_path_separator(*p))
117                         *p = WIM_PATH_SEPARATOR;
118
119         /* Strip trailing path separators.  */
120         while (p > canonical_path && *--p == WIM_PATH_SEPARATOR)
121                 *p = T('\0');
122
123         return canonical_path;
124 }