dfs_romfs.c 7.9 KB
Newer Older
1 2 3 4 5
/*
 * File      : dfs_romfs.c
 * This file is part of Device File System in RT-Thread RTOS
 * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
 *
Y
yiyue.fang 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 20 21 22 23 24 25 26
 *
 * Change Logs:
 * Date           Author       Notes
 */

#include <rtthread.h>
#include <dfs.h>
#include <dfs_fs.h>
B
bernard 已提交
27 28
#include <dfs_file.h>

29 30 31 32
#include "dfs_romfs.h"

int dfs_romfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
{
B
bernard 已提交
33
    struct romfs_dirent *root_dirent;
34

B
bernard 已提交
35 36
    if (data == NULL)
        return -EIO;
37

B
bernard 已提交
38 39
    root_dirent = (struct romfs_dirent *)data;
    fs->data = root_dirent;
40

B
bernard 已提交
41
    return RT_EOK;
42 43 44 45
}

int dfs_romfs_unmount(struct dfs_filesystem *fs)
{
B
bernard 已提交
46
    return RT_EOK;
47 48 49 50
}

int dfs_romfs_ioctl(struct dfs_fd *file, int cmd, void *args)
{
B
bernard 已提交
51
    return -EIO;
52 53
}

G
Grissiom 已提交
54 55
rt_inline int check_dirent(struct romfs_dirent *dirent)
{
56 57
    if ((dirent->type != ROMFS_DIRENT_FILE && dirent->type != ROMFS_DIRENT_DIR)
        || dirent->size == ~0)
G
Grissiom 已提交
58 59 60 61
        return -1;
    return 0;
}

62 63
struct romfs_dirent *dfs_romfs_lookup(struct romfs_dirent *root_dirent, const char *path, rt_size_t *size)
{
B
bernard 已提交
64 65 66 67
    rt_size_t index, found;
    const char *subpath, *subpath_end;
    struct romfs_dirent *dirent;
    rt_size_t dirent_size;
68

G
Grissiom 已提交
69 70
    /* Check the root_dirent. */
    if (check_dirent(root_dirent) != 0)
B
bernard 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        return NULL;

    if (path[0] == '/' && path[1] == '\0')
    {
        *size = root_dirent->size;
        return root_dirent;
    }

    /* goto root directy entries */
    dirent = (struct romfs_dirent *)root_dirent->data;
    dirent_size = root_dirent->size;

    /* get the end position of this subpath */
    subpath_end = path;
    /* skip /// */
    while (*subpath_end && *subpath_end == '/')
        subpath_end ++;
    subpath = subpath_end;
    while ((*subpath_end != '/') && *subpath_end)
        subpath_end ++;

    while (dirent != NULL)
    {
        found = 0;

        /* search in folder */
        for (index = 0; index < dirent_size; index ++)
        {
G
Grissiom 已提交
99
            if (check_dirent(&dirent[index]) != 0)
B
bernard 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
                return NULL;
            if (rt_strlen(dirent[index].name) == (subpath_end - subpath) &&
                    rt_strncmp(dirent[index].name, subpath, (subpath_end - subpath)) == 0)
            {
                dirent_size = dirent[index].size;

                /* skip /// */
                while (*subpath_end && *subpath_end == '/')
                    subpath_end ++;
                subpath = subpath_end;
                while ((*subpath_end != '/') && *subpath_end)
                    subpath_end ++;

                if (!(*subpath))
                {
                    *size = dirent_size;
                    return &dirent[index];
                }

                if (dirent[index].type == ROMFS_DIRENT_DIR)
                {
                    /* enter directory */
                    dirent = (struct romfs_dirent*)dirent[index].data;
                    found = 1;
                    break;
                }
                else
                {
                    /* return file dirent */
                    if (subpath != NULL)
                        break; /* not the end of path */

                    return &dirent[index];
                }
            }
        }

        if (!found)
            break; /* not found */
    }

    /* not found */
    return NULL;
143 144
}

B
bernard 已提交
145
int dfs_romfs_read(struct dfs_fd *file, void *buf, size_t count)
146
{
B
bernard 已提交
147 148
    rt_size_t length;
    struct romfs_dirent *dirent;
149

B
bernard 已提交
150 151
    dirent = (struct romfs_dirent *)file->data;
    RT_ASSERT(dirent != NULL);
152

G
Grissiom 已提交
153 154
    if (check_dirent(dirent) != 0)
    {
B
bernard 已提交
155
        return -EIO;
G
Grissiom 已提交
156 157
    }

B
bernard 已提交
158 159 160 161
    if (count < file->size - file->pos)
        length = count;
    else
        length = file->size - file->pos;
162

B
bernard 已提交
163 164
    if (length > 0)
        memcpy(buf, &(dirent->data[file->pos]), length);
165

B
bernard 已提交
166 167
    /* update file current position */
    file->pos += length;
168

B
bernard 已提交
169
    return length;
170 171
}

