]> wimlib.net Git - wimlib/blob - src/paths.c
win32_recurse_directory(): Handle not implemented/supported
[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] - 1;
48
49         /* Trailing slashes. */
50         while (1) {
51                 if (p == path - 1)
52                         return T("");
53                 if (*p != OS_PREFERRED_PATH_SEPARATOR)
54                         break;
55                 p--;
56         }
57
58         while ((p != path - 1) && *p != OS_PREFERRED_PATH_SEPARATOR)
59                 p--;
60
61         return p + 1;
62 }
63
64
65 /*
66  * Returns a pointer to the part of @path following the first colon in the last
67  * path component, or NULL if the last path component does not contain a colon.
68  */
69 const tchar *
70 path_stream_name(const tchar *path)
71 {
72         const tchar *base = path_basename(path);
73         const tchar *stream_name = tstrchr(base, T(':'));
74         if (!stream_name)
75                 return NULL;
76         else
77                 return stream_name + 1;
78 }
79
80
81 /* Duplicate a path; return empty string for NULL input. */
82 tchar *
83 canonicalize_fs_path(const tchar *fs_path)
84 {
85         if (!fs_path)
86                 fs_path = T("");
87         return TSTRDUP(fs_path);
88 }
89
90 /*
91  * canonicalize_wim_path - Given a user-provided path to a file within a WIM
92  * image, translate it into a "canonical" path.
93  *
94  * To do this, we translate all supported path separators
95  * (is_any_path_separator()) into the WIM_PATH_SEPARATOR, and strip any leading
96  * and trailing slashes.  The returned string is allocated.  Note that there
97  * still may be consecutive path separators within the string.  Furthermore, the
98  * string may be empty, which indicates the root dentry of the WIM image.
99  */
100 tchar *
101 canonicalize_wim_path(const tchar *wim_path)
102 {
103         tchar *p;
104         tchar *canonical_path;
105
106         if (wim_path == NULL) {
107                 wim_path = T("");
108         } else {
109                 while (is_any_path_separator(*wim_path))
110                         wim_path++;
111         }
112         canonical_path = TSTRDUP(wim_path);
113         if (canonical_path) {
114                 for (p = canonical_path; *p; p++)
115                         if (is_any_path_separator(*p))
116                                 *p = WIM_PATH_SEPARATOR;
117                 for (p = tstrchr(canonical_path, T('\0')) - 1;
118                      p >= canonical_path && *p == WIM_PATH_SEPARATOR;
119                      p--)
120                 {
121                         *p = T('\0');
122                 }
123         }
124         return canonical_path;
125 }