From 8d6c63e7b1019479ce9eacf26d89d70cf1f0aa82 Mon Sep 17 00:00:00 2001 From: Ingo Huerner Date: Wed, 30 Apr 2014 14:53:29 +0200 Subject: [PATCH] Added default file loading also to pclCreatePath function; fixed bugs 199 and 203 --- src/persistence_client_library_backup_filelist.c | 2 +- src/persistence_client_library_file.c | 87 +++++++++++++++--------- src/persistence_client_library_prct_access.c | 2 +- test/persistence_client_library_test.c | 5 +- 4 files changed, 61 insertions(+), 35 deletions(-) diff --git a/src/persistence_client_library_backup_filelist.c b/src/persistence_client_library_backup_filelist.c index 060901c..8c98898 100644 --- a/src/persistence_client_library_backup_filelist.c +++ b/src/persistence_client_library_backup_filelist.c @@ -559,7 +559,7 @@ int pclRecoverFromBackup(int backupFd, const char* original) if(handle != -1) { // copy data from one file to another - if((handle = pclBackupDoFileCopy(backupFd, handle)) == -1) + if(pclBackupDoFileCopy(backupFd, handle) == -1) { DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pclRecoverFromBackup => couldn't write whole buffer")); } diff --git a/src/persistence_client_library_file.c b/src/persistence_client_library_file.c index 8d433b1..ab8f7b2 100644 --- a/src/persistence_client_library_file.c +++ b/src/persistence_client_library_file.c @@ -41,6 +41,11 @@ +// local function prototype +int pclFileGetDefaultData(int handle, const char* resource_id, int policy); + + + int pclFileClose(int fd) { int rval = EPERS_NOT_INITIALIZED; @@ -219,38 +224,10 @@ int pclFileOpen(unsigned int ldbid, const char* resource_id, unsigned int user_n } else { - // check if there is default data available - char pathPrefix[DbPathMaxLen] = {0}; - char defaultPath[DbPathMaxLen] = {0}; - int defaultHandle = -1; - - // create path to default data - if(dbContext.configKey.policy == PersistencePolicy_wc) - { - snprintf(pathPrefix, DbPathMaxLen, gLocalCachePath, gAppId); - } - else if(dbContext.configKey.policy == PersistencePolicy_wt) - { - snprintf(pathPrefix, DbPathMaxLen, gLocalWtPath, gAppId); - } - - snprintf(defaultPath, DbPathMaxLen, "%s%s%s", pathPrefix, gDefDataFolder, resource_id); - printf("=> => => => defaultPath: %s => resourceID: %s\n", defaultPath, resource_id); - defaultHandle = open(defaultPath, O_RDONLY); - if(defaultHandle != -1) // check if default data is available + if(pclFileGetDefaultData(handle, resource_id, dbContext.configKey.policy) == -1) // try to get default data { - // copy default data - struct stat buf; - memset(&buf, 0, sizeof(buf)); - - fstat(defaultHandle, &buf); - sendfile(handle, defaultHandle, 0, buf.st_size); - close(defaultHandle); - } - else - { - printf(" = = = = Failed to open file: %d => %s\n", defaultHandle, strerror(errno)); + printf("pclFileOpen => No default data!!\n"); } } } @@ -544,7 +521,18 @@ int pclFileCreatePath(unsigned int ldbid, const char* resource_id, unsigned int if(access(*path, F_OK) == -1) { // file does not exist, create it. - int handle = pclCreateFile(*path); + int handle = 0; + if((handle = pclCreateFile(*path)) == -1) + { + DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pclFileCreatePath: error => failed to create file: "), DLT_STRING(*path)); + } + else + { + if(pclFileGetDefaultData(handle, resource_id, dbContext.configKey.policy) == -1) // try to get default data + { + printf("pclFileCreatePath => No default data!!\n"); + } + } close(handle); // don't need the open file } } @@ -631,4 +619,41 @@ int pclFileReleasePath(int pathHandle) +int pclFileGetDefaultData(int handle, const char* resource_id, int policy) +{ + // check if there is default data available + char pathPrefix[DbPathMaxLen] = { [0 ... DbPathMaxLen-1] = 0}; + char defaultPath[DbPathMaxLen] = { [0 ... DbPathMaxLen-1] = 0}; + int defaultHandle = -1; + int rval = 0; + + // create path to default data + if(policy == PersistencePolicy_wc) + { + snprintf(pathPrefix, DbPathMaxLen, gLocalCachePath, gAppId); + } + else if(policy == PersistencePolicy_wt) + { + snprintf(pathPrefix, DbPathMaxLen, gLocalWtPath, gAppId); + } + + snprintf(defaultPath, DbPathMaxLen, "%s%s%s", pathPrefix, gDefDataFolder, resource_id); + + defaultHandle = open(defaultPath, O_RDONLY); + if(defaultHandle != -1) // check if default data is available + { + // copy default data + struct stat buf; + memset(&buf, 0, sizeof(buf)); + + fstat(defaultHandle, &buf); + sendfile(handle, defaultHandle, 0, buf.st_size); + close(defaultHandle); + } + else + { + rval = -1; // no default data available + } +}// getDefault + diff --git a/src/persistence_client_library_prct_access.c b/src/persistence_client_library_prct_access.c index 5ae99cf..a44d229 100644 --- a/src/persistence_client_library_prct_access.c +++ b/src/persistence_client_library_prct_access.c @@ -186,7 +186,7 @@ int get_db_context(PersistenceInfo_s* dbContext, const char* resource_id, unsign rval = EPERS_NOPRCTABLE; } - if(resourceFound == 0) + if((resourceFound == 0) && (dbContext->context.ldbid == 0xFF) ) // create only when the resource is local data { DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("get_db_context => resource in rct table not found: "), DLT_STRING(resource_id) ); // diff --git a/test/persistence_client_library_test.c b/test/persistence_client_library_test.c index 76141f4..18d0dbf 100644 --- a/test/persistence_client_library_test.c +++ b/test/persistence_client_library_test.c @@ -48,7 +48,6 @@ char gTheAppId[MaxAppNameLen] = {0}; // definition of weekday char* dayOfWeek[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; - int myChangeCallback(pclNotification_s * notifyStruct) { printf(" ==> * - * myChangeCallback * - *\n"); @@ -1118,6 +1117,8 @@ static Suite * persistencyClientLib_suite() TCase * tc_persDeleteData = tcase_create("DeleteData"); tcase_add_test(tc_persDeleteData, test_DeleteData); + + TCase * tc_persGetDataHandle = tcase_create("GetDataHandle"); tcase_add_test(tc_persGetDataHandle, test_GetDataHandle); @@ -1194,7 +1195,7 @@ int main(int argc, char *argv[]) SRunner * sr = srunner_create(s); srunner_set_xml(sr, "/tmp/persistenceClientLibraryTest.xml"); srunner_set_log(sr, "/tmp/persistenceClientLibraryTest.log"); - srunner_run_all(sr, /*CK_NORMAL*/ CK_VERBOSE); + srunner_run_all(sr, CK_VERBOSE /*CK_NORMAL CK_VERBOSE*/); nr_failed = srunner_ntests_failed(sr); nr_run = srunner_ntests_run(sr); -- 2.7.4