dfs_file.c 19.3 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
#include <dfs.h>
#include <dfs_file.h>
B
bernard 已提交
29
#include <dfs_private.h>
30 31 32 33

/**
 * @addtogroup FileApi
 */
34

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

/**
 * this function will open a file which specified by path with specified flags.
 *
 * @param fd the file descriptor pointer to return the corresponding result.
41
 * @param path the specified file path.
42 43 44 45
 * @param flags the flags for open operator.
 *
 * @return 0 on successful, -1 on failed.
 */
46
int dfs_file_open(struct dfs_fd *fd, const char *path, int flags)
47
{
48 49 50 51 52
    struct dfs_filesystem *fs;
    char *fullpath;
    int result;

    /* parameter check */
B
bernard 已提交
53 54
    if (fd == NULL)
        return -EINVAL;
55 56

    /* make sure we have an absolute path */
B
bernard 已提交
57 58
    fullpath = dfs_normalize_path(NULL, path);
    if (fullpath == NULL)
59
    {
B
bernard 已提交
60
        return -ENOMEM;
61 62
    }

B
bernard 已提交
63 64 65 66 67 68 69 70 71
    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;
    }
72 73 74

    /* find filesystem */
    fs = dfs_filesystem_lookup(fullpath);
B
bernard 已提交
75
    if (fs == NULL)
76 77 78
    {
        rt_free(fullpath); /* release path */

B
bernard 已提交
79
        return -ENOENT;
80 81
    }

B
bernard 已提交
82 83
    dbg_log(DBG_LOG, "open in filesystem:%s\n", fs->ops->name);
    fd->fops  = fs->ops->fops; /* set file ops */
84 85 86 87 88 89

    /* initialize the fd item */
    fd->type  = FT_REGULAR;
    fd->flags = flags;
    fd->size  = 0;
    fd->pos   = 0;
B
bernard 已提交
90
    fd->data  = fs;
91 92 93

    if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
    {
B
bernard 已提交
94
        if (dfs_subdir(fs->path, fullpath) == NULL)
95 96 97 98
            fd->path = rt_strdup("/");
        else
            fd->path = rt_strdup(dfs_subdir(fs->path, fullpath));
        rt_free(fullpath);
B
bernard 已提交
99
        dbg_log(DBG_LOG, "Actual file path: %s\n", fd->path);
100 101 102 103 104 105 106
    }
    else
    {
        fd->path = fullpath;
    }

    /* specific file system open routine */
B
bernard 已提交
107
    if (fd->fops->open == NULL)
108 109 110
    {
        /* clear fd */
        rt_free(fd->path);
B
bernard 已提交
111
        fd->path = NULL;
112

B
bernard 已提交
113
        return -ENOSYS;
114 115
    }

B
bernard 已提交
116
    if ((result = fd->fops->open(fd)) < 0)
117 118 119
    {
        /* clear fd */
        rt_free(fd->path);
B
bernard 已提交
120
        fd->path = NULL;
121

B
bernard 已提交
122
        dbg_log(DBG_ERROR, "open failed\n");
123 124 125 126 127

        return result;
    }

    fd->flags |= DFS_F_OPEN;
B
bernard 已提交
128
    if (flags & O_DIRECTORY)
129 130 131 132 133
    {
        fd->type = FT_DIRECTORY;
        fd->flags |= DFS_F_DIRECTORY;
    }

B
bernard 已提交
134
    dbg_log(DBG_INFO, "open successful\n");
135
    return 0;
136 137 138 139 140 141 142 143 144
}

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

B
bernard 已提交
149 150
    if (fd == NULL)
        return -ENXIO;
151

B
bernard 已提交
152 153
    if (fd->fops->close != NULL)
        result = fd->fops->close(fd);
154

155 156 157
    /* close fd error, return */
    if (result < 0)
        return result;
158

159
    rt_free(fd->path);
B
bernard 已提交
160
    fd->path = NULL;
161

162
    return result;
163 164 165 166 167 168 169 170 171 172 173
}

/**
 * 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.
 */
