dfs_file.c 18.5 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
3
 *
4 5
 * SPDX-License-Identifier: Apache-2.0
 * 
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.
11
 */
12

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

/**
 * @addtogroup FileApi
 */
20

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

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

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

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

B
bernard 已提交
49 50 51 52 53 54 55 56 57
    dbg_log(DBG_LOG, "open file:%s\n", fullpath);

    /* Check whether file is already open */
    if (fd_is_open(fullpath) == 0)
    {
        rt_free(fullpath); /* release path */

        return -EBUSY;
    }
58 59 60

    /* find filesystem */
    fs = dfs_filesystem_lookup(fullpath);
B
bernard 已提交
61
    if (fs == NULL)
62 63 64
    {
        rt_free(fullpath); /* release path */

B
bernard 已提交
65
        return -ENOENT;
66 67
    }

B
bernard 已提交
68 69
    dbg_log(DBG_LOG, "open in filesystem:%s\n", fs->ops->name);
    fd->fops  = fs->ops->fops; /* set file ops */
70 71 72 73 74 75

    /* initialize the fd item */
    fd->type  = FT_REGULAR;
    fd->flags = flags;
    fd->size  = 0;
    fd->pos   = 0;
B
bernard 已提交
76
    fd->data  = fs;
77 78 79

    if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
    {
B
bernard 已提交
80
        if (dfs_subdir(fs->path, fullpath) == NULL)
81 82 83 84
            fd->path = rt_strdup("/");
        else
            fd->path = rt_strdup(dfs_subdir(fs->path, fullpath));
        rt_free(fullpath);
B
bernard 已提交
85
        dbg_log(DBG_LOG, "Actual file path: %s\n", fd->path);
86 87 88 89 90 91 92
    }
    else
    {
        fd->path = fullpath;
    }

    /* specific file system open routine */
B
bernard 已提交
93
    if (fd->fops->open == NULL)
94 95 96
    {
        /* clear fd */
        rt_free(fd->path);
B
bernard 已提交
97
        fd->path = NULL;
98

B
bernard 已提交
99
        return -ENOSYS;
100 101
    }

B
bernard 已提交
102
    if ((result = fd->fops->open(fd)) < 0)
103 104 105
    {
        /* clear fd */
        rt_free(fd->path);
B
bernard 已提交
106
        fd->path = NULL;
107

B
bernard 已提交
108
        dbg_log(DBG_ERROR, "open failed\n");
109 110 111 112 113

        return result;
    }

    fd->flags |= DFS_F_OPEN;
B
bernard 已提交
114
    if (flags & O_DIRECTORY)
115 116 117 118 119
    {
        fd->type = FT_DIRECTORY;
        fd->flags |= DFS_F_DIRECTORY;
    }

B
bernard 已提交
120
    dbg_log(DBG_INFO, "open successful\n");
121
    return 0;
122 123 124 125 126 127 128 129 130
}

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

B
bernard 已提交
135 136
    if (fd == NULL)
        return -ENXIO;
137

B
bernard 已提交
138 139
    if (fd->fops->close != NULL)
        result = fd->fops->close(fd);
140

141 142 143
    /* close fd error, return */
    if (result < 0)
        return result;
144

145
    rt_free(fd->path);
B
bernard 已提交
146
    fd->path = NULL;
147

148
    return result;
149 150 151 152 153 154 155 156 157 158 159
}

/**
 * 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.
 */
160
int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args)
161
{
162
    if (fd == NULL)
B
bernard 已提交
163
        return -EINVAL;
164

165 166 167 168 169 170 171 172 173 174 175 176 177
    /* regular file system fd */
    if (fd->type == FT_REGULAR)
    {
        switch (cmd)
        {
        case F_GETFL:
            return fd->flags; /* return flags */
        case F_SETFL:
            {
                int flags = (int)args;
                int mask  = O_NONBLOCK | O_APPEND;

                flags &= mask;
Z
zylx 已提交
178
                fd->flags &= ~mask;
179 180 181 182 183 184
                fd->flags |= flags;
            }
            return 0;
        }
    }

B
bernard 已提交
185 186
    if (fd->fops->ioctl != NULL)
        return fd->fops->ioctl(fd, cmd, args);
187

B
bernard 已提交
188
    return -ENOSYS;
189 190 191
}

