Subversion Repositories HelenOS

Rev

Rev 3628 | Go to most recent revision | 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
 
3505 jermar 42
#define FAT_PAD         ' ' 
43
 
44
#define FAT_DENTRY_UNUSED   0x00
45
#define FAT_DENTRY_E5_ESC   0x05
46
#define FAT_DENTRY_DOT      0x2e
47
#define FAT_DENTRY_ERASED   0xe5
48
 
3628 jermar 49
static bool is_d_char(const char ch)
3505 jermar 50
{
3628 jermar 51
    if (isalnum(ch) || ch == '_')
52
        return true;
53
    else
54
        return false;
55
}
56
 
3634 jermar 57
/** Compare path component with the name read from the dentry.
58
 *
59
 * This function compares the path component with the name read from the dentry.
60
 * The comparison is case insensitive and tolerates a mismatch on the trailing
61
 * dot character at the end of the name (i.e. when there is a dot, but no
62
 * extension).
63
 *
64
 * @param name      Node name read from the dentry.
65
 * @param component Path component.
66
 *
67
 * @return      Zero on match, non-zero otherwise.
68
 */
69
int fat_dentry_namecmp(char *name, const char *component)
70
{
71
    int rc;
72
    if (!(rc = stricmp(name, component)))
73
        return rc;
74
    if (!strchr(name, '.')) {
75
        /*
76
         * There is no '.' in the name, so we know that there is enough
77
         * space for appending an extra '.' to name.
78
         */
79
        name[strlen(name)] = '.';
80
        name[strlen(name) + 1] = '\0';
81
        rc = stricmp(name, component);
82
    }
83
    return rc;
84
}
85
 
3628 jermar 86
bool fat_dentry_name_verify(const char *name)
87
{
88
    unsigned i, dot;
89
    bool dot_found = false;
90
 
91
 
92
    for (i = 0; name[i]; i++) {
93
        if (name[i] == '.') {
94
            if (dot_found) {
95
                return false;
96
            } else {
97
                dot_found = true;
98
                dot = i;
99
            }
100
        } else {
101
            if (!is_d_char(name[i]))
102
                return false;
103
        }
104
    }
105
 
106
    if (dot_found) {
107
        if (dot > FAT_NAME_LEN)
108
            return false;
109
        if (i - dot > FAT_EXT_LEN + 1)
110
            return false;
111
    } else {
112
        if (i > FAT_NAME_LEN)
113
            return false;
114
    }
115
 
116
    return true;
117
}
118
 
119
void fat_dentry_name_get(const fat_dentry_t *d, char *buf)
120
{
3505 jermar 121
    int i;
122
 
123
    for (i = 0; i < FAT_NAME_LEN; i++) {
124
        if (d->name[i] == FAT_PAD)
125
            break;
126
        if (d->name[i] == FAT_DENTRY_E5_ESC)
127
            *buf++ = 0xe5;
128
        else
129
            *buf++ = d->name[i];
130
    }
131
    if (d->ext[0] != FAT_PAD)
132
        *buf++ = '.';
133
    for (i = 0; i < FAT_EXT_LEN; i++) {
134
        if (d->ext[i] == FAT_PAD) {
135
            *buf = '\0';
136
            return;
137
        }
138
        if (d->ext[i] == FAT_DENTRY_E5_ESC)
139
            *buf++ = 0xe5;
140
        else
141
            *buf++ = d->ext[i];
142
    }
143
    *buf = '\0';
144
}
145
 
3628 jermar 146
void fat_dentry_name_set(fat_dentry_t *d, const char *name)
3505 jermar 147
{
3628 jermar 148
    int i;
149
    const char fake_ext[] = "   ";
150
 
151
 
152
    for (i = 0; i < FAT_NAME_LEN; i++) {
153
        switch ((uint8_t) *name) {
154
        case 0xe5:
155
            d->name[i] = FAT_DENTRY_E5_ESC;
156
            name++;
157
            break;
158
        case '\0':
159
        case '.':
160
            d->name[i] = FAT_PAD;
161
            break;
162
        default:
163
            d->name[i] = toupper(*name++);
164
            break;
165
        }
166
    }
167
    if (*name++ != '.')
168
        name = fake_ext;
169
    for (i = 0; i < FAT_EXT_LEN; i++) {
170
        switch ((uint8_t) *name) {
171
        case 0xe5:
172
            d->ext[i] = FAT_DENTRY_E5_ESC;
173
            name++;
174
            break;
175
        case '\0':
176
            d->ext[i] = FAT_PAD;
177
            break;
178
        default:
179
            d->ext[i] = toupper(*name++);
180
            break;
181
        }
182
    }
183
}
184
 
185
fat_dentry_clsf_t fat_classify_dentry(const fat_dentry_t *d)
186
{
3505 jermar 187
    if (d->attr & FAT_ATTR_VOLLABEL) {
188
        /* volume label entry */
189
        return FAT_DENTRY_SKIP;
190
    }
191
    if (d->name[0] == FAT_DENTRY_ERASED) {
192
        /* not-currently-used entry */
3628 jermar 193
        return FAT_DENTRY_FREE;
3505 jermar 194
    }
195
    if (d->name[0] == FAT_DENTRY_UNUSED) {
196
        /* never used entry */
197
        return FAT_DENTRY_LAST;
198
    }
199
    if (d->name[0] == FAT_DENTRY_DOT) {
200
        /*
201
         * Most likely '.' or '..'.
202
         * It cannot occur in a regular file name.
203
         */
204
        return FAT_DENTRY_SKIP;
205
    }
206
    return FAT_DENTRY_VALID;
207
}
208
 
3504 jermar 209
/**
210
 * @}
211
 */