dfs_file.c 19.6 KB
Newer Older
1 2 3
/*
 * File      : dfs_file.c
 * This file is part of Device File System in RT-Thread RTOS
4
 * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
5
 *
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
 *
 * Change Logs:
 * Date           Author       Notes
 * 2005-02-22     Bernard      The first version.
23
 * 2011-12-08     Bernard      Merges rename patch from iamcacy.
B
Bernard Xiong 已提交
24
 * 2015-05-27     Bernard      Fix the fd clear issue.
25
 */
26

27 28 29 30 31 32
#include <dfs.h>
#include <dfs_file.h>

/**
 * @addtogroup FileApi
 */
33

34 35 36 37 38 39
/*@{*/

/**
 * this function will open a file which specified by path with specified flags.
 *
 * @param fd the file descriptor pointer to return the corresponding result.
40
 * @param path the specified file path.
41 42 43 44
 * @param flags the flags for open operator.
 *
 * @return 0 on successful, -1 on failed.
 */
45
int dfs_file_open(struct dfs_fd *fd, const char *path, int flags)
46
{
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 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 99 100
    struct dfs_filesystem *fs;
    char *fullpath;
    int result;

    /* parameter check */
    if (fd == RT_NULL)
        return -DFS_STATUS_EINVAL;

    /* make sure we have an absolute path */
    fullpath = dfs_normalize_path(RT_NULL, path);
    if (fullpath == RT_NULL)
    {
        return -1;
    }

    dfs_log(DFS_DEBUG_INFO, ("open file:%s", fullpath));

    /* find filesystem */
    fs = dfs_filesystem_lookup(fullpath);
    if (fs == RT_NULL)
    {
        rt_free(fullpath); /* release path */

        return -DFS_STATUS_ENOENT;
    }

    dfs_log(DFS_DEBUG_INFO, ("open in filesystem:%s", fs->ops->name));
    fd->fs = fs;

    /* initialize the fd item */
    fd->type  = FT_REGULAR;
    fd->flags = flags;
    fd->size  = 0;
    fd->pos   = 0;

    if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
    {
        if (dfs_subdir(fs->path, fullpath) == RT_NULL)
            fd->path = rt_strdup("/");
        else
            fd->path = rt_strdup(dfs_subdir(fs->path, fullpath));
        rt_free(fullpath);
        dfs_log(DFS_DEBUG_INFO, ("Actual file path: %s\n", fd->path));
    }
    else
    {
        fd->path = fullpath;
    }

    /* specific file system open routine */
    if (fs->ops->open == RT_NULL)
    {
        /* clear fd */
        rt_free(fd->path);
B
Bernard Xiong 已提交
101
        fd->path = RT_NULL;
102 103 104 105 106 107 108 109

        return -DFS_STATUS_ENOSYS;
    }

    if ((result = fs->ops->open(fd)) < 0)
    {
        /* clear fd */
        rt_free(fd->path);
B
Bernard Xiong 已提交
110
        fd->path = RT_NULL;
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

        dfs_log(DFS_DEBUG_INFO, ("open failed"));

        return result;
    }

    fd->flags |= DFS_F_OPEN;
    if (flags & DFS_O_DIRECTORY)
    {
        fd->type = FT_DIRECTORY;
        fd->flags |= DFS_F_DIRECTORY;
    }

    dfs_log(DFS_DEBUG_INFO, ("open successful"));
    return 0;
126 127 128 129 130 131 132 133 134
}

/**
 * this function will close a file descriptor.
 *
 * @param fd the file descriptor to be closed.
 *
 * @return 0 on successful, -1 on failed.
 */
135
int dfs_file_close(struct dfs_fd *fd)
136
{
137
    int result = 0;
138

139 140 141
    if (fd == RT_NULL)
        return -DFS_STATUS_ENXIO;

142 143
    if (fd != RT_NULL && fd->fs->ops->close != RT_NULL)
        result = fd->fs->ops->close(fd);
144

145 146 147
    /* close fd error, return */
    if (result < 0)
        return result;
148

149
    rt_free(fd->path);
B
Bernard Xiong 已提交
150
    fd->path = RT_NULL;
151

152
    return result;
153 154 155 156 157 158 159 160 161 162 163
}

/**
 * this function will perform a io control on a file descriptor.
 *
 * @param fd the file descriptor.
 * @param cmd the command to send to file descriptor.
 * @param args the argument to send to file descriptor.
 *
 * @return 0 on successful, -1 on failed.
 */
