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.

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