/**
192 193
 * this function will read specified length data from a file descriptor to a
 * buffer.
194 195 196 197 198 199 200
 *
 * @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 已提交
201
int dfs_file_read(struct dfs_fd *fd, void *buf, size_t len)
202
{
203
    int result = 0;
204

B
bernard 已提交
205 206
    if (fd == NULL)
        return -EINVAL;
207

B
bernard 已提交
208 209
    if (fd->fops->read == NULL)
        return -ENOSYS;
210

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

214
    return result;
215 216 217 218 219
}

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

B
bernard 已提交
232 233
    if (fd->fops->getdents != NULL)
        return fd->fops->getdents(fd, dirp, nbytes);
234

B
bernard 已提交
235
    return -ENOSYS;
236 237 238 239 240 241 242 243 244 245 246
}

/**
 * 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)
{
247 248 249 250 251
    int result;
    char *fullpath;
    struct dfs_filesystem *fs;

    /* Make sure we have an absolute path */
B
bernard 已提交
252 253
    fullpath = dfs_normalize_path(NULL, path);
    if (fullpath == NULL)
254
    {
B
bernard 已提交
255
        return -EINVAL;
256 257 258
    }

    /* get filesystem */
B
bernard 已提交
259
    if ((fs = dfs_filesystem_lookup(fullpath)) == NULL)
260
    {
B
bernard 已提交
261
        result = -ENOENT;
262 263 264 265 266 267
        goto __exit;
    }

    /* Check whether file is already open */
    if (fd_is_open(fullpath) == 0)
    {
B
bernard 已提交
268
        result = -EBUSY;
269 270 271
        goto __exit;
    }

B
bernard 已提交
272
    if (fs->ops->unlink != NULL)
273 274 275
    {
        if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
        {
B
bernard 已提交
276
            if (dfs_subdir(fs->path, fullpath) == NULL)
277 278 279 280 281
                result = fs->ops->unlink(fs, "/");
            else
                result = fs->ops->unlink(fs, dfs_subdir(fs->path, fullpath));
        }
        else
B
Bernard Xiong 已提交
282
            result = fs->ops->unlink(fs, fullpath);
283
    }
B
bernard 已提交
284
    else result = -ENOSYS;
285 286

__exit:
287 288
    rt_free(fullpath);
    return result;
289 290 291 292 293 294 295 296 297 298 299
}

/**
 * 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 已提交
300
int dfs_file_write(struct dfs_fd *fd, const void *buf, size_t len)
301
{
B
bernard 已提交
302 303
    if (fd == NULL)
        return -EINVAL;
304

B
bernard 已提交
305 306
    if (fd->fops->write == NULL)
        return -ENOSYS;
307

B
bernard 已提交
308
    return fd->fops->write(fd, buf, len);
309 310 311 312 313 314 315 316 317
}

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

B
bernard 已提交
323 324
    if (fd->fops->flush == NULL)
        return -ENOSYS;
325

B
bernard 已提交
326
    return fd->fops->flush(fd);
327 328 329 330 331 332
}

/**
 * this function will seek the offset for specified file descriptor.
 *
 * @param fd the file descriptor.
333
 * @param offset the offset to be sought.
334 335 336
 *
 * @return the current position after seek.
 */
B
bernard 已提交
337
int dfs_file_lseek(struct dfs_fd *fd, off_t offset)
338
{
339
    int result;
340

B
bernard 已提交
341 342
    if (fd == NULL)
        return -EINVAL;
343

B
bernard 已提交
344 345 346 347
    if (fd->fops->lseek == NULL)
        return -ENOSYS;

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

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

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

/**
 * 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)
{
366 367 368 369
    int result;
    char *fullpath;
    struct dfs_filesystem *fs;

B
bernard 已提交
370 371
    fullpath = dfs_normalize_path(NULL, path);
    if (fullpath == NULL)
372 373 374 375
    {
        return -1;
    }

B
bernard 已提交
376
    if ((fs = dfs_filesystem_lookup(fullpath)) == NULL)
377
    {
B
bernard 已提交
378 379
        dbg_log(DBG_ERROR,
                "can't find mounted filesystem on this path:%s\n", fullpath);
380 381
        rt_free(fullpath);

B
bernard 已提交
382
        return -ENOENT;
383 384 385
    }

    if ((fullpath[0] == '/' && fullpath[1] == '\0') ||
B
bernard 已提交
386
        (dfs_subdir(fs->path, fullpath) == NULL))
387 388 389 390
    {
        /* it's the root directory */
        buf->st_dev   = 0;

B
bernard 已提交
391 392 393
        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;
394 395 396 397 398 399 400

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

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

B
bernard 已提交
401
        return RT_EOK;
402 403 404
    }
    else
    {
B
bernard 已提交
405
        if (fs->ops->stat == NULL)
406 407
        {
            rt_free(fullpath);
B
bernard 已提交
408 409
            dbg_log(DBG_ERROR,
                    "the filesystem didn't implement this function\n");
410

B
bernard 已提交
411
            return -ENOSYS;
412 413 414 415 416 417 418 419 420 421 422 423
        }

        /* 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;
424 425 426
}

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

B
bernard 已提交
440 441 442
    result = RT_EOK;
    newfullpath = NULL;
    oldfullpath = NULL;
443

B
bernard 已提交
444 445
    oldfullpath = dfs_normalize_path(NULL, oldpath);
    if (oldfullpath == NULL)
446
    {
B
bernard 已提交
447
        result = -ENOENT;
448 449 450
        goto __exit;
    }

B
bernard 已提交
451 452
    newfullpath = dfs_normalize_path(NULL, newpath);
    if (newfullpath == NULL)
453
    {
B
bernard 已提交
454
        result = -ENOENT;
455 456 457 458 459 460 461 462
        goto __exit;
    }

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

    if (oldfs == newfs)
    {
B
bernard 已提交
463
        if (oldfs->ops->rename == NULL)
464
        {
B
bernard 已提交
465
            result = -ENOSYS;
466 467 468 469 470 471 472 473 474 475 476 477 478 479
        }
        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 已提交
480
        result = -EXDEV;
481
    }
482 483

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

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

#ifdef RT_USING_FINSH
#include <finsh.h>

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

B
bernard 已提交
502 503
    fullpath = NULL;
    if (pathname == NULL)
504
    {
505
#ifdef DFS_USING_WORKDIR
506 507
        /* open current working directory */
        path = rt_strdup(working_directory);
508
#else
509
        path = rt_strdup("/");
510
#endif
B
bernard 已提交
511
        if (path == NULL)
512 513 514 515 516 517 518 519
            return ; /* out of memory */
    }
    else
    {
        path = (char *)pathname;
    }

    /* list directory */