164
int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args)
165
{
166
    struct dfs_filesystem *fs;
167

168 169
    if (fd == RT_NULL || fd->type != FT_REGULAR)
        return -DFS_STATUS_EINVAL;
170

171
    fs = fd->fs;
B
Bernard Xiong 已提交
172
    if (fs->ops->ioctl != RT_NULL)
173
        return fs->ops->ioctl(fd, cmd, args);
174

175
    return -DFS_STATUS_ENOSYS;
176 177 178
}

/**
179 180
 * this function will read specified length data from a file descriptor to a
 * buffer.
181 182 183 184 185 186 187
 *
 * @param fd the file descriptor.
 * @param buf the buffer to save the read data.
 * @param len the length of data buffer to be read.
 *
 * @return the actual read data bytes or 0 on end of file or failed.
 */
188
int dfs_file_read(struct dfs_fd *fd, void *buf, rt_size_t len)
189
{
190 191
    struct dfs_filesystem *fs;
    int result = 0;
192

B
Bernard Xiong 已提交
193
    if (fd == RT_NULL)
194
        return -DFS_STATUS_EINVAL;
195

196
    fs = (struct dfs_filesystem *)fd->fs;
B
Bernard Xiong 已提交
197
    if (fs->ops->read == RT_NULL)
198
        return -DFS_STATUS_ENOSYS;
199

200 201
    if ((result = fs->ops->read(fd, buf, len)) < 0)
        fd->flags |= DFS_F_EOF;
202

203
    return result;
204 205 206 207 208
}

/**
 * this function will fetch directory entries from a directory descriptor.
 *
209
 * @param fd the directory descriptor.
210
 * @param dirp the dirent buffer to save result.
211
 * @param nbytes the available room in the buffer.
212 213 214
 *
 * @return the read dirent, others on failed.
 */
215
int dfs_file_getdents(struct dfs_fd *fd, struct dirent *dirp, rt_size_t nbytes)
216
{
217
    struct dfs_filesystem *fs;
218

219
    /* parameter check */
B
Bernard Xiong 已提交
220
    if (fd == RT_NULL || fd->type != FT_DIRECTORY)
221
        return -DFS_STATUS_EINVAL;
222

223 224 225
    fs = (struct dfs_filesystem *)fd->fs;
    if (fs->ops->getdents != RT_NULL)
        return fs->ops->getdents(fd, dirp, nbytes);
226

227
    return -DFS_STATUS_ENOSYS;
228 229 230 231 232 233 234 235 236 237 238
}

/**
 * this function will unlink (remove) a specified path file from file system.
 *
 * @param path the specified path file to be unlinked.
 *
 * @return 0 on successful, -1 on failed.
 */
int dfs_file_unlink(const char *path)
{
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
    int result;
    char *fullpath;
    struct dfs_filesystem *fs;

    /* Make sure we have an absolute path */
    fullpath = dfs_normalize_path(RT_NULL, path);
    if (fullpath == RT_NULL)
    {
        return -DFS_STATUS_EINVAL;
    }

    /* get filesystem */
    if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL)
    {
        result = -DFS_STATUS_ENOENT;
        goto __exit;
    }

    /* Check whether file is already open */
    if (fd_is_open(fullpath) == 0)
    {
        result = -DFS_STATUS_EBUSY;
        goto __exit;
    }

    if (fs->ops->unlink != RT_NULL)
    {
        if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
        {
            if (dfs_subdir(fs->path, fullpath) == RT_NULL)
                result = fs->ops->unlink(fs, "/");
            else
                result = fs->ops->unlink(fs, dfs_subdir(fs->path, fullpath));
        }
        else
B
Bernard Xiong 已提交
274
            result = fs->ops->unlink(fs, fullpath);
275 276
    }
    else result = -DFS_STATUS_ENOSYS;
277 278

__exit:
279 280
    rt_free(fullpath);
    return result;
281 282 283 284 285 286 287 288 289 290 291
}

/**
 * this function will write some specified length data to file system.
 *
 * @param fd the file descriptor.
 * @param buf the data buffer to be written.
 * @param len the data buffer length
 *
 * @return the actual written data length.
 */
