Rev 3536 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3536 | Rev 3675 | ||
---|---|---|---|
Line 34... | Line 34... | ||
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> |
|
- | 40 | #include <string.h> |
|
39 | 41 | ||
- | 42 | static bool is_d_char(const char ch) |
|
- | 43 | { |
|
40 | #define FAT_PAD ' ' |
44 | if (isalnum(ch) || ch == '_') |
- | 45 | return true; |
|
- | 46 | else |
|
- | 47 | return false; |
|
- | 48 | } |
|
41 | 49 | ||
- | 50 | /** Compare path component with the name read from the dentry. |
|
- | 51 | * |
|
- | 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 |
|
- | 54 | * dot character at the end of the name (i.e. when there is a dot, but no |
|
- | 55 | * extension). |
|
- | 56 | * |
|
- | 57 | * @param name Node name read from the dentry. |
|
- | 58 | * @param component Path component. |
|
- | 59 | * |
|
- | 60 | * @return Zero on match, non-zero otherwise. |
|
- | 61 | */ |
|
- | 62 | int fat_dentry_namecmp(char *name, const char *component) |
|
- | 63 | { |
|
- | 64 | int rc; |
|
- | 65 | if (!(rc = stricmp(name, component))) |
|
- | 66 | return rc; |
|
42 | #define FAT_DENTRY_UNUSED 0x00 |
67 | if (!strchr(name, '.')) { |
- | 68 | /* |
|
- | 69 | * There is no '.' in the name, so we know that there is enough |
|
- | 70 | * space for appending an extra '.' to name. |
|
- | 71 | */ |
|
43 | #define FAT_DENTRY_E5_ESC 0x05 |
72 | name[strlen(name)] = '.'; |
44 | #define FAT_DENTRY_DOT 0x2e |
73 | name[strlen(name) + 1] = '\0'; |
45 | #define FAT_DENTRY_ERASED 0xe5 |
74 | rc = stricmp(name, component); |
- | 75 | } |
|
- | 76 | return rc; |
|
- | 77 | } |
|
46 | 78 | ||
- | 79 | bool fat_dentry_name_verify(const char *name) |
|
- | 80 | { |
|
- | 81 | unsigned i, dot; |
|
- | 82 | bool dot_found = false; |
|
- | 83 | ||
- | 84 | ||
- | 85 | for (i = 0; name[i]; i++) { |
|
- | 86 | if (name[i] == '.') { |
|
- | 87 | if (dot_found) { |
|
- | 88 | return false; |
|
- | 89 | } else { |
|
- | 90 | dot_found = true; |
|
- | 91 | dot = i; |
|
- | 92 | } |
|
- | 93 | } else { |
|
- | 94 | if (!is_d_char(name[i])) |
|
- | 95 | return false; |
|
- | 96 | } |
|
- | 97 | } |
|
- | 98 | ||
- | 99 | if (dot_found) { |
|
- | 100 | if (dot > FAT_NAME_LEN) |
|
- | 101 | return false; |
|
- | 102 | if (i - dot > FAT_EXT_LEN + 1) |
|
- | 103 | return false; |
|
- | 104 | } else { |
|
- | 105 | if (i > FAT_NAME_LEN) |
|
- | 106 | return false; |
|
- | 107 | } |
|
- | 108 | ||
- | 109 | return true; |
|
- | 110 | } |
|
- | 111 | ||
47 | void dentry_name_canonify(fat_dentry_t *d, char *buf) |
112 | void fat_dentry_name_get(const fat_dentry_t *d, char *buf) |
48 | { |
113 | { |
49 | int i; |
114 | int i; |
50 | 115 | ||
51 | for (i = 0; i < FAT_NAME_LEN; i++) { |
116 | for (i = 0; i < FAT_NAME_LEN; i++) { |
52 | if (d->name[i] == FAT_PAD) |
117 | if (d->name[i] == FAT_PAD) |
Line 69... | Line 134... | ||
69 | *buf++ = d->ext[i]; |
134 | *buf++ = d->ext[i]; |
70 | } |
135 | } |
71 | *buf = '\0'; |
136 | *buf = '\0'; |
72 | } |
137 | } |
73 | 138 | ||
- | 139 | void fat_dentry_name_set(fat_dentry_t *d, const char *name) |
|
- | 140 | { |
|
- | 141 | int i; |
|
- | 142 | const char fake_ext[] = " "; |
|
- | 143 | ||
- | 144 | ||
- | 145 | for (i = 0; i < FAT_NAME_LEN; i++) { |
|
- | 146 | switch ((uint8_t) *name) { |
|
- | 147 | case 0xe5: |
|
- | 148 | d->name[i] = FAT_DENTRY_E5_ESC; |
|
- | 149 | name++; |
|
- | 150 | break; |
|
- | 151 | case '\0': |
|
- | 152 | case '.': |
|
- | 153 | d->name[i] = FAT_PAD; |
|
- | 154 | break; |
|
- | 155 | default: |
|
- | 156 | d->name[i] = toupper(*name++); |
|
- | 157 | break; |
|
- | 158 | } |
|
- | 159 | } |
|
- | 160 | if (*name++ != '.') |
|
- | 161 | name = fake_ext; |
|
- | 162 | for (i = 0; i < FAT_EXT_LEN; i++) { |
|
- | 163 | switch ((uint8_t) *name) { |
|
- | 164 | case 0xe5: |
|
- | 165 | d->ext[i] = FAT_DENTRY_E5_ESC; |
|
- | 166 | name++; |
|
- | 167 | break; |
|
- | 168 | case '\0': |
|
- | 169 | d->ext[i] = FAT_PAD; |
|
- | 170 | break; |
|
- | 171 | default: |
|
- | 172 | d->ext[i] = toupper(*name++); |
|
- | 173 | break; |
|
- | 174 | } |
|
- | 175 | } |
|
- | 176 | } |
|
- | 177 | ||
74 | fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d) |
178 | fat_dentry_clsf_t fat_classify_dentry(const fat_dentry_t *d) |
75 | { |
179 | { |
76 | if (d->attr & FAT_ATTR_VOLLABEL) { |
180 | if (d->attr & FAT_ATTR_VOLLABEL) { |
77 | /* volume label entry */ |
181 | /* volume label entry */ |
78 | return FAT_DENTRY_SKIP; |
182 | return FAT_DENTRY_SKIP; |
79 | } |
183 | } |
80 | if (d->name[0] == FAT_DENTRY_ERASED) { |
184 | if (d->name[0] == FAT_DENTRY_ERASED) { |
81 | /* not-currently-used entry */ |
185 | /* not-currently-used entry */ |
82 | return FAT_DENTRY_SKIP; |
186 | return FAT_DENTRY_FREE; |
83 | } |
187 | } |
84 | if (d->name[0] == FAT_DENTRY_UNUSED) { |
188 | if (d->name[0] == FAT_DENTRY_UNUSED) { |
85 | /* never used entry */ |
189 | /* never used entry */ |
86 | return FAT_DENTRY_LAST; |
190 | return FAT_DENTRY_LAST; |
87 | } |
191 | } |