dfs.c 12.0 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8
 *
 * Change Logs:
 * Date           Author       Notes
 * 2005-02-22     Bernard      The first version.
9
 * 2017-12-11     Bernard      Use rt_free to instead of free in fd_is_open().
10
 * 2018-03-20     Heyuanjie    dynamic allocation FD
11 12 13 14 15
 */

#include <dfs.h>
#include <dfs_fs.h>
#include <dfs_file.h>
B
bernard 已提交
16
#include "dfs_private.h"
17 18 19
#ifdef RT_USING_LWP
#include <lwp.h>
#endif
20 21

/* Global variables */
B
bernard 已提交
22
const struct dfs_filesystem_ops *filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX];
23 24 25 26 27 28 29 30 31
struct dfs_filesystem filesystem_table[DFS_FILESYSTEMS_MAX];

/* device filesystem lock */
static struct rt_mutex fslock;

#ifdef DFS_USING_WORKDIR
char working_directory[DFS_PATH_MAX] = {"/"};
#endif

32 33
static struct dfs_fdtable _fdtab;
static int  fd_alloc(struct dfs_fdtable *fdt, int startfd);
34 35 36 37

/**
 * @addtogroup DFS
 */
38

39 40 41 42 43
/*@{*/

/**
 * this function will initialize device file system.
 */
B
Bernard Xiong 已提交
44
int dfs_init(void)
45
{
46 47 48
    static rt_bool_t init_ok = RT_FALSE;

    if (init_ok)
wuyangyong's avatar
wuyangyong 已提交
49 50
    {
        rt_kprintf("dfs already init.\n");
51
        return 0;
wuyangyong's avatar
wuyangyong 已提交
52 53
    }

54
    /* clear filesystem operations table */
B
bernard 已提交
55
    memset((void *)filesystem_operation_table, 0, sizeof(filesystem_operation_table));
56
    /* clear filesystem table */
B
bernard 已提交
57
    memset(filesystem_table, 0, sizeof(filesystem_table));
58
    /* clean fd table */
59
    memset(&_fdtab, 0, sizeof(_fdtab));
60

61 62
    /* create device filesystem lock */
    rt_mutex_init(&fslock, "fslock", RT_IPC_FLAG_FIFO);
63 64

#ifdef DFS_USING_WORKDIR
65
    /* set current working directory */
B
bernard 已提交
66
    memset(working_directory, 0, sizeof(working_directory));
67
    working_directory[0] = '/';
68
#endif
B
bernard 已提交
69 70 71 72 73 74 75 76 77 78 79 80

#ifdef RT_USING_DFS_DEVFS
    {
        extern int devfs_init(void);

        /* if enable devfs, initialize and mount it as soon as possible */
        devfs_init();

        dfs_mount(NULL, "/dev", "devfs", 0, 0);
    }
#endif

81 82
    init_ok = RT_TRUE;

B
bernard 已提交
83
    return 0;
84
}
B
bernard 已提交
85
INIT_PREV_EXPORT(dfs_init);
86 87 88 89 90 91

/**
 * this function will lock device file system.
 *
 * @note please don't invoke it on ISR.
 */
92
void dfs_lock(void)
93
{
94 95 96 97 98 99
    rt_err_t result = -RT_EBUSY;

    while (result == -RT_EBUSY)
    {
        result = rt_mutex_take(&fslock, RT_WAITING_FOREVER);
    }
100

101 102 103 104
    if (result != RT_EOK)
    {
        RT_ASSERT(0);
    }
105 106 107 108 109 110 111
}

/**
 * this function will lock device file system.
 *
 * @note please don't invoke it on ISR.
 */