292
int dfs_file_write(struct dfs_fd *fd, const void *buf, rt_size_t len)
293
{
294
    struct dfs_filesystem *fs;
295

296 297
    if (fd == RT_NULL)
        return -DFS_STATUS_EINVAL;
298

299 300 301
    fs = fd->fs;
    if (fs->ops->write == RT_NULL)
        return -DFS_STATUS_ENOSYS;
302

303
    return fs->ops->write(fd, buf, len);
304 305 306 307 308 309 310 311 312
}

/**
 * this function will flush buffer on a file descriptor.
 *
 * @param fd the file descriptor.
 *
 * @return 0 on successful, -1 on failed.
 */
313
int dfs_file_flush(struct dfs_fd *fd)
314
{
315
    struct dfs_filesystem *fs;
316

317 318
    if (fd == RT_NULL)
        return -DFS_STATUS_EINVAL;
319

320 321 322
    fs = fd->fs;
    if (fs->ops->flush == RT_NULL)
        return -DFS_STATUS_ENOSYS;
323

324
    return fs->ops->flush(fd);
325 326 327 328 329 330
}

/**
 * this function will seek the offset for specified file descriptor.
 *
 * @param fd the file descriptor.
331
 * @param offset the offset to be sought.
332 333 334
 *
 * @return the current position after seek.
 */
335
int dfs_file_lseek(struct dfs_fd *fd, rt_off_t offset)
336
{
337 338
    int result;
    struct dfs_filesystem *fs = fd->fs;
339

340 341
    if (fd == RT_NULL)
        return -DFS_STATUS_EINVAL;
G
Grissiom 已提交
342 343 344
    fs = fd->fs;
    if (fs == RT_NULL)
        return -DFS_STATUS_EINVAL;
345 346
    if (fs->ops->lseek == RT_NULL)
        return -DFS_STATUS_ENOSYS;
347

348
    result = fs->ops->lseek(fd, offset);
349

350 351 352
    /* update current position */
    if (result >= 0)
        fd->pos = result;
353

354
    return result;
355 356 357 358 359 360 361 362 363 364 365 366
}

/**
 * this function will get file information.
 *
 * @param path the file path.
 * @param buf the data buffer to save stat description.
 *
 * @return 0 on successful, -1 on failed.
 */
int dfs_file_stat(const char *path, struct stat *buf)
{
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
    int result;
    char *fullpath;
    struct dfs_filesystem *fs;

    fullpath = dfs_normalize_path(RT_NULL, path);
    if (fullpath == RT_NULL)
    {
        return -1;
    }

    if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL)
    {
        dfs_log(DFS_DEBUG_ERROR,
                ("can't find mounted filesystem on this path:%s", fullpath));
        rt_free(fullpath);

        return -DFS_STATUS_ENOENT;
    }

    if ((fullpath[0] == '/' && fullpath[1] == '\0') ||
        (dfs_subdir(fs->path, fullpath) == RT_NULL))
    {
        /* it's the root directory */
        buf->st_dev   = 0;

        buf->st_mode  = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
                        DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
        buf->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;

        buf->st_size    = 0;
        buf->st_mtime   = 0;

        /* release full path */
        rt_free(fullpath);

        return DFS_STATUS_OK;
    }
    else
    {
        if (fs->ops->stat == RT_NULL)
        {
            rt_free(fullpath);
            dfs_log(DFS_DEBUG_ERROR,
                    ("the filesystem didn't implement this function"));

            return -DFS_STATUS_ENOSYS;
        }

        /* get the real file path and get file stat */
        if (fs->ops->flags & DFS_FS_FLAG_FULLPATH)
            result = fs->ops->stat(fs, fullpath, buf);
        else
            result = fs->ops->stat(fs, dfs_subdir(fs->path, fullpath), buf);
    }

    rt_free(fullpath);

    return result;
425 426 427
}

/**
428
 * this function will rename an old path name to a new path name.
429 430 431 432 433 434
 *
 * @param oldpath the old path name.
 * @param newpath the new path name.
 *
 * @return 0 on successful, -1 on failed.
 */
