FileMgt.cpp 3.3 KB

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