LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1465|回复: 11

能找到cp的源代码吗?

[复制链接]
发表于 2004-2-11 22:46:09 | 显示全部楼层 |阅读模式
能在内核中找到实现拷贝命令cp的c语言源代码吗?能的话告诉我在linux中哪个文件夹,谢谢啦
发表于 2004-2-11 22:49:15 | 显示全部楼层
在lfs.linuxsir.cn查看。。
 楼主| 发表于 2004-2-12 09:16:14 | 显示全部楼层
搜了cp没有搜到……内核中没有源代码吗?难道只有已经编译的了吗?
哪位大侠能不能帮我找一下呀?
发表于 2004-2-12 09:57:31 | 显示全部楼层
最初由 wuqingabc 发表
搜了cp没有搜到……内核中没有源代码吗?难道只有已经编译的了吗?
哪位大侠能不能帮我找一下呀?
看cp来自哪个软件包。。ok
 楼主| 发表于 2004-2-12 10:59:16 | 显示全部楼层
我是新手,能不能具体说说呀,谢谢你啦版主
发表于 2004-2-12 11:10:37 | 显示全部楼层
发表于 2004-2-12 11:43:37 | 显示全部楼层
man cp

fileutils-4.1
发表于 2004-2-12 11:48:59 | 显示全部楼层
http://freebsd.ntu.edu.tw/bsd/5/19/53.html
  1. ◇ [hack] File system

  2. 發信人: [email]Thinker@freebsd.ee.ntu.edu.tw[/email] (Thinker), 看板: BSD
  3. 標  題: [hack] File system
  4. 發信站: ?牧汩_始 (Thu Feb  5 20:08:49 1998)
  5. 轉信站: Maxwell!bbs.ee.ntu!freebsd.ntu!fromzero

  6. TITLE: File system

  7. KEYWORDS: [file] [vnode] [mount] [file system] [fileops] [vfsops]
  8.           [vnodeop_desc] [vnodeopv_entry_desc] [vnodeopv_desc]
  9.           [descriptor table]

  10. ABSTRACT:
  11.         本文討論關於 file system 部分, 就紅皮書未說明清楚的部分
  12.         加以補?.  紅皮書是研究 4.4BSD 者不可或缺的指引書籍, 但
  13.         ?際 implement 的一些細?, 在紅皮書裏?K沒有清楚交代.
  14.         因此在?際 trace source 時, 會有一些失落的環?不易了解.
  15.         本文就這些環?進行補?, 以便利新進之 hacker 們, 能在紅
  16.         皮書和程式碼之間做個連結.


  17. * 紅皮書: The Design and Implementation of the 4.4BSD Operating System,
  18.            Addison Wesley.


  19. file:
  20.         process 是透過 file descriptor access 檔案, 其過程為

  21.                 * 詳見紅皮書 section 6.4 & fingure 6.4

  22.         其中 file table 是 kernel global variable, 不屬於
  23.         任何一個 process.  4.4BSD 提供檔案系統和多種的 IPC
  24.         系統, file 則是做為這些不同系統的共同介面.  process 只要
  25.         透過 file 介面, 就可以使用相的方法和程式碼處理檔案, socket
  26.         和 pipe, 三種不同的系統.  file 是一個物件化的介面, 提供
  27.         一個 generic 的 structure 和 functions.  這些 function
  28.         被稱為 operator, 以 pointer 的形式記錄在 operator table 裏
  29.         , file.f_ops (type: &quotfileops *") 則指向 operator table.  檔案
  30.         系統和 socket 和 pipe 系統提供各自的 operator table, 再由
  31.         file.f_ops 選擇合適的 table.  如: socket 的 file.f_ops 則
  32.         指向 socket 的 operator table.
  33. +-
  34.    file         system
  35.    ------      -------------------------
  36.    *f_ops       pipeops (pipe)
  37.                 socketops (socket)
  38.                 vnops (vfs)
  39.    *f_data      vnode (vfs)
  40.                 socket (socket)
  41.                 pipe (pipe)
  42.    * pipeops, socketops, vnops 是 global variables, 分別屬於
  43.       pipe, socket, vfs 等三個 domain.  這三個 domain 都是透過
  44.       file 介面存取, 而不同的 domain 對 file 所定義取介面有不
  45.       同的解譯和處理.  依 domain 的不同, f_ops (type 是 fileops*)
  46.       指向不同 domain 所定義的 operator list.  kernel 透過
  47.       operator list, 以 indirect function 方式, 呼叫對映功能的
  48.       function.  (vfs: virtual file system)
  49.    * f_data 依 domain 的不同而指向不同的 structure.  在 vfs
  50.       domain 是 vnode, 在 socket domain 則是 socket 結構, 在
  51.       pipe domain 裏, 則是 pipe 結構.  kernel 使用這些 sturcture
  52.       紀錄一些相關資?.
  53. +-

  54. Vnode:
  55.         在 BSD 提供/支援多種不同的 file system, 如 ufs, nfs, procfs
  56.         ... etc, kernel 使用 vfs 統合這些不同的 file system, 提供物
  57.         件化的介面, 隱藏 file system 之間的差?.  vnode 在 vfs 裏扮
  58.         演著一個重要的角色. * 詳見紅皮書 Sectin 6.5

  59.         vnode 裏 member, v_op 的 type 為 "(vop_t **)", vop_t 的定義:
  60. +-
  61.                 typedef int     vop_t __P((void *));
  62. +-
  63.         這定義了一個標準的介面.  v_op 指向一個己定義好的 list, list
  64.         中每一個 element 都指向代表特定功能的 function (list 中每個
  65.         位置所代表的意義都已預定了), 這些 function 稱之為 operator.
  66.         每個 file system 都提供自己的 operator, 所自己的 list.  不同
  67.         file system, 則其 vnode 內的 v_op 指向 file system 所提供的
  68.         operator's list.  operator 的第一個也是唯一的一個 argument
  69.         的 type 為 "(void *)", 這代表可以傳入任何 type 的 ptr.  事?
  70.         上, 代表特定意義的 operator, 其 argument 的 type 有特定的
  71.         type.  如, VOP_CREATE 的 argument 的 type:
  72. +-
  73.                 struct vop_create_args {
  74.                     struct vnodeop_desc *a_desc;
  75.                     struct vnode *a_dvp;
  76.                     struct vnode **a_vpp;
  77.                     struct componentname *a_cnp;
  78.                     struct vattr *a_vap;
  79.                 };
  80. +-
  81.         不論任何一種 file system, 代表 VOP_CREATE 這個功能的
  82.         operator, 它的 argument 都為這個 type.  vfs 的 vnops 即是?
  83.         file 的 f_data 取得 vnode, 再透過 vnode 呼叫這些 operator, 完
  84.         成其功能.
  85. +-
  86.                 -> file -> vnode -> operator list -> operator
  87. +-

  88. Mount:
  89.         在 UNIX 系統中, 任何 file system 都必需事先 mount 在 mount point
  90.         才能被系統使用.  4.4BSD 在 kernel 裏以 struct mount 表示所有的
  91.         mount point. * 詳見紅皮書 section 6.5 和 Fingure 6.7.

  92.         mount.mnt_op 其 type 為 (struct vfsops *), 定義一些針對整個 file
  93.         system 而非個別檔案的 operator, 如 vfs_mount, vfs_unmout, vfs_sync
  94.         ... etc.

  95. 初始化:
  96.         vfs 的 initial code 是 kern/vfs_init.c 裏的 vfsubut(),
  97.         file system 的 root 則是由 kern/vfs_conf.c 裏的 vfs_mountroot().


  98. 附 1:
  99. +-
  100. /*
  101. * Kernel descriptor table.
  102. * One entry for each open kernel vnode and socket.
  103. */
  104. struct file {
  105.     LIST_ENTRY(file) f_list;/* list of active files */
  106.     short   f_flag;     /* see fcntl.h */
  107. #define DTYPE_VNODE 1   /* file */
  108. #define DTYPE_SOCKET    2   /* communications endpoint */
  109. #define DTYPE_PIPE  3   /* pipe */
  110. #define DTYPE_FIFO  4   /* fifo (named pipe) */
  111.     short   f_type;     /* descriptor type */
  112.     short   f_count;    /* reference count */
  113.     short   f_msgcount; /* references from message queue */
  114.     struct  ucred *f_cred;  /* credentials associated with descriptor */
  115.     struct  fileops {
  116.         int (*fo_read)  __P((struct file *fp, struct uio *uio,
  117.                         struct ucred *cred));
  118.         int (*fo_write) __P((struct file *fp, struct uio *uio,
  119.                         struct ucred *cred));
  120.         int (*fo_ioctl) __P((struct file *fp, int com,
  121.                         caddr_t data, struct proc *p));
  122.         int (*fo_select)    __P((struct file *fp, int which,
  123.                         struct proc *p));
  124.         int (*fo_close) __P((struct file *fp, struct proc *p));
  125.     } *f_ops;
  126.     off_t   f_offset;
  127.     caddr_t f_data;     /* vnode or socket */
  128. };


  129. /*
  130. * Structure per mounted file system.  Each mounted file system has an
  131. * array of operations and an instance record.  The file systems are
  132. * put on a doubly linked list.
  133. */
  134. struct mount {
  135.     CIRCLEQ_ENTRY(mount) mnt_list;      /* mount list */
  136.     struct vfsops   *mnt_op;        /* operations on fs */
  137.     struct vfsconf  *mnt_vfc;       /* configuration info */
  138.     struct vnode    *mnt_vnodecovered;  /* vnode we mounted on */
  139.     struct vnodelst mnt_vnodelist;      /* list of vnodes this mount */
  140.     int     mnt_flag;       /* flags */
  141.     int     mnt_maxsymlinklen;  /* max size of short symlink */
  142.     struct statfs   mnt_stat;       /* cache of filesystem stats */
  143.     qaddr_t     mnt_data;       /* private data */
  144. /*  struct vfsconf  *mnt_vfc; */        /* configuration info */
  145.     time_t      mnt_time;       /* last time written*/
  146. };


  147. /*
  148. * used to get configured filesystems information
  149. */
  150. #define VFS_MAXNAMELEN 32
  151. struct vfsconf {
  152.     void *vfc_vfsops;
  153.     char vfc_name[VFS_MAXNAMELEN];
  154.     int vfc_index;
  155.     int vfc_refcount;
  156.     int vfc_flags;
  157. };


  158. struct vfsops {
  159.     int (*vfs_mount)    __P((struct mount *mp, char *path, caddr_t data,
  160.                     struct nameidata *ndp, struct proc *p));
  161.     int (*vfs_start)    __P((struct mount *mp, int flags,
  162.                     struct proc *p));
  163.     int (*vfs_unmount)  __P((struct mount *mp, int mntflags,
  164.                     struct proc *p));
  165.     int (*vfs_root) __P((struct mount *mp, struct vnode **vpp));
  166.     int (*vfs_quotactl) __P((struct mount *mp, int cmds, uid_t uid,
  167.                     caddr_t arg, struct proc *p));
  168.     int (*vfs_statfs)   __P((struct mount *mp, struct statfs *sbp,
  169.                     struct proc *p));
  170.     int (*vfs_sync) __P((struct mount *mp, int waitfor,
  171.                     struct ucred *cred, struct proc *p));
  172.     int (*vfs_vget) __P((struct mount *mp, ino_t ino,
  173.                     struct vnode **vpp));
  174.     int (*vfs_fhtovp)   __P((struct mount *mp, struct fid *fhp,
  175.                     struct mbuf *nam, struct vnode **vpp,
  176.                     int *exflagsp, struct ucred **credanonp));
  177.     int (*vfs_vptofh)   __P((struct vnode *vp, struct fid *fhp));
  178.     int (*vfs_init) __P((void));
  179. };


  180. struct vnode {
  181.     u_long  v_flag;             /* vnode flags (see below) */
  182.     int v_writecount;           /* reference count of writers */
  183.     int v_holdcnt;          /* page & buffer references */
  184.     daddr_t v_lastr;            /* last read (read-ahead) */
  185.     u_long  v_id;               /* capability identifier */
  186.     struct  mount *v_mount;         /* ptr to vfs we are in */
  187.     vop_t   **v_op;             /* vnode operations vector */
  188.     TAILQ_ENTRY(vnode) v_freelist;      /* vnode freelist */
  189.     LIST_ENTRY(vnode) v_mntvnodes;      /* vnodes for mount point */
  190.     struct  buflists v_cleanblkhd;      /* clean blocklist head */
  191.     struct  buflists v_dirtyblkhd;      /* dirty blocklist head */
  192.     long    v_numoutput;            /* num of writes in progress */
  193.     enum    vtype v_type;           /* vnode type */
  194.     union {
  195.         struct mount    *vu_mountedhere;/* ptr to mounted vfs (VDIR) */
  196.         struct socket   *vu_socket; /* unix ipc (VSOCK) */
  197.         struct specinfo *vu_specinfo;   /* device (VCHR, VBLK) */
  198.         struct fifoinfo *vu_fifoinfo;   /* fifo (VFIFO) */
  199.         struct fifoinfo *vu_fifoinfo;   /* fifo (VFIFO) */
  200.     } v_un;
  201.     struct  nqlease *v_lease;       /* Soft reference to lease */
  202.     daddr_t v_lastw;            /* last write (write cluster) */
  203.     daddr_t v_cstart;           /* start block of cluster */
  204.     daddr_t v_lasta;            /* last allocation */
  205.     int v_clen;             /* length of current cluster */
  206.     int v_ralen;            /* Read-ahead length */
  207.     int v_usage;            /* Vnode usage counter */
  208.     daddr_t v_maxra;            /* last readahead block */
  209.     struct vm_object *v_object;     /* Place to store VM object */
  210.     enum    vtagtype v_tag;         /* type of underlying data */
  211.     void    *v_data;            /* private data for fs */
  212. };


  213. struct vnodeop_desc {
  214.     int vdesc_offset;       /* offset in vector--first for speed */
  215.     char    *vdesc_name;        /* a readable name for debugging */
  216.     int vdesc_flags;        /* VDESC_* flags */

  217.     /*
  218.      * These ops are used by bypass routines to map and locate arguments.
  219.      * Creds and procs are not needed in bypass routines, but sometimes
  220.      * they are useful to (for example) transport layers.
  221.      * Nameidata is useful because it has a cred in it.
  222.      */
  223.     int *vdesc_vp_offsets;  /* list ended by VDESC_NO_OFFSET */
  224.     int vdesc_vpp_offset;   /* return vpp location */
  225.     int vdesc_cred_offset;  /* cred location, if any */
  226.     int vdesc_proc_offset;  /* proc location, if any */
  227.     int vdesc_componentname_offset; /* if any */
  228.     /*
  229.      * Finally, we've got a list of private data (about each operation)
  230.      * for each transport layer.  (Support to manage this list is not
  231.      * yet part of BSD.)
  232.      */
  233.     caddr_t *vdesc_transports;
  234. };


  235. /*
  236. * This structure is used to configure the new vnodeops vector.
  237. */
  238. struct vnodeopv_entry_desc {
  239.     struct vnodeop_desc *opve_op;   /* which operation this is */
  240.     vop_t *opve_impl;       /* code implementing this operation */
  241. };
  242. struct vnodeopv_desc {
  243.             /* ptr to the ptr to the vector where op should go */
  244.     vop_t ***opv_desc_vector_p;
  245.     struct vnodeopv_entry_desc *opv_desc_ops;   /* null terminated list */
  246. };
  247. +-

  248. -----------------------------------------------------------------------------
  249.           沒事請叫我 Thinker, thx!              Thinker
  250.                                                 [email]Thinker.bbs@yzit.edu.tw[/email]
  251.                                                 [email]Thinker@freebsd.ee.ntu.edu.tw[/email]
  252. Origin: ?牧汩_始 freebsd.ee.ntu.edu.tw (140.112.19.123)
复制代码
发表于 2004-2-12 12:00:11 | 显示全部楼层
发表于 2004-2-12 12:05:39 | 显示全部楼层
  1. /*

  2. Simple file copy program using sendfile(2).

  3. usage: fastcp <source> <destination>

  4. Copyright (C) 2003 Jeff Tranter.


  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.

  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.

  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

  16. */


  17. #include <unistd.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include <fcntl.h>
  22. #include <stdlib.h>
  23. #include <sys/sendfile.h>
  24. #include <sys/stat.h>

  25. int main (int argc, char** argv)
  26. {
  27.   int src;               /* file descriptor for source file */
  28.   int dest;              /* file descriptor for destination file */
  29.   struct stat stat_buf;  /* hold information about input file */
  30.   off_t offset = 0;      /* byte offset used by sendfile */
  31.   int rc;                /* return code from sendfile */

  32.   /* check for two command line arguments */
  33.   if (argc != 3) {
  34.     fprintf(stderr, "usage: %s <source> <destination>\n", argv[0]);
  35.     exit(1);
  36.   }

  37.   /* check that source file exists and can be opened */
  38.   src = open(argv[1], O_RDONLY);
  39.   if (src == -1) {
  40.     fprintf(stderr, "unable to open '%s': %s\n", argv[1], strerror(errno));
  41.     exit(1);
  42.   }

  43. /* get size and permissions of the source file */
  44.   fstat(src, &stat_buf);

  45. /* open destination file */
  46.   dest = open(argv[2], O_WRONLY|O_CREAT, stat_buf.st_mode);
  47.   if (dest == -1) {
  48.     fprintf(stderr, "unable to open '%s': %s\n", argv[2], strerror(errno));
  49.     exit(1);
  50.   }

  51. /* copy file using sendfile */
  52. rc = sendfile (dest, src, &offset, stat_buf.st_size);
  53. if (rc == -1) {
  54.     fprintf(stderr, "error from sendfile: %s\n", strerror(errno));
  55.     exit(1);
  56. }
  57. if (rc != stat_buf.st_size) {
  58.    fprintf(stderr, "incomplete transfer from sendfile: %d of %d bytes\n",
  59.            rc,
  60.            (int)stat_buf.st_size);
  61.    exit(1);
  62. }

  63. /* clean up and exit */
  64. close(dest);
  65. close(src);

  66. return 0;
  67. }
复制代码


http://www.linuxgazette.com/issue91/misc/tranter/fastcp.c.txt
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表