Subversion Repositories HelenOS

Rev

Rev 3634 | Rev 4264 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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