dfs_file.c 53.3 KB
Newer Older
1
/*
B
Bernard Xiong 已提交
2
 * Copyright (c) 2006-2023, RT-Thread Development Team
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5
 *
6 7
 * Change Logs:
 * Date           Author       Notes
B
Bernard Xiong 已提交
8
 * 2023-05-05     Bernard      Implement file APIs in dfs v2.0
9
 */
10

B
Bernard Xiong 已提交
11 12 13
#include "errno.h"
#include "fcntl.h"

14
#include <dfs.h>
G
guo 已提交
15

B
Bernard Xiong 已提交
16 17 18 19 20 21 22 23 24
#include "dfs_file.h"
#include "dfs_dentry.h"
#include "dfs_fs.h"
#include "dfs_mnt.h"
#include "dfs_private.h"

#define DBG_TAG    "DFS.file"
#define DBG_LVL    DBG_WARNING
#include <rtdbg.h>
G
guo 已提交
25 26


B
Bernard Xiong 已提交
27
#define MAX_RW_COUNT 0xfffc0000
G
guo 已提交
28

B
Bernard Xiong 已提交
29 30 31 32 33 34
/*
 * rw_verify_area doesn't like huge counts. We limit
 * them to something that fits in "int" so that others
 * won't have to do range checks all the time.
 */
ssize_t rw_verify_area(struct dfs_file *file, off_t *ppos, size_t count)
G
guo 已提交
35
{
B
Bernard Xiong 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48
    off_t pos;
    ssize_t retval = -EINVAL;

    if ((size_t)count < 0)
        return retval;
    pos = *ppos;
    if (pos < 0)
    {
        if (count >= -pos) /* both values are in 0..LLONG_MAX */
            return -EOVERFLOW;
    }

    return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
G
guo 已提交
49 50
}

B
Bernard Xiong 已提交
51
void dfs_file_set_fpos(struct dfs_file *file, off_t fpos)
G
guo 已提交
52
{
B
Bernard Xiong 已提交
53 54 55 56 57 58
    if (file)
    {
        rt_mutex_take(&file->pos_lock, RT_WAITING_FOREVER);
        file->fpos = fpos;
        rt_mutex_release(&file->pos_lock);
    }
G
guo 已提交
59 60
}

B
Bernard Xiong 已提交
61
void dfs_file_init(struct dfs_file *file)
G
guo 已提交
62
{
B
Bernard Xiong 已提交
63
    if (file)
G
guo 已提交
64
    {
B
Bernard Xiong 已提交
65 66 67 68
        rt_memset(file, 0x00, sizeof(struct dfs_file));
        file->magic = DFS_FD_MAGIC;
        rt_mutex_init(&file->pos_lock, "fpos", RT_IPC_FLAG_PRIO);
        rt_atomic_store(&(file->ref_count), 1);
G
guo 已提交
69 70 71
    }
}

B
Bernard Xiong 已提交
72
void dfs_file_deinit(struct dfs_file *file)
G
guo 已提交
73
{
B
Bernard Xiong 已提交
74
    if (file)
G
guo 已提交
75
    {
B
Bernard Xiong 已提交
76
        rt_mutex_detach(&file->pos_lock);
G
guo 已提交
77 78 79
    }
}

B
Bernard Xiong 已提交
80
static void dfs_file_unref(struct dfs_file *file)
G
guo 已提交
81
{
B
Bernard Xiong 已提交
82
    rt_err_t ret = RT_EOK;
G
guo 已提交
83

B
Bernard Xiong 已提交
84 85
    ret = dfs_file_lock();
    if (ret == RT_EOK)
G
guo 已提交
86
    {
B
Bernard Xiong 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        if (rt_atomic_load(&(file->ref_count)) == 1)
        {
            /* should release this file */
            if (file->dentry)
            {
                DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_unref(dentry(%s))", file->dentry->pathname);
                dfs_dentry_unref(file->dentry);
                file->dentry = RT_NULL;
            }
            else if (file->vnode)
            {
                if (file->vnode->ref_count > 1)
                {
                    file->vnode->ref_count--;
                }
                else if (file->vnode->ref_count == 1)
                {
                    rt_free(file->vnode);
                    file->vnode = RT_NULL;
                }
            }

            LOG_I("release a file: %p", file);
        }

        dfs_file_unlock();
G
guo 已提交
113
    }
B
Bernard Xiong 已提交
114 115 116 117 118
}

struct dfs_dentry* dfs_file_follow_link(struct dfs_dentry *dentry)
{
    int ret = 0;
G
guo 已提交
119

B
Bernard Xiong 已提交
120
    if (dentry && dentry->vnode && dentry->vnode->type == FT_SYMLINK)
G
guo 已提交
121
    {
B
Bernard Xiong 已提交
122 123 124 125
        char *buf = NULL;

        buf = (char *) rt_malloc (DFS_PATH_MAX);
        if (buf)
G
guo 已提交
126
        {
B
Bernard Xiong 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
            do
            {
                ret = dentry->mnt->fs_ops->readlink(dentry, buf, DFS_PATH_MAX);
                if (ret > 0)
                {
                    struct dfs_mnt *mnt = NULL;

                    if (buf[0] != '/')
                    {
                        char *dir = dfs_dentry_pathname(dentry);

                        /* is the relative directory */
                        if (dir)
                        {
                            char *fullpath = dfs_normalize_path(dir, buf);
                            if (fullpath)
                            {
                                strncpy(buf, fullpath, DFS_PATH_MAX);

                                rt_free(fullpath);
                            }
                            rt_free(dir);
                        }
                    }

                    mnt = dfs_mnt_lookup(buf);
                    if (mnt)
                    {
                        struct dfs_dentry *de = dfs_dentry_lookup(mnt, buf, 0);

                        /* release the old dentry */
                        dfs_dentry_unref(dentry);
                        dentry = de;
                    }
                }
                else
                {
                    break;
                }
            } while (dentry && dentry->vnode->type == FT_SYMLINK);
G
guo 已提交
167
        }
B
Bernard Xiong 已提交
168 169

        rt_free(buf);
G
guo 已提交
170
    }
171

B
Bernard Xiong 已提交
172 173
    return dentry;
}
174

B
Bernard Xiong 已提交
175 176
/*
 * this function is creat a nolink path.
G
guo 已提交
177
 *
B
Bernard Xiong 已提交
178 179 180
 * @param mnt
 * @param fullpath
 * @param mode 0 middle path nolink; 1 all path nolink.
G
guo 已提交
181
 *
B
Bernard Xiong 已提交
182
 * @return new path.
G
guo 已提交
183
 */
B
Bernard Xiong 已提交
184
static char *dfs_nolink_path(struct dfs_mnt **mnt, char *fullpath, int mode)
G
guo 已提交
185
{
B
Bernard Xiong 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    int index = 0;
    char *path = RT_NULL;
    char link_fn[DFS_PATH_MAX] = {0};
    struct dfs_dentry *dentry = RT_NULL;

    path = (char *)rt_malloc(DFS_PATH_MAX);
    if (!path)
    {
        return path;
    }

    if (*mnt && fullpath)
    {
        int i = 0;
        char *fp = fullpath;

        while (*fp != '\0')
        {
            fp++;
            i++;
            if (*fp == '/')
            {
                rt_memcpy(path + index, fp - i, i);
                path[index + i] = '\0';

                dentry = dfs_dentry_lookup(*mnt, path, 0);
                if (dentry && dentry->vnode->type == FT_SYMLINK)
                {
                    int ret = -1;
G
guo 已提交
215

B
Bernard Xiong 已提交
216 217 218 219
                    if ((*mnt)->fs_ops->readlink)
                    {
                        ret = (*mnt)->fs_ops->readlink(dentry, link_fn, DFS_PATH_MAX);
                    }
G
guo 已提交
220

B
Bernard Xiong 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
                    if (ret > 0)
                    {
                        int len = rt_strlen(link_fn);
                        if (link_fn[0] == '/')
                        {
                            rt_memcpy(path, link_fn, len);
                            index = len;
                        }
                        else
                        {
                            path[index] = '/';
                            index++;
                            rt_memcpy(path + index, link_fn, len);
                            index += len;
                        }
                        path[index] = '\0';
                        *mnt = dfs_mnt_lookup(path);
                    }
                    else
                    {
                        rt_kprintf("link error: %s\n", path);
                    }
                }
                else
                {
                    index += i;
                }
                dfs_dentry_unref(dentry);
                i = 0;
            }
        }

        if (i)
        {
            rt_memcpy(path + index, fp - i, i);
            path[index + i] = '\0';

            if (mode)
            {
                dentry = dfs_dentry_lookup(*mnt, path, 0);
                if (dentry && dentry->vnode->type == FT_SYMLINK)
                {
                    int ret = -1;

                    if ((*mnt)->fs_ops->readlink)
                    {
                        ret = (*mnt)->fs_ops->readlink(dentry, link_fn, DFS_PATH_MAX);
                    }

                    if (ret > 0)
                    {
                        int len = rt_strlen(link_fn);
                        if (link_fn[0] == '/')
                        {
                            rt_memcpy(path, link_fn, len);
                            index = len;
                        }
                        else
                        {
                            path[index] = '/';
                            index++;
                            rt_memcpy(path + index, link_fn, len);
                            index += len;
                        }
                        path[index] = '\0';
                        *mnt = dfs_mnt_lookup(path);
                    }
                    else
                    {
                        rt_kprintf("link error: %s\n", path);
                    }
                }
                dfs_dentry_unref(dentry);
            }
        }
    }
    else
G
guo 已提交
298
    {
B
Bernard Xiong 已提交
299 300
        rt_free(path);
        path = RT_NULL;
G
guo 已提交
301 302
    }

B
Bernard Xiong 已提交
303
    //rt_kprintf("%s: %s => %s\n", __FUNCTION__, fullpath, path);
G
guo 已提交
304

B
Bernard Xiong 已提交
305 306
    return path;
}
G
guo 已提交
307

