root/trunk/client/revimage/decompress-test.c

Revision 12, 7.4 kB (checked in by ludo, 4 years ago)

initial ci: revimage= filesystem compression programs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
Line 
1 /*
2  * decompress-test.c
3  *
4  * $Id$
5  *
6  * Program to check that all compressed images in the current directory
7  * are not corrupted. Try to test all files ending with 3 digits
8  *
9  * Usage: decompress-test
10  */
11 /*
12  *  Linbox Rescue Server
13  *  Copyright (C) 2002-2005 Linbox FAS, Free & Alter Soft
14  *
15  *  This program is free software; you can redistribute it and/or modify
16  *  it under the terms of the GNU General Public License as published by
17  *  the Free Software Foundation; either version 2 of the License, or
18  *  (at your option) any later version.
19  *
20  *  This program is distributed in the hope that it will be useful,
21  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *  GNU General Public License for more details.
24  *
25  *  You should have received a copy of the GNU General Public License
26  *  along with this program; if not, write to the Free Software
27  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28  */
29
30 #define _GNU_SOURCE
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <stddef.h>
39 #include <dirent.h>
40
41 #include "zlib.h"
42
43 #define INSIZE 2048
44
45 unsigned char BUFFER[24064];
46 unsigned char Bitmap[24064 - 2048];
47 unsigned char IN[INSIZE];
48 unsigned char zero[512];
49
50 typedef struct params_
51 {
52   int bitindex;
53   int fo;
54   unsigned long long offset;
55 }
56 PARAMS;
57
58 int
59 eof (int fd)
60 {
61   __off64_t pos, end;
62   pos = lseek64 (fd, 0, SEEK_CUR);
63   if (pos < 0)
64     {
65       fprintf (stderr, "Error LSEEK : eof,pos\n");
66     }
67   end = lseek64 (fd, 0, SEEK_END);
68   if (end < 0)
69     {
70       fprintf (stderr, "Error LSEEK : eof,end\n");
71     }
72   if (lseek64 (fd, pos, SEEK_SET) < 0)
73     {
74       fprintf (stderr, "Error LSEEK, reseek\n");
75     }
76   if (end == pos)
77     return 1;
78   return 0;
79 }
80
81 void
82 fill (int fd, int bytes, int dir)
83 {
84   int err = 0;
85   int lg = bytes;
86
87   if (eof (fd))
88     {
89       while (bytes > 512)
90         {
91           if (write (fd, zero, 512) != 512)
92             {
93               if (!err)
94                 {
95                   err = 1;
96                   fprintf (stderr, "Error FILL : write 512!=512\n");
97                 }
98             }
99           bytes -= 512;
100         }
101       if (write (fd, zero, bytes) != bytes)
102         {
103           fprintf (stderr, "Error FILL : %d bytes write...\n", bytes);
104           err = 1;
105         }
106     }
107   else
108     {
109       if (lseek64 (fd, bytes, dir) < 0)
110         {
111           fprintf (stderr, "Error FILL : skip error");
112           err = 1;
113         }
114     }
115   if (err)
116     fprintf (stderr, "Error when filling %d bytes\n", lg);
117 }
118
119 void
120 flushToDisk (unsigned char *buff, unsigned char *bit, PARAMS * cp, int lg)
121 {
122   unsigned char *ptr = buff;
123   unsigned char mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
124   int indx = cp->bitindex;
125
126 // printf("Enter : bitindex -> %d\n",indx);
127   while (lg > 0)
128     {
129       while (!(bit[indx >> 3] & mask[indx & 7]))
130         {
131           indx++;
132           cp->offset += 512;
133           if (cp->fo)
134             fill (cp->fo, 512, SEEK_CUR);
135         }
136 //      printf("Write @offset : %lld\t",cp->offset);
137 //      {int i; for(i=0;i<15;i++) printf("%02x ",ptr[i]); printf("\n");}
138       if (cp->fo)
139         {
140           if (write (cp->fo, ptr, 512) != 512)
141             fprintf (stderr, "Flush Error : write 512!=512\n");
142         }
143       cp->offset += 512;
144       ptr += 512;
145       indx++;
146       lg -= 512;
147     }
148 // printf("Exit  : bitindex -> %d\n",indx);
149   cp->bitindex = indx;
150 }
151
152 int
153 main (int argc, char *argv[])
154 {
155   z_stream zptr;
156   int state, l;
157   int ret, firstpass, bitmaplg;
158   FILE *fi;
159   PARAMS currentparams;
160   DIR *dp;
161   struct dirent *ep;
162   char *name;
163
164   dp = opendir ("./");
165   if (dp == NULL)
166     {
167       puts ("Couldn't open the directory.");
168       exit (1);
169     }
170   else
171     {
172       while (ep = readdir (dp))
173         {
174           name = ep->d_name;
175           l = strlen(name);
176           if (l < 4) continue;
177           if (!isdigit(name[l-1]) || !isdigit(name[l-2]) || !isdigit(name[l-3]) ) continue;       
178           printf ("** Testing: %s **", name);
179
180         start:
181           state = Z_SYNC_FLUSH;
182           firstpass = 1;
183           bitmaplg = 0;
184
185           zptr.zalloc = NULL;
186           zptr.zfree = NULL;
187
188           fi = fopen (name, "rb");
189           if (fi == NULL)
190             {
191               printf ("Cannot open input file\n");
192               exit (1);
193             }
194           zptr.avail_in = fread (IN, 1, INSIZE, fi);
195
196           memset (zero, 0, 512);
197
198           currentparams.fo = 0;
199           currentparams.offset = 0;
200           currentparams.bitindex = 0;
201
202           zptr.next_in = (char *) IN;
203           zptr.next_out = (char *) BUFFER;      // was dbuf.data;
204           zptr.avail_out = 24064;
205
206           inflateInit (&zptr);
207
208           do
209             {
210 //  if (inflateSyncPoint(&zptr)) printf("#");
211
212               ret = inflate (&zptr, state);
213
214 //  printf("-> %d : %d / %d\n",ret ,zptr.avail_in ,zptr.avail_out );
215
216               if ((ret == Z_OK) && (zptr.avail_out == 0))
217                 {
218                   if (firstpass)
219                     {
220                       printf ("Params : *%s\n", BUFFER);
221                       if (strstr (BUFFER, "BLOCKS="))
222                         {
223                           /* FILE *fo;
224                           int i = 0;
225                           sscanf (strstr (BUFFER, "BLOCKS=") + 7, "%d", &i);
226                           fo = fopen ("/tmp/blocks", "wt");
227                           fprintf (fo, "%d\n", i);
228                           fclose (fo); */
229                         }
230                       if (strstr (BUFFER, "ALLOCTABLELG="))
231                         sscanf (strstr (BUFFER, "ALLOCTABLELG=") + 13, "%d",
232                                 &bitmaplg);
233                       memcpy (Bitmap, BUFFER + 2048, 24064 - 2048);
234 #if 0
235                       {
236                         int i;
237                         for (i = 0; i < 256; i++)
238                           {
239                             if ((i & 15) == 0)
240                               printf ("\n%04x : ", i);
241                             printf ("%02X ", Bitmap[i]);
242                           }
243                         printf ("\n");
244                       }
245 #endif
246                       currentparams.bitindex = 0;
247                       firstpass = 0;
248                     }
249                   else
250                     {
251                       //flushToDisk (BUFFER, Bitmap, &currentparams, 24064);
252                     }
253
254                   zptr.next_out = (char *) BUFFER;
255                   zptr.avail_out = 24064;
256                 }
257
258               if ((ret == Z_OK) && (zptr.avail_in == 0))
259                 {
260                   zptr.avail_in = fread (IN, 1, INSIZE, fi);
261                   zptr.next_in = (char *) IN;
262                 }
263             }
264           while (ret == Z_OK);
265
266 printf("ret=%d\n", ret);
267           if (ret == Z_STREAM_END)
268             {
269               {
270                 if (firstpass)
271                   {
272                     printf ("Params : *%s*\n", BUFFER);
273                     if (strstr (BUFFER, "BLOCKS="))
274                       {
275                         /* FILE *fo;
276                         int i = 0;
277                         sscanf (strstr (BUFFER, "BLOCKS=") + 7, "%d", &i);
278                         fo = fopen ("/tmp/blocks", "wt");
279                         fprintf (fo, "%d\n", i);
280                         fclose (fo); */
281                       }
282                     if (strstr (BUFFER, "ALLOCTABLELG="))
283                       sscanf (strstr (BUFFER, "ALLOCTABLELG=") + 13, "%d",
284                               &bitmaplg);
285                     memcpy (Bitmap, BUFFER + 2048, 24064 - 2048);
286                     zptr.next_out = (char *) BUFFER;
287                     zptr.avail_out = 24064;
288                   }
289               }
290
291               //printf ("Flushing to EOF ... (%d bytes)\n",
292                 //      24064 - zptr.avail_out);
293               //flushToDisk (BUFFER, Bitmap, &currentparams,
294                 //         24064 - zptr.avail_out);
295               zptr.next_out = (char *) BUFFER;
296               zptr.avail_out = 24064;
297             }
298
299           ret = inflate (&zptr, Z_FINISH);
300           inflateEnd (&zptr);
301
302
303           if (ret < 0)
304             {
305               printf ("Returned : %d\t", ret);
306               printf ("(AvailIn : %d / ", zptr.avail_in);
307               printf ("AvailOut: %d)\n", zptr.avail_out);
308               printf ("(TotalIn : %ld / ", zptr.total_in);
309               printf ("TotalOut: %ld)\n", zptr.total_out);
310               fprintf (stderr, "ZLIB error, aborting...\n");
311               exit(1);
312               goto start;
313             }
314
315           /*printf ("Offset : %lld\n", currentparams.offset);
316           printf ("Bitmap index : %d\n", currentparams.bitindex);*/
317
318           if (bitmaplg)
319             {
320               if (bitmaplg * 8 > currentparams.bitindex)
321                 {
322                   //printf ("Remaining bitmap : %d\n",
323                   //      bitmaplg * 8 - currentparams.bitindex);
324                   currentparams.offset +=
325                     512 * (bitmaplg * 8 - currentparams.bitindex);
326                   if (currentparams.fo)
327                     {
328                     //  fill (currentparams.fo,
329                     //      512 * (bitmaplg * 8 - currentparams.bitindex),
330                     //      SEEK_CUR);
331                     }
332                 }
333             }
334
335
336           fclose (fi);
337         }
338     }
339    (void) closedir (dp);
340    puts("** Verified files are OK **");
341    exit(0);
342 }
Note: See TracBrowser for help on using the browser.