2014-05-07 22:27:00 +00:00
|
|
|
/*
|
2018-04-11 14:24:29 +00:00
|
|
|
Copyright (c) 2010-2018 Roger Light <roger@atchoo.org>
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
All rights reserved. This program and the accompanying materials
|
|
|
|
are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
and Eclipse Distribution License v1.0 which accompany this distribution.
|
|
|
|
|
|
|
|
The Eclipse Public License is available at
|
|
|
|
http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
and the Eclipse Distribution License is available at
|
|
|
|
http://www.eclipse.org/org/documents/edl-v10.php.
|
|
|
|
|
|
|
|
Contributors:
|
|
|
|
Roger Light - initial implementation and documentation.
|
|
|
|
*/
|
|
|
|
|
2015-04-29 20:37:47 +00:00
|
|
|
#include "config.h"
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
#ifdef WITH_PERSISTENCE
|
|
|
|
|
|
|
|
#ifndef WIN32
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2014-09-14 17:08:09 +00:00
|
|
|
#include <time.h>
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2016-07-08 09:10:04 +00:00
|
|
|
#include "mosquitto_broker_internal.h"
|
2015-04-29 20:37:47 +00:00
|
|
|
#include "memory_mosq.h"
|
|
|
|
#include "persist.h"
|
|
|
|
#include "time_mosq.h"
|
2014-05-07 22:27:00 +00:00
|
|
|
#include "util_mosq.h"
|
|
|
|
|
|
|
|
static uint32_t db_version;
|
|
|
|
|
2019-03-14 11:08:30 +00:00
|
|
|
const unsigned char magic[15] = {0x00, 0xB5, 0x00, 'm','o','s','q','u','i','t','t','o',' ','d','b'};
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
static int persist__restore_sub(struct mosquitto_db *db, const char *client_id, const char *sub, int qos);
|
2019-03-16 09:42:15 +00:00
|
|
|
int persist__read_string(FILE *db_fptr, char **str);
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
static struct mosquitto *persist__find_or_add_context(struct mosquitto_db *db, const char *client_id, uint16_t last_mid)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
struct mosquitto *context;
|
|
|
|
|
|
|
|
context = NULL;
|
2014-06-23 16:57:35 +00:00
|
|
|
HASH_FIND(hh_id, db->contexts_by_id, client_id, strlen(client_id), context);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!context){
|
2015-05-16 14:24:24 +00:00
|
|
|
context = context__init(db, -1);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!context) return NULL;
|
2015-04-19 21:10:59 +00:00
|
|
|
context->id = mosquitto__strdup(client_id);
|
2014-06-29 22:16:10 +00:00
|
|
|
if(!context->id){
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(context);
|
2014-06-23 16:57:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2018-11-27 10:02:10 +00:00
|
|
|
context->clean_start = false;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2014-06-23 16:57:35 +00:00
|
|
|
HASH_ADD_KEYPTR(hh_id, db->contexts_by_id, context->id, strlen(context->id), context);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
if(last_mid){
|
|
|
|
context->last_mid = last_mid;
|
|
|
|
}
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
|
|
|
|
int persist__read_string(FILE *db_fptr, char **str)
|
2019-01-31 21:50:42 +00:00
|
|
|
{
|
|
|
|
uint16_t i16temp;
|
|
|
|
uint16_t slen;
|
|
|
|
char *s = NULL;
|
|
|
|
|
|
|
|
if(fread(&i16temp, 1, sizeof(uint16_t), db_fptr) != sizeof(uint16_t)){
|
|
|
|
return MOSQ_ERR_INVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
slen = ntohs(i16temp);
|
|
|
|
if(slen){
|
|
|
|
s = mosquitto__malloc(slen+1);
|
|
|
|
if(!s){
|
|
|
|
fclose(db_fptr);
|
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
|
|
|
|
return MOSQ_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
if(fread(s, 1, slen, db_fptr) != slen){
|
|
|
|
mosquitto__free(s);
|
|
|
|
return MOSQ_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
s[slen] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
*str = s;
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
static int persist__client_msg_restore(struct mosquitto_db *db, const char *client_id, uint16_t mid, uint8_t qos, uint8_t retain, uint8_t direction, uint8_t state, uint8_t dup, uint64_t store_id)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
struct mosquitto_client_msg *cmsg;
|
2016-04-18 13:41:47 +00:00
|
|
|
struct mosquitto_client_msg **msgs, **last_msg;
|
2014-11-18 19:12:08 +00:00
|
|
|
struct mosquitto_msg_store_load *load;
|
2014-05-07 22:27:00 +00:00
|
|
|
struct mosquitto *context;
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
cmsg = mosquitto__malloc(sizeof(struct mosquitto_client_msg));
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!cmsg){
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
|
2014-05-07 22:27:00 +00:00
|
|
|
return MOSQ_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
2014-11-17 21:58:53 +00:00
|
|
|
cmsg->next = NULL;
|
2014-05-07 22:27:00 +00:00
|
|
|
cmsg->store = NULL;
|
|
|
|
cmsg->mid = mid;
|
|
|
|
cmsg->qos = qos;
|
|
|
|
cmsg->retain = retain;
|
2014-11-17 21:58:53 +00:00
|
|
|
cmsg->timestamp = 0;
|
2014-05-07 22:27:00 +00:00
|
|
|
cmsg->direction = direction;
|
|
|
|
cmsg->state = state;
|
|
|
|
cmsg->dup = dup;
|
|
|
|
|
2014-11-18 19:12:08 +00:00
|
|
|
HASH_FIND(hh, db->msg_store_load, &store_id, sizeof(dbid_t), load);
|
|
|
|
if(!load){
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(cmsg);
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error restoring persistent database, message store corrupt.");
|
2014-05-07 22:27:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2014-11-18 19:12:08 +00:00
|
|
|
cmsg->store = load->store;
|
2015-01-08 18:41:14 +00:00
|
|
|
cmsg->store->ref_count++;
|
2014-11-17 22:54:39 +00:00
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
context = persist__find_or_add_context(db, client_id, 0);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!context){
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(cmsg);
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error restoring persistent database, message store corrupt.");
|
2014-05-07 22:27:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2016-04-18 13:41:47 +00:00
|
|
|
|
|
|
|
if (state == mosq_ms_queued){
|
|
|
|
msgs = &(context->queued_msgs);
|
|
|
|
last_msg = &(context->last_queued_msg);
|
|
|
|
}else{
|
|
|
|
msgs = &(context->inflight_msgs);
|
|
|
|
last_msg = &(context->last_inflight_msg);
|
|
|
|
}
|
|
|
|
if(*msgs){
|
|
|
|
(*last_msg)->next = cmsg;
|
2014-05-07 22:27:00 +00:00
|
|
|
}else{
|
2016-04-18 13:41:47 +00:00
|
|
|
*msgs = cmsg;
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2016-04-18 13:41:47 +00:00
|
|
|
*last_msg = cmsg;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
static int persist__client_chunk_restore(struct mosquitto_db *db, FILE *db_fptr)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
struct mosquitto *context;
|
2019-03-16 09:42:15 +00:00
|
|
|
struct P_client chunk;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
memset(&chunk, 0, sizeof(struct P_client));
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
rc = persist__client_chunk_read_v234(db_fptr, &chunk, db_version);
|
|
|
|
if(rc){
|
|
|
|
fclose(db_fptr);
|
|
|
|
return rc;
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
context = persist__find_or_add_context(db, chunk.client_id, chunk.F.last_mid);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(context){
|
2019-03-16 09:42:15 +00:00
|
|
|
context->disconnect_t = chunk.F.disconnect_t;
|
2014-05-07 22:27:00 +00:00
|
|
|
}else{
|
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
mosquitto__free(chunk.client_id);
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
static int persist__client_msg_chunk_restore(struct mosquitto_db *db, FILE *db_fptr)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2019-03-16 09:42:15 +00:00
|
|
|
struct P_client_msg chunk;
|
2014-05-07 22:27:00 +00:00
|
|
|
int rc;
|
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
memset(&chunk, 0, sizeof(struct P_client_msg));
|
|
|
|
|
|
|
|
rc = persist__client_msg_chunk_read_v234(db_fptr, &chunk);
|
|
|
|
if(rc){
|
2014-05-07 22:27:00 +00:00
|
|
|
fclose(db_fptr);
|
2019-03-16 09:42:15 +00:00
|
|
|
return rc;
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
rc = persist__client_msg_restore(db, chunk.client_id, chunk.F.mid, chunk.F.qos,
|
|
|
|
chunk.F.retain, chunk.F.direction, chunk.F.state, chunk.F.dup, chunk.F.store_id);
|
|
|
|
mosquitto__free(chunk.client_id);
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
static int persist__msg_store_chunk_restore(struct mosquitto_db *db, FILE *db_fptr)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2019-03-16 09:42:15 +00:00
|
|
|
struct P_msg_store chunk;
|
2014-05-07 22:27:00 +00:00
|
|
|
struct mosquitto_msg_store *stored = NULL;
|
2014-11-18 19:12:08 +00:00
|
|
|
struct mosquitto_msg_store_load *load;
|
2019-03-16 09:42:15 +00:00
|
|
|
int rc = 0;
|
2019-02-09 13:52:09 +00:00
|
|
|
int i;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
memset(&chunk, 0, sizeof(struct P_msg_store));
|
2019-01-31 21:50:42 +00:00
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
rc = persist__msg_store_chunk_read_v234(db_fptr, &chunk, db_version);
|
2019-01-31 21:50:42 +00:00
|
|
|
if(rc){
|
2019-03-16 09:42:15 +00:00
|
|
|
fclose(db_fptr);
|
2019-01-31 21:50:42 +00:00
|
|
|
return rc;
|
|
|
|
}
|
2019-03-16 09:42:15 +00:00
|
|
|
|
|
|
|
if(chunk.F.source_port){
|
|
|
|
for(i=0; i<db->config->listener_count; i++){
|
|
|
|
if(db->config->listeners[i].port == chunk.F.source_port){
|
|
|
|
chunk.source.listener = &db->config->listeners[i];
|
|
|
|
break;
|
2019-01-31 21:50:42 +00:00
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-16 09:42:15 +00:00
|
|
|
load = mosquitto__malloc(sizeof(struct mosquitto_msg_store_load));
|
|
|
|
if(!load){
|
2019-01-31 21:50:42 +00:00
|
|
|
fclose(db_fptr);
|
2019-03-16 09:42:15 +00:00
|
|
|
mosquitto__free(chunk.source.id);
|
|
|
|
mosquitto__free(chunk.source.username);
|
|
|
|
mosquitto__free(chunk.topic);
|
|
|
|
UHPA_FREE(chunk.payload, chunk.F.payloadlen);
|
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
|
|
|
|
return MOSQ_ERR_NOMEM;
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2019-01-31 21:50:42 +00:00
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
rc = db__message_store(db, &chunk.source, chunk.F.source_mid,
|
|
|
|
chunk.topic, chunk.F.qos, chunk.F.payloadlen,
|
|
|
|
&chunk.payload, chunk.F.retain, &stored, 0, NULL, chunk.F.store_id);
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
mosquitto__free(chunk.source.id);
|
|
|
|
mosquitto__free(chunk.source.username);
|
|
|
|
chunk.source.id = NULL;
|
|
|
|
chunk.source.username = NULL;
|
2014-11-18 19:12:08 +00:00
|
|
|
|
2015-06-28 20:16:48 +00:00
|
|
|
if(rc == MOSQ_ERR_SUCCESS){
|
|
|
|
load->db_id = stored->db_id;
|
|
|
|
load->store = stored;
|
2014-11-18 19:12:08 +00:00
|
|
|
|
2015-06-28 20:16:48 +00:00
|
|
|
HASH_ADD(hh, db->msg_store_load, db_id, sizeof(dbid_t), load);
|
2016-03-18 11:54:36 +00:00
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}else{
|
|
|
|
mosquitto__free(load);
|
|
|
|
fclose(db_fptr);
|
|
|
|
return rc;
|
2015-06-28 20:16:48 +00:00
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
static int persist__retain_chunk_restore(struct mosquitto_db *db, FILE *db_fptr)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2014-11-18 19:12:08 +00:00
|
|
|
struct mosquitto_msg_store_load *load;
|
2019-03-16 09:42:15 +00:00
|
|
|
struct P_retain chunk;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
memset(&chunk, 0, sizeof(struct P_retain));
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
rc = persist__retain_chunk_read_v234(db_fptr, &chunk);
|
|
|
|
if(rc){
|
2014-05-07 22:27:00 +00:00
|
|
|
fclose(db_fptr);
|
2019-03-16 09:42:15 +00:00
|
|
|
return rc;
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2019-03-16 09:42:15 +00:00
|
|
|
|
|
|
|
HASH_FIND(hh, db->msg_store_load, &chunk.F.store_id, sizeof(dbid_t), load);
|
2014-11-18 19:12:08 +00:00
|
|
|
if(load){
|
2015-05-18 08:29:22 +00:00
|
|
|
sub__messages_queue(db, NULL, load->store->topic, load->store->qos, load->store->retain, &load->store);
|
2014-11-17 22:54:39 +00:00
|
|
|
}else{
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Corrupt database whilst restoring a retained message.");
|
2014-11-17 22:54:39 +00:00
|
|
|
return MOSQ_ERR_INVAL;
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
static int persist__sub_chunk_restore(struct mosquitto_db *db, FILE *db_fptr)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2019-03-16 09:42:15 +00:00
|
|
|
struct P_sub chunk;
|
|
|
|
int rc;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
memset(&chunk, 0, sizeof(struct P_sub));
|
2014-11-17 21:58:53 +00:00
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
rc = persist__sub_chunk_read_v234(db_fptr, &chunk);
|
2019-01-31 21:50:42 +00:00
|
|
|
if(rc){
|
|
|
|
fclose(db_fptr);
|
|
|
|
return rc;
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2014-11-17 21:58:53 +00:00
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
rc = persist__restore_sub(db, chunk.client_id, chunk.topic, chunk.F.qos);
|
|
|
|
|
|
|
|
mosquitto__free(chunk.client_id);
|
|
|
|
mosquitto__free(chunk.topic);
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
int persist__restore(struct mosquitto_db *db)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
FILE *fptr;
|
|
|
|
char header[15];
|
|
|
|
int rc = 0;
|
|
|
|
uint32_t crc;
|
|
|
|
dbid_t i64temp;
|
|
|
|
uint32_t i32temp, length;
|
|
|
|
uint16_t i16temp, chunk;
|
|
|
|
uint8_t i8temp;
|
|
|
|
ssize_t rlen;
|
2018-11-07 17:43:21 +00:00
|
|
|
char *err;
|
2014-11-18 19:12:08 +00:00
|
|
|
struct mosquitto_msg_store_load *load, *load_tmp;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
assert(db);
|
|
|
|
assert(db->config);
|
2019-03-14 20:57:25 +00:00
|
|
|
|
|
|
|
if(!db->config->persistence || db->config->persistence_filepath == NULL){
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2014-11-18 19:12:08 +00:00
|
|
|
db->msg_store_load = NULL;
|
|
|
|
|
2017-07-16 21:52:01 +00:00
|
|
|
fptr = mosquitto__fopen(db->config->persistence_filepath, "rb", false);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(fptr == NULL) return MOSQ_ERR_SUCCESS;
|
2017-01-06 00:44:17 +00:00
|
|
|
rlen = fread(&header, 1, 15, fptr);
|
|
|
|
if(rlen == 0){
|
|
|
|
fclose(fptr);
|
2017-03-06 21:19:53 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Persistence file is empty.");
|
2017-01-06 00:44:17 +00:00
|
|
|
return 0;
|
|
|
|
}else if(rlen != 15){
|
|
|
|
goto error;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!memcmp(header, magic, 15)){
|
|
|
|
// Restore DB as normal
|
|
|
|
read_e(fptr, &crc, sizeof(uint32_t));
|
|
|
|
read_e(fptr, &i32temp, sizeof(uint32_t));
|
|
|
|
db_version = ntohl(i32temp);
|
|
|
|
/* IMPORTANT - this is where compatibility checks are made.
|
|
|
|
* Is your DB change still compatible with previous versions?
|
|
|
|
*/
|
|
|
|
if(db_version > MOSQ_DB_VERSION && db_version != 0){
|
2019-01-31 21:50:42 +00:00
|
|
|
if(db_version == 3){
|
|
|
|
/* Addition of source_username and source_port to msg_store chunk in v4, v1.5.6 */
|
|
|
|
}else if(db_version == 2){
|
2014-05-07 22:27:00 +00:00
|
|
|
/* Addition of disconnect_t to client chunk in v3. */
|
|
|
|
}else{
|
|
|
|
fclose(fptr);
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unsupported persistent database format version %d (need version %d).", db_version, MOSQ_DB_VERSION);
|
2014-05-07 22:27:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while(rlen = fread(&i16temp, sizeof(uint16_t), 1, fptr), rlen == 1){
|
|
|
|
chunk = ntohs(i16temp);
|
|
|
|
read_e(fptr, &i32temp, sizeof(uint32_t));
|
|
|
|
length = ntohl(i32temp);
|
|
|
|
switch(chunk){
|
|
|
|
case DB_CHUNK_CFG:
|
|
|
|
read_e(fptr, &i8temp, sizeof(uint8_t)); // shutdown
|
|
|
|
read_e(fptr, &i8temp, sizeof(uint8_t)); // sizeof(dbid_t)
|
|
|
|
if(i8temp != sizeof(dbid_t)){
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Incompatible database configuration (dbid size is %d bytes, expected %lu)",
|
2014-05-07 22:27:00 +00:00
|
|
|
i8temp, (unsigned long)sizeof(dbid_t));
|
|
|
|
fclose(fptr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
read_e(fptr, &i64temp, sizeof(dbid_t));
|
|
|
|
db->last_db_id = i64temp;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DB_CHUNK_MSG_STORE:
|
2015-05-16 14:24:24 +00:00
|
|
|
if(persist__msg_store_chunk_restore(db, fptr)) return 1;
|
2014-05-07 22:27:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DB_CHUNK_CLIENT_MSG:
|
2015-05-16 14:24:24 +00:00
|
|
|
if(persist__client_msg_chunk_restore(db, fptr)) return 1;
|
2014-05-07 22:27:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DB_CHUNK_RETAIN:
|
2015-05-16 14:24:24 +00:00
|
|
|
if(persist__retain_chunk_restore(db, fptr)) return 1;
|
2014-05-07 22:27:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DB_CHUNK_SUB:
|
2015-05-16 14:24:24 +00:00
|
|
|
if(persist__sub_chunk_restore(db, fptr)) return 1;
|
2014-05-07 22:27:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DB_CHUNK_CLIENT:
|
2015-05-16 14:24:24 +00:00
|
|
|
if(persist__client_chunk_restore(db, fptr)) return 1;
|
2014-05-07 22:27:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Unsupported chunk \"%d\" in persistent database file. Ignoring.", chunk);
|
2014-05-07 22:27:00 +00:00
|
|
|
fseek(fptr, length, SEEK_CUR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(rlen < 0) goto error;
|
|
|
|
}else{
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to restore persistent database. Unrecognised file format.");
|
2014-05-07 22:27:00 +00:00
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fptr);
|
|
|
|
|
2014-11-18 19:12:08 +00:00
|
|
|
HASH_ITER(hh, db->msg_store_load, load, load_tmp){
|
|
|
|
HASH_DELETE(hh, db->msg_store_load, load);
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(load);
|
2014-11-18 19:12:08 +00:00
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
return rc;
|
|
|
|
error:
|
2018-11-07 17:43:21 +00:00
|
|
|
err = strerror(errno);
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", err);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(fptr) fclose(fptr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
static int persist__restore_sub(struct mosquitto_db *db, const char *client_id, const char *sub, int qos)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
struct mosquitto *context;
|
|
|
|
|
|
|
|
assert(db);
|
|
|
|
assert(client_id);
|
|
|
|
assert(sub);
|
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
context = persist__find_or_add_context(db, client_id, 0);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!context) return 1;
|
2018-12-20 15:32:43 +00:00
|
|
|
/* FIXME - identifer, options need saving */
|
|
|
|
return sub__add(db, context, sub, qos, 0, 0, &db->subs);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|