308
/**
B
Bernard Xiong 已提交
309
 * this function will open a file which specified by path with specified oflags.
310 311
 *
 * @param fd the file descriptor pointer to return the corresponding result.
312
 * @param path the specified file path.
B
Bernard Xiong 已提交
313
 * @param oflags the oflags for open operator.
314 315 316
 *
 * @return 0 on successful, -1 on failed.
 */
B
Bernard Xiong 已提交
317
int dfs_file_open(struct dfs_file *file, const char *path, int oflags, mode_t mode)
318
{
B
Bernard Xiong 已提交
319 320 321 322
    int ret = -RT_ERROR;;
    char *fullpath = RT_NULL;
    struct dfs_dentry *dentry = RT_NULL;
    int fflags = dfs_fflags(oflags);
323

B
Bernard Xiong 已提交
324
    if (mode == 0)
325
    {
B
Bernard Xiong 已提交
326
        mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); /* 0666 */
327 328
    }

B
Bernard Xiong 已提交
329
    if (file && path)
G
guo 已提交
330
    {
B
Bernard Xiong 已提交
331 332
        fullpath = dfs_normalize_path(NULL, path);
        if (fullpath)
G
guo 已提交
333
        {
B
Bernard Xiong 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 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 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
            struct dfs_mnt *mnt = RT_NULL;

            DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
            mnt = dfs_mnt_lookup(fullpath);
            if (mnt)
            {
                char *tmp = dfs_nolink_path(&mnt, fullpath, 0);
                if (tmp)
                {
                    rt_free(fullpath);
                    fullpath = tmp;
                }

                DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_lookup(mnt, %s)", fullpath);
                dentry = dfs_dentry_lookup(mnt, fullpath, oflags);
                if (dentry && dentry->vnode->type == FT_SYMLINK)
                {
                    /* it's a symbol link but not follow */
                    if (oflags & O_NOFOLLOW)
                    {
                        /* no follow symbol link */
                        dfs_dentry_unref(dentry);
                        dentry = RT_NULL;
                    }
                    else
                    {
                        struct dfs_dentry *target_dentry = RT_NULL;

                        /* follow symbol link */
                        target_dentry = dfs_file_follow_link(dentry);
                        if (target_dentry)
                        {
                            dentry = target_dentry;
                        }
                    }
                }

                if (dentry)
                {
                    if (oflags & O_DIRECTORY)
                    {
                        if (dentry->vnode->type != FT_DIRECTORY)
                        {
                            dfs_dentry_unref(dentry);
                            dentry = RT_NULL;
                        }
                    }
                    else if (dentry->vnode->type == FT_DIRECTORY)
                    {
                        if (fflags & (DFS_F_FWRITE))
                        {
                            dfs_dentry_unref(dentry);
                            dentry = RT_NULL;
                        }
                        else
                        {
                            oflags |= O_DIRECTORY;
                        }
                    }
                }

                if (oflags & O_CREAT)
                {
                    if (dentry)
                    {
                        oflags &= ~O_CREAT;

                        if (oflags & O_EXCL)
                        {
                            oflags &= ~O_EXCL;
                            /* the dentry already exists */
                            dfs_dentry_unref(dentry);
                            dentry = RT_NULL;
                        }
                    }
                    else
                    {
                        /* create file/directory */
                        if (mnt->fs_ops->create_vnode)
                        {
                            struct dfs_vnode *vnode = RT_NULL;

                            DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_create(%s)", fullpath);
                            dentry = dfs_dentry_create(mnt, fullpath);
                            if (dentry)
                            {
                                mode &= ~S_IFMT;
                                DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "fs_ops->create_vnode");
                                vnode = mnt->fs_ops->create_vnode(dentry, oflags & O_DIRECTORY ? FT_DIRECTORY:FT_REGULAR, mode);

                                if (vnode)
                                {
                                    /* set vnode */
                                    dentry->vnode = vnode;  /* the refcount of created vnode is 1. no need to reference */
                                    dfs_dentry_insert(dentry);
                                }
                                else
                                {
                                    DLOG(msg, mnt->fs_ops->name, "dfs_file", DLOG_MSG_RET, "create failed.");
                                    dfs_dentry_unref(dentry);
                                    dentry = RT_NULL;
                                }
                            }
                        }
                    }
                }

                if (dentry)
                {
                    rt_bool_t permission = RT_TRUE;
                    file->dentry = dentry;
                    file->vnode = dentry->vnode;
                    file->fops  = dentry->mnt->fs_ops->default_fops;
                    file->flags = oflags;

                    /* check permission */
                    if (!(oflags & O_CREAT))
                    {
                        if (fflags & DFS_F_FWRITE)
                        {
                            if (!(file->vnode->mode & S_IWUSR))
                            {
                                permission = RT_FALSE;
                            }
                        }

                        if (fflags & DFS_F_FREAD)
                        {
                            if (!(file->vnode->mode & S_IRUSR))
                            {
                                permission = RT_FALSE;
                            }
                        }

                        if (oflags & O_EXEC)
                        {
                            if (!(file->vnode->mode & S_IXUSR))
                            {
                                permission = RT_FALSE;
                            }
                        }
                    }

                    if (permission && file->fops->open)
                    {
                        DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "fops->open(file)");
                        ret = file->fops->open(file);
                        if (ret < 0)
                        {
                            LOG_E("open %s failed in file system: %s", path, dentry->mnt->fs_ops->name);
                            DLOG(msg, mnt->fs_ops->name, "dfs_file", DLOG_MSG_RET, "open failed.");
                            dfs_file_unref(file);
                        }
                        else
                        {
                            /* for char/block device */
                            if ((S_ISCHR(file->vnode->mode)) || (S_ISBLK(file->vnode->mode)))
                            {
                                file->fops = file->vnode->fops;
                            }
                        }
                    }
                    else
                    {
                        DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "no permission or fops->open");
                        dfs_file_unref(file);
                    }
                }
                else
                {
                    LOG_I("lookup file:%s failed in file system", path);
                }
            }

            rt_free(fullpath);
G
guo 已提交
509 510
        }

B
Bernard Xiong 已提交
511
        if (ret >= 0 && (oflags & O_TRUNC))
G
guo 已提交
512
        {
B
Bernard Xiong 已提交
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
            /* trunc file */
            if (!(fflags & DFS_F_FWRITE) || file->vnode->type == FT_DIRECTORY)
            {
                /* truncate on read a only file or a directory */
                DLOG(msg, "dfs_file", "dfs_file", DLOG_MSG, "dfs_file_unref(file), trunc on RDOnly or directory");
                ret = -RT_ERROR;
            }
            else
            {
                if (file->fops->truncate)
                {
                    DLOG(msg, "dfs_file", dentry->mnt->fs_ops->name, DLOG_MSG, "fops->truncate(file, 0)");
                    ret = file->fops->truncate(file, 0);
                }
            }

            if (ret < 0)
            {
                dfs_file_unref(file);
            }

            file->flags &= ~O_TRUNC;
G
guo 已提交
535
        }
