dfs_file.c 18.4 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5
 *
6 7 8
 * Change Logs:
 * Date           Author       Notes
 * 2005-02-22     Bernard      The first version.
9
 * 2011-12-08     Bernard      Merges rename patch from iamcacy.
B
Bernard Xiong 已提交
10
 * 2015-05-27     Bernard      Fix the fd clear issue.
B
Bernard Xiong 已提交
11
 * 2019-01-24     Bernard      Remove file repeatedly open check.
12
 */
13

14 15
#include <dfs.h>
#include <dfs_file.h>
B
bernard 已提交
16
#include <dfs_private.h>
17 18 19 20

/**
 * @addtogroup FileApi
 */
21

22 23 24 25 26 27
/*@{*/

/**
 * this function will open a file which specified by path with specified flags.
 *
 * @param fd the file descriptor pointer to return the corresponding result.
28
 * @param path the specified file path.
29 30 31 32
 * @param flags the flags for open operator.
 *
 * @return 0 on successful, -1 on failed.
 */
33
int dfs_file_open(struct dfs_fd *fd, const char *path, int flags)
34
{
35 36 37 38 39
    struct dfs_filesystem *fs;
    char *fullpath;
    int result;

    /* parameter check */
B
bernard 已提交
40 41
    if (fd == NULL)
        return -EINVAL;
42 43

    /* make sure we have an absolute path */
B
bernard 已提交
44 45
    fullpath = dfs_normalize_path(NULL, path);
    if (fullpath == NULL)
46
    {
B
bernard 已提交
47
        return -ENOMEM;
48 49
    }

50
    LOG_D("open file:%s", fullpath);
B
bernard 已提交
51

52 53
    /* find filesystem */
    fs = dfs_filesystem_lookup(fullpath);
B
bernard 已提交
54
    if (fs == NULL)
55 56 57
    {
        rt_free(fullpath); /* release path */

B
bernard 已提交
58
        return -ENOENT;
59 60
    }

61
    LOG_D("open in filesystem:%s", fs->ops->name);
Y
yangfasheng 已提交
62 63
    fd->fs    = fs;             /* set file system */
    fd->fops  = fs->ops->fops;  /* set file ops */
64 65 66 67 68 69

    /* initialize the fd item */
    fd->type  = FT_REGULAR;
    fd->flags = flags;
    fd->size  = 0;
    fd->pos   = 0;
B
bernard 已提交
70
    fd->data  = fs;
71 72 73

    if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
    {
B
bernard 已提交
74
        if (dfs_subdir(fs->path, fullpath) == NULL)
75 76 77 78
            fd->path = rt_strdup("/");
        else
            fd->path = rt_strdup(dfs_subdir(fs->path, fullpath));
        rt_free(fullpath);
79
        LOG_D("Actual file path: %s", fd->path);
80 81 82 83 84 85 86
    }
    else
    {
        fd->path = fullpath;
    }

    /* specific file system open routine */
B
bernard 已提交
87
    if (fd->fops->open == NULL)
88 89 90
    {
        /* clear fd */
        rt_free(fd->path);
B
bernard 已提交
91
        fd->path = NULL;
92

B
bernard 已提交
93
        return -ENOSYS;
94 95
    }

B
bernard 已提交
96
    if ((result = fd->fops->open(fd)) < 0)
97 98 99
    {
        /* clear fd */
        rt_free(fd->path);
B
bernard 已提交
100
        fd->path = NULL;
101

armink_ztl's avatar
armink_ztl 已提交
102
        LOG_D("%s open failed", fullpath);
103 104 105 106 107

        return result;
    }

    fd->flags |= DFS_F_OPEN;
B
bernard 已提交
108
    if (flags & O_DIRECTORY)
109 110 111 112 113
    {
        fd->type = FT_DIRECTORY;
        fd->flags |= DFS_F_DIRECTORY;
    }

114
    LOG_D("open successful");
115
    return 0;
116 117 118 119 120 121 122 123 124
}

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

B
bernard 已提交
129 130
    if (fd == NULL)
        return -ENXIO;
131

B
bernard 已提交
132 133
    if (fd->fops->close != NULL)
        result = fd->fops->close(fd);
134

