diff options
author | Ingo Huerner <ingo.huerner@xse.de> | 2015-01-22 11:24:12 +0100 |
---|---|---|
committer | Ingo Huerner <ingo.huerner@xse.de> | 2015-01-22 11:24:12 +0100 |
commit | b596d8b27f952ab1bbc049b384c465d2e139e135 (patch) | |
tree | 75aaef2b3229939929eaf55b449b7b387ac83d23 | |
parent | ddccbec6d938dd3b7b8a644d5a191d1f673173a0 (diff) | |
download | persistence-client-library-b596d8b27f952ab1bbc049b384c465d2e139e135.tar.gz |
Minor changes after code review; replaced static arrays for handles by a tree
30 files changed, 673 insertions, 441 deletions
@@ -28,6 +28,7 @@ The client library has the following dependencies ** automotive-dlt (http://projects.genivi.org/diagnostic-log-trace/) ** Persistence Common Object (http://git.projects.genivi.org/?p=persistence/persistence-common-object.git;a=summary) Add "-- with_database=key-value-store" to the configure step + The default backend if Itzam/C and will not supported anymore. ** dbus-1 ** check unit test framework for C used when configured with "--enable-tests" Ubuntu: use Synaptic package manager @@ -37,7 +38,7 @@ The client library has the following dependencies ---------------------------- -Dependencies +Build the component ---------------------------- The Persistence Client Lib component uses automake to build the library. @@ -46,10 +47,7 @@ Execute the following steps in order to build the component * configure, with the following options ** --enable-tests to enable the build of the tests ** --enable-pasinterface enable the PAS interface (disabled by default) -** -- with_database decide which storage backend to use ** --enable-appcheck, performs an application check if the using application is a valid/trusted one. -*** Itzam/C is currently default backend -*** It is recommended to use the key-value-store backend ("-- with_database=key-value-store") * make diff --git a/include/persistence_client_custom.h b/include/persistence_client_custom.h index b0f9cfe..d0cd0e0 100644 --- a/include/persistence_client_custom.h +++ b/include/persistence_client_custom.h @@ -19,7 +19,6 @@ * Library provides an plugin API to extend persistence client library * @par change history * Date Author Version Description - * - 2015.01.16 ihuerner 1.7.0.0 Added function prototypes for function loaded from persistence common object * - 2015.01.14 ihuerner 1.6.0.0 Extended header documentation for function plugin_init_async. * - 2014.01.20 iieremie 1.6.0.0 multiple extensions: * - error codes @@ -99,99 +98,6 @@ The lower significant byte is equal 0 for released version only * \{ */ - - -/** - * \brief Obtain a handler to DB indicated by dbPathname - * \note : DB is created if it does not exist and (bForceCreationIfNotPresent != 0) - * - * \param dbPathname [in] absolute path to database (length limited to \ref PERS_ORG_MAX_LENGTH_PATH_FILENAME) - * \param bForceCreationIfNotPresent [in] if !=0x0, the database is created if it does not exist - * - * \return >= 0 for valid handler, negative value for error (\ref PERS_COM_ERROR_CODES_DEFINES) - */ -signed int persComDbOpen(char const * dbPathname, unsigned char bForceCreationIfNotPresent) ; - -/** - * \brief Close handler to DB - * - * \param handlerDB [in] handler obtained with persComDbOpen - * - * \return 0 for success, negative value for error (\ref PERS_COM_ERROR_CODES_DEFINES) - */ -signed int persComDbClose(signed int handlerDB) ; - -/** - * \brief write a key-value pair into local/shared database - * - * \param handlerDB [in] handler obtained with persComDbOpen - * \param key [in] key's name (length limited to \ref PERS_DB_MAX_LENGTH_KEY_NAME) - * \param data [in] buffer with key's data - * \param dataSize [in] size of key's data (max allowed \ref PERS_DB_MAX_SIZE_KEY_DATA) - * - * \return 0 for success, negative value otherwise (\ref PERS_COM_ERROR_CODES_DEFINES) - */ -signed int persComDbWriteKey(signed int handlerDB, char const * key, char const * data, signed int dataSize) ; - - -/** - * \brief read a key's value from local/shared database - * - * \param handlerDB [in] handler obtained with persComDbOpen - * \param key [in] key's name (length limited to \ref PERS_DB_MAX_LENGTH_KEY_NAME) - * \param dataBuffer_out [out]buffer where to return the read data - * \param dataBufferSize [in] size of dataBuffer_out - * - * \return read size, or negative value in case of error (\ref PERS_COM_ERROR_CODES_DEFINES) - */ -signed int persComDbReadKey(signed int handlerDB, char const * key, char* dataBuffer_out, signed int dataBufferSize) ; - -/** - * \brief read a key's value from local/shared database - * - * \param handlerDB [in] handler obtained with persComDbOpen - * \param key [in] key's name (length limited to \ref PERS_DB_MAX_LENGTH_KEY_NAME) - * - * \return key's size, or negative value in case of error (\ref PERS_COM_ERROR_CODES_DEFINES) - */ -signed int persComDbGetKeySize(signed int handlerDB, char const * key) ; - -/** - * \brief delete key from local/shared database - * - * \param handlerDB [in] handler obtained with persComDbOpen - * \param key [in] key's name (length limited to \ref PERS_DB_MAX_LENGTH_KEY_NAME) - * - * \return 0 for success, negative value otherwise (\ref PERS_COM_ERROR_CODES_DEFINES) - */ -signed int persComDbDeleteKey(signed int handlerDB, char const * key) ; - - -/** - * \brief Find the buffer's size needed to accomodate the list of keys' names in local/shared database - * - * \param handlerDB [in] handler obtained with persComDbOpen - * - * \return needed size, or negative value in case of error (\ref PERS_COM_ERROR_CODES_DEFINES) - */ -signed int persComDbGetSizeKeysList(signed int handlerDB) ; - - -/** - * \brief Obtain the list of the keys' names in local/shared database - * \note : keys in the list are separated by '\0' - * - * \param handlerDB [in] handler obtained with persComDbOpen - * \param listBuffer_out [out]buffer where to return the list of keys - * \param listBufferSize [in] size of listingBuffer_out - * \return >=0 for size of the list, or negative value in case of error (\ref PERS_COM_ERROR_CODES_DEFINES) - */ -signed int persComDbGetKeysList(signed int handlerDB, char* listBuffer_out, signed int listBufferSize) ; - - - - - /** * @brief typdef of callback function prototype for asynchronous init/deinit * diff --git a/src/Makefile.am b/src/Makefile.am index 2484658..261cb26 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -19,9 +19,6 @@ lib_LTLIBRARIES = libpersistence_client_library.la libpersistence_client_library_la_LIBADD = $(DEPS_LIBS) $(PFC_LIBS) -ldl -#if USE_XSTRACE_PERS -#libpersistence_client_library_la_LIBADD += -lxsm_user -#endif libpersistence_client_library_la_SOURCES = \ persistence_client_library.c \ @@ -37,6 +34,7 @@ libpersistence_client_library_la_SOURCES = \ persistence_client_library_data_organization.c \ persistence_client_library_backup_filelist.c \ persistence_client_library_dbus_cmd.c \ + persistence_client_library_tree_helper.c \ crc32.c \ rbtree.c diff --git a/src/persistence_client_library.c b/src/persistence_client_library.c index f6e6425..e6ad3af 100644 --- a/src/persistence_client_library.c +++ b/src/persistence_client_library.c @@ -87,6 +87,8 @@ void doInitAppcheck(const char* appName) gAppCheckFlag = 0; // currently not a "trusted" application DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("initLibrary - app check: "), DLT_STRING(appName), DLT_STRING("NOT trusted app")); } +#else + (void)appName; #endif } diff --git a/src/persistence_client_library_backup_filelist.c b/src/persistence_client_library_backup_filelist.c index 6853e23..68165b6 100644 --- a/src/persistence_client_library_backup_filelist.c +++ b/src/persistence_client_library_backup_filelist.c @@ -16,12 +16,11 @@ * @see */ + #include "persistence_client_library_backup_filelist.h" -#include "persistence_client_library_handle.h" +#include "crc32.h" #include "rbtree.h" -#include "crc32.h" -#include "persistence_client_library_data_organization.h" #if USE_FILECACHE @@ -70,24 +69,24 @@ static void fillFileBackupCharTokenArray(unsigned int customConfigFileSize, char while(i < customConfigFileSize) { - if( ((unsigned int)*tmpPointer < 127) + if( ((unsigned int)(*tmpPointer) < 127) && ((unsigned int)*tmpPointer >= 0)) - { - if(1 != gCharLookup[(unsigned int)*tmpPointer]) - { - *tmpPointer = 0; - - if(blankCount >= TOKENARRAYSIZE) // check if we are at the end of the token array - { - break; - } - gpTokenArray[blankCount] = tmpPointer+1; - blankCount++; - tokenCounter++; - } - } + { + if(1 != gCharLookup[(unsigned int)*tmpPointer]) + { + *tmpPointer = 0; + + if(blankCount >= TOKENARRAYSIZE) // check if we are at the end of the token array + { + break; + } + gpTokenArray[blankCount] = tmpPointer+1; + blankCount++; + tokenCounter++; + } + } tmpPointer++; - i++; + i++; } } @@ -103,32 +102,31 @@ static void createAndStoreFileNames() if(gRb_tree_bl != NULL) { - while( i < TOKENARRAYSIZE ) - { - if(gpTokenArray[i+1] != 0 ) - { - memset(path, 0, sizeof(path)); - snprintf(path, 128, "%s", gpTokenArray[i]); // storage type - - item = malloc(sizeof(key_value_s)); // asign key and value to the rbtree item - if(item != NULL) - { - //printf("createAndStoreFileNames => path: %s\n", path); - item->key = pclCrc32(0, (unsigned char*)path, strlen(path)); - // we don't need the path name here, we just need to know that this key is available in the tree - item->value = ""; - jsw_rbinsert(gRb_tree_bl, item); - free(item); - } - i+=1; - } - else - { - break; - } + while( i < TOKENARRAYSIZE ) + { + if(gpTokenArray[i+1] != 0 ) + { + memset(path, 0, sizeof(path)); + snprintf(path, 128, "%s", gpTokenArray[i]); // storage type + + item = malloc(sizeof(key_value_s)); // asign key and value to the rbtree item + if(item != NULL) + { + //printf("createAndStoreFileNames => path: %s\n", path); + item->key = pclCrc32(0, (unsigned char*)path, strlen(path)); + // we don't need the path name here, we just need to know that this key is available in the tree + item->value = ""; + jsw_rbinsert(gRb_tree_bl, item); + free(item); + } + i+=1; + } + else + { + break; + } } } - } @@ -138,56 +136,56 @@ int readBlacklistConfigFile(const char* filename) if(filename != NULL) { - struct stat buffer; - - memset(&buffer, 0, sizeof(buffer)); - if(stat(filename, &buffer) != -1) - { - if(buffer.st_size > 0) // check for empty file - { - char* configFileMap = 0; - int fd = open(filename, O_RDONLY); - - if(fd == -1) - { - DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("blacklist - Err file open"), DLT_STRING(filename), DLT_STRING(strerror(errno)) ); - return EPERS_COMMON; - } - - configFileMap = (char*)mmap(0, buffer.st_size, PROT_WRITE, MAP_PRIVATE, fd, 0); // map the configuration file into memory - - if(configFileMap == MAP_FAILED) - { - close(fd); - DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("blacklist - Err mapping file:"), DLT_STRING(filename), DLT_STRING(strerror(errno)) ); - - return EPERS_COMMON; - } - - fillFileBackupCharTokenArray(buffer.st_size, configFileMap); - - createAndStoreFileNames(); // create filenames and store them in the tree - - munmap(configFileMap, buffer.st_size); - - close(fd); - } - else - { - DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("blacklist - Err config file size is 0:"), DLT_STRING(filename)); - return EPERS_COMMON; - } - } - else - { - DLT_LOG(gPclDLTContext, DLT_LOG_DEBUG, DLT_STRING("blacklist - failed to stat() conf file:"), DLT_STRING(filename)); - return EPERS_COMMON; - } - } + struct stat buffer; + + memset(&buffer, 0, sizeof(buffer)); + if(stat(filename, &buffer) != -1) + { + if(buffer.st_size > 0) // check for empty file + { + char* configFileMap = 0; + int fd = open(filename, O_RDONLY); + + if(fd == -1) + { + DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("blacklist - Err file open"), DLT_STRING(filename), DLT_STRING(strerror(errno)) ); + return EPERS_COMMON; + } + + configFileMap = (char*)mmap(0, buffer.st_size, PROT_WRITE, MAP_PRIVATE, fd, 0); // map the configuration file into memory + + if(configFileMap == MAP_FAILED) + { + close(fd); + DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("blacklist - Err mapping file:"), DLT_STRING(filename), DLT_STRING(strerror(errno)) ); + + return EPERS_COMMON; + } + + fillFileBackupCharTokenArray(buffer.st_size, configFileMap); + + createAndStoreFileNames(); // create filenames and store them in the tree + + munmap(configFileMap, buffer.st_size); + + close(fd); + } + else + { + DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("blacklist - Err config file size is 0:"), DLT_STRING(filename)); + return EPERS_COMMON; + } + } + else + { + DLT_LOG(gPclDLTContext, DLT_LOG_DEBUG, DLT_STRING("blacklist - failed to stat() conf file:"), DLT_STRING(filename)); + return EPERS_COMMON; + } + } else { - DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("blacklist - config file name is NULL:")); - rval = EPERS_COMMON; + DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("blacklist - config file name is NULL:")); + rval = EPERS_COMMON; } return rval; @@ -203,7 +201,7 @@ int need_backup_key(unsigned int key) item = malloc(sizeof(key_value_s)); if(item != NULL && gRb_tree_bl != NULL) { - key_value_s* foundItem = NULL; + key_value_s* foundItem = NULL; item->key = key; foundItem = (key_value_s*)jsw_rbfind(gRb_tree_bl, item); if(foundItem != NULL) @@ -215,7 +213,7 @@ int need_backup_key(unsigned int key) else { if(item!=NULL) - free(item); + free(item); rval = CREATE_BACKUP; } @@ -271,7 +269,6 @@ void* key_val_dup(void *p) strncpy(dst->value, src->value, value_size); } - return dst; } @@ -351,14 +348,15 @@ int pclCreateFile(const char* path, int chached) #if USE_FILECACHE if(chached == 0) - { - handle = open(createPath, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); - } + { + handle = open(createPath, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); + } else { - handle = pfcOpenFile(createPath, CreateFile); + handle = pfcOpenFile(createPath, CreateFile); } #else + (void)chached; handle = open(createPath, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); #endif } @@ -589,8 +587,8 @@ int pclCreateBackup(const char* dstPath, int srcfd, const char* csumPath, const { off_t curPos = 0; - curPos = lseek(srcfd, 0, SEEK_CUR); // remember the current position - lseek(srcfd, 0, SEEK_SET); // set to beginning of file + curPos = lseek(srcfd, 0, SEEK_CUR); // remember the current position + lseek(srcfd, 0, SEEK_SET); // set to beginning of file // copy data from one file to another if((readSize = pclBackupDoFileCopy(srcfd, dstFd)) == -1) @@ -625,7 +623,7 @@ int pclCalcCrc32Csum(int fd, char crc32sum[]) char* buf; struct stat statBuf; - fstat(fd, &statBuf); + (void)fstat(fd, &statBuf); buf = malloc((unsigned int)statBuf.st_size); if(buf != 0) @@ -642,7 +640,7 @@ int pclCalcCrc32Csum(int fd, char crc32sum[]) { unsigned int crc = 0; crc = pclCrc32(crc, (unsigned char*)buf, statBuf.st_size); - snprintf(crc32sum, ChecksumBufSize-1, "%x", crc); + (void)snprintf(crc32sum, ChecksumBufSize-1, "%x", crc); } lseek(fd, curPos, SEEK_SET); // set back to the position @@ -651,7 +649,7 @@ int pclCalcCrc32Csum(int fd, char crc32sum[]) } else { - rval = -1; + rval = -1; } } return rval; diff --git a/src/persistence_client_library_backup_filelist.h b/src/persistence_client_library_backup_filelist.h index 49d8a7c..5917945 100644 --- a/src/persistence_client_library_backup_filelist.h +++ b/src/persistence_client_library_backup_filelist.h @@ -19,9 +19,7 @@ * @see */ - -//#include "../include_protected/persistence_client_library_rc_table.h" -#include <persComRct.h> +#include "persistence_client_library_handle.h" /** diff --git a/src/persistence_client_library_custom_loader.c b/src/persistence_client_library_custom_loader.c index 8b9d58a..a634bd4 100644 --- a/src/persistence_client_library_custom_loader.c +++ b/src/persistence_client_library_custom_loader.c @@ -17,15 +17,10 @@ */ #include "persistence_client_library_custom_loader.h" -#include "persistence_client_library_data_organization.h" -#include <stdio.h> #include <errno.h> -#include <fcntl.h> #include <sys/mman.h> #include <stdlib.h> -#include <string.h> -#include <unistd.h> #include <sys/stat.h> #include <dlfcn.h> diff --git a/src/persistence_client_library_custom_loader.h b/src/persistence_client_library_custom_loader.h index 7002317..49be2b9 100644 --- a/src/persistence_client_library_custom_loader.h +++ b/src/persistence_client_library_custom_loader.h @@ -20,7 +20,8 @@ */ #include "../include/persistence_client_custom.h" -#include <persComRct.h> + +#include "persistence_client_library_data_organization.h" /// enumerator used to identify the policy to manage the data typedef enum _PersistenceCustomLibs_e @@ -50,7 +51,7 @@ typedef enum _PersistenceCustomLibs_e /// enumerator fo custom library defines -enum _PersCustomLibDefines_e +typedef enum _PersCustomLibDefines_e { /// the custom library path size PersCustomPathSize = 12 diff --git a/src/persistence_client_library_data_organization.c b/src/persistence_client_library_data_organization.c index 28d9d9a..78b7069 100644 --- a/src/persistence_client_library_data_organization.c +++ b/src/persistence_client_library_data_organization.c @@ -18,11 +18,6 @@ #include "persistence_client_library_data_organization.h" -#include <string.h> -#include <stdio.h> -#include <stdlib.h> - - /// path for the backup location const char* gBackupPrefix = PERS_ORG_ROOT_PATH "/mnt-backup/"; diff --git a/src/persistence_client_library_data_organization.h b/src/persistence_client_library_data_organization.h index 85c0d4d..c735ef9 100644 --- a/src/persistence_client_library_data_organization.h +++ b/src/persistence_client_library_data_organization.h @@ -33,11 +33,8 @@ extern "C" { #include <persComDbAccess.h> #include <persComDataOrg.h> -#include <dlt/dlt.h> -#include <dlt/dlt_common.h> - +#include <dlt.h> #include <string.h> -#include <stdio.h> @@ -178,7 +175,7 @@ enum _PersistenceConstantDef /// max application name MaxAppNameLen = PERS_RCT_MAX_LENGTH_RESPONSIBLE, /// max number of parallel open persistence handles - MaxPersHandle = 256, + MaxPersHandle = 255, /// length of the config key responsible name MaxConfKeyLengthResp = 32, /// length of the config key custom name diff --git a/src/persistence_client_library_db_access.c b/src/persistence_client_library_db_access.c index 075f83e..adcd4ec 100644 --- a/src/persistence_client_library_db_access.c +++ b/src/persistence_client_library_db_access.c @@ -23,12 +23,7 @@ #include "persistence_client_library_prct_access.h" #include <persComErrors.h> -#include <persComDataOrg.h> -#include <dbus/dbus.h> -#include <string.h> -#include <stdio.h> -#include <stdlib.h> #include <errno.h> diff --git a/src/persistence_client_library_dbus_cmd.c b/src/persistence_client_library_dbus_cmd.c index aece878..c471fea 100644 --- a/src/persistence_client_library_dbus_cmd.c +++ b/src/persistence_client_library_dbus_cmd.c @@ -15,18 +15,15 @@ * @brief Implementation of the persistence client library dbus commands. * @see */ - -#include <errno.h> -#include <dlfcn.h> /* For dlclose() */ + /* For dlclose() */ #include "persistence_client_library_dbus_cmd.h" #include "persistence_client_library_handle.h" #include "persistence_client_library_custom_loader.h" -#include "persistence_client_library_prct_access.h" #include "persistence_client_library_pas_interface.h" -#include "persistence_client_library_data_organization.h" #include "persistence_client_library_db_access.h" + #if USE_FILECACHE #include <persistence_file_cache.h> @@ -40,6 +37,8 @@ extern int pfcWriteBackAndSync(int handle); #endif +#include <errno.h> +#include <dlfcn.h> // function prototype void msg_pending_func(DBusPendingCall *call, void *data); diff --git a/src/persistence_client_library_dbus_cmd.h b/src/persistence_client_library_dbus_cmd.h index 14a7cab..9efcf52 100644 --- a/src/persistence_client_library_dbus_cmd.h +++ b/src/persistence_client_library_dbus_cmd.h @@ -110,5 +110,4 @@ void process_send_pas_request(DBusConnection* conn, unsigned int requestID, int void process_send_pas_register(DBusConnection* conn, int regType, int notificationFlag); - #endif /* PERSISTENCE_CLIENT_LIBRARY_DBUS_CMD_H_ */ diff --git a/src/persistence_client_library_dbus_service.c b/src/persistence_client_library_dbus_service.c index 6b574ed..4232bbd 100644 --- a/src/persistence_client_library_dbus_service.c +++ b/src/persistence_client_library_dbus_service.c @@ -21,12 +21,8 @@ #include "persistence_client_library_lc_interface.h" #include "persistence_client_library_pas_interface.h" #include "persistence_client_library_dbus_cmd.h" -#include "persistence_client_library_data_organization.h" -#include <stdio.h> #include <errno.h> -#include <string.h> -#include <unistd.h> #include <stdlib.h> pthread_mutex_t gDbusPendingRegMtx = PTHREAD_MUTEX_INITIALIZER; diff --git a/src/persistence_client_library_dbus_service.h b/src/persistence_client_library_dbus_service.h index 17767cc..fa42ba3 100644 --- a/src/persistence_client_library_dbus_service.h +++ b/src/persistence_client_library_dbus_service.h @@ -20,6 +20,7 @@ */ #include <dbus/dbus.h> + #include <poll.h> #include <pthread.h> #include <sys/eventfd.h> diff --git a/src/persistence_client_library_file.c b/src/persistence_client_library_file.c index e0fcfce..956c8cc 100644 --- a/src/persistence_client_library_file.c +++ b/src/persistence_client_library_file.c @@ -22,8 +22,6 @@ #include "persistence_client_library_pas_interface.h" #include "persistence_client_library_handle.h" #include "persistence_client_library_prct_access.h" -#include "persistence_client_library_data_organization.h" -#include "persistence_client_library_db_access.h" #include "crc32.h" @@ -32,14 +30,10 @@ #endif -#include <fcntl.h> // for open flags #include <errno.h> -#include <string.h> -#include <unistd.h> #include <stdlib.h> #include <sys/mman.h> #include <sys/stat.h> -#include <sys/types.h> #include <sys/sendfile.h> // local function prototype @@ -87,6 +81,7 @@ int pclFileClose(int fd) { int permission = get_file_permission(fd); + if(permission != -1) // permission is here also used for range check { // check if a backup and checksum file needs to be deleted @@ -338,6 +333,7 @@ int pclFileOpenRegular(PersistenceInfo_s* dbContext, const char* resource_id, ch { if(set_file_handle_data(handle, PersistencePermission_ReadWrite, backupPath, csumPath, NULL) != -1) { + printf("%s - %d ==> set_file_backup_status\n", __FUNCTION__ , __LINE__); set_file_backup_status(handle, 1); __sync_fetch_and_add(&gOpenFdArray[handle], FileOpen); // set open flag } @@ -575,8 +571,10 @@ int pclFileWriteData(int fd, const void * buffer, int buffer_size) { if(permission != PersistencePermission_ReadOnly ) { + + // check if a backup file has to be created - if(get_file_backup_status(fd) == 0 && get_file_user_id(fd) != PCL_USER_DEFAULTDATA) + if( (get_file_backup_status(fd) == 0) && get_file_user_id(fd) != PCL_USER_DEFAULTDATA) { char csumBuf[ChecksumBufSize] = {0}; diff --git a/src/persistence_client_library_handle.c b/src/persistence_client_library_handle.c index 4ed1988..b5be100 100644 --- a/src/persistence_client_library_handle.c +++ b/src/persistence_client_library_handle.c @@ -18,10 +18,9 @@ */ #include "persistence_client_library_handle.h" +#include "persistence_client_library_tree_helper.h" #include <pthread.h> -#include <stdlib.h> -#include <string.h> pthread_mutex_t gKeyHandleAccessMtx = PTHREAD_MUTEX_INITIALIZER; @@ -42,14 +41,6 @@ static int gFreeHandleArray[MaxPersHandle] = { [0 ...MaxPersHandle-1] = 0 }; /// free handle array head index static int gFreeHandleIdxHead = 0; -// persistence key handle array -static PersistenceKeyHandle_s gKeyHandleArray[MaxPersHandle]; -// persistence file handle array -static PersistenceFileHandle_s gFileHandleArray[MaxPersHandle]; -// persistence handle array for OSS and third party handles -static PersistenceFileHandle_s gOssHandleArray[MaxPersHandle]; - - int get_persistence_handle_idx() { @@ -116,20 +107,30 @@ int set_key_handle_data(int idx, const char* id, unsigned int ldbid, unsigned i if(pthread_mutex_lock(&gKeyHandleAccessMtx) == 0) { - if((idx < MaxPersHandle) && (0 < idx)) - { - strncpy(gKeyHandleArray[idx].resource_id, id, DbResIDMaxLen); - gKeyHandleArray[idx].resource_id[DbResIDMaxLen-1] = '\0'; // Ensures 0-Termination - gKeyHandleArray[idx].ldbid = ldbid; - gKeyHandleArray[idx].user_no = user_no; - gKeyHandleArray[idx].seat_no = seat_no; - - handle = idx; - } - else - { - DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("sKeyHdata - idx out of bounds:"), DLT_INT(idx)); - } + KeyHandleTreeItem_s* item = NULL; + + if(gKeyHandleTree == NULL) + { + gKeyHandleTree = jsw_rbnew(kh_key_val_cmp, kh_key_val_dup, kh_key_val_rel); + } + + item = malloc(sizeof(KeyHandleTreeItem_s)); // assign key and value to the rbtree item + if(item != NULL) + { + item->key = idx; + + item->value.keyHandle.ldbid = ldbid; + item->value.keyHandle.user_no = user_no; + item->value.keyHandle.seat_no = seat_no; + strncpy(item->value.keyHandle.resource_id, id, DbResIDMaxLen); + item->value.keyHandle.resource_id[DbResIDMaxLen-1] = '\0'; // Ensures 0-Termination + + jsw_rbinsert(gKeyHandleTree, item); + + free(item); + + handle = idx; + } pthread_mutex_unlock(&gKeyHandleAccessMtx); } @@ -144,16 +145,26 @@ int get_key_handle_data(int idx, PersistenceKeyHandle_s* handleStruct) if(pthread_mutex_lock(&gKeyHandleAccessMtx) == 0) { - if((idx < MaxPersHandle) && (idx > 0)) - { - strncpy(handleStruct->resource_id, gKeyHandleArray[idx].resource_id, DbResIDMaxLen); - - handleStruct->ldbid = gKeyHandleArray[idx].ldbid; - handleStruct->user_no = gKeyHandleArray[idx].user_no; - handleStruct->seat_no = gKeyHandleArray[idx].seat_no; - - rval = 0; - } + if(gKeyHandleTree != NULL) + { + KeyHandleTreeItem_s* item = malloc(sizeof(KeyHandleTreeItem_s)); + if(item != NULL) + { + KeyHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (KeyHandleTreeItem_s*)jsw_rbfind(gKeyHandleTree, item); + if(foundItem != NULL) + { + handleStruct->ldbid = foundItem->value.keyHandle.ldbid; + handleStruct->user_no = foundItem->value.keyHandle.user_no; + handleStruct->seat_no = foundItem->value.keyHandle.seat_no; + strncpy(handleStruct->resource_id, foundItem->value.keyHandle.resource_id, DbResIDMaxLen); + handleStruct->resource_id[DbResIDMaxLen-1] = '\0'; // Ensures 0-Termination + rval = 0; + } + free(item); + } + } pthread_mutex_unlock(&gKeyHandleAccessMtx); } @@ -166,7 +177,12 @@ void init_key_handle_array() { if(pthread_mutex_lock(&gKeyHandleAccessMtx) == 0) { - memset(gKeyHandleArray, 0, MaxPersHandle * sizeof(PersistenceKeyHandle_s)); + if(gKeyHandleTree != NULL) + { + jsw_rbdelete (gKeyHandleTree); + } + + gKeyHandleTree = jsw_rbnew(kh_key_val_cmp, kh_key_val_dup, kh_key_val_rel); pthread_mutex_unlock(&gKeyHandleAccessMtx); } @@ -175,33 +191,65 @@ void init_key_handle_array() void clear_key_handle_array(int idx) { - if(pthread_mutex_lock(&gKeyHandleAccessMtx) == 0) - { - if(idx < MaxPersHandle && idx > 0 ) + if(pthread_mutex_lock(&gKeyHandleAccessMtx) == 0) + { + if(gKeyHandleTree != NULL) { - memset(&gKeyHandleArray[idx], 0, sizeof(gKeyHandleArray[idx])); + KeyHandleTreeItem_s* item = malloc(sizeof(KeyHandleTreeItem_s)); + if(item != NULL) + { + item->key = idx; + + if(jsw_rberase(gKeyHandleTree, item) == 0) + { + DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("clear_key_handle_array - failed remove idx: "), DLT_INT(idx)); + } + free(item); + } } - pthread_mutex_unlock(&gKeyHandleAccessMtx); - } + + pthread_mutex_unlock(&gKeyHandleAccessMtx); + } } int set_file_handle_data(int idx, PersistencePermission_e permission, const char* backup, const char* csumPath, char* filePath) { - int rval = 0; + int rval = -1; if(pthread_mutex_lock(&gFileHandleAccessMtx) == 0) { - if(idx < MaxPersHandle && idx > 0 ) - { - strcpy(gFileHandleArray[idx].backupPath, backup); - strcpy(gFileHandleArray[idx].csumPath, csumPath); - gFileHandleArray[idx].backupCreated = 0; // set to 0 by default - gFileHandleArray[idx].permission = permission; - gFileHandleArray[idx].filePath = filePath; // check to do if this works - gFileHandleArray[idx].cacheStatus = -1; // set to -1 by default - gFileHandleArray[idx].userId = 0; // default value - } + FileHandleTreeItem_s* item = NULL; + + if(gFileHandleTree == NULL) + { + gFileHandleTree = jsw_rbnew(fh_key_val_cmp, fh_key_val_dup, fh_key_val_rel); + } + + item = malloc(sizeof(FileHandleTreeItem_s)); // assign key and value to the rbtree item + if(item != NULL) + { + item->key = idx; + + item->value.fileHandle.permission = permission; + item->value.fileHandle.backupCreated = 0; // set to 0 by default + item->value.fileHandle.cacheStatus = -1; // set to -1 by default + item->value.fileHandle.userId = 0; // default value + item->value.fileHandle.filePath = filePath; + + strncpy(item->value.fileHandle.backupPath, backup, DbPathMaxLen); + item->value.fileHandle.backupPath[DbResIDMaxLen-1] = '\0'; // Ensures 0-Termination + + strncpy(item->value.fileHandle.csumPath, csumPath, DbPathMaxLen); + item->value.fileHandle.csumPath[DbResIDMaxLen-1] = '\0'; // Ensures 0-Termination + + jsw_rbinsert(gFileHandleTree, item); + + free(item); + + rval = 0; + } + pthread_mutex_unlock(&gFileHandleAccessMtx); } @@ -215,14 +263,25 @@ int get_file_permission(int idx) if(pthread_mutex_lock(&gFileHandleAccessMtx) == 0) { - if(idx < MaxPersHandle && idx > 0 ) - { - permission = gFileHandleArray[idx].permission; - } - else - { - permission = -1; - } + if(gFileHandleTree != NULL) + { + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gFileHandleTree, item); + if(foundItem != NULL) + { + permission = foundItem->value.fileHandle.permission; + } + else + { + permission = -1; + } + free(item); + } + } pthread_mutex_unlock(&gFileHandleAccessMtx); } return permission; @@ -234,10 +293,22 @@ char* get_file_backup_path(int idx) char* charPtr = NULL; if(pthread_mutex_lock(&gFileHandleAccessMtx) == 0) { - if(idx < MaxPersHandle && idx > 0 ) + if(gFileHandleTree != NULL) { - charPtr = gFileHandleArray[idx].backupPath; + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gFileHandleTree, item); + if(foundItem != NULL) + { + charPtr = foundItem->value.fileHandle.backupPath; + } + free(item); + } } + pthread_mutex_unlock(&gFileHandleAccessMtx); } return charPtr; @@ -248,10 +319,22 @@ char* get_file_checksum_path(int idx) char* charPtr = NULL; if(pthread_mutex_lock(&gFileHandleAccessMtx) == 0) { - if(idx < MaxPersHandle && idx > 0 ) + if(gFileHandleTree != NULL) { - charPtr = gFileHandleArray[idx].csumPath; + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gFileHandleTree, item); + if(foundItem != NULL) + { + charPtr = foundItem->value.fileHandle.csumPath; + } + free(item); + } } + pthread_mutex_unlock(&gFileHandleAccessMtx); } return charPtr; @@ -262,10 +345,48 @@ void set_file_backup_status(int idx, int status) { if(pthread_mutex_lock(&gFileHandleAccessMtx) == 0) { - if(MaxPersHandle >= idx && idx > 0 ) + if(gFileHandleTree != NULL) { - gFileHandleArray[idx].backupCreated = status; + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gFileHandleTree, item); + if(foundItem == NULL) + { + item->value.fileHandle.backupCreated = status; + + item->value.fileHandle.permission = PersistencePermission_LastEntry; + item->value.fileHandle.cacheStatus = -1; // set to -1 by default + item->value.fileHandle.userId = 0; // default value + item->value.fileHandle.filePath = NULL; + + jsw_rbinsert(gFileHandleTree, item); + } + else + { + FileHandleTreeItem_s* newItem = malloc(sizeof(FileHandleTreeItem_s)); + if(newItem != NULL) + { + if(newItem->value.payload != NULL) + { + memcpy(newItem->value.payload , foundItem->value.payload, sizeof(FileHandleData_u) ); // duplicate value + + newItem->key = idx; + newItem->value.fileHandle.backupCreated = status; + + jsw_rberase(gFileHandleTree, foundItem); + + jsw_rbinsert(gFileHandleTree, newItem); + } + free(newItem); + } + } + free(item); + } } + pthread_mutex_unlock(&gFileHandleAccessMtx); } } @@ -275,10 +396,22 @@ int get_file_backup_status(int idx) int backup = -1; if(pthread_mutex_lock(&gFileHandleAccessMtx) == 0) { - if(MaxPersHandle >= idx && idx > 0 ) + if(gFileHandleTree != NULL) { - backup= gFileHandleArray[idx].backupCreated; + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gFileHandleTree, item); + if(foundItem != NULL) + { + backup = foundItem->value.fileHandle.backupCreated; + } + free(item); + } } + pthread_mutex_unlock(&gFileHandleAccessMtx); } return backup; @@ -288,9 +421,50 @@ void set_file_cache_status(int idx, int status) { if(pthread_mutex_lock(&gFileHandleAccessMtx) == 0) { - if(MaxPersHandle >= idx && idx > 0 ) + if(gFileHandleTree != NULL) { - gFileHandleArray[idx].cacheStatus = status; + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gFileHandleTree, item); + if(foundItem == NULL) + { + item->value.fileHandle.cacheStatus = status; + + item->value.fileHandle.backupCreated = 0; // set to 0 by default + item->value.fileHandle.permission = PersistencePermission_LastEntry; + item->value.fileHandle.userId = 0; // default value + item->value.fileHandle.filePath = NULL; + + memset(item->value.fileHandle.csumPath , 0, DbResIDMaxLen); + item->value.fileHandle.csumPath[DbResIDMaxLen-1] = '\0'; // Ensures 0-Termination + memset(item->value.fileHandle.backupPath, 0, DbResIDMaxLen); + item->value.fileHandle.backupPath[DbResIDMaxLen-1] = '\0'; // Ensures 0-Termination + + jsw_rbinsert(gFileHandleTree, item); + } + else + { + FileHandleTreeItem_s* newItem = malloc(sizeof(FileHandleTreeItem_s)); + if(newItem != NULL) + { + if(newItem->value.payload != NULL) + { + memcpy(newItem->value.payload , foundItem->value.payload, sizeof(FileHandleData_u) ); // duplicate value + + jsw_rberase(gFileHandleTree, foundItem); + + newItem->key = idx; + newItem->value.fileHandle.cacheStatus = status; + jsw_rbinsert(gFileHandleTree, newItem); + } + free(newItem); + } + } + free(item); + } } pthread_mutex_unlock(&gFileHandleAccessMtx); } @@ -301,10 +475,21 @@ int get_file_cache_status(int idx) int status = -1; if(pthread_mutex_lock(&gFileHandleAccessMtx) == 0) { - if(MaxPersHandle >= idx && idx > 0 ) - { - status = gFileHandleArray[idx].cacheStatus; - } + if(gFileHandleTree != NULL) + { + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gFileHandleTree, item); + if(foundItem != NULL) + { + status = foundItem->value.fileHandle.cacheStatus; + } + free(item); + } + } pthread_mutex_unlock(&gFileHandleAccessMtx); } return status; @@ -315,11 +500,51 @@ void set_file_user_id(int idx, int userID) { if(pthread_mutex_lock(&gFileHandleAccessMtx) == 0) { - if(MaxPersHandle >= idx && idx > 0 ) + if(gFileHandleTree != NULL) { - gFileHandleArray[idx].userId = userID; + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gFileHandleTree, item); + if(foundItem == NULL) + { + item->value.fileHandle.userId = userID; // default value + + item->value.fileHandle.backupCreated = 0; // set to 0 by default + item->value.fileHandle.permission = -1; + item->value.fileHandle.cacheStatus = -1; // set to -1 by default + item->value.fileHandle.filePath = NULL; + + memset(item->value.fileHandle.csumPath , 0, DbResIDMaxLen); + item->value.fileHandle.csumPath[DbResIDMaxLen-1] = '\0'; // Ensures 0-Termination + memset(item->value.fileHandle.backupPath, 0, DbResIDMaxLen); + item->value.fileHandle.backupPath[DbResIDMaxLen-1] = '\0'; // Ensures 0-Termination + + jsw_rbinsert(gFileHandleTree, item); + } + else + { + FileHandleTreeItem_s* newItem = malloc(sizeof(FileHandleTreeItem_s)); + if(newItem != NULL) + { + if(newItem->value.payload != NULL) + { + memcpy(newItem->value.payload , foundItem->value.payload, sizeof(FileHandleData_u) ); // duplicate value + + jsw_rberase(gFileHandleTree, foundItem); + + newItem->key = idx; + newItem->value.fileHandle.userId = userID; + jsw_rbinsert(gFileHandleTree, newItem); + } + free(newItem); + } + } + free(item); + } } - pthread_mutex_unlock(&gFileHandleAccessMtx); } } @@ -329,9 +554,20 @@ int get_file_user_id(int idx) int id = -1; if(pthread_mutex_lock(&gFileHandleAccessMtx) == 0) { - if(MaxPersHandle >= idx && idx > 0 ) + if(gFileHandleTree != NULL) { - id = gFileHandleArray[idx].userId; + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gFileHandleTree, item); + if(foundItem != NULL) + { + id = foundItem->value.fileHandle.userId; + } + free(item); + } } pthread_mutex_unlock(&gFileHandleAccessMtx); } @@ -342,20 +578,41 @@ int get_file_user_id(int idx) //---------------------------------------------------------- int set_ossfile_handle_data(int idx, PersistencePermission_e permission, int backupCreated, - const char* backup, const char* csumPath, char* filePath) + const char* backup, const char* csumPath, char* filePath) { int rval = 0; if(pthread_mutex_lock(&gOssFileHandleAccessMtx) == 0) { - if(idx < MaxPersHandle && idx > 0 ) - { - strcpy(gOssHandleArray[idx].backupPath, backup); - strcpy(gOssHandleArray[idx].csumPath, csumPath); - gOssHandleArray[idx].backupCreated = backupCreated; - gOssHandleArray[idx].permission = permission; - gOssHandleArray[idx].filePath = filePath; // check to do if this works - } + FileHandleTreeItem_s* item = NULL; + + if(gOssFileHandleTree == NULL) + { + gOssFileHandleTree = jsw_rbnew(fh_key_val_cmp, fh_key_val_dup, fh_key_val_rel); + } + + item = malloc(sizeof(FileHandleTreeItem_s)); // assign key and value to the rbtree item + if(item != NULL) + { + item->key = idx; + + item->value.fileHandle.permission = permission; + item->value.fileHandle.backupCreated = backupCreated; + item->value.fileHandle.cacheStatus = -1; // set to -1 by default + item->value.fileHandle.userId = 0; // default value + item->value.fileHandle.filePath = filePath; + + strncpy(item->value.fileHandle.backupPath, backup, DbPathMaxLen); + item->value.fileHandle.backupPath[DbResIDMaxLen-1] = '\0'; // Ensures 0-Termination + strncpy(item->value.fileHandle.csumPath, csumPath, DbPathMaxLen); + item->value.fileHandle.csumPath[DbResIDMaxLen-1] = '\0'; // Ensures 0-Termination + + jsw_rbinsert(gOssFileHandleTree, item); + + free(item); + rval = 0; + } + pthread_mutex_unlock(&gOssFileHandleAccessMtx); } @@ -369,14 +626,25 @@ int get_ossfile_permission(int idx) if(pthread_mutex_lock(&gOssFileHandleAccessMtx) == 0) { - if(idx < MaxPersHandle && idx > 0 ) - { - permission = gOssHandleArray[idx].permission; - } - else - { - permission = -1; - } + if(gOssFileHandleTree != NULL) + { + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gOssFileHandleTree, item); + if(foundItem != NULL) + { + permission = foundItem->value.fileHandle.permission; + } + else + { + permission = -1; + } + free(item); + } + } pthread_mutex_unlock(&gOssFileHandleAccessMtx); } @@ -389,10 +657,20 @@ char* get_ossfile_backup_path(int idx) char* charPtr = NULL; if(pthread_mutex_lock(&gOssFileHandleAccessMtx) == 0) { - - if(idx < MaxPersHandle && idx > 0 ) + if(gOssFileHandleTree != NULL) { - charPtr = gOssHandleArray[idx].backupPath; + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gOssFileHandleTree, item); + if(foundItem != NULL) + { + charPtr = foundItem->value.fileHandle.backupPath; + } + free(item); + } } pthread_mutex_unlock(&gOssFileHandleAccessMtx); } @@ -406,9 +684,20 @@ char* get_ossfile_file_path(int idx) if(pthread_mutex_lock(&gOssFileHandleAccessMtx) == 0) { - if(idx < MaxPersHandle && idx > 0 ) + if(gOssFileHandleTree != NULL) { - charPtr = gOssHandleArray[idx].filePath; + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gOssFileHandleTree, item); + if(foundItem != NULL) + { + charPtr = foundItem->value.fileHandle.filePath; + } + free(item); + } } pthread_mutex_unlock(&gOssFileHandleAccessMtx); } @@ -419,9 +708,51 @@ void set_ossfile_file_path(int idx, char* file) { if(pthread_mutex_lock(&gOssFileHandleAccessMtx) == 0) { - if(idx < MaxPersHandle && idx > 0 ) + if(gFileHandleTree != NULL) { - gOssHandleArray[idx].filePath = file; + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gFileHandleTree, item); + if(foundItem == NULL) + { + item->value.fileHandle.filePath = file; + + + item->value.fileHandle.backupCreated = 0; // set to 0 by default + item->value.fileHandle.permission = -1; + item->value.fileHandle.cacheStatus = -1; // set to -1 by default + item->value.fileHandle.userId = 0; // default value + memset(item->value.fileHandle.csumPath , 0, DbResIDMaxLen); + item->value.fileHandle.csumPath[DbResIDMaxLen-1] = '\0'; // Ensures 0-Termination + + memset(item->value.fileHandle.backupPath, 0, DbResIDMaxLen); + item->value.fileHandle.backupPath[DbResIDMaxLen-1] = '\0'; // Ensures 0-Termination + + jsw_rbinsert(gFileHandleTree, item); + } + else + { + FileHandleTreeItem_s* newItem = malloc(sizeof(FileHandleTreeItem_s)); + if(newItem != NULL) + { + if(newItem->value.payload != NULL) + { + memcpy(newItem->value.payload , foundItem->value.payload, sizeof(FileHandleData_u) ); // duplicate value + + jsw_rberase(gFileHandleTree, foundItem); + + newItem->key = idx; + newItem->value.fileHandle.filePath = file; + jsw_rbinsert(gFileHandleTree, newItem); + } + free(newItem); + } + } + free(item); + } } pthread_mutex_unlock(&gOssFileHandleAccessMtx); } @@ -433,9 +764,20 @@ char* get_ossfile_checksum_path(int idx) char* charPtr = NULL; if(pthread_mutex_lock(&gOssFileHandleAccessMtx) == 0) { - if(idx < MaxPersHandle && idx > 0 ) + if(gOssFileHandleTree != NULL) { - charPtr = gOssHandleArray[idx].csumPath; + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gOssFileHandleTree, item); + if(foundItem != NULL) + { + charPtr = foundItem->value.fileHandle.csumPath; + } + free(item); + } } pthread_mutex_unlock(&gOssFileHandleAccessMtx); } @@ -447,9 +789,46 @@ void set_ossfile_backup_status(int idx, int status) { if(pthread_mutex_lock(&gOssFileHandleAccessMtx) == 0) { - if(idx < MaxPersHandle && idx > 0 ) + if(gOssFileHandleTree != NULL) { - gOssHandleArray[idx].backupCreated = status; + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gOssFileHandleTree, item); + if(foundItem == NULL) + { + item->value.fileHandle.backupCreated = status; + + item->value.fileHandle.permission = PersistencePermission_LastEntry; + item->value.fileHandle.cacheStatus = -1; // set to -1 by default + item->value.fileHandle.userId = 0; // default value + item->value.fileHandle.filePath = NULL; + + jsw_rbinsert(gOssFileHandleTree, item); + } + else + { + FileHandleTreeItem_s* newItem = malloc(sizeof(FileHandleTreeItem_s)); + if(newItem != NULL) + { + if(newItem->value.payload != NULL) + { + memcpy(newItem->value.payload , foundItem->value.payload, sizeof(FileHandleData_u) ); // duplicate value + + newItem->key = idx; + newItem->value.fileHandle.backupCreated = status; + + jsw_rberase(gFileHandleTree, foundItem); + + jsw_rbinsert(gFileHandleTree, newItem); + } + free(newItem); + } + } + free(item); + } } pthread_mutex_unlock(&gOssFileHandleAccessMtx); } @@ -461,9 +840,20 @@ int get_ossfile_backup_status(int idx) if(pthread_mutex_lock(&gOssFileHandleAccessMtx) == 0) { - if(idx < MaxPersHandle && idx > 0 ) + if(gOssFileHandleTree != NULL) { - rval = gOssHandleArray[idx].backupCreated; + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gOssFileHandleTree, item); + if(foundItem != NULL) + { + rval = foundItem->value.fileHandle.backupCreated; + } + free(item); + } } pthread_mutex_unlock(&gOssFileHandleAccessMtx); } diff --git a/src/persistence_client_library_handle.h b/src/persistence_client_library_handle.h index f0db145..1b16ae3 100644 --- a/src/persistence_client_library_handle.h +++ b/src/persistence_client_library_handle.h @@ -22,19 +22,17 @@ #include "persistence_client_library_data_organization.h" -#include <persComRct.h> - /// key handle structure definition typedef struct _PersistenceKeyHandle_s { - /// Resource ID - char resource_id[DbResIDMaxLen]; /// logical database id unsigned int ldbid; /// User No unsigned int user_no; /// Seat No unsigned int seat_no; + /// Resource ID + char resource_id[DbResIDMaxLen]; } PersistenceKeyHandle_s; diff --git a/src/persistence_client_library_key.c b/src/persistence_client_library_key.c index 18eff50..4a1deb7 100644 --- a/src/persistence_client_library_key.c +++ b/src/persistence_client_library_key.c @@ -20,13 +20,8 @@ #include "persistence_client_library_key.h" #include "persistence_client_library_handle.h" #include "persistence_client_library_pas_interface.h" -#include "persistence_client_library_custom_loader.h" #include "persistence_client_library_prct_access.h" #include "persistence_client_library_db_access.h" -#include "crc32.h" - -#include <persComRct.h> - // function declaration diff --git a/src/persistence_client_library_lc_interface.c b/src/persistence_client_library_lc_interface.c index 766b3cf..e71d3f5 100644 --- a/src/persistence_client_library_lc_interface.c +++ b/src/persistence_client_library_lc_interface.c @@ -19,22 +19,7 @@ #include "persistence_client_library_lc_interface.h" -#include "persistence_client_library_data_organization.h" -#include "persistence_client_library_db_access.h" - -#include "persistence_client_library_handle.h" -#include "persistence_client_library_pas_interface.h" -#include "persistence_client_library_dbus_service.h" -#include "persistence_client_library_custom_loader.h" -#include "persistence_client_library_prct_access.h" - - #include <errno.h> -#include <stdio.h> -#include <string.h> -#include <unistd.h> -#include <dlfcn.h> - int check_lc_request(unsigned int request, unsigned int requestID) @@ -179,4 +164,3 @@ int unregister_lifecycle(int shutdownMode) return deliverToMainloop(&data); } - diff --git a/src/persistence_client_library_lc_interface.h b/src/persistence_client_library_lc_interface.h index 0ce5572..b92418b 100644 --- a/src/persistence_client_library_lc_interface.h +++ b/src/persistence_client_library_lc_interface.h @@ -19,7 +19,7 @@ * @see */ -#include <dbus/dbus.h> +#include "persistence_client_library_dbus_service.h" /** diff --git a/src/persistence_client_library_pas_interface.c b/src/persistence_client_library_pas_interface.c index 2dc4276..625d2cf 100644 --- a/src/persistence_client_library_pas_interface.c +++ b/src/persistence_client_library_pas_interface.c @@ -18,13 +18,8 @@ */ #include "persistence_client_library_pas_interface.h" -#include "persistence_client_library_dbus_service.h" - -#include "persistence_client_library_data_organization.h" #include <errno.h> -#include <unistd.h> -#include <string.h> /// flag if access is locked static int gLockAccess = 0; diff --git a/src/persistence_client_library_pas_interface.h b/src/persistence_client_library_pas_interface.h index 6241248..9a76a35 100644 --- a/src/persistence_client_library_pas_interface.h +++ b/src/persistence_client_library_pas_interface.h @@ -20,7 +20,7 @@ * @see */ -#include <dbus/dbus.h> +#include "persistence_client_library_dbus_service.h" /** diff --git a/src/persistence_client_library_prct_access.c b/src/persistence_client_library_prct_access.c index 85750f0..57e6be5 100644 --- a/src/persistence_client_library_prct_access.c +++ b/src/persistence_client_library_prct_access.c @@ -18,14 +18,7 @@ */ #include "persistence_client_library_prct_access.h" -#include "persistence_client_library_db_access.h" #include "persistence_client_library_custom_loader.h" -#include <stdlib.h> -#include <string.h> - -#include <persComRct.h> -#include <persComDbAccess.h> -#include <persComErrors.h> /// pointer to resource table database diff --git a/src/rbtree.c b/src/rbtree.c index d3496ed..2b511c7 100644 --- a/src/rbtree.c +++ b/src/rbtree.c @@ -210,10 +210,11 @@ void *jsw_rbfind ( jsw_rbtree_t *tree, void *data ) { jsw_rbnode_t *it = tree->root; - while ( it != NULL ) { - int cmp = tree->cmp( it->data, data ); + while ( it != NULL ) + { + int cmp = tree->cmp( it->data, data); - if ( cmp == 0 ) + if( cmp == 0 ) break; /* @@ -266,22 +267,26 @@ int jsw_rbinsert ( jsw_rbtree_t *tree, void *data ) q = t->link[1] = tree->root; /* Search down the tree for a place to insert */ - for ( ; ; ) { - if ( q == NULL ) { + for ( ; ; ) + { + if ( q == NULL ) + { /* Insert a new node at the first null link */ p->link[dir] = q = new_node ( tree, data ); if ( q == NULL ) return 0; } - else if ( is_red ( q->link[0] ) && is_red ( q->link[1] ) ) { + else if ( is_red ( q->link[0] ) && is_red ( q->link[1] ) ) + { /* Simple red violation: color flip */ q->red = 1; q->link[0]->red = 0; q->link[1]->red = 0; } - if ( is_red ( q ) && is_red ( p ) ) { + if ( is_red ( q ) && is_red ( p ) ) + { /* Hard red violation: rotations necessary */ int dir2 = t->link[1] == g; @@ -325,7 +330,7 @@ int jsw_rbinsert ( jsw_rbtree_t *tree, void *data ) return 1; } -#if 0 + /** <summary> Releases a valid red black tree @@ -366,6 +371,7 @@ void jsw_rbdelete ( jsw_rbtree_t *tree ) } + /** <summary> Remove a node from a red black tree @@ -384,8 +390,9 @@ void jsw_rbdelete ( jsw_rbtree_t *tree ) */ int jsw_rberase ( jsw_rbtree_t *tree, void *data ) { - if ( tree->root != NULL ) { - jsw_rbnode_t head = {0}; /* False tree root */ + if ( tree->root != NULL ) + { + jsw_rbnode_t head = {0, NULL, {NULL, NULL} }; /* False tree root */ jsw_rbnode_t *q, *p, *g; /* Helpers */ jsw_rbnode_t *f = NULL; /* Found item */ int dir = 1; @@ -399,7 +406,8 @@ int jsw_rberase ( jsw_rbtree_t *tree, void *data ) Search and push a red node down to fix red violations as we go */ - while ( q->link[dir] != NULL ) { + while ( q->link[dir] != NULL ) + { int last = dir; /* Move the helpers down */ @@ -415,20 +423,25 @@ int jsw_rberase ( jsw_rbtree_t *tree, void *data ) f = q; /* Push the red node down with rotations and color flips */ - if ( !is_red ( q ) && !is_red ( q->link[dir] ) ) { + if ( !is_red ( q ) && !is_red ( q->link[dir] ) ) + { if ( is_red ( q->link[!dir] ) ) p = p->link[last] = jsw_single ( q, dir ); - else if ( !is_red ( q->link[!dir] ) ) { + else if ( !is_red ( q->link[!dir] ) ) + { jsw_rbnode_t *s = p->link[!last]; - if ( s != NULL ) { - if ( !is_red ( s->link[!last] ) && !is_red ( s->link[last] ) ) { + if ( s != NULL ) + { + if ( !is_red ( s->link[!last] ) && !is_red ( s->link[last] ) ) + { /* Color flip */ p->red = 0; s->red = 1; q->red = 1; } - else { + else + { int dir2 = g->link[1] == p; if ( is_red ( s->link[last] ) ) @@ -447,11 +460,11 @@ int jsw_rberase ( jsw_rbtree_t *tree, void *data ) } /* Replace and remove the saved node */ - if ( f != NULL ) { - tree->rel ( f->data ); + if ( f != NULL ) + { + tree->rel( f->data ); f->data = q->data; - p->link[p->link[1] == q] = - q->link[q->link[0] == NULL]; + p->link[p->link[1] == q] = q->link[q->link[0] == NULL]; free ( q ); } @@ -467,7 +480,7 @@ int jsw_rberase ( jsw_rbtree_t *tree, void *data ) return 1; } - +#if 0 /** <summary> Gets the number of nodes in a red black tree diff --git a/src/rbtree.h b/src/rbtree.h index 429be29..c674d99 100644 --- a/src/rbtree.h +++ b/src/rbtree.h @@ -56,13 +56,13 @@ */ #ifdef __cplusplus -#include <cstddef> + #include <cstddef> -using std::size_t; + using std::size_t; -extern "C" { + extern "C" { #else -#include <stddef.h> + #include <stddef.h> #endif /* Opaque types */ @@ -77,10 +77,10 @@ typedef void (*rel_f) ( void *p ); /* Red Black tree functions */ jsw_rbtree_t *jsw_rbnew ( cmp_f cmp, dup_f dup, rel_f rel ); -//void jsw_rbdelete ( jsw_rbtree_t *tree ); +void jsw_rbdelete ( jsw_rbtree_t *tree ); void *jsw_rbfind ( jsw_rbtree_t *tree, void *data ); int jsw_rbinsert ( jsw_rbtree_t *tree, void *data ); -//int jsw_rberase ( jsw_rbtree_t *tree, void *data ); +int jsw_rberase ( jsw_rbtree_t *tree, void *data ); //size_t jsw_rbsize ( jsw_rbtree_t *tree ); /* Traversal functions */ diff --git a/test/persistence_client_library_benchmark.c b/test/persistence_client_library_benchmark.c index c37e972..a9b4a98 100644 --- a/test/persistence_client_library_benchmark.c +++ b/test/persistence_client_library_benchmark.c @@ -24,8 +24,8 @@ #include <string.h> #include <stdlib.h> /* atoi */ -#include <dlt/dlt.h> -#include <dlt/dlt_common.h> +#include <dlt.h> +#include <dlt_common.h> #include <sys/time.h> #include <sys/resource.h> diff --git a/test/persistence_client_library_dbus_test.c b/test/persistence_client_library_dbus_test.c index dae3bc2..1b83c34 100644 --- a/test/persistence_client_library_dbus_test.c +++ b/test/persistence_client_library_dbus_test.c @@ -22,8 +22,8 @@ #include <stdio.h> -#include <dlt/dlt.h> -#include <dlt/dlt_common.h> +#include <dlt.h> +#include <dlt_common.h> int myChangeCallback(pclNotification_s * notifyStruct) diff --git a/test/persistence_client_library_test.c b/test/persistence_client_library_test.c index db3a1e8..24238b8 100644 --- a/test/persistence_client_library_test.c +++ b/test/persistence_client_library_test.c @@ -943,7 +943,7 @@ START_TEST(test_DataHandle) X_TEST_REPORT_DESCRIPTION("Test of data handle"); X_TEST_REPORT_TYPE(GOOD); */ - int handle1 = 0, handle2 = 0, i = 0; + int handle1 = 0, handle2 = 0; int handleArray[4] = {0}; int ret = 0; unsigned char buffer[READ_SIZE] = {0}; @@ -1018,19 +1018,7 @@ START_TEST(test_DataHandle) fail_unless(handle1 != -1, "Could not closefile ==> /media/mediaDB.db"); ret = pclFileClose(1024); - fail_unless(ret == EPERS_MAXHANDLE, "Could close file, but should not!!"); - - for(i = 0; i<1024; i++) - { - if( fcntl(i, F_GETFD) == -1 ) //search for an invalid fd, and try to close it. - { - ret = pclFileClose(i); - fail_unless(ret == -1, "Could close file, but should not!!"); - - break; - } - } - + fail_unless(ret == EPERS_MAXHANDLE, "1. Could close file, but should not!!"); #endif } END_TEST diff --git a/test/persistence_pfs_test.c b/test/persistence_pfs_test.c index 82290a0..dee4d9c 100644 --- a/test/persistence_pfs_test.c +++ b/test/persistence_pfs_test.c @@ -37,8 +37,8 @@ #include <stdlib.h> #include <pthread.h> -#include <dlt/dlt.h> -#include <dlt/dlt_common.h> +#include <dlt.h> +#include <dlt_common.h> #include <sys/types.h> #include <sys/stat.h> |