B
Bernard Xiong 已提交
536
    }
537

B
Bernard Xiong 已提交
538 539
    return ret;
}
540

B
Bernard Xiong 已提交
541 542 543
int dfs_file_close(struct dfs_file *file)
{
    int ret = -RT_ERROR;
544

B
Bernard Xiong 已提交
545 546 547
    if (file)
    {
        if (dfs_file_lock() == RT_EOK)
G
guo 已提交
548
        {
B
Bernard Xiong 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
            rt_atomic_t ref_count = rt_atomic_load(&(file->ref_count));

            if (ref_count == 1 && file->fops && file->fops->close)
            {
                DLOG(msg, "dfs_file", file->dentry->mnt->fs_ops->name, DLOG_MSG, "fops->close(file)");
                ret = file->fops->close(file);

                if (ret == 0) /* close file sucessfully */
                {
                    DLOG(msg, "dfs_file", "dfs_file", DLOG_MSG, "dfs_file_unref(file)");
                    dfs_file_unref(file);
                }
                else
                {
                    LOG_W("close file:%s failed on low level file system", file->dentry->pathname);
                }
            }
G
guo 已提交
566
            else
B
Bernard Xiong 已提交
567 568 569 570 571 572
            {
                DLOG(msg, "dfs_file", "dfs_file", DLOG_MSG, "dfs_file_unref(file)");
                dfs_file_unref(file);
                ret = 0;
            }
            dfs_file_unlock();
G
guo 已提交
573
        }
B
Bernard Xiong 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586
    }

    return ret;
}

ssize_t dfs_file_read(struct dfs_file *file, void *buf, size_t len)
{
    ssize_t ret = -EBADF;

    if (file)
    {
        /* check whether read */
        if (!(dfs_fflags(file->flags) & DFS_F_FREAD))
G
guo 已提交
587
        {
B
Bernard Xiong 已提交
588
            ret = -EPERM;
G
guo 已提交
589
        }
B
Bernard Xiong 已提交
590
        else if (!file->fops || !file->fops->read)
G
guo 已提交
591
        {
B
Bernard Xiong 已提交
592
            ret = -ENOSYS;
G
guo 已提交
593
        }
B
Bernard Xiong 已提交
594 595 596 597
        else if (file->vnode && file->vnode->type != FT_DIRECTORY)
        {
            off_t pos;
            pos = file->fpos;
G
guo 已提交
598

B
Bernard Xiong 已提交
599 600 601 602
            ret = rw_verify_area(file, &pos, len);
            if (ret > 0)
            {
                len = ret;
G
guo 已提交
603

B
Bernard Xiong 已提交
604 605 606 607 608 609 610
                ret = file->fops->read(file, buf, len, &pos);
                if (ret > 0)
                {
                    dfs_file_set_fpos(file, pos);
                }
            }
        }
611 612
    }

B
Bernard Xiong 已提交
613 614 615 616 617 618
    return ret;
}

ssize_t dfs_file_write(struct dfs_file *file, const void *buf, size_t len)
{
    size_t ret = -EBADF;
G
guo 已提交
619

B
Bernard Xiong 已提交
620
    if (file)
621
    {
B
Bernard Xiong 已提交
622 623 624 625 626 627
        if (!(dfs_fflags(file->flags) & DFS_F_FWRITE))
        {
            LOG_W("bad write flags.");
            ret = -EBADF;
        }
        else if (!file->fops || !file->fops->write)
G
guo 已提交
628
        {
B
Bernard Xiong 已提交
629 630 631 632 633 634 635 636 637 638
            LOG_W("no fops write.");
            ret = -ENOSYS;
        }
        else if (file->vnode && file->vnode->type != FT_DIRECTORY)
        {
            off_t pos;

            pos = file->fpos;
            ret = rw_verify_area(file, &pos, len);
            if (ret > 0)
G
guo 已提交
639
            {
B
Bernard Xiong 已提交
640 641 642 643 644 645 646 647
                len = ret;
                DLOG(msg, "dfs_file", file->dentry->mnt->fs_ops->name, DLOG_MSG,
                    "dfs_file_write(fd, buf, %d)", len);
                ret = file->fops->write(file, buf, len, &pos);
                if (ret > 0)
                {
                    dfs_file_set_fpos(file, pos);
                }
G
guo 已提交
648 649
            }
        }
B
Bernard Xiong 已提交
650
    }
651

B
Bernard Xiong 已提交
652 653
    return ret;
}
654

B
Bernard Xiong 已提交
655 656 657 658 659 660 661 662 663 664 665 666
int generic_dfs_lseek(struct dfs_file *file, off_t offset, int whence)
{
    off_t foffset;

    if (whence == SEEK_SET)
        foffset = offset;
    else if (whence == SEEK_CUR)
        foffset = file->fpos + offset;
    else if (whence == SEEK_END)
        foffset = file->vnode->size + offset;
    else
        return -EINVAL;
667

B
Bernard Xiong 已提交
668
    dfs_file_set_fpos(file, foffset);
669

B
Bernard Xiong 已提交
670
    return foffset;
671 672
}

B
Bernard Xiong 已提交
673
off_t dfs_file_lseek(struct dfs_file *file, off_t offset, int wherece)
674
{
B
Bernard Xiong 已提交
675
    off_t retval;
676

B
Bernard Xiong 已提交
677 678
    if (!file)
        return -EBADF;
679

B
Bernard Xiong 已提交
680 681
    retval = -EINVAL;
    if (file->fops->lseek)
G
guo 已提交
682
    {
B
Bernard Xiong 已提交
683 684
        retval = file->fops->lseek(file, offset, wherece);
        if (retval >= 0)
G
guo 已提交
685
        {
B
Bernard Xiong 已提交
686
            dfs_file_set_fpos(file, retval);
G
guo 已提交
687
        }
B
Bernard Xiong 已提交
688
    }
689

B
Bernard Xiong 已提交
690 691
    return retval;
}
G
guo 已提交
692

B
Bernard Xiong 已提交
693 694 695 696 697 698 699 700 701 702 703 704 705
int dfs_file_stat(const char *path, struct stat *buf)
{
    int ret = -ENOENT;
    char *fullpath = RT_NULL;
    struct dfs_mnt *mnt = RT_NULL;
    struct dfs_dentry *dentry = RT_NULL;

    fullpath = dfs_normalize_path(NULL, path);
    if (fullpath)
    {
        DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
        mnt = dfs_mnt_lookup(fullpath);
        if (mnt)
G
guo 已提交
706
        {
B
Bernard Xiong 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
            char *tmp = dfs_nolink_path(&mnt, fullpath, 1);
            if (tmp)
            {
                rt_free(fullpath);
                fullpath = tmp;
            }

            DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dentry = dfs_dentry_lookup(mnt, %s)", fullpath);
            dentry = dfs_dentry_lookup(mnt, fullpath, 0);
            if (dentry)
            {
                DLOG(msg, "dentry", "dfs_file", DLOG_MSG_RET, "return dentry");
                if (mnt->fs_ops->stat)
                {
                    DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "fs_ops->stat(dentry, buf)");
                    ret = mnt->fs_ops->stat(dentry, buf);
                }

                /* unref dentry */
                DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_unref(dentry)");
                dfs_dentry_unref(dentry);
                dentry = RT_NULL;
            }
G
guo 已提交
730 731
        }

B
Bernard Xiong 已提交
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
        rt_free(fullpath);
        fullpath = RT_NULL;
    }
    else
    {
        ret = -ENOMEM;
    }

    return ret;
}

int dfs_file_lstat(const char *path, struct stat *buf)
{
    int ret = -ENOENT;
    char *fullpath = RT_NULL;
    struct dfs_mnt *mnt = RT_NULL;
    struct dfs_dentry *dentry = RT_NULL;

    fullpath = dfs_normalize_path(NULL, path);
    if (fullpath)
    {
        DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
        mnt = dfs_mnt_lookup(fullpath);
        if (mnt)
G
guo 已提交
756
        {
B
Bernard Xiong 已提交
757 758 759 760 761 762
            char *tmp = dfs_nolink_path(&mnt, fullpath, 0);
            if (tmp)
            {
                rt_free(fullpath);
                fullpath = tmp;
            }
G
guo 已提交
763

B
Bernard Xiong 已提交
764 765 766
            DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dentry = dfs_dentry_lookup(mnt, %s)", fullpath);
            dentry = dfs_dentry_lookup(mnt, fullpath, 0);
            if (dentry)
G
guo 已提交
767
            {
B
Bernard Xiong 已提交
768 769 770 771 772 773 774 775 776 777 778
                DLOG(msg, "dentry", "dfs_file", DLOG_MSG_RET, "return dentry");
                if (mnt->fs_ops->stat)
                {
                    DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "fs_ops->stat(dentry, buf)");
                    ret = mnt->fs_ops->stat(dentry, buf);
                }

                /* unref dentry */
                DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_unref(dentry)");
                dfs_dentry_unref(dentry);
                dentry = RT_NULL;
G
guo 已提交
779 780
            }
        }
B
Bernard Xiong 已提交
781 782 783 784 785 786 787

        rt_free(fullpath);
        fullpath = RT_NULL;
    }
    else
    {
        ret = -ENOMEM;
G
guo 已提交
788
    }
