home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / win95 / ext2tool.exe / EXT2FS / CLOSEFS.C < prev    next >
C/C++ Source or Header  |  1995-05-10  |  3KB  |  107 lines

  1. /*
  2.  * closefs.c --- close an ext2 filesystem
  3.  * 
  4.  * Copyright (C) 1993, 1994 Theodore Ts'o.  This file may be redistributed
  5.  * under the terms of the GNU Public License.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <errno.h>
  12. #include <time.h>
  13.  
  14. #include <linux/ext2_fs.h>
  15.  
  16. #include "ext2fs.h"
  17.  
  18. errcode_t ext2fs_flush(ext2_filsys fs)
  19. {
  20.     int        i,j;
  21.     int        group_block;
  22.     errcode_t    retval;
  23.     char        *group_ptr;
  24.     unsigned long    fs_state;
  25.     
  26.     EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  27.  
  28.     /*
  29.      * Write out master superblock.  This has to be done
  30.      * separately, since it is located at a fixed location
  31.      * (SUPERBLOCK_OFFSET).
  32.      */
  33.     fs->super->s_wtime = time(NULL);
  34.     io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
  35.     retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE, fs->super);
  36.     if (retval)
  37.         return retval;
  38.     io_channel_set_blksize(fs->io, fs->blocksize);
  39.  
  40.     /*
  41.      * Save the state of the FS and set it to non valid for the
  42.      * backup superblocks
  43.      */
  44.     fs_state = fs->super->s_state;
  45.     fs->super->s_state &= ~EXT2_VALID_FS;
  46.  
  47.     /*
  48.      * Write out the master group descriptors, and the backup
  49.      * superblocks and group descriptors.
  50.      */
  51.     group_block = fs->super->s_first_data_block;
  52.     for (i = 0; i < fs->group_desc_count; i++) {
  53.         if (i !=0 ) {
  54.             retval = io_channel_write_blk(fs->io, group_block,
  55.                               -SUPERBLOCK_SIZE,
  56.                               fs->super);
  57.             if (retval) {
  58.                 fs->super->s_state = fs_state;
  59.                 return retval;
  60.             }
  61.         }
  62.         group_ptr = (char *) fs->group_desc;
  63.         for (j=0; j < fs->desc_blocks; j++) {
  64.             retval = io_channel_write_blk(fs->io,
  65.                               group_block+1+j, 1,
  66.                               group_ptr);
  67.             if (retval) {
  68.                 fs->super->s_state = fs_state;
  69.                 return retval;
  70.             }
  71.             group_ptr += fs->blocksize;
  72.         }
  73.         group_block += EXT2_BLOCKS_PER_GROUP(fs->super);
  74.     }
  75.  
  76.     fs->super->s_state = fs_state;
  77.  
  78.     /*
  79.      * If the write_bitmaps() function is present, call it to
  80.      * flush the bitmaps.  This is done this way so that a simple
  81.      * program that doesn't mess with the bitmaps doesn't need to
  82.      * drag in the bitmaps.c code.
  83.      */
  84.     if (fs->write_bitmaps) {
  85.         retval = fs->write_bitmaps(fs);
  86.         if (retval)
  87.             return retval;
  88.     }
  89.         
  90.     return 0;
  91. }
  92.  
  93. errcode_t ext2fs_close(ext2_filsys fs)
  94. {
  95.     errcode_t    retval;
  96.     
  97.     EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  98.  
  99.     if (fs->flags & EXT2_FLAG_DIRTY) {
  100.         retval = ext2fs_flush(fs);
  101.         if (retval)
  102.             return retval;
  103.     }
  104.     ext2fs_free(fs);
  105.     return 0;
  106. }
  107.