diff options
author | Ingo Huerner <ingo.huerner@xse.de> | 2014-07-16 09:48:04 +0200 |
---|---|---|
committer | Ingo Huerner <ingo.huerner@xse.de> | 2014-07-16 09:48:04 +0200 |
commit | 28823cabacd175c09c80849e5b4468fd0a828a9f (patch) | |
tree | 05d35144d6e0fef2c8c7198e1624bdc93d224a27 | |
parent | 7a58293cfcba5cf180b81b0e3d0ec38ee6c9dc2d (diff) | |
download | persistence-client-library-28823cabacd175c09c80849e5b4468fd0a828a9f.tar.gz |
Now files will be correctly handled (cached/non cached)
-rw-r--r-- | src/persistence_client_library_backup_filelist.c | 18 | ||||
-rw-r--r-- | src/persistence_client_library_backup_filelist.h | 4 | ||||
-rw-r--r-- | src/persistence_client_library_db_access.c | 1 | ||||
-rw-r--r-- | src/persistence_client_library_file.c | 151 | ||||
-rw-r--r-- | src/persistence_client_library_handle.c | 28 | ||||
-rw-r--r-- | src/persistence_client_library_handle.h | 24 | ||||
-rw-r--r-- | test/data/PAS_data.tar.gz | bin | 5918 -> 5897 bytes | |||
-rw-r--r-- | test/persistence_client_library_test.c | 44 |
8 files changed, 217 insertions, 53 deletions
diff --git a/src/persistence_client_library_backup_filelist.c b/src/persistence_client_library_backup_filelist.c index 7b596dc..0af204e 100644 --- a/src/persistence_client_library_backup_filelist.c +++ b/src/persistence_client_library_backup_filelist.c @@ -317,7 +317,7 @@ static int pclBackupDoFileCopy(int srcFd, int dstFd) } -int pclCreateFile(const char* path) +int pclCreateFile(const char* path, int chached) { const char* delimiters = "/\n"; // search for blank and end of line char* tokenArray[24]; @@ -360,10 +360,20 @@ int pclCreateFile(const char* path) // finally create the file strncat(createPath, "/", DbPathMaxLen-1); strncat(createPath, tokenArray[i], DbPathMaxLen-1); + + + #if USE_FILECACHE - handle = pfcOpenFile(createPath, CreateFile); + if(chached == 0) + { + handle = open(createPath, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); + } + else + { + handle = pfcOpenFile(createPath, CreateFile); + } #else - 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); #endif } else @@ -576,7 +586,7 @@ int pclCreateBackup(const char* dstPath, int srcfd, const char* csumPath, const char pathToCreate[DbPathMaxLen] = {0}; strncpy(pathToCreate, dstPath, DbPathMaxLen); - handle = pclCreateFile(pathToCreate); + handle = pclCreateFile(pathToCreate, 0); close(handle); // don't need the open file } diff --git a/src/persistence_client_library_backup_filelist.h b/src/persistence_client_library_backup_filelist.h index c940967..49d8a7c 100644 --- a/src/persistence_client_library_backup_filelist.h +++ b/src/persistence_client_library_backup_filelist.h @@ -38,10 +38,12 @@ int readBlacklistConfigFile(const char* filename); * @brief create the file * * @param path of the file to be created + * @param cached 1 if file should be cached, + * 0 if file should not be cached * * @return the handle to his file */ -int pclCreateFile(const char* path); +int pclCreateFile(const char* path, int chached); /** diff --git a/src/persistence_client_library_db_access.c b/src/persistence_client_library_db_access.c index cad0e5d..2bb6d58 100644 --- a/src/persistence_client_library_db_access.c +++ b/src/persistence_client_library_db_access.c @@ -482,6 +482,7 @@ int persistence_set_data(char* dbPath, char* key, PersistenceInfo_s* info, unsig snprintf(pathKeyString, 128, "0x%08X/%s", info->context.ldbid, info->configKey.customID); } write_size = gPersCustomFuncs[idx].custom_plugin_set_data(pathKeyString, (char*)buffer, buffer_size); + //write_size = persComDbWriteKey(handleDB, key, (char*)buffer, buffer_size) ; if ((0 < write_size) && ((unsigned int)write_size == buffer_size)) /* Check return value and send notification if OK */ { diff --git a/src/persistence_client_library_file.c b/src/persistence_client_library_file.c index c2d20e7..f487d7c 100644 --- a/src/persistence_client_library_file.c +++ b/src/persistence_client_library_file.c @@ -92,7 +92,14 @@ int pclFileClose(int fd) } __sync_fetch_and_sub(&gOpenFdArray[fd], FileClosed); // set closed flag #if USE_FILECACHE - rval = pfcCloseFile(fd); + if(get_file_cache_status(fd) == 1) + { + rval = pfcCloseFile(fd); + } + else + { + rval = close(fd); + } #else rval = close(fd); #endif @@ -114,14 +121,25 @@ int pclFileGetSize(int fd) if(gPclInitialized >= PCLinitialized) { + struct stat buf; #if USE_FILECACHE - size = pfcFileGetSize(fd); + if(get_file_cache_status(fd) == 1) + { + size = pfcFileGetSize(fd); + } + else + { + size = fstat(fd, &buf); + + if(size != -1) + { + size = buf.st_size; + } + } #else - struct stat buf; - size = fstat(fd, &buf); - //DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclFileGetSize fd: "), DLT_INT(fd)); + size = fstat(fd, &buf); if(size != -1) { @@ -169,6 +187,7 @@ int pclFileOpen(unsigned int ldbid, const char* resource_id, unsigned int user_n { int shared_DB = 0; int wantBackup = 1; + int cacheStatus = -1; PersistenceInfo_s dbContext; char dbKey[DbKeyMaxLen] = {0}; // database key @@ -185,7 +204,10 @@ int pclFileOpen(unsigned int ldbid, const char* resource_id, unsigned int user_n // get database context: database path and database key shared_DB = get_db_context(&dbContext, resource_id, ResIsFile, dbKey, dbPath); - if(dbContext.configKey.type == PersistenceResourceType_file) // check if the resource is really a file + // + // check if the resource is marked as a file resource + // + if(dbContext.configKey.type == PersistenceResourceType_file) { // create backup path int length = 0; @@ -204,7 +226,10 @@ int pclFileOpen(unsigned int ldbid, const char* resource_id, unsigned int user_n snprintf(backupPath, DbPathMaxLen-1, "%s%s%s", gBackupPrefix, fileSubPath, gBackupPostfix); snprintf(csumPath, DbPathMaxLen-1, "%s%s%s", gBackupPrefix, fileSubPath, gBackupCsPostfix); - if(shared_DB >= 0) // check valid database context + // + // check valid database context + // + if(shared_DB >= 0) { int flags = pclGetPosixPermission(dbContext.configKey.permission); @@ -229,24 +254,44 @@ int pclFileOpen(unsigned int ldbid, const char* resource_id, unsigned int user_n } #if USE_FILECACHE - if(handle > 0) // when the file is open, close it and do a new open unde PFC control { close(handle); } - handle = pfcOpenFile(dbPath, DontCreateFile); + if(strstr(dbPath, WTPREFIX) != NULL) + { + // if it's a write through resource, add the O_SYNC and O_DIRECT flag to prevent caching + handle = open(dbPath, flags); + cacheStatus = 0; + } + else + { + handle = pfcOpenFile(dbPath, DontCreateFile); + cacheStatus = 1; + } + #else if(handle <= 0) // check if open is needed or already done in verifyConsistency { handle = open(dbPath, flags); + + if(strstr(dbPath, WTPREFIX) != NULL) + { + cacheStatus = 0; + } + else + { + cacheStatus = 1; + } } #endif - - if(handle == -1 && errno == ENOENT) // file does not exist, create file and folder + // + // file does not exist, create it and get default data + // + if(handle == -1 && errno == ENOENT) { - - if((handle = pclCreateFile(dbPath)) == -1) + if((handle = pclCreateFile(dbPath, cacheStatus)) == -1) { DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pclFileOpen: error => failed to create file: "), DLT_STRING(dbPath)); } @@ -257,6 +302,8 @@ int pclFileOpen(unsigned int ldbid, const char* resource_id, unsigned int user_n DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("pclFileOpen: no default data available: "), DLT_STRING(resource_id)); } } + + set_file_cache_status(handle, cacheStatus); } if(dbContext.configKey.permission != PersistencePermission_ReadOnly) @@ -272,13 +319,16 @@ int pclFileOpen(unsigned int ldbid, const char* resource_id, unsigned int user_n handle = EPERS_MAXHANDLE; } } - } - else // requested resource is not in the RCT, so create resource as local/cached. + // + // requested resource is not in the RCT, so create resource as local/cached. + // + else { // assemble file string for local cached location snprintf(dbPath, DbPathMaxLen, gLocalCacheFilePath, gAppId, user_no, seat_no, resource_id); - handle = pclCreateFile(dbPath); + handle = pclCreateFile(dbPath, 1); + set_file_cache_status(handle, 1); if(handle != -1) { @@ -289,7 +339,11 @@ int pclFileOpen(unsigned int ldbid, const char* resource_id, unsigned int user_n } else { +#if USE_FILECACHE + pfcCloseFile(handle); +#else close(handle); +#endif handle = EPERS_MAXHANDLE; } } @@ -297,9 +351,9 @@ int pclFileOpen(unsigned int ldbid, const char* resource_id, unsigned int user_n } else { - handle = EPERS_RESOURCE_NO_FILE; + handle = EPERS_RESOURCE_NO_FILE; // resource is not marked as file in RCT } - } + } // initialized return handle; } @@ -310,11 +364,19 @@ int pclFileReadData(int fd, void * buffer, int buffer_size) { int readSize = EPERS_NOT_INITIALIZED; + //DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclFileReadData fd: "), DLT_INT(fd)); if(gPclInitialized >= PCLinitialized) { #if USE_FILECACHE - readSize = pfcReadFile(fd, buffer, buffer_size); + if(get_file_cache_status(fd) == 1) + { + readSize = pfcReadFile(fd, buffer, buffer_size); + } + else + { + readSize = read(fd, buffer, buffer_size); + } #else readSize = read(fd, buffer, buffer_size); #endif @@ -384,7 +446,14 @@ int pclFileSeek(int fd, long int offset, int whence) if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked { #if USE_FILECACHE - rval = pfcFileSeek(fd, offset, whence); + if(get_file_cache_status(fd) == 1) + { + rval = pfcFileSeek(fd, offset, whence); + } + else + { + rval = lseek(fd, offset, whence); + } #else rval = lseek(fd, offset, whence); #endif @@ -453,9 +522,24 @@ int pclFileWriteData(int fd, const void * buffer, int buffer_size) } #if USE_FILECACHE - size = pfcWriteFile(fd, buffer, buffer_size); + if(get_file_cache_status(fd) == 1) + { + size = pfcWriteFile(fd, buffer, buffer_size); + } + else + { + size = write(fd, buffer, buffer_size); + + if(fsync(fd) == -1) + DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("pclFileWriteData: Failed to fsync ==>!"), DLT_STRING(strerror(errno))); + } #else size = write(fd, buffer, buffer_size); + if(get_file_cache_status(fd) == 1) + { + if(fsync(fd) == -1) + DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("pclFileWriteData: Failed to fsync ==>!"), DLT_STRING(strerror(errno))); + } #endif } else @@ -510,7 +594,7 @@ int pclFileCreatePath(unsigned int ldbid, const char* resource_id, unsigned int // file will be opened writable, so check about data consistency if( dbContext.configKey.permission != PersistencePermission_ReadOnly - && pclBackupNeeded( get_raw_string(dbPath)) == CREATE_BACKUP) + && pclBackupNeeded(get_raw_string(dbPath)) == CREATE_BACKUP) { snprintf(backupPath, DbPathMaxLen-1, "%s%s", dbPath, gBackupPostfix); snprintf(csumPath, DbPathMaxLen-1, "%s%s", dbPath, gBackupCsPostfix); @@ -547,9 +631,20 @@ 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 = 0; - if((handle = pclCreateFile(*path)) == -1) + int handle = 0, cacheStatus = -1; + if(strstr(dbPath, WTPREFIX) != NULL) + { + cacheStatus = 0; + } + else + { + cacheStatus = 1; + } + + handle = pclCreateFile(*path, cacheStatus); // file does not exist, create it. + set_file_cache_status(handle, cacheStatus); + + if(handle == -1) { DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pclFileCreatePath: error => failed to create file: "), DLT_STRING(*path)); } @@ -562,7 +657,6 @@ int pclFileCreatePath(unsigned int ldbid, const char* resource_id, unsigned int close(handle); // don't need the open file } } - __sync_fetch_and_add(&gOpenHandleArray[handle], FileOpen); // set open flag set_ossfile_handle_data(handle, dbContext.configKey.permission, 0/*backupCreated*/, backupPath, csumPath, *path); @@ -581,7 +675,10 @@ int pclFileCreatePath(unsigned int ldbid, const char* resource_id, unsigned int } } } - else // requested resource is not in the RCT, so create resource as local/cached. + // + // requested resource is not in the RCT, so create resource as local/cached. + // + else { // assemble file string for local cached location snprintf(dbPath, DbPathMaxLen, gLocalCacheFilePath, gAppId, user_no, seat_no, resource_id); diff --git a/src/persistence_client_library_handle.c b/src/persistence_client_library_handle.c index aac4a71..60ea521 100644 --- a/src/persistence_client_library_handle.c +++ b/src/persistence_client_library_handle.c @@ -193,9 +193,10 @@ int set_file_handle_data(int idx, PersistencePermission_e permission, const char { strcpy(gFileHandleArray[idx].backupPath, backup); strcpy(gFileHandleArray[idx].csumPath, csumPath); - gFileHandleArray[idx].backupCreated = 0; // set to 0 by default + 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].filePath = filePath; // check to do if this works + gFileHandleArray[idx].cacheStatus = -1; // set to -1 by default } pthread_mutex_unlock(&gFileHandleAccessMtx); } @@ -250,6 +251,29 @@ int get_file_backup_status(int idx) return gFileHandleArray[idx].backupCreated; } +void set_file_cache_status(int idx, int status) +{ + if(pthread_mutex_lock(&gFileHandleAccessMtx) == 0) + { + gFileHandleArray[idx].cacheStatus = status; + + pthread_mutex_unlock(&gFileHandleAccessMtx); + } +} + +int get_file_cache_status(int idx) +{ + int status = -1; + if(pthread_mutex_lock(&gFileHandleAccessMtx) == 0) + { + if(MaxPersHandle >= idx) + { + status = gFileHandleArray[idx].cacheStatus; + } + pthread_mutex_unlock(&gFileHandleAccessMtx); + } + return status; +} //---------------------------------------------------------- //---------------------------------------------------------- diff --git a/src/persistence_client_library_handle.h b/src/persistence_client_library_handle.h index d4d1260..48ab055 100644 --- a/src/persistence_client_library_handle.h +++ b/src/persistence_client_library_handle.h @@ -45,6 +45,8 @@ typedef struct _PersistenceFileHandle_s PersistencePermission_e permission; /// flag to indicate if a backup has already been created int backupCreated; + /// flag to indicate if file must be cached + int cacheStatus; /// path to the backup file char backupPath[DbPathMaxLen]; /// path to the checksum file @@ -197,6 +199,28 @@ void set_file_backup_status(int idx, int status); */ int get_file_backup_status(int idx); + +/** + * @brief set the file cache status + * @attention "No index check will be done" + * + * @param idx the index + * @param status the cache status, 0 file must not be cached, + * 1 file must be cached + */ +void set_file_cache_status(int idx, int status); + + +/** + * @brief get the cache status of the file + * @attention "No index check will be done" + * + * @param idx the index + * + * @return 0 if file must not be cached, + * 1 if file must be cached + */ +int get_file_cache_status(int idx); //---------------------------------------------------------------- //---------------------------------------------------------------- diff --git a/test/data/PAS_data.tar.gz b/test/data/PAS_data.tar.gz Binary files differindex 6fc9000..235ef1b 100644 --- a/test/data/PAS_data.tar.gz +++ b/test/data/PAS_data.tar.gz diff --git a/test/persistence_client_library_test.c b/test/persistence_client_library_test.c index cb9cf21..e159491 100644 --- a/test/persistence_client_library_test.c +++ b/test/persistence_client_library_test.c @@ -644,7 +644,6 @@ START_TEST(test_DataFile) #if 1 writeBuffer = malloc(writeSize); - // fill buffer a sequence for(i = 0; i<(writeSize/8); i++) { @@ -672,6 +671,7 @@ START_TEST(test_DataFile) size = pclFileReadData(fd, buffer, READ_SIZE); + //printf("pclFileReadData:\n ist : \"%s\"\n soll: \"%s\" ==> ret: %d => fd: %d\n", buffer, refBuffer, size, fd); x_fail_unless(strncmp((char*)buffer, refBuffer, strlen(refBuffer)) == 0, "Buffer not correctly read => media/mediaDB.db"); x_fail_unless(size == (strlen(refBuffer)+1), "Wrong size returned"); // strlen + 1 ==> inlcude cr/lf @@ -715,11 +715,11 @@ START_TEST(test_DataFile) // negative test size = pclFileGetSize(1024); - x_fail_unless(size == EPERS_NOKEYDATA, "Got size, but should not"); - + x_fail_unless(size < 0 , "Got size, but should not"); + /* ret = pclFileClose(fd); x_fail_unless(ret == 0, "Failed to close file"); - +*/ // test backup blacklist functionality fdArray[0] = pclFileOpen(0xFF, "media/doNotBackupMe.txt_START", 1, 1); @@ -960,19 +960,6 @@ START_TEST(test_DataHandle) ret = pclInitLibrary(gTheAppId, shutdownReg); x_fail_unless(ret <= 1, "Failed to init PCL"); #if 1 - // test file handles - handle1 = pclFileOpen(0xFF, "media/mediaDB.db", 1, 1); - x_fail_unless(handle1 != -1, "Could not open file ==> /media/mediaDB.db"); - - ret = pclFileClose(handle1); - x_fail_unless(handle1 != -1, "Could not closefile ==> /media/mediaDB.db"); - - ret = pclFileClose(1024); - x_fail_unless(ret == EPERS_MAXHANDLE, "Could close file, but should not!!"); - - ret = pclFileClose(17); - x_fail_unless(ret == -1, "Could close file, but should not!!"); - // test multiple handles handleArray[0] = pclFileOpen(0xFF, "media/mediaDB_write_01.db", 1, 1); x_fail_unless(handle1 != -1, "Could not open file ==> /media/mediaDB_write_01.db"); @@ -986,22 +973,26 @@ START_TEST(test_DataHandle) handleArray[3] = pclFileOpen(0xFF, "media/mediaDB_write_04.db", 1, 1); x_fail_unless(handle1 != -1, "Could not open file ==> /media/mediaDB_write_04.db"); + memset(buffer, 0, READ_SIZE); ret = pclFileReadData(handleArray[0], buffer, READ_SIZE); x_fail_unless(ret >= 0, "Failed to read handle idx \"0\"!!"); x_fail_unless(strncmp((char*)buffer, "/user/1/seat/1/media/mediaDB_write_01.db", strlen("/user/1/seat/1/media/mediaDB_write_01.db")) == 0, "Buffer not correctly read => mediaDB_write_01.db"); + memset(buffer, 0, READ_SIZE); ret = pclFileReadData(handleArray[1], buffer, READ_SIZE); x_fail_unless(strncmp((char*)buffer, "/user/1/seat/1/media/mediaDB_write_02.db", strlen("/user/1/seat/1/media/mediaDB_write_02.db")) == 0, "Buffer not correctly read => mediaDB_write_02.db"); + memset(buffer, 0, READ_SIZE); ret = pclFileReadData(handleArray[2], buffer, READ_SIZE); x_fail_unless(strncmp((char*)buffer, "/user/1/seat/1/media/mediaDB_write_03.db", strlen("/user/1/seat/1/media/mediaDB_write_03.db")) == 0, "Buffer not correctly read => mediaDB_write_03.db"); + memset(buffer, 0, READ_SIZE); (void)pclFileReadData(handleArray[3], buffer, READ_SIZE); x_fail_unless(strncmp((char*)buffer, "/user/1/seat/1/media/mediaDB_write_04.db", strlen("/user/1/seat/1/media/mediaDB_write_04.db")) @@ -1028,6 +1019,21 @@ START_TEST(test_DataHandle) ret = pclKeyHandleClose(1024); x_fail_unless(ret == EPERS_MAXHANDLE, "Max handle!!"); + + + // test file handles + handle1 = pclFileOpen(0xFF, "media/mediaDB.db", 1, 1); + x_fail_unless(handle1 != -1, "Could not open file ==> /media/mediaDB.db"); + + ret = pclFileClose(handle1); + x_fail_unless(handle1 != -1, "Could not closefile ==> /media/mediaDB.db"); + + ret = pclFileClose(1024); + x_fail_unless(ret == EPERS_MAXHANDLE, "Could close file, but should not!!"); + + ret = pclFileClose(19); + x_fail_unless(ret == -1, "Could close file, but should not!!"); + #endif pclDeinitLibrary(); } @@ -1279,7 +1285,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_create.db"; + const char* thePath = "/Data/mnt-c/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; @@ -1410,7 +1416,7 @@ START_TEST(test_utf8_string) (void)pclInitLibrary(gTheAppId, shutdownReg); ret = pclKeyReadData(0xFF, "utf8String", 3, 2, buffer, READ_SIZE); - x_fail_unless(ret == strlen(utf8StringBuffer), "Wrong write size"); + x_fail_unless(ret == strlen(utf8StringBuffer), "Wrong read size"); x_fail_unless(strncmp((char*)buffer, utf8StringBuffer, ret-1) == 0, "Buffer not correctly read => 1"); size = pclKeyGetSize(0xFF, "utf8String", 3, 2); |