789

B
Bernard Xiong 已提交
790 791 792
    rt_set_errno(-ret);

    return ret;
793 794
}

B
Bernard Xiong 已提交
795
int dfs_file_fstat(struct dfs_file *file, struct stat *buf)
796
{
B
Bernard Xiong 已提交
797 798 799
    size_t ret = -EBADF;

    if (file)
G
guo 已提交
800
    {
B
Bernard Xiong 已提交
801 802 803 804 805 806 807 808 809 810 811 812
        if (file->fops && file->fops->ioctl)
        {
            // ret = file->fops->fstat(file, buf);
        }
        else
        {
            ret = -ENOSYS;
        }
    }
    else
    {
        ret = -EBADF;
G
guo 已提交
813
    }
814

B
Bernard Xiong 已提交
815 816 817 818 819 820 821 822 823 824 825 826
    return ret;
}

int dfs_file_setattr(const char *path, struct dfs_attr *attr)
{
    int ret = -RT_ERROR;
    char *fullpath = RT_NULL;
    struct dfs_mnt *mnt = RT_NULL;
    struct dfs_dentry *dentry = RT_NULL;

    fullpath = dfs_normalize_path(NULL, path);
    if (fullpath)
827
    {
B
Bernard Xiong 已提交
828 829 830
        DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
        mnt = dfs_mnt_lookup(fullpath);
        if (mnt)
831
        {
B
Bernard Xiong 已提交
832 833 834 835 836 837 838 839 840 841
            char *tmp = dfs_nolink_path(&mnt, fullpath, 0);
            if (tmp)
            {
                rt_free(fullpath);
                fullpath = tmp;
            }

            DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dentry = dfs_dentry_lookup(mnt, %s)", fullpath);
            dentry = dfs_dentry_lookup(mnt, fullpath, 0);
            if (dentry)
842
            {
B
Bernard Xiong 已提交
843 844 845 846 847 848
                DLOG(msg, "dentry", "dfs_file", DLOG_MSG_RET, "return dentry");
                if (mnt->fs_ops->setattr)
                {
                    DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "fs_ops->setattr(dentry, attr)");
                    ret = mnt->fs_ops->setattr(dentry, attr);
                }
849

B
Bernard Xiong 已提交
850 851 852 853
                /* unref dentry */
                DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_unref(dentry)");
                dfs_dentry_unref(dentry);
                dentry = RT_NULL;
854 855 856
            }
        }

B
Bernard Xiong 已提交
857 858
        rt_free(fullpath);
        fullpath = RT_NULL;
G
guo 已提交
859
    }
860

B
Bernard Xiong 已提交
861
    return ret;
862 863
}

B
Bernard Xiong 已提交
864
int dfs_file_ioctl(struct dfs_file *file, int cmd, void *args)
865
{
B
Bernard Xiong 已提交
866
    size_t ret = 0;
867

B
Bernard Xiong 已提交
868
    if (file)
G
guo 已提交
869
    {
B
Bernard Xiong 已提交
870 871 872 873 874 875 876 877
        if (file->fops && file->fops->ioctl)
        {
            ret = file->fops->ioctl(file, cmd, args);
        }
        else
        {
            ret = -ENOSYS;
        }
G
guo 已提交
878
    }
B
Bernard Xiong 已提交
879
    else
G
guo 已提交
880
    {
B
Bernard Xiong 已提交
881
        ret = -EBADF;
G
guo 已提交
882
    }
883

B
Bernard Xiong 已提交
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
    return ret;
}

int dfs_file_fcntl(int fd, int cmd, unsigned long arg)
{
    int ret = 0;
    struct dfs_file *file;

    file = fd_get(fd);
    if (file)
    {
        switch (cmd)
        {
        case F_DUPFD:
            ret = dfs_dup(fd, arg);
            break;
        case F_GETFD:
            ret = file->mode;
            break;
        case F_SETFD:
            file->mode = arg;
            break;
        case F_GETFL:
            ret = file->flags;
            break;
        case F_SETFL:
            file->flags = arg;
            break;
        case F_GETLK:
            break;
        case F_SETLK:
        case F_SETLKW:
            break;
        default:
            ret = -EPERM;
            break;
        }
    }
    else
G
guo 已提交
923
    {
B
Bernard Xiong 已提交
924
        ret = -EBADF;
G
guo 已提交
925
    }
926

B
Bernard Xiong 已提交
927
    return ret;
928 929
}

B
Bernard Xiong 已提交
930
int dfs_file_fsync(struct dfs_file *file)
931
{
B
Bernard Xiong 已提交
932 933 934
    int ret = -EBADF;

    if (file)
G
guo 已提交
935
    {
B
Bernard Xiong 已提交
936 937 938 939
        if (file->fops->flush)
        {
            ret = file->fops->flush(file);
        }
G
guo 已提交
940
    }
941

B
Bernard Xiong 已提交
942 943 944 945 946 947 948 949 950 951 952 953
    return ret;
}

int dfs_file_unlink(const char *path)
{
    int ret = -RT_ERROR;
    char *fullpath = RT_NULL;
    struct dfs_mnt *mnt = RT_NULL;
    struct dfs_dentry *dentry = RT_NULL;

    fullpath = dfs_normalize_path(NULL, path);
    if (fullpath)
G
guo 已提交
954
    {
B
Bernard Xiong 已提交
955 956 957 958 959 960 961 962 963 964
        DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
        mnt = dfs_mnt_lookup(fullpath);
        if (mnt)
        {
            char *tmp = dfs_nolink_path(&mnt, fullpath, 0);
            if (tmp)
            {
                rt_free(fullpath);
                fullpath = tmp;
            }
G
guo 已提交
965

B
Bernard Xiong 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
            if (strcmp(mnt->fullpath, fullpath) != 0)
            {
                DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_lookup(mnt, %s)", fullpath);
                dentry = dfs_dentry_lookup(mnt, fullpath, 0);
                if (dentry)
                {
                    rt_bool_t has_child = RT_FALSE;

                    has_child = dfs_mnt_has_child_mnt(mnt, fullpath);
                    if (has_child == RT_FALSE && rt_atomic_load(&(dentry->ref_count)) == 1)
                    {
                        /* no child mnt point, unlink it */
                        ret = -RT_ERROR;

                        if (mnt->fs_ops->unlink)
                        {
                            ret = mnt->fs_ops->unlink(dentry);
                        }
                    }
                    else
                    {
                        ret = -EBUSY;
                    }

                    /* release this dentry */
                    dfs_dentry_unref(dentry);
                }
                else
                {
                    /* no this entry */
                    ret = -ENOENT;
                }
            }
            else
            {
                /* it's a mount point, failed for busy */
                ret = -EBUSY;
            }
        }
        else
        {
            ret = -ENOENT;
        }

        /* release fullpath */
        rt_free(fullpath);
    }
    else
G
guo 已提交
1014
    {
B
Bernard Xiong 已提交
1015
        ret = -ENOMEM;
G
guo 已提交
1016
    }
1017

B
Bernard Xiong 已提交
1018
    return ret;
1019 1020
}