112
void dfs_unlock(void)
113
{
114
    rt_mutex_release(&fslock);
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
static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
{
    int idx;

    /* find an empty fd entry */
    for (idx = startfd; idx < fdt->maxfd; idx++)
    {
        if (fdt->fds[idx] == RT_NULL)
            break;
        if (fdt->fds[idx]->ref_count == 0)
            break;
    }

    /* allocate a larger FD container */
    if (idx == fdt->maxfd && fdt->maxfd < DFS_FD_MAX)
    {
        int cnt, index;
        struct dfs_fd **fds;

        /* increase the number of FD with 4 step length */
        cnt = fdt->maxfd + 4;
        cnt = cnt > DFS_FD_MAX? DFS_FD_MAX : cnt;

        fds = rt_realloc(fdt->fds, cnt * sizeof(struct dfs_fd *));
Y
yangfasheng 已提交
141
        if (fds == NULL) goto __exit; /* return fdt->maxfd */
142 143 144 145 146 147 148 149 150 151 152 153 154 155

        /* clean the new allocated fds */
        for (index = fdt->maxfd; index < cnt; index ++)
        {
            fds[index] = NULL;
        }

        fdt->fds   = fds;
        fdt->maxfd = cnt;
    }

    /* allocate  'struct dfs_fd' */
    if (idx < fdt->maxfd &&fdt->fds[idx] == RT_NULL)
    {
Y
yangfasheng 已提交
156
        fdt->fds[idx] = rt_calloc(1, sizeof(struct dfs_fd));
157 158 159 160
        if (fdt->fds[idx] == RT_NULL)
            idx = fdt->maxfd;
    }

Y
yangfasheng 已提交
161
__exit:
162 163 164
    return idx;
}

165 166 167 168 169 170 171 172
/**
 * @ingroup Fd
 * This function will allocate a file descriptor.
 *
 * @return -1 on failed or the allocated file descriptor.
 */
int fd_new(void)
{
173 174
    struct dfs_fd *d;
    int idx;
175
    struct dfs_fdtable *fdt;
176

177
    fdt = dfs_fdtable_get();
178 179
    /* lock filesystem */
    dfs_lock();
180

181
    /* find an empty fd entry */
182
    idx = fd_alloc(fdt, 0);
183

184
    /* can't find an empty fd entry */
185
    if (idx == fdt->maxfd)
186
    {
187
        idx = -(1 + DFS_FD_OFFSET);
188
        LOG_E( "DFS fd new is failed! Could not found an empty fd entry.");
189 190
        goto __result;
    }
191

192
    d = fdt->fds[idx];
193
    d->ref_count = 1;
194
    d->magic = DFS_FD_MAGIC;
195 196

__result:
197
    dfs_unlock();
B
bernard 已提交
198
    return idx + DFS_FD_OFFSET;
199 200 201 202 203 204 205 206 207 208 209
}

/**
 * @ingroup Fd
 *
 * This function will return a file descriptor structure according to file
 * descriptor.
 *
 * @return NULL on on this file descriptor or the file descriptor structure
 * pointer.
 */
210
struct dfs_fd *fd_get(int fd)
211
{
212
    struct dfs_fd *d;
213
    struct dfs_fdtable *fdt;
214

215
    fdt = dfs_fdtable_get();
B
bernard 已提交
216
    fd = fd - DFS_FD_OFFSET;
217
    if (fd < 0 || fd >= fdt->maxfd)
B
bernard 已提交
218
        return NULL;
219

220
    dfs_lock();
221
    d = fdt->fds[fd];
222

223
    /* check dfs_fd valid or not */
224
    if ((d == NULL) || (d->magic != DFS_FD_MAGIC))
225 226
    {
        dfs_unlock();
B
bernard 已提交
227
        return NULL;
228 229
    }

230 231 232
    /* increase the reference count */
    d->ref_count ++;
    dfs_unlock();
233

234
    return d;
235 236 237 238 239 240 241
}

/**
 * @ingroup Fd
 *
 * This function will put the file descriptor.
 */
242
void fd_put(struct dfs_fd *fd)
243
{
B
bernard 已提交
244
    RT_ASSERT(fd != NULL);
245

246
    dfs_lock();
247

248
    fd->ref_count --;
249

250 251 252
    /* clear this fd entry */
    if (fd->ref_count == 0)
    {
253 254 255 256 257 258 259 260 261 262 263 264 265
        int index;
        struct dfs_fdtable *fdt;

        fdt = dfs_fdtable_get();
        for (index = 0; index < fdt->maxfd; index ++)
        {
            if (fdt->fds[index] == fd)
            {
                rt_free(fd);
                fdt->fds[index] = 0;
                break;
            }
        }
266 267
    }
    dfs_unlock();
B
bernard 已提交
268
}
269

270
/**
271 272 273
 * @ingroup Fd
 *
 * This function will return whether this file has been opend.
274
 *
275 276 277 278
 * @param pathname the file path name.
 *
 * @return 0 on file has been open successfully, -1 on open failed.
 */
279
int fd_is_open(const char *pathname)
280
{
281 282 283 284
    char *fullpath;
    unsigned int index;
    struct dfs_filesystem *fs;
    struct dfs_fd *fd;
285
    struct dfs_fdtable *fdt;
286

287
    fdt = dfs_fdtable_get();
B
bernard 已提交
288 289
    fullpath = dfs_normalize_path(NULL, pathname);
    if (fullpath != NULL)
290 291 292
    {
        char *mountpath;
        fs = dfs_filesystem_lookup(fullpath);
B
bernard 已提交
293
        if (fs == NULL)
294 295
        {
            /* can't find mounted file system */
296
            rt_free(fullpath);
297 298 299 300 301 302 303

            return -1;
        }

        /* get file path name under mounted file system */
        if (fs->path[0] == '/' && fs->path[1] == '\0')
            mountpath = fullpath;
304
        else
305 306 307
            mountpath = fullpath + strlen(fs->path);

        dfs_lock();
B
bernard 已提交
308

309
        for (index = 0; index < fdt->maxfd; index++)
310
        {
311
            fd = fdt->fds[index];
Y
yangfasheng 已提交
312
            if (fd == NULL || fd->fops == NULL || fd->path == NULL) continue;
313

B
bernard 已提交
314
            if (fd->fops == fs->ops->fops && strcmp(fd->path, mountpath) == 0)
315 316 317 318 319 320 321 322 323 324 325 326 327 328
            {
                /* found file in file descriptor table */
                rt_free(fullpath);
                dfs_unlock();

                return 0;
            }
        }
        dfs_unlock();

        rt_free(fullpath);
    }

    return -1;
329 330 331 332 333 334 335 336 337 338
}

/**
 * this function will return a sub-path name under directory.
 *
 * @param directory the parent directory.
 * @param filename the filename.
 *
 * @return the subdir pointer in filename
 */
339
const char *dfs_subdir(const char *directory, const char *filename)
340
{
341
    const char *dir;
342

343
    if (strlen(directory) == strlen(filename)) /* it's a same path */
B
bernard 已提交
344
        return NULL;
345

346 347 348 349 350
    dir = filename + strlen(directory);
    if ((*dir != '/') && (dir != filename))
    {
        dir --;
    }
351

352
    return dir;
353
}
354
RTM_EXPORT(dfs_subdir);
355

356
/**
357 358
 * this function will normalize a path according to specified parent directory
 * and file name.
359 360 361 362
 *
 * @param directory the parent path
 * @param filename the file name
 *
363
 * @return the built full file path (absolute path)
364
 */
365
char *dfs_normalize_path(const char *directory, const char *filename)
366
{
367 368
    char *fullpath;
    char *dst0, *dst, *src;
369

370
    /* check parameters */
B
bernard 已提交
371
    RT_ASSERT(filename != NULL);
372 373

#ifdef DFS_USING_WORKDIR
B
bernard 已提交
374
    if (directory == NULL) /* shall use working directory */
375
        directory = &working_directory[0];
376
#else
B
bernard 已提交
377
    if ((directory == NULL) && (filename[0] != '/'))
378 379
    {
        rt_kprintf(NO_WORKING_DIR);
380

B
bernard 已提交
381
        return NULL;
382
    }
383 384
#endif

385 386 387 388
    if (filename[0] != '/') /* it's a absolute path, use it directly */
    {
        fullpath = rt_malloc(strlen(directory) + strlen(filename) + 2);

B
bernard 已提交
389 390
        if (fullpath == NULL)
            return NULL;
G
geniusgogo 已提交
391

392
        /* join path and file name */
393
        rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2,
394 395 396 397 398
            "%s/%s", directory, filename);
    }
    else
    {
        fullpath = rt_strdup(filename); /* copy string */
399

B
bernard 已提交
400 401
        if (fullpath == NULL)
            return NULL;
402 403 404 405
    }

    src = fullpath;
    dst = fullpath;
406

407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
    dst0 = dst;
    while (1)
    {
        char c = *src;

        if (c == '.')
        {
            if (!src[1]) src ++; /* '.' and ends */
            else if (src[1] == '/')
            {
                /* './' case */
                src += 2;

                while ((*src == '/') && (*src != '\0'))
                    src ++;
                continue;
            }
            else if (src[1] == '.')
            {
                if (!src[2])
                {
                    /* '..' and ends case */
                    src += 2;
                    goto up_one;
                }
                else if (src[2] == '/')
                {
                    /* '../' case */
                    src += 3;

                    while ((*src == '/') && (*src != '\0'))
                        src ++;
                    goto up_one;
                }
            }
        }

        /* copy up the next '/' and erase all '/' */
        while ((c = *src++) != '\0' && c != '/')
            *dst ++ = c;

        if (c == '/')
        {
            *dst ++ = '/';
            while (c == '/')
                c = *src++;

            src --;
        }
        else if (!c)
            break;

        continue;
460 461

up_one:
462 463 464
        dst --;
        if (dst < dst0)
        {
465
            rt_free(fullpath);
B
bernard 已提交
466
            return NULL;
467 468 469 470 471 472 473 474 475 476 477 478
        }
        while (dst0 < dst && dst[-1] != '/')
            dst --;
    }

    *dst = '\0';

    /* remove '/' in the end of path if exist */
    dst --;
    if ((dst != fullpath) && (*dst == '/'))
        *dst = '\0';

479 480 481 482 483 484 485
    /* final check fullpath is not empty, for the special path of lwext "/.." */
    if ('\0' == fullpath[0])
    {
        fullpath[0] = '/';
        fullpath[1] = '\0';
    }

486
    return fullpath;
487
}
488
RTM_EXPORT(dfs_normalize_path);
489 490 491 492 493 494 495 496