135 136 137
    /* close fd error, return */
    if (result < 0)
        return result;
138

139
    rt_free(fd->path);
B
bernard 已提交
140
    fd->path = NULL;
141

142
    return result;
143 144 145 146 147 148 149 150 151 152 153
}

/**
 * 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.
 */
154
int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args)
155
{
156
    if (fd == NULL)
B
bernard 已提交
157
        return -EINVAL;
158

159 160 161 162 163 164 165 166 167
    /* regular file system fd */
    if (fd->type == FT_REGULAR)
    {
        switch (cmd)
        {
        case F_GETFL:
            return fd->flags; /* return flags */
        case F_SETFL:
            {
168
                int flags = (int)(rt_base_t)args;
169 170 171
                int mask  = O_NONBLOCK | O_APPEND;

                flags &= mask;
Z
zylx 已提交
172
                fd->flags &= ~mask;
173 174 175 176 177 178
                fd->flags |= flags;
            }
            return 0;
        }
    }

B
bernard 已提交
179 180
    if (fd->fops->ioctl != NULL)
        return fd->fops->ioctl(fd, cmd, args);
181

B
bernard 已提交
182
    return -ENOSYS;
183 184 185
}

/**
186 187
 * this function will read specified length data from a file descriptor to a
 * buffer.
188 189 190 191 192 193 194
 *
 * @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.
 */
B
bernard 已提交
195
int dfs_file_read(struct dfs_fd *fd, void *buf, size_t len)
196
{
197
    int result = 0;
198

B
bernard 已提交
199 200
    if (fd == NULL)
        return -EINVAL;
201

B
bernard 已提交
202 203
    if (fd->fops->read == NULL)
        return -ENOSYS;
204

B
bernard 已提交
205
    if ((result = fd->fops->read(fd, buf, len)) < 0)
206
        fd->flags |= DFS_F_EOF;
207

208
    return result;
209 210 211 212 213
}

/**
 * this function will fetch directory entries from a directory descriptor.
 *
214
 * @param fd the directory descriptor.
215
 * @param dirp the dirent buffer to save result.
216
 * @param nbytes the available room in the buffer.
217 218 219
 *
 * @return the read dirent, others on failed.
 */
B
bernard 已提交
220
int dfs_file_getdents(struct dfs_fd *fd, struct dirent *dirp, size_t nbytes)
221
{
222
    /* parameter check */
B
bernard 已提交
223 224
    if (fd == NULL || fd->type != FT_DIRECTORY)
        return -EINVAL;
225

B
bernard 已提交
226 227
    if (fd->fops->getdents != NULL)
        return fd->fops->getdents(fd, dirp, nbytes);
228

B
bernard 已提交
229
    return -ENOSYS;
230 231 232 233 234 235 236 237 238 239 240
}

/**
 * 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)
{
241 242 243 244 245
    int result;
    char *fullpath;
    struct dfs_filesystem *fs;

    /* Make sure we have an absolute path */
B
bernard 已提交
246 247
    fullpath = dfs_normalize_path(NULL, path);
    if (fullpath == NULL)
248
    {
B
bernard 已提交
249
        return -EINVAL;
250 251 252
    }

    /* get filesystem */
B
bernard 已提交
253
    if ((fs = dfs_filesystem_lookup(fullpath)) == NULL)
254
    {
B
bernard 已提交
255
        result = -ENOENT;
256 257 258 259 260 261
        goto __exit;
    }

    /* Check whether file is already open */
    if (fd_is_open(fullpath) == 0)
    {
B
bernard 已提交
262
        result = -EBUSY;
263 264 265
        goto __exit;
    }

B
bernard 已提交
266
    if (fs->ops->unlink != NULL)
267 268 269
    {
        if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
        {
B
bernard 已提交
270
            if (dfs_subdir(fs->path, fullpath) == NULL)
271 272 273 274 275
                result = fs->ops->unlink(fs, "/");
            else
                result = fs->ops->unlink(fs, dfs_subdir(fs->path, fullpath));
        }
        else
B
Bernard Xiong 已提交
276
            result = fs->ops->unlink(fs, fullpath);
277
    }
B
bernard 已提交
278
    else result = -ENOSYS;
279 280

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