B
Bernard Xiong 已提交
1021
int dfs_file_link(const char *oldname, const char *newname)
1022
{
B
Bernard Xiong 已提交
1023 1024 1025 1026
    int ret = -1;
    struct stat stat;
    struct dfs_mnt *mnt = RT_NULL;
    char *old_fullpath, *new_fullpath;
1027

B
Bernard Xiong 已提交
1028
    if (dfs_file_isdir(oldname) == 0)
1029
    {
B
Bernard Xiong 已提交
1030
        return ret;
1031 1032
    }

B
Bernard Xiong 已提交
1033
    if (dfs_file_lstat(newname, &stat) >= 0)
1034
    {
B
Bernard Xiong 已提交
1035
        return ret;
1036 1037
    }

B
Bernard Xiong 已提交
1038 1039
    old_fullpath = dfs_normalize_path(NULL, oldname);
    if (old_fullpath)
1040
    {
B
Bernard Xiong 已提交
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
        DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", old_fullpath);
        mnt = dfs_mnt_lookup(old_fullpath);
        if (mnt == RT_NULL)
        {
            rt_free(old_fullpath);
            return -1;
        }

        char *tmp = dfs_nolink_path(&mnt, old_fullpath, 0);
        if (tmp)
        {
            rt_free(old_fullpath);
            old_fullpath = tmp;
        }
1055 1056
    }

B
Bernard Xiong 已提交
1057 1058
    new_fullpath = dfs_normalize_path(NULL, newname);
    if (new_fullpath)
1059
    {
B
Bernard Xiong 已提交
1060 1061
        char *tmp = dfs_nolink_path(&mnt, new_fullpath, 0);
        if (tmp)
1062
        {
B
Bernard Xiong 已提交
1063 1064
            rt_free(new_fullpath);
            new_fullpath = tmp;
1065 1066
        }
    }
1067

B
Bernard Xiong 已提交
1068 1069 1070
    if (old_fullpath && new_fullpath)
    {
        struct dfs_dentry *old_dentry, *new_dentry;
1071

B
Bernard Xiong 已提交
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
        DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_lookup(mnt, %s)", old_fullpath);
        old_dentry = dfs_dentry_lookup(mnt, old_fullpath, 0);
        DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_create(%s)", new_fullpath);
        new_dentry = dfs_dentry_create(mnt, new_fullpath);

        if (old_dentry && new_dentry)
        {
            if (mnt->fs_ops->link)
            {
                ret = mnt->fs_ops->link(old_dentry, new_dentry);
            }
        }

        dfs_dentry_unref(old_dentry);
        dfs_dentry_unref(new_dentry);
    }

    if (old_fullpath)
G
guo 已提交
1090
    {
B
Bernard Xiong 已提交
1091
        rt_free(old_fullpath);
G
guo 已提交
1092
    }
1093

B
Bernard Xiong 已提交
1094
    if (new_fullpath)
G
guo 已提交
1095
    {
B
Bernard Xiong 已提交
1096
        rt_free(new_fullpath);
G
guo 已提交
1097
    }
1098

B
Bernard Xiong 已提交
1099
    return ret;
1100 1101
}

B
Bernard Xiong 已提交
1102 1103
/* symlink creates a symbolic link named `linkpath` which contains the string `target`. */
int dfs_file_symlink(const char *target, const char *linkpath)
1104
{
B
Bernard Xiong 已提交
1105 1106 1107 1108
    int ret = -RT_ERROR;
    char *fullpath = RT_NULL, *parent = RT_NULL;
    struct dfs_mnt *mnt = RT_NULL;
    struct dfs_dentry *dentry = RT_NULL;
1109

B
Bernard Xiong 已提交
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
    if (target && linkpath)
    {
        if (linkpath[0] != '/')
        {
           fullpath = dfs_normalize_path(NULL, linkpath);
        }
        else
        {
            fullpath = (char*)linkpath;
        }
1120

B
Bernard Xiong 已提交
1121 1122 1123 1124
        /* linkpath should be not exist */
        if (dfs_file_access(fullpath, O_RDONLY) != 0)
        {
            char *index;
1125

B
Bernard Xiong 已提交
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
            /* get parent path */
            index = strrchr(fullpath, '/');
            if (index)
            {
                int length = index - fullpath;
                if (length > 0)
                {
                    parent = (char*) rt_malloc (length + 1);
                    if (parent)
                    {
                        memcpy(parent, fullpath, length);
                        parent[length] = '\0';
                    }
                }
                else
                {
                    parent = (char*) rt_malloc (1 + 1);
                    if (parent)
                    {
                        parent[0] = '/';
                        parent[1] = '\0';
                    }
                }
            }
1150

B
Bernard Xiong 已提交
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
            if (parent)
            {
                DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
                mnt = dfs_mnt_lookup(parent);
                if (mnt)
                {
                    char *tmp = dfs_nolink_path(&mnt, parent, 0);
                    if (tmp)
                    {
                        rt_free(parent);
                        parent = tmp;
                    }
1163

B
Bernard Xiong 已提交
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
                    DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_lookup(mnt, %s)", fullpath);
                    dentry = dfs_dentry_lookup(mnt, parent, 0);
                    if (dentry)
                    {
                        if (dentry->mnt->fs_ops->symlink)
                        {
                            char *path = dfs_normalize_path(NULL, target);
                            if (path)
                            {
                                char *tmp = dfs_nolink_path(&mnt, path, 0);
                                if (tmp)
                                {
                                    rt_free(path);
                                    path = tmp;
                                }
                                else
                                {
                                    tmp = path;
                                }

                                if (dfs_file_access(path, O_RDONLY) == 0)
                                {
                                    ret = rt_strncmp(parent, path, strlen(parent));
                                    if (ret == 0)
                                    {
                                        tmp = path + strlen(parent);
                                        if (*tmp == '/')
                                        {
                                            tmp ++;
                                        }
                                    }

                                    ret = mnt->fs_ops->symlink(dentry, tmp, index + 1);
                                }
                                else
                                {
                                    ret = -ENOENT;
                                }

                                rt_free(path);
                            }
                        }
                        else
                        {
                            ret = -ENOSYS;
                        }

                        dfs_dentry_unref(dentry);
                    }
                    else
                    {
                        ret = -ENOENT;
                    }
                }
                else
                {
                    ret = -ENOENT;
                }
B
bernard 已提交
1222

B
Bernard Xiong 已提交
1223 1224 1225
                rt_free(parent);
            }
        }
1226

B
Bernard Xiong 已提交
1227 1228 1229 1230 1231 1232 1233
        if (fullpath != linkpath)
            rt_free(fullpath);
    }
    else
    {
        ret = -EINVAL;
    }
1234

B
Bernard Xiong 已提交
1235
    return ret;
1236 1237
}

B
Bernard Xiong 已提交
1238
int dfs_file_readlink(const char *path, char *buf, int bufsize)
1239
{
B
Bernard Xiong 已提交
1240 1241 1242 1243
    int ret = -RT_ERROR;
    char *fullpath = RT_NULL;
    struct dfs_mnt *mnt = RT_NULL;
    struct dfs_dentry *dentry = RT_NULL;
1244

B
bernard 已提交
1245
    fullpath = dfs_normalize_path(NULL, path);
B
Bernard Xiong 已提交
1246
    if (fullpath)
1247
    {
B
Bernard Xiong 已提交
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
        DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
        mnt = dfs_mnt_lookup(fullpath);
        if (mnt)
        {
            char *tmp = dfs_nolink_path(&mnt, fullpath, 0);
            if (tmp)
            {
                rt_free(fullpath);
                fullpath = tmp;
            }
1258

B
Bernard Xiong 已提交
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
            DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_lookup(mnt, %s)", fullpath);
            dentry = dfs_dentry_lookup(mnt, fullpath, 0);
            if (dentry)
            {
                if (mnt->fs_ops->readlink)
                {
                    ret = mnt->fs_ops->readlink(dentry, buf, bufsize);
                }
                else
                {
                    ret = -ENOSYS;
                }
1271

B
Bernard Xiong 已提交
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
                /* release this dentry */
                dfs_dentry_unref(dentry);
            }
            else
            {
                /* no this entry */
                ret = -ENOENT;
            }
        }
        else
        {
            ret = -ENOENT;
        }
1285

B
Bernard Xiong 已提交
1286 1287 1288 1289
        /* release fullpath */
        rt_free(fullpath);
    }
    else
1290
    {
B
Bernard Xiong 已提交
1291 1292
        ret = -ENOMEM;
    }
1293

B
Bernard Xiong 已提交
1294 1295
    return ret;
}
1296

