Rev 3675 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3675 | Rev 4377 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jakub Jermar |
2 | * Copyright (c) 2008 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup fs |
29 | /** @addtogroup fs |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file fat_dentry.c |
34 | * @file fat_dentry.c |
35 | * @brief Functions that work with FAT directory entries. |
35 | * @brief Functions that work with FAT directory entries. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include "fat_dentry.h" |
38 | #include "fat_dentry.h" |
39 | #include <ctype.h> |
39 | #include <ctype.h> |
40 | #include <string.h> |
40 | #include <string.h> |
41 | 41 | ||
42 | static bool is_d_char(const char ch) |
42 | static bool is_d_char(const char ch) |
43 | { |
43 | { |
44 | if (isalnum(ch) || ch == '_') |
44 | if (isalnum(ch) || ch == '_') |
45 | return true; |
45 | return true; |
46 | else |
46 | else |
47 | return false; |
47 | return false; |
48 | } |
48 | } |
49 | 49 | ||
50 | /** Compare path component with the name read from the dentry. |
50 | /** Compare path component with the name read from the dentry. |
51 | * |
51 | * |
52 | * This function compares the path component with the name read from the dentry. |
52 | * This function compares the path component with the name read from the dentry. |
53 | * The comparison is case insensitive and tolerates a mismatch on the trailing |
53 | * The comparison is case insensitive and tolerates a mismatch on the trailing |
54 | * dot character at the end of the name (i.e. when there is a dot, but no |
54 | * dot character at the end of the name (i.e. when there is a dot, but no |
55 | * extension). |
55 | * extension). |
56 | * |
56 | * |
57 | * @param name Node name read from the dentry. |
57 | * @param name Node name read from the dentry. |
58 | * @param component Path component. |
58 | * @param component Path component. |
59 | * |
59 | * |
60 | * @return Zero on match, non-zero otherwise. |
60 | * @return Zero on match, non-zero otherwise. |
61 | */ |
61 | */ |
62 | int fat_dentry_namecmp(char *name, const char *component) |
62 | int fat_dentry_namecmp(char *name, const char *component) |
63 | { |
63 | { |
64 | int rc; |
64 | int rc; |
- | 65 | size_t size; |
|
- | 66 | ||
65 | if (!(rc = stricmp(name, component))) |
67 | if (!(rc = stricmp(name, component))) |
66 | return rc; |
68 | return rc; |
67 | if (!strchr(name, '.')) { |
69 | if (!str_chr(name, '.')) { |
68 | /* |
70 | /* |
69 | * There is no '.' in the name, so we know that there is enough |
71 | * There is no '.' in the name, so we know that there is enough |
70 | * space for appending an extra '.' to name. |
72 | * space for appending an extra '.' to name. |
71 | */ |
73 | */ |
- | 74 | size = str_size(name); |
|
72 | name[strlen(name)] = '.'; |
75 | name[size] = '.'; |
73 | name[strlen(name) + 1] = '\0'; |
76 | name[size + 1] = '\0'; |
74 | rc = stricmp(name, component); |
77 | rc = stricmp(name, component); |
75 | } |
78 | } |
76 | return rc; |
79 | return rc; |
77 | } |
80 | } |
78 | 81 | ||
79 | bool fat_dentry_name_verify(const char *name) |
82 | bool fat_dentry_name_verify(const char *name) |
80 | { |
83 | { |
81 | unsigned i, dot; |
84 | unsigned i, dot; |
82 | bool dot_found = false; |
85 | bool dot_found = false; |
83 | 86 | ||
84 | 87 | ||
85 | for (i = 0; name[i]; i++) { |
88 | for (i = 0; name[i]; i++) { |
86 | if (name[i] == '.') { |
89 | if (name[i] == '.') { |
87 | if (dot_found) { |
90 | if (dot_found) { |
88 | return false; |
91 | return false; |
89 | } else { |
92 | } else { |
90 | dot_found = true; |
93 | dot_found = true; |
91 | dot = i; |
94 | dot = i; |
92 | } |
95 | } |
93 | } else { |
96 | } else { |
94 | if (!is_d_char(name[i])) |
97 | if (!is_d_char(name[i])) |
95 | return false; |
98 | return false; |
96 | } |
99 | } |
97 | } |
100 | } |
98 | 101 | ||
99 | if (dot_found) { |
102 | if (dot_found) { |
100 | if (dot > FAT_NAME_LEN) |
103 | if (dot > FAT_NAME_LEN) |
101 | return false; |
104 | return false; |
102 | if (i - dot > FAT_EXT_LEN + 1) |
105 | if (i - dot > FAT_EXT_LEN + 1) |
103 | return false; |
106 | return false; |
104 | } else { |
107 | } else { |
105 | if (i > FAT_NAME_LEN) |
108 | if (i > FAT_NAME_LEN) |
106 | return false; |
109 | return false; |
107 | } |
110 | } |
108 | 111 | ||
109 | return true; |
112 | return true; |
110 | } |
113 | } |
111 | 114 | ||
112 | void fat_dentry_name_get(const fat_dentry_t *d, char *buf) |
115 | void fat_dentry_name_get(const fat_dentry_t *d, char *buf) |
113 | { |
116 | { |
114 | int i; |
117 | unsigned int i; |
115 | 118 | ||
116 | for (i = 0; i < FAT_NAME_LEN; i++) { |
119 | for (i = 0; i < FAT_NAME_LEN; i++) { |
117 | if (d->name[i] == FAT_PAD) |
120 | if (d->name[i] == FAT_PAD) |
118 | break; |
121 | break; |
- | 122 | ||
119 | if (d->name[i] == FAT_DENTRY_E5_ESC) |
123 | if (d->name[i] == FAT_DENTRY_E5_ESC) |
120 | *buf++ = 0xe5; |
124 | *buf++ = 0xe5; |
- | 125 | else { |
|
- | 126 | if (d->lcase & FAT_LCASE_LOWER_NAME) |
|
- | 127 | *buf++ = tolower(d->name[i]); |
|
121 | else |
128 | else |
122 | *buf++ = d->name[i]; |
129 | *buf++ = d->name[i]; |
- | 130 | } |
|
123 | } |
131 | } |
- | 132 | ||
124 | if (d->ext[0] != FAT_PAD) |
133 | if (d->ext[0] != FAT_PAD) |
125 | *buf++ = '.'; |
134 | *buf++ = '.'; |
- | 135 | ||
126 | for (i = 0; i < FAT_EXT_LEN; i++) { |
136 | for (i = 0; i < FAT_EXT_LEN; i++) { |
127 | if (d->ext[i] == FAT_PAD) { |
137 | if (d->ext[i] == FAT_PAD) { |
128 | *buf = '\0'; |
138 | *buf = '\0'; |
129 | return; |
139 | return; |
130 | } |
140 | } |
- | 141 | ||
131 | if (d->ext[i] == FAT_DENTRY_E5_ESC) |
142 | if (d->ext[i] == FAT_DENTRY_E5_ESC) |
132 | *buf++ = 0xe5; |
143 | *buf++ = 0xe5; |
- | 144 | else { |
|
- | 145 | if (d->lcase & FAT_LCASE_LOWER_EXT) |
|
- | 146 | *buf++ = tolower(d->ext[i]); |
|
133 | else |
147 | else |
134 | *buf++ = d->ext[i]; |
148 | *buf++ = d->ext[i]; |
- | 149 | } |
|
135 | } |
150 | } |
- | 151 | ||
136 | *buf = '\0'; |
152 | *buf = '\0'; |
137 | } |
153 | } |
138 | 154 | ||
139 | void fat_dentry_name_set(fat_dentry_t *d, const char *name) |
155 | void fat_dentry_name_set(fat_dentry_t *d, const char *name) |
140 | { |
156 | { |
141 | int i; |
157 | unsigned int i; |
142 | const char fake_ext[] = " "; |
158 | const char fake_ext[] = " "; |
143 | - | ||
- | 159 | bool lower_name = true; |
|
- | 160 | bool lower_ext = true; |
|
144 | 161 | ||
145 | for (i = 0; i < FAT_NAME_LEN; i++) { |
162 | for (i = 0; i < FAT_NAME_LEN; i++) { |
146 | switch ((uint8_t) *name) { |
163 | switch ((uint8_t) *name) { |
147 | case 0xe5: |
164 | case 0xe5: |
148 | d->name[i] = FAT_DENTRY_E5_ESC; |
165 | d->name[i] = FAT_DENTRY_E5_ESC; |
149 | name++; |
166 | name++; |
150 | break; |
167 | break; |
151 | case '\0': |
168 | case '\0': |
152 | case '.': |
169 | case '.': |
153 | d->name[i] = FAT_PAD; |
170 | d->name[i] = FAT_PAD; |
154 | break; |
171 | break; |
155 | default: |
172 | default: |
- | 173 | if (isalpha(*name)) { |
|
- | 174 | if (!islower(*name)) |
|
- | 175 | lower_name = false; |
|
- | 176 | } |
|
- | 177 | ||
156 | d->name[i] = toupper(*name++); |
178 | d->name[i] = toupper(*name++); |
157 | break; |
179 | break; |
158 | } |
180 | } |
159 | } |
181 | } |
- | 182 | ||
160 | if (*name++ != '.') |
183 | if (*name++ != '.') |
161 | name = fake_ext; |
184 | name = fake_ext; |
- | 185 | ||
162 | for (i = 0; i < FAT_EXT_LEN; i++) { |
186 | for (i = 0; i < FAT_EXT_LEN; i++) { |
163 | switch ((uint8_t) *name) { |
187 | switch ((uint8_t) *name) { |
164 | case 0xe5: |
188 | case 0xe5: |
165 | d->ext[i] = FAT_DENTRY_E5_ESC; |
189 | d->ext[i] = FAT_DENTRY_E5_ESC; |
166 | name++; |
190 | name++; |
167 | break; |
191 | break; |
168 | case '\0': |
192 | case '\0': |
169 | d->ext[i] = FAT_PAD; |
193 | d->ext[i] = FAT_PAD; |
170 | break; |
194 | break; |
171 | default: |
195 | default: |
- | 196 | if (isalpha(*name)) { |
|
- | 197 | if (!islower(*name)) |
|
- | 198 | lower_ext = false; |
|
- | 199 | } |
|
- | 200 | ||
172 | d->ext[i] = toupper(*name++); |
201 | d->ext[i] = toupper(*name++); |
173 | break; |
202 | break; |
174 | } |
203 | } |
175 | } |
204 | } |
- | 205 | ||
- | 206 | if (lower_name) |
|
- | 207 | d->lcase |= FAT_LCASE_LOWER_NAME; |
|
- | 208 | else |
|
- | 209 | d->lcase &= ~FAT_LCASE_LOWER_NAME; |
|
- | 210 | ||
- | 211 | if (lower_ext) |
|
- | 212 | d->lcase |= FAT_LCASE_LOWER_EXT; |
|
- | 213 | else |
|
- | 214 | d->lcase &= ~FAT_LCASE_LOWER_EXT; |
|
176 | } |
215 | } |
177 | 216 | ||
178 | fat_dentry_clsf_t fat_classify_dentry(const fat_dentry_t *d) |
217 | fat_dentry_clsf_t fat_classify_dentry(const fat_dentry_t *d) |
179 | { |
218 | { |
180 | if (d->attr & FAT_ATTR_VOLLABEL) { |
219 | if (d->attr & FAT_ATTR_VOLLABEL) { |
181 | /* volume label entry */ |
220 | /* volume label entry */ |
182 | return FAT_DENTRY_SKIP; |
221 | return FAT_DENTRY_SKIP; |
183 | } |
222 | } |
184 | if (d->name[0] == FAT_DENTRY_ERASED) { |
223 | if (d->name[0] == FAT_DENTRY_ERASED) { |
185 | /* not-currently-used entry */ |
224 | /* not-currently-used entry */ |
186 | return FAT_DENTRY_FREE; |
225 | return FAT_DENTRY_FREE; |
187 | } |
226 | } |
188 | if (d->name[0] == FAT_DENTRY_UNUSED) { |
227 | if (d->name[0] == FAT_DENTRY_UNUSED) { |
189 | /* never used entry */ |
228 | /* never used entry */ |
190 | return FAT_DENTRY_LAST; |
229 | return FAT_DENTRY_LAST; |
191 | } |
230 | } |
192 | if (d->name[0] == FAT_DENTRY_DOT) { |
231 | if (d->name[0] == FAT_DENTRY_DOT) { |
193 | /* |
232 | /* |
194 | * Most likely '.' or '..'. |
233 | * Most likely '.' or '..'. |
195 | * It cannot occur in a regular file name. |
234 | * It cannot occur in a regular file name. |
196 | */ |
235 | */ |
197 | return FAT_DENTRY_SKIP; |
236 | return FAT_DENTRY_SKIP; |
198 | } |
237 | } |
199 | return FAT_DENTRY_VALID; |
238 | return FAT_DENTRY_VALID; |
200 | } |
239 | } |
201 | 240 | ||
202 | /** |
241 | /** |
203 | * @} |
242 | * @} |
204 | */ |
243 | */ |
205 | 244 |