Chinaunix首页 | 论坛 | 博客
  • 博客访问: 107629
  • 博文数量: 23
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 235
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-19 22:44
文章分类

全部博文(23)

文章存档

2011年(1)

2008年(1)

2007年(21)

我的朋友
最近访客

分类: C/C++

2007-04-30 18:28:00


/*************************************************************************
 * my ftw.h
 * ************************************************************************/


#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#define test
#include "/dvr/debug/my_debug.h"
#include <errno.h>


#define MFF_MODE S_IRWXU | S_IRWXG
#define BUF_SIZE 8192

long long copy_dir(char *src, char *dst);
int copy_file(char *src, char *dst);


extern int errno;

int
main(int argc, char *argv[])
{
    long long size1;
    long long size2;

    size1 = 0;
    size2 = 0;

    if(argc < 2)
        ERRX("argv error");
// printf("%s----%s\n", argv[1], argv[2]);

    if(argc == 2)
        size1 = copy_dir (argv[1], NULL);
    else
        size2 = copy_dir(argv[1], argv[2]);

    printf("All size is %lld kbytes\n", size1);
    return 0;
}




long long
copy_dir(char *src, char *dst)
{
    DIR *dp;
    struct dirent *dirp;
    struct stat stat_src;
    char tmp_dst[NAME_MAX] = { 0 };
    char tmp_src[NAME_MAX] = { 0 };
    long long size_f;


    size_f = 0;

    if((dp = opendir(src)) == NULL)
        ERRX("this dir can not open");

    while((dirp = readdir(dp)) != NULL)
    {
        if(!((strcmp(dirp->d_name, "."))
         && (strcmp(dirp->d_name, ".."))))
            continue;

        memset(tmp_src, 0, NAME_MAX);
        strcat(tmp_src, src);
        if(src[strlen(tmp_src) - 1] != '\/')
            strcat(tmp_src, "\/");
        strcat(tmp_src, dirp->d_name);

        if(dst != NULL)
        {
            memset(tmp_dst, 0, NAME_MAX);
            strcat(tmp_dst, dst);
            if(dst[strlen(tmp_dst) - 1] != '\/')
                strcat(tmp_dst, "\/");
            strcat(tmp_dst, dirp->d_name);
        }


        if(lstat(tmp_src, &stat_src) < 0)
        {
            printf("%d --error file %s\n",errno,tmp_src);
            ERRX("stat error");
        }

        if(S_ISREG(stat_src.st_mode) != 0)
        {
            if(dst != NULL)
                copy_file(tmp_src, tmp_dst);
       // printf("file size is %lld bytes\n", size_f);

            size_f += stat_src.st_size;

        }

        if(S_ISDIR(stat_src.st_mode) != 0)
        {
           // printf("%s\n", src);

            if(dst != NULL)
            {
                mkdir(tmp_dst, S_IRWXU | S_IRWXG);
                printf("mkdir %s\n", tmp_dst);
             size_f += copy_dir(tmp_src, tmp_dst);
            }
            else
                size_f += copy_dir(tmp_src, NULL);
        }
    }
    closedir(dp);
    return size_f;
}

int
copy_file(char *src, char *dst)
{
    char buf[BUF_SIZE];
    int fd_src, fd_dst;

    int size_r;

    printf("Copying%%%s----%s\n", src, dst);
    if((fd_src = open(src, O_RDONLY)) < 0)
        ERRX("open error");
    if((fd_dst = open(dst, O_CREAT | O_WRONLY | O_TRUNC, MFF_MODE)) < 0)
        ERRX("open - create file error");

    memset(buf, 0, BUF_SIZE);
    while(1)
    {
        size_r = read(fd_src, buf, BUF_SIZE);
        if(size_r == -1)
            ERRX("read error");
        if(size_r == 0)
            break;

        if(size_r != write(fd_dst, buf, size_r))
            ERRX("write error");
    }
    close(fd_src);
    close(fd_dst);

    return 0;
}

阅读(1734) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~