B
Bernard Xiong 已提交
1297 1298 1299 1300 1301
int dfs_file_rename(const char *old_file, const char *new_file)
{
    int ret = -1;
    struct dfs_mnt *mnt = RT_NULL;
    char *old_fullpath, *new_fullpath;
1302

B
Bernard Xiong 已提交
1303 1304
    old_fullpath = dfs_normalize_path(NULL, old_file);
    if (old_fullpath)
1305
    {
B
Bernard Xiong 已提交
1306 1307 1308
        DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", old_fullpath);
        mnt = dfs_mnt_lookup(old_fullpath);
        if (mnt == RT_NULL)
1309
        {
B
Bernard Xiong 已提交
1310 1311 1312
            rt_free(old_fullpath);
            return -1;
        }
1313

B
Bernard Xiong 已提交
1314 1315 1316 1317 1318
        char *tmp = dfs_nolink_path(&mnt, old_fullpath, 0);
        if (tmp)
        {
            rt_free(old_fullpath);
            old_fullpath = tmp;
1319
        }
B
Bernard Xiong 已提交
1320
    }
1321

B
Bernard Xiong 已提交
1322 1323 1324 1325 1326 1327 1328 1329 1330
    new_fullpath = dfs_normalize_path(NULL, new_file);
    if (new_fullpath)
    {
        char *tmp = dfs_nolink_path(&mnt, new_fullpath, 0);
        if (tmp)
        {
            rt_free(new_fullpath);
            new_fullpath = tmp;
        }
1331 1332
    }

B
Bernard Xiong 已提交
1333 1334 1335
    if (old_fullpath && new_fullpath)
    {
        struct dfs_dentry *old_dentry, *new_dentry;
1336

B
Bernard Xiong 已提交
1337 1338 1339 1340
        DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_lookup(mnt, %s)", old_fullpath);
        old_dentry = dfs_dentry_lookup(mnt, old_fullpath, 0);
        DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_create(%s)", new_fullpath);
        new_dentry = dfs_dentry_create(mnt, new_fullpath);
1341

B
Bernard Xiong 已提交
1342 1343 1344 1345 1346 1347 1348
        if (old_dentry && new_dentry)
        {
            if (mnt->fs_ops->rename)
            {
                ret = mnt->fs_ops->rename(old_dentry, new_dentry);
            }
        }
1349

B
Bernard Xiong 已提交
1350 1351 1352
        dfs_dentry_unref(old_dentry);
        dfs_dentry_unref(new_dentry);
    }
1353

B
Bernard Xiong 已提交
1354
    if (old_fullpath)
1355
    {
B
Bernard Xiong 已提交
1356
        rt_free(old_fullpath);
1357 1358
    }

B
Bernard Xiong 已提交
1359
    if (new_fullpath)
G
guo 已提交
1360
    {
B
Bernard Xiong 已提交
1361
        rt_free(new_fullpath);
G
guo 已提交
1362 1363
    }

B
Bernard Xiong 已提交
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
    return ret;
}

int dfs_file_ftruncate(struct dfs_file *file, off_t length)
{
    int ret = 0;

    if (file)
    {
        if (file->fops->truncate)
        {
            ret = file->fops->truncate(file, length);
        }
        else
        {
            ret = -ENOSYS;
        }
    }
    else
1383
    {
B
Bernard Xiong 已提交
1384
        ret = -EBADF;
1385 1386
    }

B
Bernard Xiong 已提交
1387 1388 1389 1390 1391 1392
    return ret;
}

int dfs_file_flush(struct dfs_file *file)
{
    int ret = 0;
1393

B
Bernard Xiong 已提交
1394
    if (file)
1395
    {
B
Bernard Xiong 已提交
1396
        if (file->fops->flush)
1397
        {
B
Bernard Xiong 已提交
1398
            ret = file->fops->flush(file);
1399 1400 1401
        }
        else
        {
B
Bernard Xiong 已提交
1402
            ret = -ENOSYS;
1403 1404 1405 1406
        }
    }
    else
    {
B
Bernard Xiong 已提交
1407
        ret = -EBADF;
1408
    }
1409

B
Bernard Xiong 已提交
1410 1411 1412 1413 1414 1415 1416 1417
    return ret;
}

int dfs_file_getdents(struct dfs_file *file, struct dirent *dirp, size_t nbytes)
{
    int ret = -RT_ERROR;

    if (file)
G
guo 已提交
1418
    {
B
Bernard Xiong 已提交
1419 1420 1421 1422 1423 1424 1425 1426
        if (file->vnode && S_ISDIR(file->vnode->mode))
        {
            if (file->fops && file->fops->getdents)
            {
                DLOG(msg, "dfs_file", file->dentry->mnt->fs_ops->name, DLOG_MSG, "fops->getdents()");
                ret = file->fops->getdents(file, dirp, nbytes);
            }
        }
G
guo 已提交
1427
    }
B
Bernard Xiong 已提交
1428
    else
G
guo 已提交
1429
    {
B
Bernard Xiong 已提交
1430
        ret = -EBADF;
G
guo 已提交
1431
    }
1432

B
Bernard Xiong 已提交
1433
    return ret;
1434 1435
}

1436
/**
B
Bernard Xiong 已提交
1437
 * this function will check the path is it a directory.
1438
 *
B
Bernard Xiong 已提交
1439
 * @param path the file path.
1440
 *
B
Bernard Xiong 已提交
1441
 * @return 0 on is dir, -1 on not dir.
1442
 */
B
Bernard Xiong 已提交
1443
int dfs_file_isdir(const char *path)
1444
{
B
Bernard Xiong 已提交
1445 1446 1447 1448
    int ret = -RT_ERROR;
    char *fullpath = RT_NULL;
    struct dfs_mnt *mnt = RT_NULL;
    struct dfs_dentry *dentry = RT_NULL;
1449

B
Bernard Xiong 已提交
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
    fullpath = dfs_normalize_path(NULL, path);
    if (fullpath)
    {
        DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
        mnt = dfs_mnt_lookup(fullpath);
        if (mnt)
        {
            char *tmp = dfs_nolink_path(&mnt, fullpath, 1);
            if (tmp)
            {
                rt_free(fullpath);
                fullpath = tmp;
            }

            DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dentry = dfs_dentry_lookup(mnt, %s)", fullpath);
            dentry = dfs_dentry_lookup(mnt, fullpath, 0);
            if (dentry)
            {
                DLOG(msg, "dentry", "dfs_file", DLOG_MSG_RET, "return dentry");
                if (mnt->fs_ops->stat)
                {
                    struct stat stat = {0};
                    DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "fs_ops->stat(dentry, buf)");
                    ret = mnt->fs_ops->stat(dentry, &stat);
                    if (ret == RT_EOK && S_ISDIR(stat.st_mode))
                    {
                        ret = RT_EOK;
                    }
                    else
                    {
                        ret = -RT_ERROR;
                    }
                }

                /* unref dentry */
                DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_unref(dentry)");
                dfs_dentry_unref(dentry);
                dentry = RT_NULL;
            }
        }

        rt_free(fullpath);
        fullpath = RT_NULL;
    }

    return ret;
}

int dfs_file_access(const char *path, mode_t mode)
{
    int ret;
    struct dfs_file file;
1502

B
Bernard Xiong 已提交
1503
    dfs_file_init(&file);
1504

B
Bernard Xiong 已提交
1505 1506 1507 1508 1509 1510 1511 1512 1513
    if (dfs_file_open(&file, path, O_RDONLY, mode) >= 0)
    {
        ret = 0;
        dfs_file_close(&file);
    }
    else
    {
        ret = -1;
    }
1514

B
Bernard Xiong 已提交
1515
    dfs_file_deinit(&file);
1516

B
Bernard Xiong 已提交
1517
    return ret;
1518 1519
}

B
Bernard Xiong 已提交
1520
int dfs_file_mmap2(struct dfs_file *file, struct dfs_mmap2_args *mmap2)
G
guo 已提交
1521 1522 1523
{
    int ret = 0;

B
Bernard Xiong 已提交
1524
    if (file && mmap2)
G
guo 已提交
1525
    {
B
Bernard Xiong 已提交
1526
        if (file->vnode->type != FT_DEVICE || !file->vnode->fops->ioctl)
G
guo 已提交
1527 1528 1529
        {
            rt_set_errno(EINVAL);
        }
B
Bernard Xiong 已提交
1530
        else if (file->vnode->type == FT_DEVICE && file->vnode->fops->ioctl)
G
guo 已提交
1531
        {
B
Bernard Xiong 已提交
1532
            ret = file->vnode->fops->ioctl(file, RT_FIOMMAP2, mmap2);
G
guo 已提交
1533 1534
            if (ret != 0)
            {
B
Bernard Xiong 已提交
1535
                ret = ret > 0 ? ret : -ret;
G
guo 已提交
1536 1537 1538 1539 1540 1541 1542 1543
                rt_set_errno(ret);
            }
        }
    }

    return ret;
}

