Config.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * This is the source code of tgnet library v. 1.1
  3. * It is licensed under GNU GPL v. 2 or later.
  4. * You should have received a copy of the license in this archive (see LICENSE).
  5. *
  6. * Copyright Nikolai Kudashov, 2015-2018.
  7. */
  8. #include <sys/stat.h>
  9. #include <unistd.h>
  10. #include <errno.h>
  11. #include <cstring>
  12. #include "Config.h"
  13. #include "ConnectionsManager.h"
  14. #include "FileLog.h"
  15. #include "BuffersStorage.h"
  16. Config::Config(int32_t instance, std::string fileName) {
  17. instanceNum = instance;
  18. configPath = ConnectionsManager::getInstance(instanceNum).currentConfigPath + fileName;
  19. backupPath = configPath + ".bak";
  20. FILE *backup = fopen(backupPath.c_str(), "rb");
  21. if (backup != nullptr) {
  22. if (LOGS_ENABLED) DEBUG_D("Config(%p, %s) backup file found %s", this, configPath.c_str(), backupPath.c_str());
  23. fclose(backup);
  24. remove(configPath.c_str());
  25. rename(backupPath.c_str(), configPath.c_str());
  26. }
  27. }
  28. NativeByteBuffer *Config::readConfig() {
  29. NativeByteBuffer *buffer = nullptr;
  30. FILE *file = fopen(configPath.c_str(), "rb");
  31. if (file != nullptr) {
  32. fseek(file, 0, SEEK_END);
  33. long fileSize = ftell(file);
  34. if (fseek(file, 0, SEEK_SET)) {
  35. if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) failed fseek to begin, reopen it", this, configPath.c_str());
  36. fclose(file);
  37. file = fopen(configPath.c_str(), "rb");
  38. }
  39. uint32_t size = 0;
  40. size_t bytesRead = fread(&size, sizeof(uint32_t), 1, file);
  41. if (LOGS_ENABLED) DEBUG_D("Config(%p, %s) load, size = %u, fileSize = %u", this, configPath.c_str(), size, (uint32_t) fileSize);
  42. if (bytesRead > 0 && size > 0 && (int32_t) size < fileSize) {
  43. buffer = BuffersStorage::getInstance().getFreeBuffer(size);
  44. if (fread(buffer->bytes(), sizeof(uint8_t), size, file) != size) {
  45. buffer->reuse();
  46. buffer = nullptr;
  47. }
  48. }
  49. fclose(file);
  50. }
  51. return buffer;
  52. }
  53. void Config::writeConfig(NativeByteBuffer *buffer) {
  54. if (LOGS_ENABLED) DEBUG_D("Config(%p, %s) start write config", this, configPath.c_str());
  55. FILE *file = fopen(configPath.c_str(), "rb");
  56. FILE *backup = fopen(backupPath.c_str(), "rb");
  57. bool error = false;
  58. bool hasBackupFile = false;
  59. if (file != nullptr) {
  60. if (backup == nullptr) {
  61. fclose(file);
  62. if (rename(configPath.c_str(), backupPath.c_str()) != 0) {
  63. if (LOGS_ENABLED) DEBUG_E("Config(%p) unable to rename file %s to backup file %s", this, configPath.c_str(), backupPath.c_str());
  64. error = true;
  65. } else {
  66. hasBackupFile = true;
  67. }
  68. } else {
  69. fclose(file);
  70. fclose(backup);
  71. remove(configPath.c_str());
  72. }
  73. }
  74. if (error) {
  75. return;
  76. }
  77. file = fopen(configPath.c_str(), "wb");
  78. if (chmod(configPath.c_str(), 0660)) {
  79. if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) chmod failed", this, configPath.c_str());
  80. }
  81. if (file == nullptr) {
  82. if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) unable to open file for writing", this, configPath.c_str());
  83. return;
  84. }
  85. uint32_t size = buffer->position();
  86. if (fwrite(&size, sizeof(uint32_t), 1, file) == 1) {
  87. if (fwrite(buffer->bytes(), sizeof(uint8_t), size, file) != size) {
  88. if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) failed to write config data to file", this, configPath.c_str());
  89. error = true;
  90. }
  91. } else {
  92. if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) failed to write config size to file", this, configPath.c_str());
  93. error = true;
  94. }
  95. if (fflush(file)) {
  96. if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) fflush failed", this, configPath.c_str());
  97. error = true;
  98. }
  99. int fd = fileno(file);
  100. if (fd == -1) {
  101. if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) fileno failed", this, configPath.c_str());
  102. error = true;
  103. } else {
  104. if (LOGS_ENABLED) DEBUG_D("Config(%p, %s) fileno = %d", this, configPath.c_str(), fd);
  105. }
  106. if (fd != -1 && fsync(fd) == -1) {
  107. if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) fsync failed", this, configPath.c_str());
  108. error = true;
  109. }
  110. if (fclose(file)) {
  111. if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) fclose failed", this, configPath.c_str());
  112. error = true;
  113. }
  114. if (error) {
  115. if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) failed to write config", this, configPath.c_str());
  116. if (remove(configPath.c_str())) {
  117. if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) remove config failed", this, configPath.c_str());
  118. }
  119. } else {
  120. if (hasBackupFile && remove(backupPath.c_str())) {
  121. if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) remove backup failed, %s", this, backupPath.c_str(), strerror(errno));
  122. }
  123. }
  124. if (!error) {
  125. if (LOGS_ENABLED) DEBUG_D("Config(%p, %s) config write ok", this, configPath.c_str());
  126. }
  127. }