diff --git a/meshcore/agentcore.c b/meshcore/agentcore.c index 6b6ac4a..fdab684 100644 --- a/meshcore/agentcore.c +++ b/meshcore/agentcore.c @@ -4111,6 +4111,7 @@ int MeshAgent_AgentMode(MeshAgentHostContainer *agentHost, int paramLen, char ** int ixr = 0; int installFlag = 0; int fetchstate = 0; + int readonly = 0; for (ri = 0; ri < paramLen; ++ri) { @@ -4143,6 +4144,11 @@ int MeshAgent_AgentMode(MeshAgentHostContainer *agentHost, int paramLen, char ** { if (agentHost->masterDb == NULL) { agentHost->masterDb = ILibSimpleDataStore_CreateCachedOnly(); } ILibSimpleDataStore_Cached(agentHost->masterDb, param[ri] + 2, ix - 2, param[ri] + ix + 1, len - (ix + 1)); + if (ix - 2 == 8 && strncmp(param[ri] + 2, "readonly", 8) == 0 && strncmp(param[ri] + ix + 1, "1", 1) == 0) + { + // Read-only File System specified + readonly = 1; + } ++ixr; } } @@ -4218,7 +4224,7 @@ int MeshAgent_AgentMode(MeshAgentHostContainer *agentHost, int paramLen, char ** } else { - if (agentHost->masterDb == NULL || ILibSimpleDataStore_IsCacheOnly(agentHost->masterDb)) + if (agentHost->masterDb == NULL || (ILibSimpleDataStore_IsCacheOnly(agentHost->masterDb) && readonly == 0)) { void **data = (void**)ILibMemory_SmartAllocate(4 * sizeof(void*)); data[0] = agentHost; diff --git a/microstack/ILibSimpleDataStore.c b/microstack/ILibSimpleDataStore.c index 7bca8dc..4204a6d 100644 --- a/microstack/ILibSimpleDataStore.c +++ b/microstack/ILibSimpleDataStore.c @@ -106,6 +106,7 @@ typedef struct ILibSimpleDataStore_TableEntry } ILibSimpleDataStore_TableEntry; typedef struct ILibSimpleDataStore_CacheEntry { + char valueHash[SHA384HASHSIZE]; int valueLength; char value[]; }ILibSimpleDataStore_CacheEntry; @@ -146,6 +147,14 @@ void ILibSimpleDataStore_CachedEx(ILibSimpleDataStore dataStore, char* key, int ILibSimpleDataStore_CacheEntry *entry = (ILibSimpleDataStore_CacheEntry*)ILibMemory_Allocate(sizeof(ILibSimpleDataStore_CacheEntry) + valueLen, 0, NULL, NULL); entry->valueLength = valueLen; if (valueLen > 0) { memcpy_s(entry->value, valueLen, value, valueLen); } + if (vhash != NULL) + { + memcpy_s(entry->valueHash, sizeof(entry->valueHash), vhash, SHA384HASHSIZE); + } + else + { + ILibSimpleDataStore_SHA384(value, valueLen, entry->valueHash); + } ILibHashtable_Put(root->cacheTable, NULL, key, keyLen, entry); if (vhash != NULL) { ILibMemory_Free(key); } @@ -883,9 +892,32 @@ __EXPORT_TYPE int ILibSimpleDataStore_GetEx(ILibSimpleDataStore dataStore, char* __EXPORT_TYPE char* ILibSimpleDataStore_GetHashEx(ILibSimpleDataStore dataStore, char* key, int keyLen) { ILibSimpleDataStore_Root *root = (ILibSimpleDataStore_Root*)dataStore; - ILibSimpleDataStore_TableEntry *entry; + ILibSimpleDataStore_TableEntry *entry = NULL; if (root == NULL) return NULL; + if (root->cacheTable != NULL) + { + ILibSimpleDataStore_CacheEntry *centry = (ILibSimpleDataStore_CacheEntry*)ILibHashtable_Get(root->cacheTable, NULL, key, keyLen); + if (centry == NULL) + { + // Let's check if this is a compressed record entry + size_t tmplen = 0; + char *tmpkey = (char*)ILibMemory_SmartAllocate(keyLen + sizeof(uint32_t)); + memcpy_s(tmpkey, ILibMemory_Size(tmpkey), key, keyLen); + ((uint32_t*)(tmpkey + keyLen))[0] = crc32c(0, (unsigned char*)key, keyLen); + centry = (ILibSimpleDataStore_CacheEntry*)ILibHashtable_Get(root->cacheTable, NULL, tmpkey, (int)ILibMemory_Size(tmpkey)); + if (centry != NULL) + { + ILibMemory_Free(tmpkey); + return(centry->valueHash); + } + } + if (centry != NULL) + { + return(centry->valueHash); + } + } + entry = (ILibSimpleDataStore_TableEntry*)ILibHashtable_Get(root->keyTable, NULL, key, keyLen); if (entry == NULL) { @@ -1062,7 +1094,7 @@ __EXPORT_TYPE int ILibSimpleDataStore_Compact(ILibSimpleDataStore dataStore) void* state[2]; int retVal = 0; - if (root == NULL || root->dirtySize < root->minimumDirtySize) return 1; // Error + if (root == NULL || root->dirtySize < root->minimumDirtySize || root->filePath == NULL) return 1; // Error tmp = ILibString_Cat(root->filePath, -1, ".tmp", -1); // Create the name of the temporary data store // Start by opening a temporary .tmp file. Will be used to write the compacted data store.