CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Functions
spu Namespace Reference

Functions

void create_dir (char *, int)
 
FILE * create_file (char *, int)
 
int def (FILE *, FILE *, int)
 
int inf (FILE *, FILE *)
 
int is_end_of_archive (const char *)
 
void md5_File (std::string, char *)
 
int parseoct (const char *, size_t)
 
void Untar (FILE *, const char *)
 
int Unzip (std::string, std::string)
 
int verify_checksum (const char *)
 
void zerr (int)
 

Function Documentation

void spu::create_dir ( char *  pathname,
int  mode 
)

Definition at line 204 of file SherpackUtilities.cc.

References eostools::mkdir(), NULL, AlCaHLTBitMon_ParallelJobs::p, and alignCSCRings::r.

Referenced by create_file(), and Untar().

204  {
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 }
#define NULL
Definition: scimark2.h:8
def mkdir
Definition: eostools.py:250
void create_dir(char *, int)
FILE * spu::create_file ( char *  pathname,
int  mode 
)

Definition at line 230 of file SherpackUtilities.cc.

References create_dir(), f, NULL, and AlCaHLTBitMon_ParallelJobs::p.

Referenced by Untar().

230  {
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 }
#define NULL
Definition: scimark2.h:8
double f[11][100]
void create_dir(char *, int)
int spu::def ( FILE *  source,
FILE *  dest,
int  level 
)

Definition at line 13 of file SherpackUtilities.cc.

References assert(), CHUNK, recoMuon::in, GenerateHcalLaserBadRunList::out, and runTheMatrix::ret.

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 }
tuple ret
prodAgent to be discontinued
assert(m_qm.get())
#define CHUNK
tuple level
Definition: testEve_cfg.py:34
static std::string const source
Definition: EdmProvDump.cc:43
int spu::inf ( FILE *  source,
FILE *  dest 
)

Definition at line 69 of file SherpackUtilities.cc.

References assert(), CHUNK, recoMuon::in, GenerateHcalLaserBadRunList::out, and runTheMatrix::ret.

Referenced by Unzip().

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 }
tuple ret
prodAgent to be discontinued
assert(m_qm.get())
#define CHUNK
static std::string const source
Definition: EdmProvDump.cc:43
int spu::is_end_of_archive ( const char *  p)

Definition at line 195 of file SherpackUtilities.cc.

References gen::n.

Referenced by Untar().

195  {
196  int n;
197  for (n = 511; n >= 0; --n)
198  if (p[n] != '\0')
199  return (0);
200  return (1);
201 }
void spu::md5_File ( std::string  filename,
char *  result 
)

Definition at line 481 of file SherpackUtilities.cc.

References ztee::fd, relval_2017::k, SiPixelLorentzAngle_cfi::read, and tmp.

Referenced by spf::SherpackFetcher::Fetch().

481  {
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 }
tuple result
Definition: mps_fire.py:95
tuple fd
Definition: ztee.py:136
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
tuple filename
Definition: lut2db_cfg.py:20
int spu::parseoct ( const char *  p,
size_t  n 
)

Definition at line 178 of file SherpackUtilities.cc.

References i, gen::n, and AlCaHLTBitMon_ParallelJobs::p.

Referenced by Untar(), and verify_checksum().

178  {
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 }
int i
Definition: DBlmapReader.cc:9
void spu::Untar ( FILE *  a,
const char *  path 
)

Definition at line 261 of file SherpackUtilities.cc.

References create_dir(), create_file(), f, is_end_of_archive(), relval_2017::k, NULL, parseoct(), and verify_checksum().

Referenced by spf::SherpackFetcher::Fetch().

261  {
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 }
#define NULL
Definition: scimark2.h:8
FILE * create_file(char *, int)
int verify_checksum(const char *)
double f[11][100]
int is_end_of_archive(const char *)
double a
Definition: hdecay.h:121
void create_dir(char *, int)
int parseoct(const char *, size_t)
int spu::Unzip ( std::string  infile,
std::string  outfile 
)

Definition at line 154 of file SherpackUtilities.cc.

References recoMuon::in, inf(), GenerateHcalLaserBadRunList::out, runTheMatrix::ret, SET_BINARY_MODE, and zerr().

Referenced by spf::SherpackFetcher::Fetch().

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 }
tuple ret
prodAgent to be discontinued
void zerr(int)
string inf
Definition: EcalCondDB.py:94
#define SET_BINARY_MODE(file)
int spu::verify_checksum ( const char *  p)

Definition at line 247 of file SherpackUtilities.cc.

References gen::n, and parseoct().

Referenced by Untar().

247  {
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 }
int parseoct(const char *, size_t)
void spu::zerr ( int  ret)

Definition at line 129 of file SherpackUtilities.cc.

Referenced by MultiFileBlob::expand(), MultiFileBlob::finalized(), FileBlob::getUncompressedBlob(), FileBlob::read(), Unzip(), and FileBlob::write().

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 }
tuple ret
prodAgent to be discontinued