Subversion Repositories HelenOS

Rev

Rev 4327 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3504 jermar 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
 
3505 jermar 38
#include "fat_dentry.h"
3628 jermar 39
#include <ctype.h>
3634 jermar 40
#include <string.h>
3504 jermar 41
 
3628 jermar 42
static bool is_d_char(const char ch)
3505 jermar 43
{
3628 jermar 44
    if (isalnum(ch) || ch == '_')
45
        return true;
46
    else
47
        return false;
48
}
49
 
3634 jermar 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;
4327 mejdrech 65
    size_t size;
66
 
3634 jermar 67
    if (!(rc = stricmp(name, component)))
68
        return rc;
4327 mejdrech 69
    if (!str_chr(name, '.')) {
3634 jermar 70
        /*
71
         * There is no '.' in the name, so we know that there is enough
72
         * space for appending an extra '.' to name.
73
         */
4327 mejdrech 74
        size = str_size(name);
75
        name[size] = '.';
76
        name[size + 1] = '\0';
3634 jermar 77
        rc = stricmp(name, component);
78
    }
79
    return rc;
80
}
81
 
3628 jermar 82
bool fat_dentry_name_verify(const char *name)
83
{
84
    unsigned i, dot;
85
    bool dot_found = false;
86
 
87
 
88
    for (i = 0; name[i]; i++) {
89
        if (name[i] == '.') {
90
            if (dot_found) {
91
                return false;
92
            } else {
93
                dot_found = true;
94
                dot = i;
95
            }
96
        } else {
97
            if (!is_d_char(name[i]))
98
                return false;
99
        }
100
    }
101
 
102
    if (dot_found) {
103
        if (dot > FAT_NAME_LEN)
104
            return false;
105
        if (i - dot > FAT_EXT_LEN + 1)
106
            return false;
107
    } else {
108
        if (i > FAT_NAME_LEN)
109
            return false;
110
    }
111
 
112
    return true;
113
}
114
 
115
void fat_dentry_name_get(const fat_dentry_t *d, char *buf)
116
{
4581 mejdrech 117
    unsigned int i;
118
 
3505 jermar 119
    for (i = 0; i < FAT_NAME_LEN; i++) {
120
        if (d->name[i] == FAT_PAD)
121
            break;
4581 mejdrech 122
 
3505 jermar 123
        if (d->name[i] == FAT_DENTRY_E5_ESC)
124
            *buf++ = 0xe5;
4581 mejdrech 125
        else {
126
            if (d->lcase & FAT_LCASE_LOWER_NAME)
127
                *buf++ = tolower(d->name[i]);
128
            else
129
                *buf++ = d->name[i];
130
        }
3505 jermar 131
    }
4581 mejdrech 132
 
3505 jermar 133
    if (d->ext[0] != FAT_PAD)
134
        *buf++ = '.';
4581 mejdrech 135
 
3505 jermar 136
    for (i = 0; i < FAT_EXT_LEN; i++) {
137
        if (d->ext[i] == FAT_PAD) {
138
            *buf = '\0';
139
            return;
140
        }
4581 mejdrech 141
 
3505 jermar 142
        if (d->ext[i] == FAT_DENTRY_E5_ESC)
143
            *buf++ = 0xe5;
4581 mejdrech 144
        else {
145
            if (d->lcase & FAT_LCASE_LOWER_EXT)
146
                *buf++ = tolower(d->ext[i]);
147
            else
148
                *buf++ = d->ext[i];
149
        }
3505 jermar 150
    }
4581 mejdrech 151
 
3505 jermar 152
    *buf = '\0';
153
}
154
 
3628 jermar 155
void fat_dentry_name_set(fat_dentry_t *d, const char *name)
3505 jermar 156
{
4581 mejdrech 157
    unsigned int i;
3628 jermar 158
    const char fake_ext[] = "   ";
4581 mejdrech 159
    bool lower_name = true;
160
    bool lower_ext = true;
161
 
3628 jermar 162
    for (i = 0; i < FAT_NAME_LEN; i++) {
163
        switch ((uint8_t) *name) {
164
        case 0xe5:
165
            d->name[i] = FAT_DENTRY_E5_ESC;
166
            name++;
167
            break;
168
        case '\0':
169
        case '.':
170
            d->name[i] = FAT_PAD;
171
            break;
172
        default:
4581 mejdrech 173
            if (isalpha(*name)) {
174
                if (!islower(*name))
175
                    lower_name = false;
176
            }
177
 
3628 jermar 178
            d->name[i] = toupper(*name++);
179
            break;
180
        }
181
    }
4581 mejdrech 182
 
3628 jermar 183
    if (*name++ != '.')
184
        name = fake_ext;
4581 mejdrech 185
 
3628 jermar 186
    for (i = 0; i < FAT_EXT_LEN; i++) {
187
        switch ((uint8_t) *name) {
188
        case 0xe5:
189
            d->ext[i] = FAT_DENTRY_E5_ESC;
190
            name++;
191
            break;
192
        case '\0':
193
            d->ext[i] = FAT_PAD;
194
            break;
195
        default:
4581 mejdrech 196
            if (isalpha(*name)) {
197
                if (!islower(*name))
198
                    lower_ext = false;
199
            }
200
 
3628 jermar 201
            d->ext[i] = toupper(*name++);
202
            break;
203
        }
204
    }
4581 mejdrech 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;
3628 jermar 215
}
216
 
217
fat_dentry_clsf_t fat_classify_dentry(const fat_dentry_t *d)
218
{
3505 jermar 219
    if (d->attr & FAT_ATTR_VOLLABEL) {
220
        /* volume label entry */
221
        return FAT_DENTRY_SKIP;
222
    }
223
    if (d->name[0] == FAT_DENTRY_ERASED) {
224
        /* not-currently-used entry */
3628 jermar 225
        return FAT_DENTRY_FREE;
3505 jermar 226
    }
227
    if (d->name[0] == FAT_DENTRY_UNUSED) {
228
        /* never used entry */
229
        return FAT_DENTRY_LAST;
230
    }
231
    if (d->name[0] == FAT_DENTRY_DOT) {
232
        /*
233
         * Most likely '.' or '..'.
234
         * It cannot occur in a regular file name.
235
         */
236
        return FAT_DENTRY_SKIP;
237
    }
238
    return FAT_DENTRY_VALID;
239
}
240
 
3504 jermar 241
/**
242
 * @}
243
 */