Telegram-Android/TMessagesProj/jni/tgnet/Config.cpp

132 lines
4.9 KiB
C++
Raw Permalink Normal View History

2015-09-24 22:52:02 +02:00
/*
2018-07-30 04:07:02 +02:00
* This is the source code of tgnet library v. 1.1
2015-09-24 22:52:02 +02:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2018-07-30 04:07:02 +02:00
* Copyright Nikolai Kudashov, 2015-2018.
2015-09-24 22:52:02 +02:00
*/
2015-11-26 22:04:02 +01:00
#include <sys/stat.h>
#include <unistd.h>
2018-07-30 04:07:02 +02:00
#include <errno.h>
#include <cstring>
2015-09-24 22:52:02 +02:00
#include "Config.h"
#include "ConnectionsManager.h"
#include "FileLog.h"
#include "BuffersStorage.h"
2018-07-30 04:07:02 +02:00
Config::Config(int32_t instance, std::string fileName) {
instanceNum = instance;
configPath = ConnectionsManager::getInstance(instanceNum).currentConfigPath + fileName;
2015-09-24 22:52:02 +02:00
backupPath = configPath + ".bak";
FILE *backup = fopen(backupPath.c_str(), "rb");
if (backup != nullptr) {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_D("Config(%p, %s) backup file found %s", this, configPath.c_str(), backupPath.c_str());
2015-11-26 22:04:02 +01:00
fclose(backup);
2015-09-24 22:52:02 +02:00
remove(configPath.c_str());
rename(backupPath.c_str(), configPath.c_str());
}
}
NativeByteBuffer *Config::readConfig() {
NativeByteBuffer *buffer = nullptr;
FILE *file = fopen(configPath.c_str(), "rb");
if (file != nullptr) {
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
2015-11-26 22:04:02 +01:00
if (fseek(file, 0, SEEK_SET)) {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) failed fseek to begin, reopen it", this, configPath.c_str());
2015-11-26 22:04:02 +01:00
fclose(file);
file = fopen(configPath.c_str(), "rb");
}
2015-09-24 22:52:02 +02:00
uint32_t size = 0;
2018-07-30 04:07:02 +02:00
size_t bytesRead = fread(&size, sizeof(uint32_t), 1, file);
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_D("Config(%p, %s) load, size = %u, fileSize = %u", this, configPath.c_str(), size, (uint32_t) fileSize);
2015-09-24 22:52:02 +02:00
if (bytesRead > 0 && size > 0 && (int32_t) size < fileSize) {
buffer = BuffersStorage::getInstance().getFreeBuffer(size);
if (fread(buffer->bytes(), sizeof(uint8_t), size, file) != size) {
buffer->reuse();
buffer = nullptr;
}
}
fclose(file);
}
return buffer;
}
void Config::writeConfig(NativeByteBuffer *buffer) {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_D("Config(%p, %s) start write config", this, configPath.c_str());
2015-09-24 22:52:02 +02:00
FILE *file = fopen(configPath.c_str(), "rb");
FILE *backup = fopen(backupPath.c_str(), "rb");
bool error = false;
2018-07-30 04:07:02 +02:00
bool hasBackupFile = false;
2015-09-24 22:52:02 +02:00
if (file != nullptr) {
if (backup == nullptr) {
fclose(file);
if (rename(configPath.c_str(), backupPath.c_str()) != 0) {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_E("Config(%p) unable to rename file %s to backup file %s", this, configPath.c_str(), backupPath.c_str());
2015-09-24 22:52:02 +02:00
error = true;
2018-07-30 04:07:02 +02:00
} else {
hasBackupFile = true;
2015-09-24 22:52:02 +02:00
}
} else {
fclose(file);
fclose(backup);
remove(configPath.c_str());
}
}
if (error) {
return;
}
file = fopen(configPath.c_str(), "wb");
2015-11-26 22:04:02 +01:00
if (chmod(configPath.c_str(), 0660)) {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) chmod failed", this, configPath.c_str());
2015-11-26 22:04:02 +01:00
}
2015-09-24 22:52:02 +02:00
if (file == nullptr) {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) unable to open file for writing", this, configPath.c_str());
2015-09-24 22:52:02 +02:00
return;
}
uint32_t size = buffer->position();
if (fwrite(&size, sizeof(uint32_t), 1, file) == 1) {
if (fwrite(buffer->bytes(), sizeof(uint8_t), size, file) != size) {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) failed to write config data to file", this, configPath.c_str());
2015-09-24 22:52:02 +02:00
error = true;
}
} else {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) failed to write config size to file", this, configPath.c_str());
2015-11-26 22:04:02 +01:00
error = true;
}
if (fflush(file)) {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) fflush failed", this, configPath.c_str());
2015-11-26 22:04:02 +01:00
error = true;
}
int fd = fileno(file);
if (fd == -1) {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) fileno failed", this, configPath.c_str());
2015-11-26 22:04:02 +01:00
error = true;
} else {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_D("Config(%p, %s) fileno = %d", this, configPath.c_str(), fd);
2015-11-26 22:04:02 +01:00
}
if (fd != -1 && fsync(fd) == -1) {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) fsync failed", this, configPath.c_str());
2015-11-26 22:04:02 +01:00
error = true;
}
if (fclose(file)) {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) fclose failed", this, configPath.c_str());
2015-09-24 22:52:02 +02:00
error = true;
}
if (error) {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) failed to write config", this, configPath.c_str());
2015-11-26 22:04:02 +01:00
if (remove(configPath.c_str())) {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) remove config failed", this, configPath.c_str());
2015-11-26 22:04:02 +01:00
}
2015-09-24 22:52:02 +02:00
} else {
2018-07-30 04:07:02 +02:00
if (hasBackupFile && remove(backupPath.c_str())) {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_E("Config(%p, %s) remove backup failed, %s", this, backupPath.c_str(), strerror(errno));
2015-11-26 22:04:02 +01:00
}
}
if (!error) {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_D("Config(%p, %s) config write ok", this, configPath.c_str());
2015-09-24 22:52:02 +02:00
}
}