home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / win95 / ext2tool.exe / SRC / E2CP.C < prev    next >
C/C++ Source or Header  |  1996-01-09  |  3KB  |  147 lines

  1. /***************************************************************************
  2.  * e2cp.c - Copy an ext2 file
  3.  *
  4.  * Copyright (C) 1995 Claus Tondering, ct@login.dknet.dk
  5.  * This file may be redistributed under the terms of the GNU Public License.
  6.  ***************************************************************************/
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <sys/types.h>
  12.  
  13. #include "ext2_fs.h"
  14. #include "ext2fs/ext2fs.h"
  15. #include "ldisk.h"
  16. #include "istat.h"
  17. #include "e2err.h"
  18.  
  19. extern io_manager msdos_io_manager;
  20.  
  21. char omode[] = "wb";
  22. FILE *ofile;
  23. char *blockbuf;
  24.  
  25. #define MIN(a,b) ((a)<(b) ? (a) : (b))
  26.  
  27. /**********************************************************************
  28.  * usage prints usage information and exits
  29.  **********************************************************************/
  30.  
  31. void
  32. usage()
  33. {
  34.     fprintf(stderr, "usage: e2cp [-bt] source_file destination_file\n");
  35.     exit(1);
  36. }
  37.  
  38.  
  39. /**********************************************************************
  40.  * myproc is called once for every block in the file
  41.  **********************************************************************/
  42.  
  43. static int myproc(ext2_filsys fs,
  44.                   blk_t *blocknr,
  45.                   int blockcnt,
  46.                   void *private)
  47. {
  48.     int err;
  49.     struct ext2_inode *ino = private;
  50.     static nextblock = 0;
  51.  
  52.     if (blockcnt < 0)            /* We're reading an inderect block */
  53.         return 0;
  54.  
  55.     if (nextblock+1 < blockcnt)
  56.         memset(blockbuf, 0, fs->blocksize);
  57.  
  58.     while (nextblock++ < blockcnt)
  59.         if (fwrite(blockbuf, fs->blocksize, 1, ofile) == 0) {
  60.             fprintf(stderr,"Cannot write destination file\n");
  61.             return BLOCK_ABORT;
  62.         }
  63.  
  64.     err = io_channel_read_blk(fs->io, *blocknr, 1, blockbuf);
  65.     if (err) {
  66.         fprintf(stderr,"Cannot read source file\n");
  67.         return BLOCK_ABORT;
  68.     }
  69.  
  70.     if (fwrite(blockbuf, MIN(fs->blocksize, ino->i_size - blockcnt*fs->blocksize), 1, ofile) == 0) {
  71.         fprintf(stderr,"Cannot write destination file\n");
  72.         return BLOCK_ABORT;
  73.     }
  74.     
  75.     return 0;
  76. }
  77.  
  78.  
  79. /**********************************************************************
  80.  * main routine
  81.  **********************************************************************/
  82.  
  83. main(int argc, char **argv)
  84. {
  85.     int err, c;
  86.     char *src, *dst;
  87.     ext2_filsys fs;
  88.     ino_t ino;
  89.     struct ext2_inode e2ino;
  90.  
  91.     opterr = 0;
  92.     while ((c=getopt(argc, argv, "bt")) != -1) {
  93.         switch (c) {
  94.          case 'b': 
  95.          case 't': omode[1] = c; break;
  96.          case '?': usage();
  97.         }
  98.     }
  99.  
  100.     if (argc!=optind+2)
  101.         usage();
  102.  
  103.     src = argv[optind];
  104.     dst = argv[optind+1];
  105.  
  106.  
  107.     /* Open file system */
  108.     err = ext2fs_open(0, 0, 0, 0, msdos_io_manager, &fs);
  109.     if (err)
  110.         e2_err("Cannot open ext2 file system",err);
  111.  
  112.     /* Allocate file buffer */
  113.     blockbuf = malloc(fs->blocksize);
  114.     if (!blockbuf) {
  115.         fprintf(stderr,"Cannot malloc buffer\n");
  116.         exit(1);
  117.     }
  118.  
  119.     /* Lookup specified name */
  120.     err = ext2fs_namei(fs, 2, cwdino, src, &ino);
  121.     if (err)
  122.         e2_err("Cannot find source file",err);
  123.  
  124.     /* Read specified inode */
  125.     err = ext2fs_read_inode(fs, ino, &e2ino);
  126.     if (err)
  127.         e2_err("Cannot read inode information",err);
  128.  
  129.     /* Is it a regular file? */
  130.     if (!S_ISREG(e2ino.i_mode)) {
  131.         fprintf(stderr,"%s is not a regular file\n",src);
  132.         exit(1);
  133.     }
  134.  
  135.     ofile=fopen(dst,omode);
  136.     if (!ofile) {
  137.         fprintf(stderr,"Cannot create %s\n",dst);
  138.         exit(1);
  139.     }
  140.  
  141.     err = ext2fs_block_iterate(fs, ino, 0, 0, myproc, &e2ino);
  142.     if (err)
  143.         e2_err("Cannot read source file",err);
  144.  
  145.     return 0;
  146. }
  147.