CMS 3D CMS Logo

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