diff options
author | Ingo Huerner <ingo.huerner@xse.de> | 2013-12-09 09:27:45 +0100 |
---|---|---|
committer | Ingo Huerner <ingo.huerner@xse.de> | 2013-12-09 09:27:45 +0100 |
commit | e13e80bc4d767605b85e87d459e1f3016cdfddd1 (patch) | |
tree | 4ddc8bb424226f3163692d0026214c67c8758ea7 | |
parent | da863e2e60b23b1fd718b39e12a5ed26807dc752 (diff) | |
download | persistence-client-library-e13e80bc4d767605b85e87d459e1f3016cdfddd1.tar.gz |
Fixed PCL bugs: 137, 138, 141
-rw-r--r-- | include/persistence_client_library_error_def.h | 2 | ||||
-rw-r--r-- | include/persistence_client_library_file.h | 2 | ||||
-rw-r--r-- | include/persistence_client_library_key.h | 5 | ||||
-rw-r--r-- | src/persistence_client_library_file.c | 87 | ||||
-rw-r--r-- | src/persistence_client_library_key.c | 59 | ||||
-rw-r--r-- | src/persistence_client_library_prct_access.c | 2 | ||||
-rw-r--r-- | test/data/Data.tar.gz | bin | 49626 -> 49923 bytes | |||
-rw-r--r-- | test/persistence_client_library_test.c | 7 |
8 files changed, 122 insertions, 42 deletions
diff --git a/include/persistence_client_library_error_def.h b/include/persistence_client_library_error_def.h index 8a4a63f..04bbf0f 100644 --- a/include/persistence_client_library_error_def.h +++ b/include/persistence_client_library_error_def.h @@ -105,6 +105,8 @@ extern "C" { #define EPERS_NOTIFY_NOT_ALLOWED (-37) // the requested resource is not a file #define EPERS_RESOURCE_NO_FILE (-38) +// write to requested resource failed, read onyl resource +#define EPERS_RESOURCE_READ_ONLY (-39) #ifdef __cplusplus } diff --git a/include/persistence_client_library_file.h b/include/persistence_client_library_file.h index c357714..2cdbccd 100644 --- a/include/persistence_client_library_file.h +++ b/include/persistence_client_library_file.h @@ -177,7 +177,7 @@ int pclFileUnmapData(void* address, long size); * * @return positive value (0 or greater): bytes written; * On error a negative value will be returned with th following error codes: - * ::EPERS_LOCKFS, ::EPERS_NOT_INITIALIZED or ::EPERS_COMMON + * ::EPERS_LOCKFS, ::EPERS_NOT_INITIALIZED or ::EPERS_COMMON ::EPERS_RESOURCE_READ_ONLY * If ::EPERS_COMMON will be returned errno will be set. */ int pclFileWriteData(int fd, const void * buffer, int buffer_size); diff --git a/include/persistence_client_library_key.h b/include/persistence_client_library_key.h index fa9fa16..52e4aa6 100644 --- a/include/persistence_client_library_key.h +++ b/include/persistence_client_library_key.h @@ -223,7 +223,7 @@ int pclKeyHandleUnRegisterNotifyOnChange(int key_handle, pclChangeNotifyCallback * @return positive value (0 or greater): the bytes written; * On error a negative value will be returned with the following error codes: * ::EPERS_LOCKFS ::EPERS_MAX_BUFF_SIZE ::EPERS_NOTIFY_SIG ::EPERS_DB_VALUE_SIZE ::EPERS_DB_KEY_SIZE - * ::EPERS_NOPRCTABLE ::EPERS_DB_VALUE_SIZE + * ::EPERS_NOPRCTABLE ::EPERS_DB_VALUE_SIZE ::EPERS_RESOURCE_READ_ONLY */ int pclKeyHandleWriteData(int key_handle, unsigned char* buffer, int buffer_size); @@ -294,7 +294,8 @@ int pclKeyUnRegisterNotifyOnChange( unsigned int ldbid, const char * resource_ * * @return positive value (0 or greater): the bytes written; * On error a negative value will be returned with the following error codes: - * ::EPERS_LOCKFS ::EPERS_BADPOL ::EPERS_BUFLIMIT ::EPERS_DB_VALUE_SIZE ::EPERS_DB_KEY_SIZE ::EPERS_NOTIFY_SIG + * ::EPERS_LOCKFS ::EPERS_BADPOL ::EPERS_BUFLIMIT ::EPERS_DB_VALUE_SIZE ::EPERS_DB_KEY_SIZE + * ::EPERS_NOTIFY_SIG ::EPERS_RESOURCE_READ_ONLY */ int pclKeyWriteData(unsigned int ldbid, const char* resource_id, unsigned int user_no, unsigned int seat_no, unsigned char* buffer, int buffer_size); diff --git a/src/persistence_client_library_file.c b/src/persistence_client_library_file.c index b065df9..dada09e 100644 --- a/src/persistence_client_library_file.c +++ b/src/persistence_client_library_file.c @@ -369,22 +369,28 @@ int pclFileWriteData(int fd, const void * buffer, int buffer_size) { if(fd < MaxPersHandle) { - // check if a backup file has to be created - if( gFileHandleArray[fd].permission != PersistencePermission_ReadOnly - && gFileHandleArray[fd].backupCreated == 0) + if(gFileHandleArray[fd].permission != PersistencePermission_ReadOnly) { - char csumBuf[ChecksumBufSize] = {0}; + // check if a backup file has to be created + if(gFileHandleArray[fd].backupCreated == 0) + { + char csumBuf[ChecksumBufSize] = {0}; - // calculate checksum - pclCalcCrc32Csum(fd, csumBuf); + // calculate checksum + pclCalcCrc32Csum(fd, csumBuf); - // create checksum and backup file - pclCreateBackup(gFileHandleArray[fd].backupPath, fd, gFileHandleArray[fd].csumPath, csumBuf); + // create checksum and backup file + pclCreateBackup(gFileHandleArray[fd].backupPath, fd, gFileHandleArray[fd].csumPath, csumBuf); - gFileHandleArray[fd].backupCreated = 1; - } + gFileHandleArray[fd].backupCreated = 1; + } - size = write(fd, buffer, buffer_size); + size = write(fd, buffer, buffer_size); + } + else + { + size = EPERS_RESOURCE_READ_ONLY; + } } } else @@ -463,7 +469,14 @@ int pclFileCreatePath(unsigned int ldbid, const char* resource_id, unsigned int *size = strlen(dbPath); *path = malloc(*size); memcpy(*path, dbPath, *size); + *path[*size] = '\0'; gOssHandleArray[handle].filePath = *path; + + if(access(*path, F_OK) == -1) + { + // file does not exist, create it. + pclCreateFileAndPath(*path); + } } else { @@ -924,4 +937,56 @@ int pclBackupNeeded(const char* path) +int pclCreateFileAndPath(const char* path) +{ + const char* delimiters = "/\n"; // search for blank and end of line + char* tokenArray[24]; + char* thePath = (char*)path; + char createPath[DbPathMaxLen] = {0}; + int numTokens = 0, i = 0, validPath = 1; + int rval = -1; + + tokenArray[numTokens++] = strtok(thePath, delimiters); + while(tokenArray[numTokens-1] != NULL ) + { + tokenArray[numTokens] = strtok(NULL, delimiters); + if(tokenArray[numTokens] != NULL) + { + numTokens++; + if(numTokens >= 24) + { + validPath = 0; + break; + } + } + else + { + break; + } + } + + if(validPath == 1) + { + snprintf(createPath, DbPathMaxLen, "/%s",tokenArray[0] ); + for(i=1; i<numTokens-1; i++) + { + // create folders + strncat(createPath, "/", DbPathMaxLen-1); + strncat(createPath, tokenArray[i], DbPathMaxLen-1); + mkdir(createPath, 0744); + } + // finally create the file + strncat(createPath, "/", DbPathMaxLen-1); + strncat(createPath, tokenArray[i], DbPathMaxLen-1); + rval = open(createPath, O_CREAT|O_RDWR |O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); + close(rval); + } + else + { + DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclCreateFileAndPath ==> no valid path to create:"), DLT_STRING(path)); + } + + return rval; +} + diff --git a/src/persistence_client_library_key.c b/src/persistence_client_library_key.c index acd3852..94f03d5 100644 --- a/src/persistence_client_library_key.c +++ b/src/persistence_client_library_key.c @@ -292,36 +292,44 @@ int pclKeyHandleWriteData(int key_handle, unsigned char* buffer, int buffer_size { if(key_handle < MaxPersHandle) { - if(PersistenceStorage_custom == gKeyHandleArray[key_handle].info.configKey.storage) + if(gKeyHandleArray[key_handle].info.configKey.permission != O_RDONLY) // don't write to a read only resource { - int idx = custom_client_name_to_id(gKeyHandleArray[key_handle].dbPath, 1); - - if( (idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_handle_set_data != NULL) ) + if(PersistenceStorage_custom == gKeyHandleArray[key_handle].info.configKey.storage) { - size = gPersCustomFuncs[idx].custom_plugin_handle_set_data(key_handle, (char*)buffer, buffer_size-1); + int idx = custom_client_name_to_id(gKeyHandleArray[key_handle].dbPath, 1); - if(size >= 0) // success ==> send change notification + if( (idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_handle_set_data != NULL) ) { - int rval = pers_send_Notification_Signal(gKeyHandleArray[key_handle].dbKey, - &(gKeyHandleArray[key_handle].info.context), pclNotifyStatus_changed); + size = gPersCustomFuncs[idx].custom_plugin_handle_set_data(key_handle, (char*)buffer, buffer_size-1); - if(rval <= 0) + if(size >= 0) // success ==> send change notification { - DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclKeyHandleWriteData: error - failed to send notification")); - size = rval; + int rval = pers_send_Notification_Signal(gKeyHandleArray[key_handle].dbKey, + &(gKeyHandleArray[key_handle].info.context), pclNotifyStatus_changed); + + if(rval <= 0) + { + DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclKeyHandleWriteData: error - failed to send notification")); + size = rval; + } } } + else + { + size = EPERS_NOPLUGINFUNCT; + } } else { - size = EPERS_NOPLUGINFUNCT; + size = pers_db_write_key(gKeyHandleArray[key_handle].dbPath, gKeyHandleArray[key_handle].dbKey, + &gKeyHandleArray[key_handle].info, buffer, buffer_size); } } else { - size = pers_db_write_key(gKeyHandleArray[key_handle].dbPath, gKeyHandleArray[key_handle].dbKey, - &gKeyHandleArray[key_handle].info, buffer, buffer_size); + size = EPERS_RESOURCE_READ_ONLY; } + } else { @@ -525,18 +533,25 @@ int pclKeyWriteData(unsigned int ldbid, const char* resource_id, unsigned int us if( (data_size >= 0) && (dbContext.configKey.type == PersistenceResourceType_key)) { - // get hash value of data to verify storing - hash_val_data = pclCrc32(hash_val_data, buffer, buffer_size); - - // store data - if( dbContext.configKey.storage < PersistenceStorage_LastEntry - && dbContext.configKey.storage >= PersistenceStorage_local) // check if store policy is valid + if(dbContext.configKey.permission != O_RDONLY) // don't write to a read only resource { - data_size = pers_db_write_key(dbPath, dbKey, &dbContext, buffer, buffer_size); + // get hash value of data to verify storing + hash_val_data = pclCrc32(hash_val_data, buffer, buffer_size); + + // store data + if( dbContext.configKey.storage < PersistenceStorage_LastEntry + && dbContext.configKey.storage >= PersistenceStorage_local) // check if store policy is valid + { + data_size = pers_db_write_key(dbPath, dbKey, &dbContext, buffer, buffer_size); + } + else + { + data_size = EPERS_BADPOL; + } } else { - data_size = EPERS_BADPOL; + data_size = EPERS_RESOURCE_READ_ONLY; } } else diff --git a/src/persistence_client_library_prct_access.c b/src/persistence_client_library_prct_access.c index d8f169b..b27c125 100644 --- a/src/persistence_client_library_prct_access.c +++ b/src/persistence_client_library_prct_access.c @@ -192,7 +192,7 @@ int get_db_context(PersistenceInfo_s* dbContext, const char* resource_id, unsign // dbContext->configKey.policy = PersistencePolicy_wc; dbContext->configKey.storage = PersistenceStorage_local; - dbContext->configKey.permission = 0; // TODO define default permission + dbContext->configKey.permission = O_RDWR; dbContext->configKey.max_size = defaultMaxKeyValDataSize; if(isFile == PersistenceResourceType_file) { diff --git a/test/data/Data.tar.gz b/test/data/Data.tar.gz Binary files differindex 740b4da..37a9f76 100644 --- a/test/data/Data.tar.gz +++ b/test/data/Data.tar.gz diff --git a/test/persistence_client_library_test.c b/test/persistence_client_library_test.c index 789c9cc..412042e 100644 --- a/test/persistence_client_library_test.c +++ b/test/persistence_client_library_test.c @@ -966,7 +966,7 @@ START_TEST(test_GetPath) { int ret = 0; char* path = NULL; - const char* thePath = "/Data/mnt-wt/lt-persistence_client_library_test/user/1/seat/1/media/mediaDB.db"; + const char* thePath = "/Data/mnt-wt/lt-persistence_client_library_test/user/1/seat/1/media/mediaDB_create.db"; unsigned int pathSize = 0; unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL; @@ -974,8 +974,7 @@ START_TEST(test_GetPath) ret = pclInitLibrary(gTheAppId, shutdownReg); fail_unless(ret <= 1, "Failed to init PCL"); #if 1 - ret = pclFileCreatePath(0xFF, "media/mediaDB.db", 1, 1, &path, &pathSize); - //printf("PATH: %s \n", path); + ret = pclFileCreatePath(0xFF, "media/mediaDB_create.db", 1, 1, &path, &pathSize); fail_unless(strncmp((char*)path, thePath, strlen((char*)path)) == 0, "Path not correct"); fail_unless(pathSize == strlen((char*)path), "Path size not correct"); @@ -1075,8 +1074,6 @@ int main(int argc, char *argv[]) nr_failed = srunner_ntests_failed(sr); srunner_free(sr); -#else - #endif // unregister debug log and trace |