[6ebaff9] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Jakub Jermar
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 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
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 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
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 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
|
---|
| 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
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup fs
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file fat_dentry.c
|
---|
| 35 | * @brief Functions that work with FAT directory entries.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
[033ef7d3] | 38 | #include "fat_dentry.h"
|
---|
[0fdd6bb] | 39 | #include <ctype.h>
|
---|
[19f857a] | 40 | #include <str.h>
|
---|
[033ef7d3] | 41 |
|
---|
[0fdd6bb] | 42 | static bool is_d_char(const char ch)
|
---|
| 43 | {
|
---|
| 44 | if (isalnum(ch) || ch == '_')
|
---|
| 45 | return true;
|
---|
| 46 | else
|
---|
| 47 | return false;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[14c331a] | 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;
|
---|
[92fd52d7] | 65 | size_t size;
|
---|
| 66 |
|
---|
[14c331a] | 67 | if (!(rc = stricmp(name, component)))
|
---|
| 68 | return rc;
|
---|
[7afb4a5] | 69 | if (!str_chr(name, '.')) {
|
---|
[14c331a] | 70 | /*
|
---|
| 71 | * There is no '.' in the name, so we know that there is enough
|
---|
| 72 | * space for appending an extra '.' to name.
|
---|
| 73 | */
|
---|
[92fd52d7] | 74 | size = str_size(name);
|
---|
| 75 | name[size] = '.';
|
---|
| 76 | name[size + 1] = '\0';
|
---|
[14c331a] | 77 | rc = stricmp(name, component);
|
---|
| 78 | }
|
---|
| 79 | return rc;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[0fdd6bb] | 82 | bool fat_dentry_name_verify(const char *name)
|
---|
| 83 | {
|
---|
[ed903174] | 84 | unsigned int i;
|
---|
| 85 | unsigned int dot = 0;
|
---|
[0fdd6bb] | 86 | bool dot_found = false;
|
---|
| 87 |
|
---|
| 88 |
|
---|
| 89 | for (i = 0; name[i]; i++) {
|
---|
| 90 | if (name[i] == '.') {
|
---|
| 91 | if (dot_found) {
|
---|
| 92 | return false;
|
---|
| 93 | } else {
|
---|
| 94 | dot_found = true;
|
---|
| 95 | dot = i;
|
---|
| 96 | }
|
---|
| 97 | } else {
|
---|
| 98 | if (!is_d_char(name[i]))
|
---|
| 99 | return false;
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | if (dot_found) {
|
---|
| 104 | if (dot > FAT_NAME_LEN)
|
---|
| 105 | return false;
|
---|
| 106 | if (i - dot > FAT_EXT_LEN + 1)
|
---|
| 107 | return false;
|
---|
| 108 | } else {
|
---|
| 109 | if (i > FAT_NAME_LEN)
|
---|
| 110 | return false;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | return true;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | void fat_dentry_name_get(const fat_dentry_t *d, char *buf)
|
---|
[033ef7d3] | 117 | {
|
---|
[a248234] | 118 | unsigned int i;
|
---|
| 119 |
|
---|
[033ef7d3] | 120 | for (i = 0; i < FAT_NAME_LEN; i++) {
|
---|
| 121 | if (d->name[i] == FAT_PAD)
|
---|
| 122 | break;
|
---|
[a248234] | 123 |
|
---|
[033ef7d3] | 124 | if (d->name[i] == FAT_DENTRY_E5_ESC)
|
---|
| 125 | *buf++ = 0xe5;
|
---|
[a248234] | 126 | else {
|
---|
| 127 | if (d->lcase & FAT_LCASE_LOWER_NAME)
|
---|
| 128 | *buf++ = tolower(d->name[i]);
|
---|
| 129 | else
|
---|
| 130 | *buf++ = d->name[i];
|
---|
| 131 | }
|
---|
[033ef7d3] | 132 | }
|
---|
[a248234] | 133 |
|
---|
[033ef7d3] | 134 | if (d->ext[0] != FAT_PAD)
|
---|
| 135 | *buf++ = '.';
|
---|
[a248234] | 136 |
|
---|
[033ef7d3] | 137 | for (i = 0; i < FAT_EXT_LEN; i++) {
|
---|
| 138 | if (d->ext[i] == FAT_PAD) {
|
---|
| 139 | *buf = '\0';
|
---|
| 140 | return;
|
---|
| 141 | }
|
---|
[a248234] | 142 |
|
---|
[033ef7d3] | 143 | if (d->ext[i] == FAT_DENTRY_E5_ESC)
|
---|
| 144 | *buf++ = 0xe5;
|
---|
[a248234] | 145 | else {
|
---|
| 146 | if (d->lcase & FAT_LCASE_LOWER_EXT)
|
---|
| 147 | *buf++ = tolower(d->ext[i]);
|
---|
| 148 | else
|
---|
| 149 | *buf++ = d->ext[i];
|
---|
| 150 | }
|
---|
[033ef7d3] | 151 | }
|
---|
[a248234] | 152 |
|
---|
[033ef7d3] | 153 | *buf = '\0';
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[0fdd6bb] | 156 | void fat_dentry_name_set(fat_dentry_t *d, const char *name)
|
---|
| 157 | {
|
---|
[a248234] | 158 | unsigned int i;
|
---|
[0fdd6bb] | 159 | const char fake_ext[] = " ";
|
---|
[a248234] | 160 | bool lower_name = true;
|
---|
| 161 | bool lower_ext = true;
|
---|
| 162 |
|
---|
[0fdd6bb] | 163 | for (i = 0; i < FAT_NAME_LEN; i++) {
|
---|
| 164 | switch ((uint8_t) *name) {
|
---|
| 165 | case 0xe5:
|
---|
| 166 | d->name[i] = FAT_DENTRY_E5_ESC;
|
---|
| 167 | name++;
|
---|
| 168 | break;
|
---|
| 169 | case '\0':
|
---|
| 170 | case '.':
|
---|
| 171 | d->name[i] = FAT_PAD;
|
---|
| 172 | break;
|
---|
| 173 | default:
|
---|
[a248234] | 174 | if (isalpha(*name)) {
|
---|
| 175 | if (!islower(*name))
|
---|
| 176 | lower_name = false;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[0fdd6bb] | 179 | d->name[i] = toupper(*name++);
|
---|
| 180 | break;
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
[a248234] | 183 |
|
---|
[0fdd6bb] | 184 | if (*name++ != '.')
|
---|
| 185 | name = fake_ext;
|
---|
[a248234] | 186 |
|
---|
[0fdd6bb] | 187 | for (i = 0; i < FAT_EXT_LEN; i++) {
|
---|
| 188 | switch ((uint8_t) *name) {
|
---|
| 189 | case 0xe5:
|
---|
| 190 | d->ext[i] = FAT_DENTRY_E5_ESC;
|
---|
| 191 | name++;
|
---|
| 192 | break;
|
---|
| 193 | case '\0':
|
---|
| 194 | d->ext[i] = FAT_PAD;
|
---|
| 195 | break;
|
---|
| 196 | default:
|
---|
[a248234] | 197 | if (isalpha(*name)) {
|
---|
| 198 | if (!islower(*name))
|
---|
| 199 | lower_ext = false;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[0fdd6bb] | 202 | d->ext[i] = toupper(*name++);
|
---|
| 203 | break;
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
[a248234] | 206 |
|
---|
| 207 | if (lower_name)
|
---|
| 208 | d->lcase |= FAT_LCASE_LOWER_NAME;
|
---|
| 209 | else
|
---|
| 210 | d->lcase &= ~FAT_LCASE_LOWER_NAME;
|
---|
| 211 |
|
---|
| 212 | if (lower_ext)
|
---|
| 213 | d->lcase |= FAT_LCASE_LOWER_EXT;
|
---|
| 214 | else
|
---|
| 215 | d->lcase &= ~FAT_LCASE_LOWER_EXT;
|
---|
[0fdd6bb] | 216 | }
|
---|
| 217 |
|
---|
| 218 | fat_dentry_clsf_t fat_classify_dentry(const fat_dentry_t *d)
|
---|
[033ef7d3] | 219 | {
|
---|
| 220 | if (d->attr & FAT_ATTR_VOLLABEL) {
|
---|
| 221 | /* volume label entry */
|
---|
| 222 | return FAT_DENTRY_SKIP;
|
---|
| 223 | }
|
---|
| 224 | if (d->name[0] == FAT_DENTRY_ERASED) {
|
---|
| 225 | /* not-currently-used entry */
|
---|
[0fdd6bb] | 226 | return FAT_DENTRY_FREE;
|
---|
[033ef7d3] | 227 | }
|
---|
| 228 | if (d->name[0] == FAT_DENTRY_UNUSED) {
|
---|
| 229 | /* never used entry */
|
---|
| 230 | return FAT_DENTRY_LAST;
|
---|
| 231 | }
|
---|
| 232 | if (d->name[0] == FAT_DENTRY_DOT) {
|
---|
| 233 | /*
|
---|
| 234 | * Most likely '.' or '..'.
|
---|
| 235 | * It cannot occur in a regular file name.
|
---|
| 236 | */
|
---|
| 237 | return FAT_DENTRY_SKIP;
|
---|
| 238 | }
|
---|
| 239 | return FAT_DENTRY_VALID;
|
---|
| 240 | }
|
---|
[6ebaff9] | 241 |
|
---|
| 242 | /**
|
---|
| 243 | * @}
|
---|
| 244 | */
|
---|