1544
#ifdef RT_USING_FINSH
B
Bernard Xiong 已提交
1545 1546 1547 1548 1549 1550 1551

#define _COLOR_RED      "\033[31m"
#define _COLOR_GREEN    "\033[32m"
#define _COLOR_BLUE     "\033[34m"
#define _COLOR_CYAN     "\033[36m"
#define _COLOR_WHITE    "\033[37m"
#define _COLOR_NORMAL   "\033[0m"
1552

1553
void ls(const char *pathname)
1554
{
G
guo 已提交
1555
    struct dirent dirent;
1556 1557 1558
    struct stat stat;
    int length;
    char *fullpath, *path;
B
Bernard Xiong 已提交
1559
    struct dfs_file file;
1560

B
bernard 已提交
1561
    if (pathname == NULL)
1562
    {
1563
#ifdef DFS_USING_WORKDIR
1564 1565
        /* open current working directory */
        path = rt_strdup(working_directory);
1566
#else
1567
        path = rt_strdup("/");
1568
#endif
B
bernard 已提交
1569
        if (path == NULL)
B
Bernard Xiong 已提交
1570 1571 1572
        {
            return; /* out of memory */
        }
1573 1574 1575
    }
    else
    {
B
Bernard Xiong 已提交
1576 1577 1578 1579 1580
        path = dfs_normalize_path(NULL, (char *)pathname);
        if (path == NULL)
        {
            return; /* out of memory */
        }
1581 1582
    }

B
Bernard Xiong 已提交
1583 1584
    dfs_file_init(&file);

1585
    /* list directory */
B
Bernard Xiong 已提交
1586 1587
    DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_open(%s, O_DIRECTORY, 0)", path);
    if (dfs_file_open(&file, path, O_DIRECTORY, 0) >= 0)
1588
    {
B
Bernard Xiong 已提交
1589 1590
        char *link_fn = (char *) rt_malloc (DFS_PATH_MAX);
        if (link_fn)
1591
        {
B
Bernard Xiong 已提交
1592 1593
            rt_kprintf("Directory %s:\n", path);
            do
1594
            {
B
Bernard Xiong 已提交
1595
                memset(&dirent, 0, sizeof(struct dirent));
1596

B
Bernard Xiong 已提交
1597 1598 1599 1600
                DLOG(group, "foreach_item");
                DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_getdents(&dirent)");
                length = dfs_file_getdents(&file, &dirent, sizeof(struct dirent));
                if (length > 0)
1601
                {
B
Bernard Xiong 已提交
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
                    DLOG(msg, "dfs_file", "dfs", DLOG_MSG_RET, "dirent.d_name=%s", dirent.d_name);
                    memset(&stat, 0, sizeof(struct stat));

                    /* build full path for each file */
                    fullpath = dfs_normalize_path(path, dirent.d_name);
                    if (fullpath == NULL)
                        break;

                    DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_lstat(%s, &stat)", fullpath);
                    if (dfs_file_lstat(fullpath, &stat) == 0)
1612
                    {
B
Bernard Xiong 已提交
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
                        if (S_ISDIR(stat.st_mode))
                        {
                            rt_kprintf(_COLOR_BLUE "%-20s" _COLOR_NORMAL, dirent.d_name);
                            rt_kprintf("%-25s\n", "<DIR>");
                        }
                        else if (S_ISLNK(stat.st_mode))
                        {
                            int ret = 0;

                            rt_kprintf(_COLOR_CYAN "%-20s" _COLOR_NORMAL, dirent.d_name);

                            ret = dfs_file_readlink(fullpath, link_fn, DFS_PATH_MAX);
                            if (ret > 0)
                            {
                                char *link_path = link_fn;
                                struct dfs_mnt *mnt = RT_NULL;

                                mnt = dfs_mnt_lookup(fullpath);
                                if (mnt)
                                {
                                    char *tmp = dfs_nolink_path(&mnt, fullpath, 0);
                                    if (tmp)
                                    {
                                        char *index;

                                        index = strrchr(fullpath, '/');
                                        if (index)
                                        {
                                            int length = index - fullpath;
                                            char *parent = (char*) rt_malloc (length + 1);
                                            if (parent)
                                            {
                                                rt_memcpy(parent, fullpath, length);
                                                parent[length] = '\0';

                                                ret = rt_strncmp(parent, link_fn, length);
                                                if (ret == 0)
                                                {
                                                    link_path = link_fn + length;
                                                    if (*link_path == '/')
                                                    {
                                                        link_path ++;
                                                    }
                                                }
                                                rt_free(parent);
                                            }
                                        }
                                        rt_free(tmp);
                                    }
                                }

                                rt_kprintf("-> %s\n", link_path);
                            }
                            else
                            {
                                rt_kprintf(_COLOR_RED "-> link_error\n" _COLOR_NORMAL);
                            }
                        }
1671 1672 1673 1674 1675
                        else if (stat.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
                        {
                            rt_kprintf(_COLOR_GREEN "%-20s" _COLOR_NORMAL, dirent.d_name);
                            rt_kprintf("%-25lu\n", (unsigned long)stat.st_size);
                        }
B
Bernard Xiong 已提交
1676 1677 1678 1679 1680
                        else
                        {
                            rt_kprintf("%-20s", dirent.d_name);
                            rt_kprintf("%-25lu\n", (unsigned long)stat.st_size);
                        }
1681 1682 1683
                    }
                    else
                    {
1684
                        rt_kprintf(_COLOR_RED "%-20s" _COLOR_NORMAL, dirent.d_name);
1685
                    }
B
Bernard Xiong 已提交
1686 1687

                    rt_free(fullpath);
1688 1689
                }
                else
B
Bernard Xiong 已提交
1690 1691 1692 1693 1694 1695 1696 1697
                {
                    DLOG(msg, "dfs_file", "dfs", DLOG_MSG_RET, "return NULL");
                }

                DLOG(group_end);
            } while (length > 0);

            rt_free(link_fn);
1698
        }
1699

B
Bernard Xiong 已提交
1700 1701
        DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_close()");
        dfs_file_close(&file);
1702 1703 1704 1705 1706
    }
    else
    {
        rt_kprintf("No such directory\n");
    }
1707

B
Bernard Xiong 已提交
1708 1709 1710 1711
    dfs_file_deinit(&file);

    DLOG(msg, "dfs_file", "dfs", DLOG_MSG_RET, "return");
    rt_free(path);
1712
}
1713

1714
void cat(const char *filename)
1715
{
G
guo 已提交
1716
    int length = 0;
1717
    char buffer[81];
B
Bernard Xiong 已提交
1718
    struct dfs_file file;
1719

B
Bernard Xiong 已提交
1720
    if (filename && dfs_file_isdir(filename) == 0)
1721
    {
B
Bernard Xiong 已提交
1722 1723 1724 1725 1726
        rt_kprintf("cat: %s Is a directory\n", filename);
        return;
    }

    dfs_file_init(&file);
1727

B
Bernard Xiong 已提交
1728 1729 1730 1731 1732
    DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_open(%s, O_RDONLY, 0)", filename);
    if (dfs_file_open(&file, filename, O_RDONLY, 0) < 0)
    {
        rt_kprintf("Open %s failed\n", filename);
        dfs_file_deinit(&file);
1733 1734 1735 1736 1737
        return;
    }

    do
    {
G
guo 已提交
1738
        rt_memset(buffer, 0x0, sizeof(buffer));
B
Bernard Xiong 已提交
1739 1740
        DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_read(fd, buffer, %d)", sizeof(buffer) - 1);
        length = dfs_file_read(&file, (void *)buffer, sizeof(buffer) - 1);
1741 1742
        if (length > 0)
        {
G
guo 已提交
1743
            buffer[length] = '\0';
B
Bernard Xiong 已提交
1744
            rt_kprintf("%s", buffer);
1745
        }
G
guo 已提交
1746
    } while (length > 0);
褚仕成 已提交
1747
    rt_kprintf("\n");
1748

B
Bernard Xiong 已提交
1749 1750 1751
    DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_close()");
    dfs_file_close(&file);
    dfs_file_deinit(&file);
1752 1753
}

1754
#define BUF_SZ  4096
P
prife 已提交
1755
static void copyfile(const char *src, const char *dst)
1756
{
B
Bernard Xiong 已提交
1757 1758
    int ret;
    struct dfs_file src_file, dst_file;
1759
    rt_uint8_t *block_ptr;
1760
    rt_int32_t read_bytes;
1761

1762
    block_ptr = (rt_uint8_t *)rt_malloc(BUF_SZ);
B
bernard 已提交
1763
    if (block_ptr == NULL)
1764 1765 1766 1767 1768
    {
        rt_kprintf("out of memory\n");
        return;
    }

B
Bernard Xiong 已提交
1769 1770 1771 1772
    dfs_file_init(&src_file);

    ret = dfs_file_open(&src_file, src, O_RDONLY, 0);
    if (ret < 0)
1773
    {
B
Bernard Xiong 已提交
1774
        dfs_file_deinit(&src_file);
1775 1776 1777 1778
        rt_free(block_ptr);
        rt_kprintf("Read %s failed\n", src);
        return;
    }
B
Bernard Xiong 已提交
1779 1780 1781 1782 1783

    dfs_file_init(&dst_file);

    ret = dfs_file_open(&dst_file, dst, O_WRONLY | O_CREAT, 0);
    if (ret < 0)
1784
    {
B
Bernard Xiong 已提交
1785 1786 1787
        dfs_file_deinit(&dst_file);
        dfs_file_close(&src_file);
        dfs_file_deinit(&src_file);
1788 1789 1790 1791 1792 1793 1794
        rt_free(block_ptr);
        rt_kprintf("Write %s failed\n", dst);
        return;
    }

    do
    {
B
Bernard Xiong 已提交
1795
        read_bytes = dfs_file_read(&src_file, block_ptr, BUF_SZ);
1796 1797
        if (read_bytes > 0)
        {
B
Bernard Xiong 已提交
1798 1799
            int length;

B
Bernard Xiong 已提交
1800
            length = dfs_file_write(&dst_file, block_ptr, read_bytes);
B
Bernard Xiong 已提交
1801 1802 1803 1804 1805 1806
            if (length != read_bytes)
            {
                /* write failed. */
                rt_kprintf("Write file data failed, errno=%d\n", length);
                break;
            }
1807
        }
B
Bernard Xiong 已提交
1808
    } while (read_bytes > 0);
1809

B
Bernard Xiong 已提交
1810 1811 1812 1813
    dfs_file_close(&dst_file);
    dfs_file_deinit(&dst_file);
    dfs_file_close(&src_file);
    dfs_file_deinit(&src_file);
1814
    rt_free(block_ptr);
1815
}
P
prife 已提交
1816 1817

extern int mkdir(const char *path, mode_t mode);
1818
static void copydir(const char *src, const char *dst)
P
prife 已提交
1819 1820 1821 1822
{
    struct dirent dirent;
    struct stat stat;
    int length;
B
Bernard Xiong 已提交
1823 1824 1825 1826 1827
    struct dfs_file file;

    dfs_file_init(&file);

    if (dfs_file_open(&file, src, O_DIRECTORY, 0) < 0)
P
prife 已提交
1828 1829
    {
        rt_kprintf("open %s failed\n", src);
B
Bernard Xiong 已提交
1830
        dfs_file_deinit(&file);
P
prife 已提交
1831 1832 1833 1834 1835
        return ;
    }

    do
    {
1836
        rt_memset(&dirent, 0, sizeof(struct dirent));
B
bernard 已提交
1837

B
Bernard Xiong 已提交
1838
        length = dfs_file_getdents(&file, &dirent, sizeof(struct dirent));
P
prife 已提交
1839 1840
        if (length > 0)
        {
1841 1842
            char *src_entry_full = NULL;
            char *dst_entry_full = NULL;
P
prife 已提交
1843 1844 1845 1846 1847

            if (strcmp(dirent.d_name, "..") == 0 || strcmp(dirent.d_name, ".") == 0)
                continue;

            /* build full path for each file */
B
bernard 已提交
1848
            if ((src_entry_full = dfs_normalize_path(src, dirent.d_name)) == NULL)
P
prife 已提交
1849 1850 1851 1852
            {
                rt_kprintf("out of memory!\n");
                break;
            }
B
bernard 已提交
1853
            if ((dst_entry_full = dfs_normalize_path(dst, dirent.d_name)) == NULL)
P
prife 已提交
1854 1855 1856 1857 1858 1859
            {
                rt_kprintf("out of memory!\n");
                rt_free(src_entry_full);
                break;
            }

1860
            rt_memset(&stat, 0, sizeof(struct stat));
B
Bernard Xiong 已提交
1861
            if (dfs_file_lstat(src_entry_full, &stat) != 0)
P
prife 已提交
1862 1863 1864 1865 1866
            {
                rt_kprintf("open file: %s failed\n", dirent.d_name);
                continue;
            }

B
bernard 已提交
1867
            if (S_ISDIR(stat.st_mode))
P
prife 已提交
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
            {
                mkdir(dst_entry_full, 0);
                copydir(src_entry_full, dst_entry_full);
            }
            else
            {
                copyfile(src_entry_full, dst_entry_full);
            }
            rt_free(src_entry_full);
            rt_free(dst_entry_full);
        }
1879 1880
    }
    while (length > 0);
P
prife 已提交
1881

B
Bernard Xiong 已提交
1882 1883
    dfs_file_close(&file);
    dfs_file_deinit(&file);
P
prife 已提交
1884 1885 1886 1887
}

static const char *_get_path_lastname(const char *path)
{
1888
    char *ptr;
1889
    if ((ptr = (char *)strrchr(path, '/')) == NULL)
P
prife 已提交
1890 1891 1892 1893 1894
        return path;

    /* skip the '/' then return */
    return ++ptr;
}
1895

P
prife 已提交
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908
void copy(const char *src, const char *dst)
{
#define FLAG_SRC_TYPE      0x03
#define FLAG_SRC_IS_DIR    0x01
#define FLAG_SRC_IS_FILE   0x02
#define FLAG_SRC_NON_EXSIT 0x00

#define FLAG_DST_TYPE      0x0C
#define FLAG_DST_IS_DIR    0x04
#define FLAG_DST_IS_FILE   0x08
#define FLAG_DST_NON_EXSIT 0x00

    struct stat stat;
B
bernard 已提交
1909
    uint32_t flag = 0;
P
prife 已提交
1910 1911

    /* check the staus of src and dst */
B
Bernard Xiong 已提交
1912
    if (dfs_file_lstat(src, &stat) < 0)
P
prife 已提交
1913 1914 1915 1916
    {
        rt_kprintf("copy failed, bad %s\n", src);
        return;
    }
B
bernard 已提交
1917
    if (S_ISDIR(stat.st_mode))
P
prife 已提交
1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
        flag |= FLAG_SRC_IS_DIR;
    else
        flag |= FLAG_SRC_IS_FILE;

    if (dfs_file_stat(dst, &stat) < 0)
    {
        flag |= FLAG_DST_NON_EXSIT;
    }
    else
    {
B
bernard 已提交
1928
        if (S_ISDIR(stat.st_mode))
P
prife 已提交
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
            flag |= FLAG_DST_IS_DIR;
        else
            flag |= FLAG_DST_IS_FILE;
    }

    //2. check status
    if ((flag & FLAG_SRC_IS_DIR) && (flag & FLAG_DST_IS_FILE))
    {
        rt_kprintf("cp faild, cp dir to file is not permitted!\n");
        return ;
    }

    //3. do copy
    if (flag & FLAG_SRC_IS_FILE)
    {
        if (flag & FLAG_DST_IS_DIR)
        {
1946
            char *fdst;
P
prife 已提交
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964
            fdst = dfs_normalize_path(dst, _get_path_lastname(src));
            if (fdst == NULL)
            {
                rt_kprintf("out of memory\n");
                return;
            }
            copyfile(src, fdst);
            rt_free(fdst);
        }
        else
        {
            copyfile(src, dst);
        }
    }
    else //flag & FLAG_SRC_IS_DIR
    {
        if (flag & FLAG_DST_IS_DIR)
        {
1965
            char *fdst;
P
prife 已提交
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
            fdst = dfs_normalize_path(dst, _get_path_lastname(src));
            if (fdst == NULL)
            {
                rt_kprintf("out of memory\n");
                return;
            }
            mkdir(fdst, 0);
            copydir(src, fdst);
            rt_free(fdst);
        }
        else if ((flag & FLAG_DST_TYPE) == FLAG_DST_NON_EXSIT)
        {
            mkdir(dst, 0);
            copydir(src, dst);
        }
        else
        {
            copydir(src, dst);
        }
    }
}
FINSH_FUNCTION_EXPORT(copy, copy file or dir)
1988

B
Bernard Xiong 已提交
1989
#endif