CMS 3D CMS Logo

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