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

  1. /*
  2.  * bitmaps.c --- routines to read, write, and manipulate the inode and
  3.  * block bitmaps.
  4.  *
  5.  * Copyright (C) 1993 Theodore Ts'o.  This file may be redistributed
  6.  * under the terms of the GNU Public License.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <stdlib.h>
  13. #include <fcntl.h>
  14. #include <time.h>
  15. #include <sys/stat.h>
  16. #include <errno.h>
  17. #include <sys/types.h>
  18.  
  19. #include <linux/ext2_fs.h>
  20.  
  21. #include "ext2fs.h"
  22.  
  23. errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
  24.                        const char *descr,
  25.                        ext2fs_inode_bitmap *ret)
  26. {
  27.     ext2fs_inode_bitmap bitmap;
  28.     int    size;
  29.  
  30.     EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  31.  
  32.     fs->write_bitmaps = ext2fs_write_bitmaps;
  33.  
  34.     bitmap = malloc(sizeof(struct ext2fs_struct_inode_bitmap));
  35.     if (!bitmap)
  36.         return ENOMEM;
  37.  
  38.     bitmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
  39.     bitmap->fs = fs;
  40.     bitmap->start = 1;
  41.     bitmap->end = fs->super->s_inodes_count;
  42.     bitmap->real_end = (EXT2_INODES_PER_GROUP(fs->super)
  43.                 * fs->group_desc_count);
  44.     if (descr) {
  45.         bitmap->description = malloc(strlen(descr)+1);
  46.         if (!bitmap->description) {
  47.             free(bitmap);
  48.             return ENOMEM;
  49.         }
  50.         strcpy(bitmap->description, descr);
  51.     } else
  52.         bitmap->description = 0;
  53.  
  54.     size = ((bitmap->real_end - bitmap->start) / 8) + 1;
  55.     bitmap->bitmap = malloc(size);
  56.     if (!bitmap->bitmap) {
  57.         free(bitmap->description);
  58.         free(bitmap);
  59.         return ENOMEM;
  60.     }
  61.  
  62.     memset(bitmap->bitmap, 0, size);
  63.     *ret = bitmap;
  64.     return 0;
  65. }
  66.  
  67. errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
  68.                        const char *descr,
  69.                        ext2fs_block_bitmap *ret)
  70. {
  71.     ext2fs_block_bitmap bitmap;
  72.     int    size;
  73.  
  74.     EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  75.  
  76.     fs->write_bitmaps = ext2fs_write_bitmaps;
  77.  
  78.     bitmap = malloc(sizeof(struct ext2fs_struct_inode_bitmap));
  79.     if (!bitmap)
  80.         return ENOMEM;
  81.  
  82.     bitmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
  83.     bitmap->fs = fs;
  84.     bitmap->start = fs->super->s_first_data_block;
  85.     bitmap->end = fs->super->s_blocks_count-1;
  86.     bitmap->real_end = (EXT2_BLOCKS_PER_GROUP(fs->super) 
  87.                 * fs->group_desc_count)-1 + bitmap->start;
  88.     if (descr) {
  89.         bitmap->description = malloc(strlen(descr)+1);
  90.         if (!bitmap->description) {
  91.             free(bitmap);
  92.             return ENOMEM;
  93.         }
  94.         strcpy(bitmap->description, descr);
  95.     } else
  96.         bitmap->description = 0;
  97.  
  98.     size = ((bitmap->real_end - bitmap->start) / 8) + 1;
  99.     bitmap->bitmap = malloc(size);
  100.     if (!bitmap->bitmap) {
  101.         free(bitmap->description);
  102.         free(bitmap);
  103.         return ENOMEM;
  104.     }
  105.  
  106.     memset(bitmap->bitmap, 0, size);
  107.     *ret = bitmap;
  108.     return 0;
  109. }
  110.  
  111. errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
  112.                     ino_t end, ino_t *oend)
  113. {
  114.     EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP);
  115.     
  116.     if (end > bitmap->real_end)
  117.         return EXT2_ET_FUDGE_INODE_BITMAP_END;
  118.     if (oend)
  119.         *oend = bitmap->end;
  120.     bitmap->end = end;
  121.     return 0;
  122. }
  123.  
  124. errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
  125.                     blk_t end, blk_t *oend)
  126. {
  127.     EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
  128.     
  129.     if (end > bitmap->real_end)
  130.         return EXT2_ET_FUDGE_BLOCK_BITMAP_END;
  131.     if (oend)
  132.         *oend = bitmap->end;
  133.     bitmap->end = end;
  134.     return 0;
  135. }
  136.  
  137. void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)
  138. {
  139.     if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
  140.         return;
  141.  
  142.     memset(bitmap->bitmap, 0,
  143.            ((bitmap->real_end - bitmap->start) / 8) + 1);
  144. }
  145.  
  146. void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)
  147. {
  148.     if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
  149.         return;
  150.  
  151.     memset(bitmap->bitmap, 0,
  152.            ((bitmap->real_end - bitmap->start) / 8) + 1);
  153. }
  154.  
  155. void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap)
  156. {
  157.     if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
  158.         return;
  159.  
  160.     bitmap->magic = 0;
  161.     if (bitmap->description) {
  162.         free(bitmap->description);
  163.         bitmap->description = 0;
  164.     }
  165.     if (bitmap->bitmap) {
  166.         free(bitmap->bitmap);
  167.         bitmap->bitmap = 0;
  168.     }
  169.     free(bitmap);
  170. }
  171.  
  172. void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap)
  173. {
  174.     if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
  175.         return;
  176.  
  177.     bitmap->magic = 0;
  178.     if (bitmap->description) {
  179.         free(bitmap->description);
  180.         bitmap->description = 0;
  181.     }
  182.     if (bitmap->bitmap) {
  183.         free(bitmap->bitmap);
  184.         bitmap->bitmap = 0;
  185.     }
  186.     free(bitmap);
  187. }
  188.  
  189.