Rev 4264 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4264 | Rev 4265 | ||
---|---|---|---|
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; |
65 | size_t size; |
66 | 66 | ||
67 | if (!(rc = stricmp(name, component))) |
67 | if (!(rc = stricmp(name, component))) |
68 | return rc; |
68 | return rc; |
69 | if (!strchr(name, '.')) { |
69 | if (!str_chr(name, '.')) { |
70 | /* |
70 | /* |
71 | * 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 |
72 | * space for appending an extra '.' to name. |
72 | * space for appending an extra '.' to name. |
73 | */ |
73 | */ |
74 | size = str_size(name); |
74 | size = str_size(name); |
75 | name[size] = '.'; |
75 | name[size] = '.'; |
76 | name[size + 1] = '\0'; |
76 | name[size + 1] = '\0'; |
77 | rc = stricmp(name, component); |
77 | rc = stricmp(name, component); |
78 | } |
78 | } |
79 | return rc; |
79 | return rc; |
80 | } |
80 | } |
81 | 81 | ||
82 | bool fat_dentry_name_verify(const char *name) |
82 | bool fat_dentry_name_verify(const char *name) |
83 | { |
83 | { |
84 | unsigned i, dot; |
84 | unsigned i, dot; |
85 | bool dot_found = false; |
85 | bool dot_found = false; |
86 | 86 | ||
87 | 87 | ||
88 | for (i = 0; name[i]; i++) { |
88 | for (i = 0; name[i]; i++) { |
89 | if (name[i] == '.') { |
89 | if (name[i] == '.') { |
90 | if (dot_found) { |
90 | if (dot_found) { |
91 | return false; |
91 | return false; |
92 | } else { |
92 | } else { |
93 | dot_found = true; |
93 | dot_found = true; |
94 | dot = i; |
94 | dot = i; |
95 | } |
95 | } |
96 | } else { |
96 | } else { |
97 | if (!is_d_char(name[i])) |
97 | if (!is_d_char(name[i])) |
98 | return false; |
98 | return false; |
99 | } |
99 | } |
100 | } |
100 | } |
101 | 101 | ||
102 | if (dot_found) { |
102 | if (dot_found) { |
103 | if (dot > FAT_NAME_LEN) |
103 | if (dot > FAT_NAME_LEN) |
104 | return false; |
104 | return false; |
105 | if (i - dot > FAT_EXT_LEN + 1) |
105 | if (i - dot > FAT_EXT_LEN + 1) |
106 | return false; |
106 | return false; |
107 | } else { |
107 | } else { |
108 | if (i > FAT_NAME_LEN) |
108 | if (i > FAT_NAME_LEN) |
109 | return false; |
109 | return false; |
110 | } |
110 | } |
111 | 111 | ||
112 | return true; |
112 | return true; |
113 | } |
113 | } |
114 | 114 | ||
115 | 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) |
116 | { |
116 | { |
117 | int i; |
117 | int i; |
118 | 118 | ||
119 | for (i = 0; i < FAT_NAME_LEN; i++) { |
119 | for (i = 0; i < FAT_NAME_LEN; i++) { |
120 | if (d->name[i] == FAT_PAD) |
120 | if (d->name[i] == FAT_PAD) |
121 | break; |
121 | break; |
122 | if (d->name[i] == FAT_DENTRY_E5_ESC) |
122 | if (d->name[i] == FAT_DENTRY_E5_ESC) |
123 | *buf++ = 0xe5; |
123 | *buf++ = 0xe5; |
124 | else |
124 | else |
125 | *buf++ = d->name[i]; |
125 | *buf++ = d->name[i]; |
126 | } |
126 | } |
127 | if (d->ext[0] != FAT_PAD) |
127 | if (d->ext[0] != FAT_PAD) |
128 | *buf++ = '.'; |
128 | *buf++ = '.'; |
129 | for (i = 0; i < FAT_EXT_LEN; i++) { |
129 | for (i = 0; i < FAT_EXT_LEN; i++) { |
130 | if (d->ext[i] == FAT_PAD) { |
130 | if (d->ext[i] == FAT_PAD) { |
131 | *buf = '\0'; |
131 | *buf = '\0'; |
132 | return; |
132 | return; |
133 | } |
133 | } |
134 | if (d->ext[i] == FAT_DENTRY_E5_ESC) |
134 | if (d->ext[i] == FAT_DENTRY_E5_ESC) |
135 | *buf++ = 0xe5; |
135 | *buf++ = 0xe5; |
136 | else |
136 | else |
137 | *buf++ = d->ext[i]; |
137 | *buf++ = d->ext[i]; |
138 | } |
138 | } |
139 | *buf = '\0'; |
139 | *buf = '\0'; |
140 | } |
140 | } |
141 | 141 | ||
142 | void fat_dentry_name_set(fat_dentry_t *d, const char *name) |
142 | void fat_dentry_name_set(fat_dentry_t *d, const char *name) |
143 | { |
143 | { |
144 | int i; |
144 | int i; |
145 | const char fake_ext[] = " "; |
145 | const char fake_ext[] = " "; |
146 | 146 | ||
147 | 147 | ||
148 | for (i = 0; i < FAT_NAME_LEN; i++) { |
148 | for (i = 0; i < FAT_NAME_LEN; i++) { |
149 | switch ((uint8_t) *name) { |
149 | switch ((uint8_t) *name) { |
150 | case 0xe5: |
150 | case 0xe5: |
151 | d->name[i] = FAT_DENTRY_E5_ESC; |
151 | d->name[i] = FAT_DENTRY_E5_ESC; |
152 | name++; |
152 | name++; |
153 | break; |
153 | break; |
154 | case '\0': |
154 | case '\0': |
155 | case '.': |
155 | case '.': |
156 | d->name[i] = FAT_PAD; |
156 | d->name[i] = FAT_PAD; |
157 | break; |
157 | break; |
158 | default: |
158 | default: |
159 | d->name[i] = toupper(*name++); |
159 | d->name[i] = toupper(*name++); |
160 | break; |
160 | break; |
161 | } |
161 | } |
162 | } |
162 | } |
163 | if (*name++ != '.') |
163 | if (*name++ != '.') |
164 | name = fake_ext; |
164 | name = fake_ext; |
165 | for (i = 0; i < FAT_EXT_LEN; i++) { |
165 | for (i = 0; i < FAT_EXT_LEN; i++) { |
166 | switch ((uint8_t) *name) { |
166 | switch ((uint8_t) *name) { |
167 | case 0xe5: |
167 | case 0xe5: |
168 | d->ext[i] = FAT_DENTRY_E5_ESC; |
168 | d->ext[i] = FAT_DENTRY_E5_ESC; |
169 | name++; |
169 | name++; |
170 | break; |
170 | break; |
171 | case '\0': |
171 | case '\0': |
172 | d->ext[i] = FAT_PAD; |
172 | d->ext[i] = FAT_PAD; |
173 | break; |
173 | break; |
174 | default: |
174 | default: |
175 | d->ext[i] = toupper(*name++); |
175 | d->ext[i] = toupper(*name++); |
176 | break; |
176 | break; |
177 | } |
177 | } |
178 | } |
178 | } |
179 | } |
179 | } |
180 | 180 | ||
181 | fat_dentry_clsf_t fat_classify_dentry(const fat_dentry_t *d) |
181 | fat_dentry_clsf_t fat_classify_dentry(const fat_dentry_t *d) |
182 | { |
182 | { |
183 | if (d->attr & FAT_ATTR_VOLLABEL) { |
183 | if (d->attr & FAT_ATTR_VOLLABEL) { |
184 | /* volume label entry */ |
184 | /* volume label entry */ |
185 | return FAT_DENTRY_SKIP; |
185 | return FAT_DENTRY_SKIP; |
186 | } |
186 | } |
187 | if (d->name[0] == FAT_DENTRY_ERASED) { |
187 | if (d->name[0] == FAT_DENTRY_ERASED) { |
188 | /* not-currently-used entry */ |
188 | /* not-currently-used entry */ |
189 | return FAT_DENTRY_FREE; |
189 | return FAT_DENTRY_FREE; |
190 | } |
190 | } |
191 | if (d->name[0] == FAT_DENTRY_UNUSED) { |
191 | if (d->name[0] == FAT_DENTRY_UNUSED) { |
192 | /* never used entry */ |
192 | /* never used entry */ |
193 | return FAT_DENTRY_LAST; |
193 | return FAT_DENTRY_LAST; |
194 | } |
194 | } |
195 | if (d->name[0] == FAT_DENTRY_DOT) { |
195 | if (d->name[0] == FAT_DENTRY_DOT) { |
196 | /* |
196 | /* |
197 | * Most likely '.' or '..'. |
197 | * Most likely '.' or '..'. |
198 | * It cannot occur in a regular file name. |
198 | * It cannot occur in a regular file name. |
199 | */ |
199 | */ |
200 | return FAT_DENTRY_SKIP; |
200 | return FAT_DENTRY_SKIP; |
201 | } |
201 | } |
202 | return FAT_DENTRY_VALID; |
202 | return FAT_DENTRY_VALID; |
203 | } |
203 | } |
204 | 204 | ||
205 | /** |
205 | /** |
206 | * @} |
206 | * @} |
207 | */ |
207 | */ |
208 | 208 |