You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
4.1 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "storageProvider.h"
  2. #define LOG(X) m_pLog->Log( "storageProvider", this, X )
  3. #define LOG_DEBUG( X ) m_pLog->Log( "storageProvider", this, _LOG_DEBUG, X )
  4. #define LOG_ERROR( X ) m_pLog->Log( "storageProvider", this, _LOG_ERROR, X )
  5. storageProvider* storageProvider::_instance = 0;
  6. /**
  7. * Returns the single instance of the object.
  8. */
  9. storageProvider* storageProvider::getInstance(CSTR Base) {
  10. // check if the instance has been created yet
  11. if (0 == _instance) {
  12. // if not, then create it
  13. _instance = new storageProvider(Base);
  14. }
  15. // return the single instance
  16. return _instance;
  17. };
  18. storageProvider::storageProvider(CSTR BasePath) :m_mCounterProtectorMutex(){
  19. m_pLog = basicLogCollection::getInstance();
  20. LOG("Start Storage");
  21. m_pCWD = (CSTR) malloc(MAX_PATH_LEN);
  22. m_pBasePath = (CSTR) malloc(MAX_PATH_LEN);
  23. m_pEntryFolder = (CSTR) malloc( 9 );
  24. m_pSeriesBase = (CSTR) malloc( 33 );
  25. m_nSeriesCounter = 0;
  26. if( m_pBasePath!= NULL ) {
  27. #ifdef __linux__
  28. getcwd(m_pCWD, MAX_PATH_LEN );
  29. #else
  30. _getcwd(m_pCWD, MAX_PATH_LEN );
  31. #endif
  32. //strncpy( m_pBasePath, BasePath, 1024 );
  33. sprintf(m_pBasePath, "%s/%s", m_pCWD, BasePath);
  34. #ifdef DEBUG
  35. char Buffer[256];
  36. sprintf(Buffer,"Base Path OK [%s]", m_pBasePath );
  37. LOG(Buffer);
  38. #endif
  39. startNewEntry();
  40. } else {
  41. LOG_ERROR("No Base Path Provided!");
  42. };
  43. };
  44. storageProvider::~storageProvider(){
  45. if( m_pCWD!=NULL ) {
  46. free(m_pCWD);
  47. };
  48. if( m_pBasePath!=NULL ) {
  49. free(m_pBasePath);
  50. };
  51. if( m_pSeriesBase!=NULL ) {
  52. free(m_pSeriesBase);
  53. };
  54. if( m_pEntryFolder!= NULL ) {
  55. free(m_pEntryFolder);
  56. };
  57. };
  58. bool storageProvider::startNewSeries( void ){
  59. char temp[128];
  60. //#ifdef DEBUG
  61. char Buffer[256];
  62. //#endif
  63. #ifdef __linux__
  64. sprintf( temp, "%lld", getMilliseconds() );
  65. #else
  66. sprintf( temp, "%I64u", getMilliseconds());
  67. #endif
  68. //#ifdef DEBUG
  69. sprintf(Buffer,"Stor: %s", temp);
  70. LOG(Buffer);
  71. //#endif
  72. sprintf( m_pSeriesBase, "%s", MD5String( temp ) );
  73. #ifdef DEBUG
  74. sprintf(Buffer,"Stor Ser Base: %s", m_pSeriesBase);
  75. LOG(Buffer);
  76. #endif
  77. m_nSeriesCounter = 0;
  78. return true;
  79. };
  80. bool storageProvider::startNewEntry( void ){
  81. char Buffer[256];
  82. if( fileExist( m_pBasePath ) == 0 ) {
  83. sprintf( Buffer, "No BasePath[%s] Detected", m_pBasePath );
  84. LOG(Buffer);
  85. return false;
  86. };
  87. sprintf( m_pCWD, "%s/%s", m_pBasePath ,getTStr((CSTR)"%Y"));
  88. sprintf( Buffer,"Test Base: %s", m_pCWD );
  89. LOG(Buffer);
  90. int RetVal;
  91. if( fileExist( m_pCWD ) == 0 ){
  92. #ifdef __linux__
  93. RetVal = mkdir( m_pCWD, STORAGE_DIR_MODE );
  94. #else
  95. RetVal = _mkdir( m_pCWD );
  96. #endif
  97. if( RetVal!= 0 ) {
  98. return false;
  99. };
  100. };
  101. sprintf( m_pCWD, "%s/%s", m_pBasePath ,getTStr((CSTR)"%Y/%m"));
  102. sprintf( Buffer,"Test Base: %s", m_pCWD );
  103. LOG(Buffer);
  104. if( fileExist( m_pCWD ) == 0 ){
  105. #ifdef __linux__
  106. RetVal = mkdir( m_pCWD, STORAGE_DIR_MODE );
  107. #else
  108. RetVal = _mkdir( m_pCWD );
  109. #endif
  110. if( RetVal!= 0 ) {
  111. return false;
  112. };
  113. };
  114. sprintf( m_pCWD, "%s/%s", m_pBasePath ,getTStr((CSTR)"%Y/%m/%d"));
  115. sprintf( Buffer,"Test Base: %s", m_pCWD );
  116. LOG(Buffer);
  117. if( fileExist( m_pCWD ) == 0 ){
  118. #ifdef __linux__
  119. RetVal = mkdir( m_pCWD, STORAGE_DIR_MODE );
  120. #else
  121. RetVal = _mkdir( m_pCWD );
  122. #endif
  123. if( RetVal!= 0 ) {
  124. return false;
  125. };
  126. };
  127. sprintf( m_pEntryFolder, "%s", getTStr((CSTR)"%H%M%S"));
  128. char tVal[1024];
  129. sprintf( tVal, "%s/%s", m_pCWD , m_pEntryFolder );
  130. sprintf( Buffer,"Test Base: %s", tVal );
  131. LOG(Buffer);
  132. #ifdef __linux__
  133. RetVal = mkdir( tVal, STORAGE_DIR_MODE );
  134. #else
  135. RetVal = _mkdir( tVal );
  136. #endif
  137. if( RetVal!= 0 ) {
  138. return false;
  139. };
  140. return startNewSeries();
  141. };
  142. CSTR storageProvider::getNewFileName( void ){
  143. char tVal[1024];
  144. CSTR RetVal;
  145. m_mCounterProtectorMutex.lock();
  146. m_nSeriesCounter++;
  147. sprintf( tVal, "%s/%s/%s_%d", m_pCWD, m_pEntryFolder, m_pSeriesBase, m_nSeriesCounter );
  148. RetVal = (CSTR) malloc( strlen(tVal)+1);
  149. strncpy( RetVal, tVal, strlen(tVal) );
  150. RetVal[strlen(tVal)] = (char)0;
  151. m_mCounterProtectorMutex.unlock();
  152. return RetVal;
  153. };
  154. #undef LOG
  155. #undef LOG_DEBUG
  156. #undef LOG_ERROR