root/trunk/client/revimage/image_e2fs.c

Revision 197, 5.8 kB (checked in by ludo, 2 years ago)

new client/server protocol for the UI (from r3948)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
Line 
1 /*
2  * image_e2fs derived from :
3  *
4  * dumpe2fs.c           - List the control structures of a second
5  *                        extended filesystem
6  *
7  * Copyright (C) 1992, 1993, 1994  Remy Card <card@masi.ibp.fr>
8  *                                 Laboratoire MASI, Institut Blaise Pascal
9  *                                 Universite Pierre et Marie Curie (Paris VI)
10  *
11  * Copyright 1995, 1996, 1997 by Theodore Ts'o.
12  *
13  * %Begin-Header%
14  * This file may be redistributed under the terms of the GNU Public
15  * License.
16  * %End-Header%
17  */
18 /*
19  *  Linbox Rescue Server
20  *  Copyright (C) 2002-2005 Linbox FAS, Free & Alter Soft
21  *
22  *  This program is free software; you can redistribute it and/or modify
23  *  it under the terms of the GNU General Public License as published by
24  *  the Free Software Foundation; either version 2 of the License, or
25  *  (at your option) any later version.
26  *
27  *  This program is distributed in the hope that it will be useful,
28  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
29  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  *  GNU General Public License for more details.
31  *
32  *  You should have received a copy of the GNU General Public License
33  *  along with this program; if not, write to the Free Software
34  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35  */
36
37 /*
38  * History:
39  * 94/01/09     - Creation
40  * 94/02/27     - Ported to use the ext2fs library
41  */
42
43 #include "config.h"
44
45 #include <fcntl.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #include "ext2fs/ext2_fs.h"
52 #include "ext2fs/ext2fs.h"
53 #include "e2p/e2p.h"
54
55 #include "compress.h"
56 #include "client.h"
57
58 #define in_use(m, x)    (ext2fs_test_bit ((x), (m)))
59
60 #define _(a) a
61
62 char info1[32], info2[32];
63
64 static void
65 setbit (unsigned char *p, int num)
66 {
67   unsigned char mask[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
68   p[num >> 3] |= mask[num & 7];
69 }
70
71 static void
72 list_desc (ext2_filsys fs, PARAMS * p)
73 {
74   unsigned long i, j, k, ptr;
75   blk_t group_blk, next_blk;
76   char *block_bitmap = fs->block_map->bitmap;
77   char *inode_bitmap = fs->inode_map->bitmap;
78   int inode_blocks_per_group;
79   int group_desc_blocks;
80   int nbsect;
81   unsigned long used = 0;
82
83   inode_blocks_per_group = ((fs->super->s_inodes_per_group *
84                              EXT2_INODE_SIZE (fs->super)) +
85                             EXT2_BLOCK_SIZE (fs->super) - 1) /
86     EXT2_BLOCK_SIZE (fs->super);
87   group_desc_blocks = ((fs->super->s_blocks_count -
88                         fs->super->s_first_data_block +
89                         EXT2_BLOCKS_PER_GROUP (fs->super) - 1) /
90                        EXT2_BLOCKS_PER_GROUP (fs->super) +
91                        EXT2_DESC_PER_BLOCK (fs->super) - 1) /
92     EXT2_DESC_PER_BLOCK (fs->super);
93
94   nbsect = fs->blocksize / 512;
95   p->bitmaplg = (fs->super->s_blocks_count * nbsect + 7) / 8;
96   p->bitmap = (unsigned char *) calloc (p->bitmaplg, 1);
97
98   ptr = 0;
99   for (i = 0; i < fs->super->s_first_data_block; i++)
100     for (j = 0; j < nbsect; j++)
101       setbit (p->bitmap, ptr++);
102
103   group_blk = fs->super->s_first_data_block;
104   for (i = 0; i < fs->group_desc_count; i++)
105     {
106       next_blk = group_blk + fs->super->s_blocks_per_group;
107       if (next_blk > fs->super->s_blocks_count)
108         next_blk = fs->super->s_blocks_count;
109       /*debug ( _("Group %lu: (Blocks %u -- %u)\t"), i,
110          group_blk, next_blk -1 );
111
112          if (ext2fs_bg_has_super (fs, i))
113          debug ( _("  %s Superblock at %u,"
114          "  Group Descriptors at %u-%u\n"),
115          i == 0 ? _("Primary") : _("Backup"),
116          group_blk, group_blk + 1,
117          group_blk + group_desc_blocks);
118          debug ( _("  Block bitmap at %u (+%d), "
119          "Inode bitmap at %u (+%d)\n  "
120          "Inode table at %u-%u (+%d)\n"),
121          fs->group_desc[i].bg_block_bitmap,
122          fs->group_desc[i].bg_block_bitmap - group_blk,
123          fs->group_desc[i].bg_inode_bitmap,
124          fs->group_desc[i].bg_inode_bitmap - group_blk,
125          fs->group_desc[i].bg_inode_table,
126          fs->group_desc[i].bg_inode_table +
127          inode_blocks_per_group - 1,
128          fs->group_desc[i].bg_inode_table - group_blk);
129
130          debug (_("  %d free blocks, %d free inodes, %d directories\n"),
131          fs->group_desc[i].bg_free_blocks_count,
132          fs->group_desc[i].bg_free_inodes_count,
133          fs->group_desc[i].bg_used_dirs_count);
134        */
135       for (j = 0; j < next_blk - group_blk; j++)
136         {
137           if (in_use (block_bitmap, j))
138             for (k = 0; k < nbsect; k++)
139               {
140                 used++;
141                 setbit (p->bitmap, ptr + k);
142               }
143           ptr += nbsect;
144         }
145
146       block_bitmap += fs->super->s_blocks_per_group / 8;
147       inode_bitmap += fs->super->s_inodes_per_group / 8;
148
149       group_blk = next_blk;
150     }
151
152   sprintf(info1, "%lu", ((long) fs->super->s_blocks_count * nbsect));
153   sprintf(info2, "%lu", used);
154   print_sect_info(((long) fs->super->s_blocks_count * nbsect), used);
155  
156 }
157
158
159 /*  */
160 int
161 main (int argc, char **argv)
162 {
163   errcode_t retval;
164   ext2_filsys fs;
165   char *device_name;
166   PARAMS p;
167   int big_endian;
168   int fd;
169
170   if (argc != 3)
171     {
172       fprintf (stderr, "Usage : image_e2fs [device] [image prefix name]\n");
173       exit (1);
174     }
175
176   initialize_ext2_error_table ();
177
178   device_name = argv[1];
179
180   retval = ext2fs_open (device_name, 0, 0, 0, unix_io_manager, &fs);
181
182   if (retval)
183     {
184       debug (_("Couldn't find valid filesystem superblock.\n"));
185       exit (1);
186     }
187
188   big_endian = ((fs->flags & EXT2_FLAG_SWAP_BYTES) != 0);
189
190   if (big_endian)
191     debug (_("Note: This is a byte-swapped filesystem\n"));
192   //list_super (fs->super);
193
194   retval = ext2fs_read_bitmaps (fs);
195   if (retval)
196     {
197       debug (_("Couldn't read bitmaps.\n"));
198       ext2fs_close (fs);
199       exit (1);
200     }
201
202   list_desc (fs, &p);
203   ext2fs_close (fs);
204
205   if (argv[2][0] == '?')
206     exit (0);
207
208   ui_send("init_backup", 5, argv[1], argv[2], info1, info2, argv[0]);
209   fd = open (argv[1], O_RDONLY);
210   p.nb_sect = 0;
211   compress_volume (fd, argv[2], &p, "E2FS=1|TYPE=131");
212   close (fd);
213
214   exit (0);
215 }
Note: See TracBrowser for help on using the browser.