174
int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args)
175
{
176
    if (fd == NULL)
B
bernard 已提交
177
        return -EINVAL;
178

179 180 181 182 183 184 185 186 187 188 189 190 191
    /* 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 已提交
192
                fd->flags &= ~mask;
193 194 195 196 197 198
                fd->flags |= flags;
            }
            return 0;
        }
    }

B
bernard 已提交
199 200
    if (fd->fops->ioctl != NULL)
        return fd->fops->ioctl(fd, cmd, args);
201

B
bernard 已提交
202
    return -ENOSYS;
203 204 205
}

/**
206 207
 * this function will read specified length data from a file descriptor to a
 * buffer.
208 209 210 211 212 213 214
 *
 * @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 已提交
215
int dfs_file_read(struct dfs_fd *fd, void *buf, size_t len)
216
{
217
    int result = 0;
218

B
bernard 已提交
219 220
    if (fd == NULL)
        return -EINVAL;
221

B
bernard 已提交
222 223
    if (fd->fops->read == NULL)
        return -ENOSYS;
224

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

228
    return result;
229 230 231 232 233
}

/**
 * this function will fetch directory entries from a directory descriptor.
 *
234
 * @param fd the directory descriptor.
235
 * @param dirp the dirent buffer to save result.
236
 * @param nbytes the available room in the buffer.
237 238 239
 *
 * @return the read dirent, others on failed.
 */
B
bernard 已提交
240
int dfs_file_getdents(struct dfs_fd *fd, struct dirent *dirp, size_t nbytes)
241
{
242
    /* parameter check */
B
bernard 已提交
243 244
    if (fd == NULL || fd->type != FT_DIRECTORY)
        return -EINVAL;
245

B
bernard 已提交
246 247
    if (fd->fops->getdents != NULL)
        return fd->fops->getdents(fd, dirp, nbytes);
248

B
bernard 已提交
249
    return -ENOSYS;
250 251 252 253 254 255 256 257 258 259 260
}

/**
 * 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)
{
261 262 263 264 265
    int result;
    char *fullpath;
    struct dfs_filesystem *fs;

    /* Make sure we have an absolute path */
B
bernard 已提交
266 267
    fullpath = dfs_normalize_path(NULL, path);
    if (fullpath == NULL)
268
    {
B
bernard 已提交
269
        return -EINVAL;
270 271 272
    }

    /* get filesystem */
B
bernard 已提交
273
    if ((fs = dfs_filesystem_lookup(fullpath)) == NULL)
274
    {
B
bernard 已提交
275
        result = -ENOENT;
276 277 278 279 280 281
        goto __exit;
    }

    /* Check whether file is already open */
    if (fd_is_open(fullpath) == 0)
    {
B
bernard 已提交
282
        result = -EBUSY;
283 284 285
        goto __exit;
    }

B
bernard 已提交
286
    if (fs->ops->unlink != NULL)
287 288 289
    {
        if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
        {
B
bernard 已提交
290
            if (dfs_subdir(fs->path, fullpath) == NULL)
291 292 293 294 295
                result = fs->ops->unlink(fs, "/");
            else
                result = fs->ops->unlink(fs, dfs_subdir(fs->path, fullpath));
        }
        else
B
Bernard Xiong 已提交
296
            result = fs->ops->unlink(fs, fullpath);
297
    }
B
bernard 已提交
298
    else result = -ENOSYS;
299 300

__exit:
301 302
    rt_free(fullpath);
    return result;
303 304 305 306 307 308 309 310 311 312 313
}

/**
 * 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 已提交
314
int dfs_file_write(struct dfs_fd *fd, const void *buf, size_t len)
315
{
B
bernard 已提交
316 317
    if (fd == NULL)
        return -EINVAL;
318

B
bernard 已提交
319 320
    if (fd->fops->write == NULL)
        return -ENOSYS;
321

B
bernard 已提交
322
    return fd->fops->write(fd, buf, len);
323 324 325 326 327 328 329 330 331
}

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

B
bernard 已提交
337 338
    if (fd->fops->flush == NULL)
        return -ENOSYS;
339

B
bernard 已提交
340
    return fd->fops->flush(fd);
341 342 343 344 345 346
}

/**
 * this function will seek the offset for specified file descriptor.
 *
 * @param fd the file descriptor.
347
 * @param offset the offset to be sought.
348 349 350
 *
 * @return the current position after seek.
 */
B
bernard 已提交
351
int dfs_file_lseek(struct dfs_fd *fd, off_t offset)
352
{
353
    int result;
354

B
bernard 已提交
355 356
    if (fd == NULL)
        return -EINVAL;
357

B
bernard 已提交
358 359 360 361
    if (fd->fops->lseek == NULL)
        return -ENOSYS;

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

363 364 365
    /* update current position */
    if (result >= 0)
        fd->pos = result;
366

367
    return result;
368 369 370 371 372 373 374 375 376 377 378 379
}

/**
 * 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)
{
380 381 382 383
    int result;
    char *fullpath;
    struct dfs_filesystem *fs;

B
bernard 已提交
384 385
    fullpath = dfs_normalize_path(NULL, path);
    if (fullpath == NULL)
386 387 388 389
    {
        return -1;
    }

B
bernard 已提交
390
    if ((fs = dfs_filesystem_lookup(fullpath)) == NULL)
391
    {
B
bernard 已提交
392 393
        dbg_log(DBG_ERROR,
                "can't find mounted filesystem on this path:%s\n", fullpath);
394 395
        rt_free(fullpath);

B
bernard 已提交
396
        return -ENOENT;
397 398 399
    }

    if ((fullpath[0] == '/' && fullpath[1] == '\0') ||
B
bernard 已提交
400
        (dfs_subdir(fs->path, fullpath) == NULL))
401 402 403 404
    {
        /* it's the root directory */
        buf->st_dev   = 0;

