发新话题
打印

【求助】linux

【求助】linux

大家好,冒昧提出一个问题.在linux系统中,如果实现文件的拷贝,使用的命令是cp,我在内核源代码中没有找到这个命令的源代码,请问有哪位高手知道?我在内核源代码的阅读中发现了打开关闭文件以及读写文件的源代码,但是没有说实现文件拷贝的源代码。这个拷贝的主要过程是什么?是不是同时打开两个文件,一个读一个写,这样就完成了文件的拷贝呢? 希望知情人相告,谢谢!      

TOP

extern int cp_main(int argc, char **argv)
{
        struct stat source_stat;
        struct stat dest_stat;
        const char *last;
        const char *dest;
        int s_flags;
        int d_flags;
        int flags;
        int status = 0;

        /* Since these are enums, #if tests will not work.  So use assert()s. */
        assert(FILEUTILS_PRESERVE_STATUS == 1);
        assert(FILEUTILS_DEREFERENCE == 2);
        assert(FILEUTILS_RECUR == 4);
        assert(FILEUTILS_FORCE == 8);
        assert(FILEUTILS_INTERACTIVE == 16);

        flags = bb_getopt_ulflags(argc, argv, cp_opts);

        if (flags & 32) {
                flags |= (FILEUTILS_PRESERVE_STATUS | FILEUTILS_RECUR | FILEUTILS_DEREFERENCE);
        }

        flags ^= FILEUTILS_DEREFERENCE;                /* The sense of this flag was reversed. */

        if (optind + 2 > argc) {
                bb_show_usage();
        }

        last = argv[argc - 1];
        argv += optind;

        /* If there are only two arguments and...  */
        if (optind + 2 == argc) {
                s_flags = cp_mv_stat2(*argv, &source_stat,
                                                                 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
                if ((s_flags < 0) || ((d_flags = cp_mv_stat(last, &dest_stat)) < 0)) {
                        exit(EXIT_FAILURE);
                }
                /* ...if neither is a directory or...  */
                if ( !((s_flags | d_flags) & 2) ||
                        /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */
                        /* ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) */
                        /* Simplify the above since FILEUTILS_RECUR >> 1 == 2. */
                        ((((flags & FILEUTILS_RECUR) >> 1) & s_flags) && !d_flags)
                ) {
                        /* ...do a simple copy.  */
                                dest = last;
                                goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */
                }
        }

        do {
                dest = concat_path_file(last, bb_get_last_path_component(*argv));
        DO_COPY:
                if (copy_file(*argv, dest, flags) < 0) {
                        status = 1;
                }
                if (*++argv == last) {
                        break;
                }
                free((void *) dest);
        } while (1);

        exit(status);
}      

TOP

上面的是我在busybox中复制过来的。      

TOP

在哪个目录下了      

TOP

cp的源码不是在fileutils中吗?如下:



int fd1, fd2, len;
char buf[BUF_SIZ];
fd1 = open("filename1", O_READONLY);
fd2 = open("filename2", O_WRITEONLY);
while ((len = read(fd1, buf, BUF_SIZ)) > 0) {
  write(fd2, buf, len);
  if (len < BUF_SIZ)
    break;
}
close(fd2);
close(fd1);

[/COLOR][/FONT]
有必要那么复杂吗?不过这个帖子跟内核无关,应该转移了。      
时刻警惕小日本!!!

TOP

有没有人告诉我FILEUTILS_PRESERVE_STATUS这几个宏表示什么意思?
我只知道link其他几个不认识!      

TOP

发新话题