/**
 * 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.
 */
B
bernard 已提交
294
int dfs_file_write(struct dfs_fd *fd, const void *buf, size_t len)
295
{
B
bernard 已提交
296 297
    if (fd == NULL)
        return -EINVAL;
298

B
bernard 已提交
299 300
    if (fd->fops->write == NULL)
        return -ENOSYS;
301

B
bernard 已提交
302
    return fd->fops->write(fd, buf, len);
303 304 305 306 307 308 309 310 311
}

/**
 * this function will flush buffer on a file descriptor.
 *
 * @param fd the file descriptor.
 *
 * @return 0 on successful, -1 on failed.
 */
312
int dfs_file_flush(struct dfs_fd *fd)
313
{
B
bernard 已提交
314 315
    if (fd == NULL)
        return -EINVAL;
316

B
bernard 已提交
317 318
    if (fd->fops->flush == NULL)
        return -ENOSYS;
319

B
bernard 已提交
320
    return fd->fops->flush(fd);
321 322 323 324 325 326
}

/**
 * this function will seek the offset for specified file descriptor.
 *
 * @param fd the file descriptor.
327
 * @param offset the offset to be sought.
328 329 330
 *
 * @return the current position after seek.
 */
B
bernard 已提交
331
int dfs_file_lseek(struct dfs_fd *fd, off_t offset)
332
{
333
    int result;
334

B
bernard 已提交
335 336
    if (fd == NULL)
        return -EINVAL;
337

B
bernard 已提交
338 339 340 341
    if (fd->fops->lseek == NULL)
        return -ENOSYS;

    result = fd->fops->lseek(fd, offset);
342

343 344 345
    /* update current position */
    if (result >= 0)
        fd->pos = result;
346

347
    return result;
348 349 350 351 352 353 354 355 356 357 358 359
}

/**
 * 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)
{
360 361 362 363
    int result;
    char *fullpath;
    struct dfs_filesystem *fs;

B
bernard 已提交
364 365
    fullpath = dfs_normalize_path(NULL, path);
    if (fullpath == NULL)
366 367 368 369
    {
        return -1;
    }

B
bernard 已提交
370
    if ((fs = dfs_filesystem_lookup(fullpath)) == NULL)
371
    {
372 373
        LOG_E(
                "can't find mounted filesystem on this path:%s", fullpath);
374 375
        rt_free(fullpath);

B
bernard 已提交
376
        return -ENOENT;
377 378 379
    }

    if ((fullpath[0] == '/' && fullpath[1] == '\0') ||
B
bernard 已提交
380
        (dfs_subdir(fs->path, fullpath) == NULL))
381 382 383 384
    {
        /* it's the root directory */
        buf->st_dev   = 0;

B
bernard 已提交
385 386 387
        buf->st_mode  = S_IRUSR | S_IRGRP | S_IROTH |
                        S_IWUSR | S_IWGRP | S_IWOTH;
        buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
388 389 390 391 392 393 394

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

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

B
bernard 已提交
395
        return RT_EOK;