B
bernard 已提交
172
int dfs_romfs_lseek(struct dfs_fd *file, off_t offset)
173
{
B
bernard 已提交
174 175 176 177 178
    if (offset <= file->size)
    {
        file->pos = offset;
        return file->pos;
    }
179

B
bernard 已提交
180
    return -EIO;
181 182 183 184
}

int dfs_romfs_close(struct dfs_fd *file)
{
B
bernard 已提交
185 186
    file->data = NULL;
    return RT_EOK;
187 188 189 190
}

int dfs_romfs_open(struct dfs_fd *file)
{
B
bernard 已提交
191 192 193
    rt_size_t size;
    struct romfs_dirent *dirent;
    struct romfs_dirent *root_dirent;
194

B
bernard 已提交
195
    root_dirent = (struct romfs_dirent *)file->data;
196

197
    if (check_dirent(root_dirent) != 0)
B
bernard 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
        return -EIO;

    if (file->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR))
        return -EINVAL;

    dirent = dfs_romfs_lookup(root_dirent, file->path, &size);
    if (dirent == NULL)
        return -ENOENT;

    /* entry is a directory file type */
    if (dirent->type == ROMFS_DIRENT_DIR)
    {
        if (!(file->flags & O_DIRECTORY))
            return -ENOENT;
    }
    else
    {
        /* entry is a file, but open it as a directory */
        if (file->flags & O_DIRECTORY)
            return -ENOENT;
    }

    file->data = dirent;
    file->size = size;
    file->pos = 0;

    return RT_EOK;
225 226 227 228
}

int dfs_romfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
{
B
bernard 已提交
229 230 231
    rt_size_t size;
    struct romfs_dirent *dirent;
    struct romfs_dirent *root_dirent;
232

B
bernard 已提交
233 234
    root_dirent = (struct romfs_dirent *)fs->data;
    dirent = dfs_romfs_lookup(root_dirent, path, &size);
235

B
bernard 已提交
236 237
    if (dirent == NULL)
        return -ENOENT;
238

B
bernard 已提交
239 240 241
    st->st_dev = 0;
    st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
    S_IWUSR | S_IWGRP | S_IWOTH;
242

B
bernard 已提交
243 244 245 246 247
    if (dirent->type == ROMFS_DIRENT_DIR)
    {
        st->st_mode &= ~S_IFREG;
        st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
    }
248

B
bernard 已提交
249 250
    st->st_size = dirent->size;
    st->st_mtime = 0;
251

B
bernard 已提交
252
    return RT_EOK;
253 254
}

B
bernard 已提交
255
int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count)
256
{
B
bernard 已提交
257 258 259 260
    rt_size_t index;
    const char *name;
    struct dirent *d;
    struct romfs_dirent *dirent, *sub_dirent;
261

B
bernard 已提交
262
    dirent = (struct romfs_dirent *)file->data;
G
Grissiom 已提交
263
    if (check_dirent(dirent) != 0)
B
bernard 已提交
264 265
        return -EIO;
    RT_ASSERT(dirent->type == ROMFS_DIRENT_DIR);
266

B
bernard 已提交
267 268
    /* enter directory */
    dirent = (struct romfs_dirent *)dirent->data;
G
Grissiom 已提交
269

B
bernard 已提交
270 271 272 273
    /* make integer count */
    count = (count / sizeof(struct dirent));
    if (count == 0)
        return -EINVAL;
G
Grissiom 已提交
274

B
bernard 已提交
275 276 277 278
    index = 0;
    for (index = 0; index < count && file->pos < file->size; index ++)
    {
        d = dirp + index;
279

B
bernard 已提交
280 281
        sub_dirent = &dirent[file->pos];
        name = sub_dirent->name;
282

B
bernard 已提交
283 284 285 286 287
        /* fill dirent */
        if (sub_dirent->type == ROMFS_DIRENT_DIR)
            d->d_type = DT_DIR;
        else
            d->d_type = DT_REG;
288

B
bernard 已提交
289 290 291
        d->d_namlen = rt_strlen(name);
        d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
        rt_strncpy(d->d_name, name, rt_strlen(name) + 1);
292

B
bernard 已提交
293 294 295
        /* move to next position */
        ++ file->pos;
    }
296

B
bernard 已提交
297
    return index * sizeof(struct dirent);
298 299
}

B
bernard 已提交
300
static const struct dfs_file_ops _rom_fops =
301
{
B
bernard 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    dfs_romfs_open,
    dfs_romfs_close,
    dfs_romfs_ioctl,
    dfs_romfs_read,
    NULL,
    NULL,
    dfs_romfs_lseek,
    dfs_romfs_getdents,
};
static const struct dfs_filesystem_ops _romfs =
{
    "rom",
    DFS_FS_FLAG_DEFAULT,
    &_rom_fops,

    dfs_romfs_mount,
    dfs_romfs_unmount,
    NULL,
    NULL,

    NULL,
    dfs_romfs_stat,
    NULL,
325 326 327 328 329 330
};

int dfs_romfs_init(void)
{
    /* register rom file system */
    dfs_register(&_romfs);
B
bernard 已提交
331
    return 0;
332
}
B
Bernard Xiong 已提交
333
INIT_FS_EXPORT(dfs_romfs_init);
334