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