396 397 398
    }
    else
    {
B
bernard 已提交
399
        if (fs->ops->stat == NULL)
400 401
        {
            rt_free(fullpath);
402 403
            LOG_E(
                    "the filesystem didn't implement this function");
404

B
bernard 已提交
405
            return -ENOSYS;
406 407 408 409 410 411 412 413 414 415 416 417
        }

        /* 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;
418 419 420
}

/**
421
 * this function will rename an old path name to a new path name.
422 423 424 425 426 427
 *
 * @param oldpath the old path name.
 * @param newpath the new path name.
 *
 * @return 0 on successful, -1 on failed.
 */
428
int dfs_file_rename(const char *oldpath, const char *newpath)
429
{
430 431 432 433
    int result;
    struct dfs_filesystem *oldfs, *newfs;
    char *oldfullpath, *newfullpath;

B
bernard 已提交
434 435 436
    result = RT_EOK;
    newfullpath = NULL;
    oldfullpath = NULL;
437

B
bernard 已提交
438 439
    oldfullpath = dfs_normalize_path(NULL, oldpath);
    if (oldfullpath == NULL)
440
    {
B
bernard 已提交
441
        result = -ENOENT;
442 443 444
        goto __exit;
    }

B
bernard 已提交
445 446
    newfullpath = dfs_normalize_path(NULL, newpath);
    if (newfullpath == NULL)
447
    {
B
bernard 已提交
448
        result = -ENOENT;
449 450 451 452 453 454 455 456
        goto __exit;
    }

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

    if (oldfs == newfs)
    {
B
bernard 已提交
457
        if (oldfs->ops->rename == NULL)
458
        {
B
bernard 已提交
459
            result = -ENOSYS;
460 461 462 463 464 465 466 467 468 469 470 471 472 473
        }
        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
    {
B
bernard 已提交
474
        result = -EXDEV;
475
    }
476 477

__exit:
478 479
    rt_free(oldfullpath);
    rt_free(newfullpath);
480

481 482
    /* not at same file system, return EXDEV */
    return result;
483 484 485 486 487 488 489
}

#ifdef RT_USING_FINSH
#include <finsh.h>

static struct dfs_fd fd;
static struct dirent dirent;
490
void ls(const char *pathname)
491
{
492 493 494
    struct stat stat;
    int length;
    char *fullpath, *path;
495

B
bernard 已提交
496 497
    fullpath = NULL;
    if (pathname == NULL)
498
    {
499
#ifdef DFS_USING_WORKDIR
500 501
        /* open current working directory */
        path = rt_strdup(working_directory);
502
#else
503
        path = rt_strdup("/");
504
#endif
B
bernard 已提交
505
        if (path == NULL)
506 507 508 509 510 511 512 513
            return ; /* out of memory */
    }
    else
    {
        path = (char *)pathname;
    }

    /* list directory */
B
bernard 已提交
514
    if (dfs_file_open(&fd, path, O_DIRECTORY) == 0)
515 516 517 518
    {
        rt_kprintf("Directory %s:\n", path);
        do
        {
B
bernard 已提交
519
            memset(&dirent, 0, sizeof(struct dirent));
520 521 522
            length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent));
            if (length > 0)
            {
B
bernard 已提交
523
                memset(&stat, 0, sizeof(struct stat));
524 525 526

                /* build full path for each file */
                fullpath = dfs_normalize_path(path, dirent.d_name);
B
bernard 已提交
527
                if (fullpath == NULL)
528 529 530 531 532
                    break;

                if (dfs_file_stat(fullpath, &stat) == 0)
                {
                    rt_kprintf("%-20s", dirent.d_name);
B
bernard 已提交
533
                    if (S_ISDIR(stat.st_mode))
534 535 536 537 538 539 540 541 542 543 544 545
                    {
                        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);
            }
546 547
        }
        while (length > 0);
548 549 550 551 552 553 554

        dfs_file_close(&fd);
    }
    else
    {
        rt_kprintf("No such directory\n");
    }
B
bernard 已提交
555
    if (pathname == NULL)
556
        rt_free(path);
557
}
B
bernard 已提交
558
FINSH_FUNCTION_EXPORT(ls, list directory contents);
559

560
void rm(const char *filename)
561
{
562 563 564 565
    if (dfs_file_unlink(filename) < 0)
    {
        rt_kprintf("Delete %s failed\n", filename);
    }
566
}
B
bernard 已提交
567
FINSH_FUNCTION_EXPORT(rm, remove files or directories);
568

569
void cat(const char *filename)
570
{
B
bernard 已提交
571
    uint32_t length;
572 573
    char buffer[81];

B
bernard 已提交
574
    if (dfs_file_open(&fd, filename, O_RDONLY) < 0)
575 576 577 578 579 580 581 582
    {
        rt_kprintf("Open %s failed\n", filename);

        return;
    }

    do
    {
B
bernard 已提交
583
        memset(buffer, 0, sizeof(buffer));
584
        length = dfs_file_read(&fd, buffer, sizeof(buffer) - 1);
585 586 587 588
        if (length > 0)
        {
            rt_kprintf("%s", buffer);
        }
589 590
    }
    while (length > 0);
591 592

    dfs_file_close(&fd);
593
}
B
bernard 已提交
594
FINSH_FUNCTION_EXPORT(cat, print file);
595

596
#define BUF_SZ  4096
P
prife 已提交
597
static void copyfile(const char *src, const char *dst)
598
{
599 600
    struct dfs_fd src_fd;
    rt_uint8_t *block_ptr;
601
    rt_int32_t read_bytes;
602

603
    block_ptr = (rt_uint8_t *)rt_malloc(BUF_SZ);
B
bernard 已提交
604
    if (block_ptr == NULL)
605 606 607 608 609 610
    {
        rt_kprintf("out of memory\n");

        return;
    }

B
bernard 已提交
611
    if (dfs_file_open(&src_fd, src, O_RDONLY) < 0)
612 613 614 615 616 617
    {
        rt_free(block_ptr);
        rt_kprintf("Read %s failed\n", src);

        return;
    }
B
bernard 已提交
618
    if (dfs_file_open(&fd, dst, O_WRONLY | O_CREAT) < 0)
619 620 621 622 623 624 625 626 627 628 629 630 631 632
    {
        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 已提交
633 634
            int length;

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

    dfs_file_close(&src_fd);
    dfs_file_close(&fd);
    rt_free(block_ptr);
649
}
P
prife 已提交
650 651

extern int mkdir(const char *path, mode_t mode);
652
static void copydir(const char *src, const char *dst)
P
prife 已提交
653 654 655 656
{
    struct dirent dirent;
    struct stat stat;
    int length;
657
    struct dfs_fd cpfd;
B
bernard 已提交
658
    if (dfs_file_open(&cpfd, src, O_DIRECTORY) < 0)
P
prife 已提交
659 660 661 662 663 664 665
    {
        rt_kprintf("open %s failed\n", src);
        return ;
    }

    do
    {
B
bernard 已提交
666 667
        memset(&dirent, 0, sizeof(struct dirent));

668
        length = dfs_file_getdents(&cpfd, &dirent, sizeof(struct dirent));
P
prife 已提交
669 670
        if (length > 0)
        {
671 672
            char *src_entry_full = NULL;
            char *dst_entry_full = NULL;
P
prife 已提交
673 674 675 676 677

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

            /* build full path for each file */
B
bernard 已提交
678
            if ((src_entry_full = dfs_normalize_path(src, dirent.d_name)) == NULL)
P
prife 已提交
679 680 681 682
            {
                rt_kprintf("out of memory!\n");
                break;
            }
B
bernard 已提交
683
            if ((dst_entry_full = dfs_normalize_path(dst, dirent.d_name)) == NULL)
P
prife 已提交
684 685 686 687 688 689
            {
                rt_kprintf("out of memory!\n");
                rt_free(src_entry_full);
                break;
            }

B
bernard 已提交
690
            memset(&stat, 0, sizeof(struct stat));
P
prife 已提交
691 692 693 694 695 696
            if (dfs_file_stat(src_entry_full, &stat) != 0)
            {
                rt_kprintf("open file: %s failed\n", dirent.d_name);
                continue;
            }

B
bernard 已提交
697
            if (S_ISDIR(stat.st_mode))
P
prife 已提交
698 699 700 701 702 703 704 705 706 707 708
            {
                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);
        }
709 710
    }
    while (length > 0);
P
prife 已提交
711

712
    dfs_file_close(&cpfd);
P
prife 已提交
713 714 715 716
}

static const char *_get_path_lastname(const char *path)
{
717
    char *ptr;
718
    if ((ptr = (char *)strrchr(path, '/')) == NULL)
P
prife 已提交
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
        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;
B
bernard 已提交
737
    uint32_t flag = 0;
P
prife 已提交
738 739 740 741 742 743 744

    /* check the staus of src and dst */
    if (dfs_file_stat(src, &stat) < 0)
    {
        rt_kprintf("copy failed, bad %s\n", src);
        return;
    }
B
bernard 已提交
745
    if (S_ISDIR(stat.st_mode))
P
prife 已提交
746 747 748 749 750 751 752 753 754 755
        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 已提交
756
        if (S_ISDIR(stat.st_mode))
P
prife 已提交
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
            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)
        {
774
            char *fdst;
P
prife 已提交
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
            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)
        {
793
            char *fdst;
P
prife 已提交
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
            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)
816 817 818 819

#endif
/* @} */