CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SherpackUtilities.cc
Go to the documentation of this file.
2 #include <unistd.h>
3 namespace spu {
4 
5 // functions for inflating (and deflating)
6 
7 //~ /* Compress from file source to file dest until EOF on source.
8  //~ def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
9  //~ allocated for processing, Z_STREAM_ERROR if an invalid compression
10  //~ level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
11  //~ version of the library linked do not match, or Z_ERRNO if there is
12  //~ an error reading or writing the files. */
13 int def(FILE *source, FILE *dest, int level)
14 {
15  int ret, flush;
16  unsigned have;
17  z_stream strm;
18  unsigned char in[CHUNK];
19  unsigned char out[CHUNK];
20 
21  /* allocate deflate state */
22  strm.zalloc = Z_NULL;
23  strm.zfree = Z_NULL;
24  strm.opaque = Z_NULL;
25  ret = deflateInit(&strm, level);
26  if (ret != Z_OK)
27  return ret;
28 
29  /* compress until end of file */
30  do {
31  strm.avail_in = fread(in, 1, CHUNK, source);
32  if (ferror(source)) {
33  (void)deflateEnd(&strm);
34  return Z_ERRNO;
35  }
36  flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
37  strm.next_in = in;
38 
39  /* run deflate() on input until output buffer not full, finish
40  compression if all of source has been read in */
41  do {
42  strm.avail_out = CHUNK;
43  strm.next_out = out;
44  ret = deflate(&strm, flush); /* no bad return value */
45  assert(ret != Z_STREAM_ERROR); /* state not clobbered */
46  have = CHUNK - strm.avail_out;
47  if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
48  (void)deflateEnd(&strm);
49  return Z_ERRNO;
50  }
51  } while (strm.avail_out == 0);
52  assert(strm.avail_in == 0); /* all input will be used */
53 
54  /* done when last data in file processed */
55  } while (flush != Z_FINISH);
56  assert(ret == Z_STREAM_END); /* stream will be complete */
57 
58  /* clean up and return */
59  (void)deflateEnd(&strm);
60  return Z_OK;
61 }
62 
63 /* Decompress from file source to file dest until stream ends or EOF.
64  inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
65  allocated for processing, Z_DATA_ERROR if the deflate data is
66  invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
67  the version of the library linked do not match, or Z_ERRNO if there
68  is an error reading or writing the files. */
69 int inf(FILE *source, FILE *dest)
70 {
71  int ret;
72  unsigned have;
73  z_stream strm;
74  unsigned char in[CHUNK];
75  unsigned char out[CHUNK];
76 
77  /* allocate inflate state */
78  strm.zalloc = Z_NULL;
79  strm.zfree = Z_NULL;
80  strm.opaque = Z_NULL;
81  strm.avail_in = 0;
82  strm.next_in = Z_NULL;
83  //~ ret = inflateInit(&strm,15);
84  ret = inflateInit2(&strm, (16+MAX_WBITS));
85  if (ret != Z_OK)
86  return ret;
87 
88  /* decompress until deflate stream ends or end of file */
89  do {
90  strm.avail_in = fread(in, 1, CHUNK, source);
91  if (ferror(source)) {
92  (void)inflateEnd(&strm);
93  return Z_ERRNO;
94  }
95  if (strm.avail_in == 0)
96  break;
97  strm.next_in = in;
98 
99  /* run inflate() on input until output buffer not full */
100  do {
101  strm.avail_out = CHUNK;
102  strm.next_out = out;
103  ret = inflate(&strm, Z_NO_FLUSH);
104  assert(ret != Z_STREAM_ERROR); /* state not clobbered */
105  switch (ret) {
106  case Z_NEED_DICT:
107  ret = Z_DATA_ERROR; /* and fall through */
108  case Z_DATA_ERROR:
109  case Z_MEM_ERROR:
110  (void)inflateEnd(&strm);
111  return ret;
112  }
113  have = CHUNK - strm.avail_out;
114  if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
115  (void)inflateEnd(&strm);
116  return Z_ERRNO;
117  }
118  } while (strm.avail_out == 0);
119 
120  /* done when inflate() says it's done */
121  } while (ret != Z_STREAM_END);
122 
123  /* clean up and return */
124  (void)inflateEnd(&strm);
125  return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
126 }
127 
128 /* report a zlib or i/o error */
129 void zerr(int ret)
130 {
131  fputs("zpipe: ", stderr);
132  switch (ret) {
133  case Z_ERRNO:
134  if (ferror(stdin))
135  fputs("error reading stdin\n", stderr);
136  if (ferror(stdout))
137  fputs("error writing stdout\n", stderr);
138  break;
139  case Z_STREAM_ERROR:
140  fputs("invalid compression level\n", stderr);
141  break;
142  case Z_DATA_ERROR:
143  fputs("invalid or incomplete deflate data\n", stderr);
144  break;
145  case Z_MEM_ERROR:
146  fputs("out of memory\n", stderr);
147  break;
148  case Z_VERSION_ERROR:
149  fputs("zlib version mismatch!\n", stderr);
150  }
151 }
152 
153 /* compress or decompress from stdin to stdout */
155 {
156  int ret;
157  FILE *in = fopen(infile.c_str(),"r");
158  if (!in) return -1;
159  FILE *out = fopen(outfile.c_str(),"w");
160  if (!out) return -2;
161  /* avoid end-of-line conversions */
162  SET_BINARY_MODE(in);
163  SET_BINARY_MODE(out);
164 
165  ret = inf(in, out);
166  if (ret != Z_OK)
167  zerr(ret);
168 
169  fclose(in);
170  fclose(out);
171  return ret;
172 }
173 
174 
175 
176 // functions for untaring Sherpacks
177 /* Parse an octal number, ignoring leading and trailing nonsense. */
178 int parseoct(const char *p, size_t n) {
179  int i = 0;
180 
181  while (*p < '0' || *p > '7') {
182  ++p;
183  --n;
184  }
185  while (*p >= '0' && *p <= '7' && n > 0) {
186  i *= 8;
187  i += *p - '0';
188  ++p;
189  --n;
190  }
191  return (i);
192 }
193 
194 /* Returns true if this is 512 zero bytes. */
195 int is_end_of_archive(const char *p) {
196  int n;
197  for (n = 511; n >= 0; --n)
198  if (p[n] != '\0')
199  return (0);
200  return (1);
201 }
202 
203 /* Create a directory, including parent directories as necessary. */
204 void create_dir(char *pathname, int mode) {
205  char *p;
206  int r;
207 
208  /* Strip trailing '/' */
209  if (pathname[strlen(pathname) - 1] == '/')
210  pathname[strlen(pathname) - 1] = '\0';
211 
212  /* Try creating the directory. */
213  r = mkdir(pathname, mode);
214 
215  if (r != 0) {
216  /* On failure, try creating parent directory. */
217  p = strrchr(pathname, '/');
218  if (p != NULL) {
219  *p = '\0';
220  create_dir(pathname, 0755);
221  *p = '/';
222  r = mkdir(pathname, mode);
223  }
224  }
225  if (r != 0)
226  fprintf(stderr, "Could not create directory %s\n", pathname);
227 }
228 
229 /* Create a file, including parent directory as necessary. */
230 FILE* create_file(char *pathname, int mode) {
231  FILE *f;
232  f = fopen(pathname, "w+");
233  if (f == NULL) {
234  /* Try creating parent dir and then creating file. */
235  char *p = strrchr(pathname, '/');
236  if (p != NULL) {
237  *p = '\0';
238  create_dir(pathname, 0755);
239  *p = '/';
240  f = fopen(pathname, "w+");
241  }
242  }
243  return (f);
244 }
245 
246 /* Verify the tar checksum. */
247 int verify_checksum(const char *p) {
248  int n, u = 0;
249  for (n = 0; n < 512; ++n) {
250  if (n < 148 || n > 155)
251  /* Standard tar checksum adds unsigned bytes. */
252  u += ((unsigned char *)p)[n];
253  else
254  u += 0x20;
255 
256  }
257  return (u == parseoct(p + 148, 8));
258 }
259 
260 /* Extract a tar archive. */
261 void Untar(FILE *a, const char *path) {
262  bool longpathname=false;
263  bool longlinkname=false;
264  char newlongpathname[512];
265  char newlonglinkname[512];
266  char buff[512];
267  FILE *f = NULL;
268  size_t bytes_read;
269  int filesize;
270 
271  printf("Extracting from %s\n", path);
272  for (;;) {
273 
274  bytes_read = fread(buff, 1, 512, a);
275  if (bytes_read < 512) {
276  fprintf(stderr,
277  "Short read on %s: expected 512, got %d\n",path, (int)bytes_read);
278  return;
279  }
280  if (is_end_of_archive(buff)) {
281  printf("End of %s\n", path);
282  return;
283  }
284  if (!verify_checksum(buff)) {
285  fprintf(stderr, "Checksum failure\n");
286  return;
287  }
288  filesize = parseoct(buff + 124, 12);
289 // printf("%c %d\n",buff[156],filesize);
290  switch (buff[156]) {
291  case '1':
292  printf(" Ignoring hardlink %s\n", buff);
293  break;
294  case '2':
295  if (longpathname && longlinkname){
296  longlinkname=false;
297  longpathname=false;
298  printf(" Extracting symlink %s\n", newlongpathname);
299  symlink(newlonglinkname,newlongpathname);
300  } else if (longpathname) {
301  longpathname=false;
302  printf(" Extracting symlink %s\n", newlongpathname);
303  symlink(buff+157,newlongpathname);
304  } else if (longlinkname) {
305  longlinkname=false;
306  printf(" Extracting symlink %s\n", buff);
307  symlink(newlonglinkname,buff);
308  } else {
309  printf(" Extracting symlink %s\n", buff);
310  symlink(buff+157,buff);
311  }
312  break;
313  case '3':
314  printf(" Ignoring character device %s\n", buff);
315  break;
316  case '4':
317  printf(" Ignoring block device %s\n", buff);
318  break;
319  case '5':
320  if (!longpathname){
321  int endposition=-1;
322  for (int k=99; k>=0; k--){
323  if (buff[k]=='\0') endposition=k;
324  }
325  if (endposition==-1) {
326  //~ printf("OLDNAME : %s\n",buff);
327  longpathname=true;
328  for (int k=0; k<100; k++){
329  newlongpathname[k]=buff[k];
330  }
331  newlongpathname[100]='\0';
332  //~ printf("NEWNAME : %s\n",newlongpathname);
333  }
334  }
335 
336  if (longpathname) {
337  printf(" Extracting dir %s\n", newlongpathname);
338  create_dir(newlongpathname, parseoct(buff + 100, 8));
339  longpathname=false;
340  } else {
341 
342  printf(" Extracting dir %s\n", buff);
343  create_dir(buff, parseoct(buff + 100, 8));
344  }
345  //~ printf(" Extracting dir %s\n", buff);
346  //~ create_dir(buff, parseoct(buff + 100, 8));
347  filesize = 0;
348  break;
349  case '6':
350  printf(" Ignoring FIFO %s\n", buff);
351  break;
352  case 'L':
353  longpathname=true;
354  //~ printf(" Long Filename found 0 %s\n", buff);
355  //~ printf(" Long Filename found 100 %s\n", buff+100);
356  //~ printf(" Long Filename found 108 %s\n", buff+108);
357  //~ printf(" Long Filename found 116 %s\n", buff+116);
358  //~ printf(" Long Filename found 124 %s\n", buff+124);
359  //~ printf(" Long Filename found 136 %s\n", buff+136);
360  //~ printf(" Long Filename found 148 %s\n", buff+148);
361  //~ printf(" Long Filename found 156 %s\n", buff+156);
362  //~ printf(" Long Filename found 157 %s\n", buff+157);
363  //~ printf(" Long Filename found 158 %s\n", buff+158);
364  //~ printf(" Long Filename found 159 %s\n", buff+159);
365  //~ printf(" Long Filename found 257 %s\n", buff+257);
366  //~ printf(" Long Filename found 263 %s\n", buff+263);
367  //~ printf(" Long Filename found 265 %s\n", buff+265);
368  //~ printf(" Long Filename found 297 %s\n", buff+297);
369  //~ printf(" Long Filename found 329 %s\n", buff+329);
370  //~ printf(" Long Filename found 337 %s\n", buff+337);
371  //~ printf(" Long Filename found 345 %s\n", buff+345);
372  //~ printf(" Long Filename found 346 %s\n", buff+346);
373  //~ printf(" Long Filename found 347 %s\n", buff+347);
374  break;
375 
376  case 'K':
377  longlinkname=true;
378  break;
379 
380  default:
381  if (!longpathname){
382  int endposition=-1;
383  for (int k=99; k>=0; k--){
384  if (buff[k]=='\0') endposition=k;
385  }
386  if (endposition==-1) {
387  //~ printf("OLDNAME : %s\n",buff);
388  longpathname=true;
389  for (int k=0; k<100; k++){
390  newlongpathname[k]=buff[k];
391  }
392  newlongpathname[100]='\0';
393  //~ printf("NEWNAME : %s\n",newlongpathname);
394  }
395  }
396  if (longpathname) {
397  printf(" Extracting file %s\n", newlongpathname);
398  f = create_file(newlongpathname, parseoct(buff + 100, 8));
399  longpathname=false;
400  } else {
401 
402  printf(" Extracting file %s\n", buff);
403  f = create_file(buff, parseoct(buff + 100, 8));
404  }
405  break;
406  }
407 
408  if (longlinkname || longpathname) {
409  if (buff[156]=='K'){
410 
411  for (int ll=0; ll<512; ll++){ printf("%c",buff[ll]);} printf("\n");
412  bytes_read = fread(buff, 1, 512, a);
413  for (int ll=0; ll<512; ll++){ printf("%c",buff[ll]);} printf("\n");
414  for (int k=0; k<filesize; k++){
415  newlonglinkname[k]=buff[k];
416  }
417  newlonglinkname[filesize]='\0';
418  for (int k=filesize+1; k<512; k++){
419  newlonglinkname[k]='0';
420  }
421  //~ printf("NEW LinkNAME: %s\n",newlonglinkname);
422  } else if (buff[156]=='L'){
423  bytes_read = fread(buff, 1, 512, a);
424  for (int k=0; k<filesize; k++){
425  newlongpathname[k]=buff[k];
426  }
427  newlongpathname[filesize]='\0';
428  for (int k=filesize+1; k<512; k++){
429  newlongpathname[k]='0';
430  }
431  //~ printf("NEW FILENAME: %s\n",newlongpathname);
432  }
433  }
434 
435  //~
436  //~ if (longpathname) {
437  //~ bytes_read = fread(buff, 1, 512, a);
438  //~ for (int k=0; k<filesize; k++){
439  //~ newlongpathname[k]=buff[k];
440  //~ }
441  //~ newlongpathname[filesize]='\0';
442  //~ for (int k=filesize+1; k<512; k++){
443  //~ newlongpathname[k]='0';
444  //~ }
445  //~ printf("NEW FILENAME: %s\n",newlongpathname);
446  //~
447  //~ }
448  //~ else if (!longpathname && !longlinkname) {
449  if (!longpathname && !longlinkname) {
450  while (filesize > 0) {
451  bytes_read = fread(buff, 1, 512, a);
452  if (bytes_read < 512) {
453  fprintf(stderr,
454  "Short read on %s: Expected 512, got %d\n", path, (int)bytes_read);
455  return;
456  }
457  if (filesize < 512)
458  bytes_read = filesize;
459  if (f != NULL) {
460  if (fwrite(buff, 1, bytes_read, f)
461  != bytes_read)
462  {
463  fprintf(stderr, "Failed write\n");
464  fclose(f);
465  f = NULL;
466  }
467  }
468  filesize -= bytes_read;
469  }
470  if (f != NULL) {
471  fclose(f);
472  f = NULL;
473  }
474  }
475  }
476 }
477 
478 
479 
480 // function for calculating the MD5 checksum of a file
482  char buffer[4096];
483  MD5_CTX md5;
484  MD5_Init(&md5);
485 
486  //Open File
487  int fd = open(filename.c_str(), O_RDONLY);
488  int nb_read;
489  while ((nb_read = read(fd, buffer, 4096 - 1)))
490  {
491  MD5_Update(&md5, buffer, nb_read);
492  memset(buffer, 0, 4096);
493  }
494  unsigned char tmp[MD5_DIGEST_LENGTH];
495  MD5_Final(tmp, &md5);
496 
497  //Convert the result
498  for (int k = 0; k < MD5_DIGEST_LENGTH; ++k) {
499  sprintf(result + k * 2, "%02x", tmp[k]);
500  }
501 }
502 
503 
504 } // End namespace spu
505 
int def(FILE *, FILE *, int)
int i
Definition: DBlmapReader.cc:9
void zerr(int)
void md5_File(std::string, char *)
#define NULL
Definition: scimark2.h:8
FILE * create_file(char *, int)
int verify_checksum(const char *)
tuple path
else: Piece not in the list, fine.
tuple result
Definition: query.py:137
#define CHUNK
double f[11][100]
void Untar(FILE *, const char *)
int is_end_of_archive(const char *)
tuple out
Definition: dbtoconf.py:99
#define SET_BINARY_MODE(file)
int inf(FILE *, FILE *)
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
double a
Definition: hdecay.h:121
tuple filename
Definition: lut2db_cfg.py:20
tuple level
Definition: testEve_cfg.py:34
void create_dir(char *, int)
int parseoct(const char *, size_t)
static std::string const source
Definition: EdmProvDump.cc:43
int Unzip(std::string, std::string)