ConfigFileUtils.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2015 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "jfile.hpp"
  14. #include "jmutex.hpp"
  15. #include "ConfigFileUtils.hpp"
  16. using namespace CONFIGURATOR;
  17. CConfigFileUtils* CConfigFileUtils::getInstance()
  18. {
  19. static Owned<CConfigFileUtils> s_configFileSingleton;
  20. static CSingletonLock slock;
  21. if (slock.lock() == true)
  22. {
  23. if (s_configFileSingleton.get() == NULL)
  24. s_configFileSingleton.setown(new CConfigFileUtils(DEFAULT_CONFIGURATION_PATH));
  25. slock.unlock();
  26. }
  27. return s_configFileSingleton.get();
  28. }
  29. CConfigFileUtils::CConfigFileUtils(const char* pDir, const char* pMask) : m_pDirPath(pDir), m_pMask(pMask)
  30. {
  31. }
  32. CConfigFileUtils::~CConfigFileUtils()
  33. {
  34. closeConfigurationFile();
  35. }
  36. enum CConfigFileUtils::CF_ERROR_CODES CConfigFileUtils::createNewConfigurationFile(const char* pConfigFileName)
  37. {
  38. notify(IConfigFileUtilsObserver::CF_FILE_CREATE_EVENT);
  39. return CF_NO_ERROR;
  40. }
  41. enum CConfigFileUtils::CF_ERROR_CODES CConfigFileUtils::openConfigurationFile(const char* pConfigFileName)
  42. {
  43. if (m_pFileIO.get() != NULL)
  44. return CF_FILE_ALREADY_OPEN;
  45. else if (m_pFileIO.get() == NULL)
  46. return CF_FILE_NOT_OPEN;
  47. else
  48. {
  49. m_pFile.setown(createIFile(pConfigFileName));
  50. m_pFileIO.setown(m_pFile->open(IFOcreaterw));
  51. notify(CONFIGURATOR::IConfigFileUtilsObserver::CF_FILE_OPEN_EVENT);
  52. return CF_NO_ERROR;
  53. }
  54. }
  55. enum CConfigFileUtils::CF_ERROR_CODES CConfigFileUtils::closeConfigurationFile()
  56. {
  57. if (m_pFileIO.get() != NULL)
  58. {
  59. m_pFileIO->close();
  60. m_pFileIO.clear();
  61. m_pFile.clear();
  62. notify(CONFIGURATOR::IConfigFileUtilsObserver::CF_FILE_CLOSE_EVENT);
  63. return CF_NO_ERROR;
  64. }
  65. else
  66. return CF_FILE_NOT_OPEN;
  67. }
  68. enum CConfigFileUtils::CF_ERROR_CODES CConfigFileUtils::writeToOpenConfigurationFile(const char* pBuffer, unsigned int length)
  69. {
  70. if (length == 0)
  71. return CF_WRITE_BUFFER_EMPTY;
  72. else if (m_pFile.get() == NULL)
  73. return CF_FILE_NOT_OPEN;
  74. else
  75. {
  76. m_pFileIO->write(0, length, pBuffer);
  77. notify(CONFIGURATOR::IConfigFileUtilsObserver::CF_FILE_WRITE_EVENT);
  78. return CF_NO_ERROR;
  79. }
  80. }
  81. enum CConfigFileUtils::CF_ERROR_CODES CConfigFileUtils::populateConfigFileArray()
  82. {
  83. m_configFileArray.kill();
  84. if (m_pDir.get() == NULL)
  85. {
  86. m_pDir.set(createIFile(m_pDirPath));
  87. if ( (m_pDir.get() == NULL) || (m_pDir->exists() != true) || (m_pDir->isDirectory() == false) )
  88. return CF_DIRECTORY_ACCESS_ERROR;
  89. }
  90. Owned<IDirectoryIterator> dirIter = m_pDir->directoryFiles(m_pMask);
  91. if(dirIter.get() != NULL)
  92. {
  93. IFile &file = dirIter->query();
  94. StringBuffer fname(file.queryFilename());
  95. getFileNameOnly(fname, false);
  96. m_configFileArray.append(fname);
  97. return CF_NO_ERROR;
  98. }
  99. else
  100. return CF_DIRECTORY_ACCESS_ERROR;
  101. }
  102. enum CConfigFileUtils::CF_ERROR_CODES CConfigFileUtils::writeConfigurationToFile(const char *pFilePath, const char* pBuffer, unsigned int length)
  103. {
  104. assert(pBuffer != NULL);
  105. assert(length != 0);
  106. assert(pBuffer != NULL && *pBuffer != 0);
  107. Owned<IFile> pFile;
  108. Owned<IFileIO> pFileIO;
  109. IFileIO *pFIO = NULL;
  110. pFile.setown(createIFile(pFilePath));
  111. try
  112. {
  113. pFIO = pFile->open(IFOcreaterw);
  114. }
  115. catch(IErrnoException *pException)
  116. {
  117. pException->Release();
  118. }
  119. if (pFIO == NULL)
  120. return CF_FILE_PERMISSIONS;
  121. pFileIO.setown(pFIO);
  122. pFileIO->write(0, length, pBuffer);
  123. notify(CONFIGURATOR::IConfigFileUtilsObserver::CF_FILE_WRITE_NO_CHECK);
  124. return CF_NO_ERROR;
  125. }