FileMgt.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @file FileMgt.cpp
  3. * @brief Misc file management utilities
  4. * @author Erik Rodner
  5. * @date 10/26/2007
  6. i*/
  7. #include <stdlib.h>
  8. #ifndef WIN32
  9. #include <unistd.h>
  10. #endif
  11. #include <sys/file.h>
  12. #include <sys/types.h> // For stat()
  13. #include <sys/stat.h> // For stat()
  14. #include <iostream>
  15. #include <algorithm>
  16. #include "FileMgt.h"
  17. #include "ossettings.h"
  18. #include "StringTools.h"
  19. #include <errno.h>
  20. #include <string.h>
  21. using namespace NICE;
  22. using namespace std;
  23. using namespace NICE;
  24. void FileMgt::DirectoryRecursive ( std::vector<string> & files, const std::string & dir )
  25. {
  26. std::string command = "find -L \"" + dir + "\" -type f";
  27. //cerr << "FileMgt::DirectoryRecursive: find command (" << command << ")" << endl;
  28. FILE *pipe = popen ( command.c_str(), "r" );
  29. //cerr << "FileMgt::DirectoryRecursive: popen issued !" << endl;
  30. if ( pipe == NULL ) {
  31. int errsv = errno; //just to be sure that the writing on std::cerr does not change the errno
  32. cerr << "FileMgt::DirectoryRecursive: find command failed (" << command << ")" << endl;
  33. std::cerr << "FileMgt::DirectoryRecursive: popen error message is " << strerror(errsv) << std::endl;
  34. return;
  35. }
  36. char line[MAXLINESIZE];
  37. while (! feof(pipe) )
  38. {
  39. if ( fgets ( line, MAXLINESIZE, pipe ) == NULL )
  40. break;
  41. std::string line_stl ( line );
  42. line_stl = StringTools::chomp (line_stl);
  43. files.push_back ( line_stl );
  44. }
  45. pclose ( pipe );
  46. std::sort ( files.begin(), files.end() );
  47. }
  48. std::string FileMgt::createTempFile ( const std::string & templatefn )
  49. {
  50. int max_iterations = 1024;
  51. int iteration = 0;
  52. char fn[1024];
  53. char subst[1024];
  54. int pid = (int)getpid();
  55. struct stat statbuf;
  56. int statval = -1;
  57. int fd = -1;
  58. //srand(time(NULL));
  59. do {
  60. long id = rand() % 1000000000;
  61. snprintf( subst, 1023, "%04d_%010ld", pid, id );
  62. snprintf( fn, 1023, templatefn.c_str(), subst );
  63. statval = stat( fn, &statbuf );
  64. iteration++;
  65. if ( statval != 0 )
  66. {
  67. fd = creat (fn, 0600 );
  68. close(fd);
  69. }
  70. } while ( (fd < 0) && (iteration < max_iterations) );
  71. if ( fd < 0 )
  72. {
  73. string errormessage = "FileMgt::createTempFile: FATAL ERROR unable to create temporary filename with template "+templatefn+".\n";
  74. fprintf (stderr, errormessage.c_str());
  75. throw (errormessage.c_str());
  76. }
  77. return string(fn);
  78. }
  79. void FileMgt::deleteTempFile ( const std::string & tempfile )
  80. {
  81. if ( unlink(tempfile.c_str()) != 0 )
  82. {
  83. string errormessage = "FileMgt::deleteTempFile: FATAL ERROR removing "+tempfile+".\n";
  84. fprintf (stderr, errormessage.c_str());
  85. throw (errormessage.c_str());
  86. }
  87. }
  88. bool FileMgt::fileExists ( const std::string & file )
  89. {
  90. struct stat info;
  91. int result = stat ( file.c_str(), &info );
  92. return ( result == 0 );
  93. }