/**
 * This function will get the file descriptor table of current process.
 */
struct dfs_fdtable* dfs_fdtable_get(void)
{
    struct dfs_fdtable *fdt;
#ifdef RT_USING_LWP
497
    struct rt_lwp *lwp;
498

H
heyuanjie 已提交
499
    lwp = (struct rt_lwp *)rt_thread_self()->lwp;
500
    if (lwp)
501 502 503
        fdt = &lwp->fdt;
    else
        fdt = &_fdtab;
504 505 506 507 508 509 510
#else
    fdt = &_fdtab;
#endif

    return fdt;
}

511
#ifdef RT_USING_FINSH
B
bernard 已提交
512 513 514 515
#include <finsh.h>
int list_fd(void)
{
    int index;
516 517 518 519
    struct dfs_fdtable *fd_table;
    
    fd_table = dfs_fdtable_get();
    if (!fd_table) return -1;
B
bernard 已提交
520 521

    rt_enter_critical();
522 523 524
    
    rt_kprintf("fd type    ref magic  path\n");
    rt_kprintf("-- ------  --- ----- ------\n");
525
    for (index = 0; index < fd_table->maxfd; index ++)
B
bernard 已提交
526
    {
527
        struct dfs_fd *fd = fd_table->fds[index];
B
bernard 已提交
528

529
        if (fd && fd->fops)
B
bernard 已提交
530
        {
531 532 533 534 535 536 537 538
            rt_kprintf("%2d ", index);
            if (fd->type == FT_DIRECTORY)    rt_kprintf("%-7.7s ", "dir");
            else if (fd->type == FT_REGULAR) rt_kprintf("%-7.7s ", "file");
            else if (fd->type == FT_SOCKET)  rt_kprintf("%-7.7s ", "socket");
            else if (fd->type == FT_USER)    rt_kprintf("%-7.7s ", "user");
            else rt_kprintf("%-8.8s ", "unknown");
            rt_kprintf("%3d ", fd->ref_count);
            rt_kprintf("%04x  ", fd->magic);
B
bernard 已提交
539 540
            if (fd->path)
            {
541 542 543 544 545
                rt_kprintf("%s\n", fd->path);
            }
            else
            {
                rt_kprintf("\n");
B
bernard 已提交
546 547 548 549 550 551 552 553
            }
        }
    }
    rt_exit_critical();

    return 0;
}
MSH_CMD_EXPORT(list_fd, list file descriptor);
554
#endif
555 556
/*@}*/