B
bernard 已提交
405 406 407
        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;
408 409 410 411 412 413 414

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

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

B
bernard 已提交
415
        return RT_EOK;
416 417 418
    }
    else
    {
B
bernard 已提交
419
        if (fs->ops->stat == NULL)
420 421
        {
            rt_free(fullpath);
B
bernard 已提交
422 423
            dbg_log(DBG_ERROR,
                    "the filesystem didn't implement this function\n");
424

B
bernard 已提交
425
            return -ENOSYS;
426 427 428 429 430 431 432 433 434 435 436 437
        }

        /* 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;
438 439 440
}

/**
441
 * this function will rename an old path name to a new path name.
442 443 444 445 446 447
 *
 * @param oldpath the old path name.
 * @param newpath the new path name.
 *
 * @return 0 on successful, -1 on failed.
 */
448
int dfs_file_rename(const char *oldpath, const char *newpath)
449
{
450 451 452 453
    int result;
    struct dfs_filesystem *oldfs, *newfs;
    char *oldfullpath, *newfullpath;

B
bernard 已提交
454 455 456
    result = RT_EOK;
    newfullpath = NULL;
    oldfullpath = NULL;
457

B
bernard 已提交
458 459
    oldfullpath = dfs_normalize_path(NULL, oldpath);
    if (oldfullpath == NULL)
460
    {
B
bernard 已提交
461
        result = -ENOENT;
462 463 464
        goto __exit;
    }

B
bernard 已提交
465 466
    newfullpath = dfs_normalize_path(NULL, newpath);
    if (newfullpath == NULL)
467
    {
B
bernard 已提交
468
        result = -ENOENT;
469 470 471 472 473 474 475 476
        goto __exit;
    }

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

    if (oldfs == newfs)
    {
B
bernard 已提交
477
        if (oldfs->ops->rename == NULL)
478
        {
B
bernard 已提交
479
            result = -ENOSYS;
480 481 482 483 484 485 486 487 488 489 490 491 492 493
        }
        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 已提交
494
        result = -EXDEV;
495
    }
496 497

__exit:
498 499
    rt_free(oldfullpath);
    rt_free(newfullpath);
500

501 502
    /* not at same file system, return EXDEV */
    return result;
503 504 505 506 507 508 509
}

#ifdef RT_USING_FINSH
#include <finsh.h>

static struct dfs_fd fd;
static struct dirent dirent;
510
void ls(const char *pathname)
511
{
512 513 514
    struct stat stat;
    int length;
    char *fullpath, *path;
515

B
bernard 已提交
516 517
    fullpath = NULL;
    if (pathname == NULL)
518
    {
519
#ifdef DFS_USING_WORKDIR
520 521
        /* open current working directory */
        path = rt_strdup(working_directory);
522
#else
523
        path = rt_strdup("/");
524
#endif
B
bernard 已提交
525
        if (path == NULL)
526 527 528 529 530 531 532 533
            return ; /* out of memory */
    }
    else
    {
        path = (char *)pathname;
    }

    /* list directory */
B
bernard 已提交
534
    if (dfs_file_open(&fd, path, O_DIRECTORY) == 0)
535 536 537 538
    {
        rt_kprintf("Directory %s:\n", path);
        do
        {
B
bernard 已提交
539
            memset(&dirent, 0, sizeof(struct dirent));
540 541 542
            length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent));
            if (length > 0)
            {
B
bernard 已提交
543
                memset(&stat, 0, sizeof(struct stat));
544 545 546

                /* build full path for each file */
                fullpath = dfs_normalize_path(path, dirent.d_name);
B
bernard 已提交
547
                if (fullpath == NULL)
548 549 550 551 552
                    break;

                if (dfs_file_stat(fullpath, &stat) == 0)
                {
                    rt_kprintf("%-20s", dirent.d_name);
B
bernard 已提交
553
                    if (S_ISDIR(stat.st_mode))
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
                    {
                        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 已提交
574
    if (pathname == NULL)
575
        rt_free(path);
576
}
B
bernard 已提交
577
FINSH_FUNCTION_EXPORT(ls, list directory contents);
578

579
void rm(const char *filename)
580
{
581 582 583 584
    if (dfs_file_unlink(filename) < 0)
    {
        rt_kprintf("Delete %s failed\n", filename);
    }
585
}
B
bernard 已提交
586
FINSH_FUNCTION_EXPORT(rm, remove files or directories);
587

588 589
void cat(const char* filename)
{
B
bernard 已提交
590
    uint32_t length;
591 592
    char buffer[81];

B
bernard 已提交
593
    if (dfs_file_open(&fd, filename, O_RDONLY) < 0)
594 595 596 597 598 599 600 601
    {
        rt_kprintf("Open %s failed\n", filename);

        return;
    }

    do
    {
B
bernard 已提交
602
        memset(buffer, 0, sizeof(buffer));
603 604 605 606 607 608 609 610
        length = dfs_file_read(&fd, buffer, sizeof(buffer)-1 );
        if (length > 0)
        {
            rt_kprintf("%s", buffer);
        }
    }while (length > 0);

    dfs_file_close(&fd);
611
}
B
bernard 已提交
612
FINSH_FUNCTION_EXPORT(cat, print file);
613

614
#define BUF_SZ  4096
P
prife 已提交
615
static void copyfile(const char *src, const char *dst)
616
{
617 618
    struct dfs_fd src_fd;
    rt_uint8_t *block_ptr;
619
    rt_int32_t read_bytes;
620 621

    block_ptr = rt_malloc(BUF_SZ);
B
bernard 已提交
622
    if (block_ptr == NULL)
623 624 625 626 627 628
    {
        rt_kprintf("out of memory\n");

        return;
    }

B
bernard 已提交
629
    if (dfs_file_open(&src_fd, src, O_RDONLY) < 0)
630 631 632 633 634 635
    {
        rt_free(block_ptr);
        rt_kprintf("Read %s failed\n", src);

        return;
    }
B
bernard 已提交
636
    if (dfs_file_open(&fd, dst, O_WRONLY | O_CREAT) < 0)
637 638 639 640 641 642 643 644 645 646 647 648 649 650
    {
        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 已提交
651 652
            int length;

B
bernard 已提交
653
            length = dfs_file_write(&fd, block_ptr, read_bytes);
B
Bernard Xiong 已提交
654 655 656 657 658 659
            if (length != read_bytes)
            {
                /* write failed. */
                rt_kprintf("Write file data failed, errno=%d\n", length);
                break;
            }
660 661 662 663 664 665
        }
    } while (read_bytes > 0);

    dfs_file_close(&src_fd);
    dfs_file_close(&fd);
    rt_free(block_ptr);
666
}
P
prife 已提交
667 668 669 670 671 672 673

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;
674
    struct dfs_fd cpfd;
B
bernard 已提交
675
    if (dfs_file_open(&cpfd, src, O_DIRECTORY) < 0)
P
prife 已提交
676 677 678 679 680 681 682
    {
        rt_kprintf("open %s failed\n", src);
        return ;
    }

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

685
        length = dfs_file_getdents(&cpfd, &dirent, sizeof(struct dirent));
P
prife 已提交
686 687
        if (length > 0)
        {
B
bernard 已提交
688 689
            char * src_entry_full = NULL;
            char * dst_entry_full = NULL;
P
prife 已提交
690 691 692 693 694

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

            /* build full path for each file */
B
bernard 已提交
695
            if ((src_entry_full = dfs_normalize_path(src, dirent.d_name)) == NULL)
P
prife 已提交
696 697 698 699
            {
                rt_kprintf("out of memory!\n");
                break;
            }
B
bernard 已提交
700
            if ((dst_entry_full = dfs_normalize_path(dst, dirent.d_name)) == NULL)
P
prife 已提交
701 702 703 704 705 706
            {
                rt_kprintf("out of memory!\n");
                rt_free(src_entry_full);
                break;
            }

B
bernard 已提交
707
            memset(&stat, 0, sizeof(struct stat));
P
prife 已提交
708 709 710 711 712 713
            if (dfs_file_stat(src_entry_full, &stat) != 0)
            {
                rt_kprintf("open file: %s failed\n", dirent.d_name);
                continue;
            }

B
bernard 已提交
714
            if (S_ISDIR(stat.st_mode))
P
prife 已提交
715 716 717 718 719 720 721 722 723 724 725 726 727
            {
                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);

728
    dfs_file_close(&cpfd);
P
prife 已提交
729 730 731 732 733
}

static const char *_get_path_lastname(const char *path)
{
    char * ptr;
B
bernard 已提交
734
    if ((ptr = strrchr(path, '/')) == NULL)
P
prife 已提交
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
        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 已提交
753
    uint32_t flag = 0;
P
prife 已提交
754 755 756 757 758 759 760

    /* 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 已提交
761
    if (S_ISDIR(stat.st_mode))
P
prife 已提交
762 763 764 765 766 767 768 769 770 771
        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 已提交
772
        if (S_ISDIR(stat.st_mode))
P
prife 已提交
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 818 819 820 821 822 823 824 825 826 827 828 829 830 831
            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)
832 833 834 835

#endif
/* @} */