435
int dfs_file_rename(const char *oldpath, const char *newpath)
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
    int result;
    struct dfs_filesystem *oldfs, *newfs;
    char *oldfullpath, *newfullpath;

    result = DFS_STATUS_OK;
    newfullpath = RT_NULL;
    oldfullpath = RT_NULL;

    oldfullpath = dfs_normalize_path(RT_NULL, oldpath);
    if (oldfullpath == RT_NULL)
    {
        result = -DFS_STATUS_ENOENT;
        goto __exit;
    }

    newfullpath = dfs_normalize_path(RT_NULL, newpath);
    if (newfullpath == RT_NULL)
    {
        result = -DFS_STATUS_ENOENT;
        goto __exit;
    }

    oldfs = dfs_filesystem_lookup(oldfullpath);
    newfs = dfs_filesystem_lookup(newfullpath);

    if (oldfs == newfs)
    {
        if (oldfs->ops->rename == RT_NULL)
        {
            result = -DFS_STATUS_ENOSYS;
        }
        else
        {
            if (oldfs->ops->flags & DFS_FS_FLAG_FULLPATH)
                result = oldfs->ops->rename(oldfs, oldfullpath, newfullpath);
            else
                /* use sub directory to rename in file system */
                result = oldfs->ops->rename(oldfs,
                                            dfs_subdir(oldfs->path, oldfullpath),
                                            dfs_subdir(newfs->path, newfullpath));
        }
    }
    else
    {
        result = -DFS_STATUS_EXDEV;
    }
483 484

__exit:
485 486
    rt_free(oldfullpath);
    rt_free(newfullpath);
487

488 489
    /* not at same file system, return EXDEV */
    return result;
490 491 492 493 494 495 496
}

#ifdef RT_USING_FINSH
#include <finsh.h>

static struct dfs_fd fd;
static struct dirent dirent;
497
void ls(const char *pathname)
498
{
499 500 501
    struct stat stat;
    int length;
    char *fullpath, *path;
502

503 504 505
    fullpath = RT_NULL;
    if (pathname == RT_NULL)
    {
506
#ifdef DFS_USING_WORKDIR
507 508
        /* open current working directory */
        path = rt_strdup(working_directory);
509
#else
510
        path = rt_strdup("/");
511
#endif
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
        if (path == RT_NULL)
            return ; /* out of memory */
    }
    else
    {
        path = (char *)pathname;
    }

    /* list directory */
    if (dfs_file_open(&fd, path, DFS_O_DIRECTORY) == 0)
    {
        rt_kprintf("Directory %s:\n", path);
        do
        {
            rt_memset(&dirent, 0, sizeof(struct dirent));
            length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent));
            if (length > 0)
            {
                rt_memset(&stat, 0, sizeof(struct stat));

                /* build full path for each file */
                fullpath = dfs_normalize_path(path, dirent.d_name);
B
Bernard Xiong 已提交
534
                if (fullpath == RT_NULL)
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
                    break;

                if (dfs_file_stat(fullpath, &stat) == 0)
                {
                    rt_kprintf("%-20s", dirent.d_name);
                    if ( DFS_S_ISDIR(stat.st_mode))
                    {
                        rt_kprintf("%-25s\n", "<DIR>");
                    }
                    else
                    {
                        rt_kprintf("%-25lu\n", stat.st_size);
                    }
                }
                else
                    rt_kprintf("BAD file: %s\n", dirent.d_name);
                rt_free(fullpath);
            }
        }while(length > 0);

        dfs_file_close(&fd);
    }
    else
    {
        rt_kprintf("No such directory\n");
    }
B
Bernard Xiong 已提交
561
    if (pathname == RT_NULL)
562
        rt_free(path);
563
}
B
bernard 已提交
564
FINSH_FUNCTION_EXPORT(ls, list directory contents);
565

566
void rm(const char *filename)
567
{
568 569 570 571
    if (dfs_file_unlink(filename) < 0)
    {
        rt_kprintf("Delete %s failed\n", filename);
    }
572
}
B
bernard 已提交
573
FINSH_FUNCTION_EXPORT(rm, remove files or directories);
574

575 576
void cat(const char* filename)
{
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
    rt_uint32_t length;
    char buffer[81];

    if (dfs_file_open(&fd, filename, DFS_O_RDONLY) < 0)
    {
        rt_kprintf("Open %s failed\n", filename);

        return;
    }

    do
    {
        rt_memset(buffer, 0, sizeof(buffer));
        length = dfs_file_read(&fd, buffer, sizeof(buffer)-1 );
        if (length > 0)
        {
            rt_kprintf("%s", buffer);
        }
    }while (length > 0);

    dfs_file_close(&fd);
598
}
B
bernard 已提交
599
FINSH_FUNCTION_EXPORT(cat, print file);
600