B
bernard 已提交
520
    if (dfs_file_open(&fd, path, O_DIRECTORY) == 0)
521 522 523 524
    {
        rt_kprintf("Directory %s:\n", path);
        do
        {
B
bernard 已提交
525
            memset(&dirent, 0, sizeof(struct dirent));
526 527 528
            length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent));
            if (length > 0)
            {
B
bernard 已提交
529
                memset(&stat, 0, sizeof(struct stat));
530 531 532

                /* build full path for each file */
                fullpath = dfs_normalize_path(path, dirent.d_name);
B
bernard 已提交
533
                if (fullpath == NULL)
534 535 536 537 538
                    break;

                if (dfs_file_stat(fullpath, &stat) == 0)
                {
                    rt_kprintf("%-20s", dirent.d_name);
B
bernard 已提交
539
                    if (S_ISDIR(stat.st_mode))
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
                    {
                        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 已提交
560
    if (pathname == NULL)
561
        rt_free(path);
562
}
B
bernard 已提交
563
FINSH_FUNCTION_EXPORT(ls, list directory contents);
564

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

574 575
void cat(const char* filename)
{
B
bernard 已提交
576
    uint32_t length;
577 578
    char buffer[81];

B
bernard 已提交
579
    if (dfs_file_open(&fd, filename, O_RDONLY) < 0)
580 581 582 583 584 585 586 587
    {
        rt_kprintf("Open %s failed\n", filename);

        return;
    }

    do
    {
B
bernard 已提交
588
        memset(buffer, 0, sizeof(buffer));
589 590 591 592 593 594 595 596
        length = dfs_file_read(&fd, buffer, sizeof(buffer)-1 );
        if (length > 0)
        {
            rt_kprintf("%s", buffer);
        }
    }while (length > 0);

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

600
#define BUF_SZ  4096
P
prife 已提交
601
static void copyfile(const char *src, const char *dst)
602
{
603 604
    struct dfs_fd src_fd;
    rt_uint8_t *block_ptr;
605
    rt_int32_t read_bytes;
606 607

    block_ptr = rt_malloc(BUF_SZ);
B
bernard 已提交
608
    if (block_ptr == NULL)
609 610 611 612 613 614
    {
        rt_kprintf("out of memory\n");

        return;
    }

B
bernard 已提交
615
    if (dfs_file_open(&src_fd, src, O_RDONLY) < 0)
616 617 618 619 620 621
    {
        rt_free(block_ptr);
        rt_kprintf("Read %s failed\n", src);

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

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

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

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;
660
    struct dfs_fd cpfd;
B
bernard 已提交
661
    if (dfs_file_open(&cpfd, src, O_DIRECTORY) < 0)
P
prife 已提交
662 663 664 665 666 667 668
    {
        rt_kprintf("open %s failed\n", src);
        return ;
    }

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

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

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

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

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

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

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

    /* 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 已提交
747
    if (S_ISDIR(stat.st_mode))
P
prife 已提交
748 749 750 751 752 753 754 755 756 757
        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 已提交
758
        if (S_ISDIR(stat.st_mode))
P
prife 已提交
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
            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
/* @} */