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.

162 lines
4.0 KiB

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( 1024 );
  22. m_pBasePath = (CSTR) malloc( 1024 );
  23. m_pEntryFolder = (CSTR) malloc( 9 );
  24. m_pSeriesBase = (CSTR) malloc( 33 );
  25. m_nSeriesCounter = 0;
  26. if( m_pBasePath!= NULL ) {
  27. strncpy( m_pBasePath, BasePath, 1024 );
  28. #ifdef DEBUG
  29. char Buffer[256];
  30. sprintf(Buffer,"Base Path OK [%s]", m_pBasePath );
  31. LOG(Buffer);
  32. #endif
  33. startNewEntry();
  34. } else {
  35. LOG_ERROR("No Base Path Provided!");
  36. };
  37. };
  38. storageProvider::~storageProvider(){
  39. if( m_pCWD!=NULL ) {
  40. free(m_pCWD);
  41. };
  42. if( m_pBasePath!=NULL ) {
  43. free(m_pBasePath);
  44. };
  45. if( m_pSeriesBase!=NULL ) {
  46. free(m_pSeriesBase);
  47. };
  48. if( m_pEntryFolder!= NULL ) {
  49. free(m_pEntryFolder);
  50. };
  51. };
  52. bool storageProvider::startNewSeries( void ){
  53. char temp[128];
  54. //#ifdef DEBUG
  55. char Buffer[256];
  56. //#endif
  57. #ifdef __linux__
  58. sprintf( temp, "%lld", getMilliseconds() );
  59. #else
  60. sprintf( temp, "%I64u", getMilliseconds());
  61. #endif
  62. //#ifdef DEBUG
  63. sprintf(Buffer,"Stor: %s", temp);
  64. LOG(Buffer);
  65. //#endif
  66. sprintf( m_pSeriesBase, "%s", MD5String( temp ) );
  67. #ifdef DEBUG
  68. sprintf(Buffer,"Stor Ser Base: %s", m_pSeriesBase);
  69. LOG(Buffer);
  70. #endif
  71. m_nSeriesCounter = 0;
  72. return true;
  73. };
  74. bool storageProvider::startNewEntry( void ){
  75. char Buffer[256];
  76. if( fileExist( m_pBasePath ) == 0 ) {
  77. sprintf( Buffer, "No BasePath[%s] Detected", m_pBasePath );
  78. LOG(Buffer);
  79. return false;
  80. };
  81. sprintf( m_pCWD, "%s/%s", m_pBasePath ,getTStr((CSTR)"%Y"));
  82. sprintf( Buffer,"Test Base: %s", m_pCWD );
  83. LOG(Buffer);
  84. int RetVal;
  85. if( fileExist( m_pCWD ) == 0 ){
  86. #ifdef __linux__
  87. RetVal = mkdir( m_pCWD, STORAGE_DIR_MODE );
  88. #else
  89. RetVal = _mkdir( m_pCWD );
  90. #endif
  91. if( RetVal!= 0 ) {
  92. return false;
  93. };
  94. };
  95. sprintf( m_pCWD, "%s/%s", m_pBasePath ,getTStr((CSTR)"%Y/%m"));
  96. sprintf( Buffer,"Test Base: %s", m_pCWD );
  97. LOG(Buffer);
  98. if( fileExist( m_pCWD ) == 0 ){
  99. #ifdef __linux__
  100. RetVal = mkdir( m_pCWD, STORAGE_DIR_MODE );
  101. #else
  102. RetVal = _mkdir( m_pCWD );
  103. #endif
  104. if( RetVal!= 0 ) {
  105. return false;
  106. };
  107. };
  108. sprintf( m_pCWD, "%s/%s", m_pBasePath ,getTStr((CSTR)"%Y/%m/%d"));
  109. sprintf( Buffer,"Test Base: %s", m_pCWD );
  110. LOG(Buffer);
  111. if( fileExist( m_pCWD ) == 0 ){
  112. #ifdef __linux__
  113. RetVal = mkdir( m_pCWD, STORAGE_DIR_MODE );
  114. #else
  115. RetVal = _mkdir( m_pCWD );
  116. #endif
  117. if( RetVal!= 0 ) {
  118. return false;
  119. };
  120. };
  121. sprintf( m_pEntryFolder, "%s", getTStr((CSTR)"%H%M%S"));
  122. char tVal[1024];
  123. sprintf( tVal, "%s/%s", m_pCWD , m_pEntryFolder );
  124. sprintf( Buffer,"Test Base: %s", tVal );
  125. LOG(Buffer);
  126. #ifdef __linux__
  127. RetVal = mkdir( tVal, STORAGE_DIR_MODE );
  128. #else
  129. RetVal = _mkdir( tVal );
  130. #endif
  131. if( RetVal!= 0 ) {
  132. return false;
  133. };
  134. return startNewSeries();
  135. };
  136. CSTR storageProvider::getNewFileName( void ){
  137. char tVal[1024];
  138. CSTR RetVal;
  139. m_mCounterProtectorMutex.lock();
  140. m_nSeriesCounter++;
  141. sprintf( tVal, "%s/%s/%s_%d", m_pCWD, m_pEntryFolder, m_pSeriesBase, m_nSeriesCounter );
  142. RetVal = (CSTR) malloc( strlen(tVal)+1);
  143. strncpy( RetVal, tVal, strlen(tVal) );
  144. RetVal[strlen(tVal)] = (char)0;
  145. m_mCounterProtectorMutex.unlock();
  146. return RetVal;
  147. };
  148. #undef LOG
  149. #undef LOG_DEBUG
  150. #undef LOG_ERROR