601
#define BUF_SZ  4096
P
prife 已提交
602
static void copyfile(const char *src, const char *dst)
603
{
604 605
    struct dfs_fd src_fd;
    rt_uint8_t *block_ptr;
606
    rt_int32_t read_bytes;
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637

    block_ptr = rt_malloc(BUF_SZ);
    if (block_ptr == RT_NULL)
    {
        rt_kprintf("out of memory\n");

        return;
    }

    if (dfs_file_open(&src_fd, src, DFS_O_RDONLY) < 0)
    {
        rt_free(block_ptr);
        rt_kprintf("Read %s failed\n", src);

        return;
    }
    if (dfs_file_open(&fd, dst, DFS_O_WRONLY | DFS_O_CREAT) < 0)
    {
        rt_free(block_ptr);
        dfs_file_close(&src_fd);

        rt_kprintf("Write %s failed\n", dst);

        return;
    }

    do
    {
        read_bytes = dfs_file_read(&src_fd, block_ptr, BUF_SZ);
        if (read_bytes > 0)
        {
B
Bernard Xiong 已提交
638 639
            int length;

B
bernard 已提交
640
            length = dfs_file_write(&fd, block_ptr, read_bytes);
B
Bernard Xiong 已提交
641 642 643 644 645 646
            if (length != read_bytes)
            {
                /* write failed. */
                rt_kprintf("Write file data failed, errno=%d\n", length);
                break;
            }
647 648 649 650 651 652
        }
    } while (read_bytes > 0);

    dfs_file_close(&src_fd);
    dfs_file_close(&fd);
    rt_free(block_ptr);
653
}
P
prife 已提交
654 655 656 657 658 659 660

extern int mkdir(const char *path, mode_t mode);
static void copydir(const char * src, const char * dst)
{
    struct dirent dirent;
    struct stat stat;
    int length;
661 662
    struct dfs_fd cpfd;
    if (dfs_file_open(&cpfd, src, DFS_O_DIRECTORY) < 0)
P
prife 已提交
663 664 665 666 667 668 669 670
    {
        rt_kprintf("open %s failed\n", src);
        return ;
    }

    do
    {
        rt_memset(&dirent, 0, sizeof(struct dirent));
671
        length = dfs_file_getdents(&cpfd, &dirent, sizeof(struct dirent));
P
prife 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
        if (length > 0)
        {
            char * src_entry_full = RT_NULL;
            char * dst_entry_full = RT_NULL;

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

            /* build full path for each file */
            if ((src_entry_full = dfs_normalize_path(src, dirent.d_name)) == RT_NULL)
            {
                rt_kprintf("out of memory!\n");
                break;
            }
            if ((dst_entry_full = dfs_normalize_path(dst, dirent.d_name)) == RT_NULL)
            {
                rt_kprintf("out of memory!\n");
                rt_free(src_entry_full);
                break;
            }

            rt_memset(&stat, 0, sizeof(struct stat));
            if (dfs_file_stat(src_entry_full, &stat) != 0)
            {
                rt_kprintf("open file: %s failed\n", dirent.d_name);
                continue;
            }

            if (DFS_S_ISDIR(stat.st_mode))
            {
                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);
        }
    }while(length > 0);

714
    dfs_file_close(&cpfd);
P
prife 已提交
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
}

static const char *_get_path_lastname(const char *path)
{
    char * ptr;
    if ((ptr = strrchr(path, '/')) == RT_NULL)
        return path;

    /* skip the '/' then return */
    return ++ptr;
}
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;
    rt_uint32_t flag = 0;

    /* check the staus of src and dst */
    if (dfs_file_stat(src, &stat) < 0)
    {
        rt_kprintf("copy failed, bad %s\n", src);
        return;
    }
    if (DFS_S_ISDIR(stat.st_mode))
        flag |= FLAG_SRC_IS_DIR;
    else
        flag |= FLAG_SRC_IS_FILE;

    if (dfs_file_stat(dst, &stat) < 0)
    {
        flag |= FLAG_DST_NON_EXSIT;
    }
    else
    {
        if (DFS_S_ISDIR(stat.st_mode))
            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)
        {
            char * fdst;
            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)
        {
            char * fdst;
            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)
818 819 820 821

#endif
/* @} */