diff options
author | Ingo Huerner <ingo.huerner@xse.de> | 2015-03-17 10:14:03 +0100 |
---|---|---|
committer | Ingo Huerner <ingo.huerner@xse.de> | 2015-03-17 10:14:03 +0100 |
commit | 944899b3120d81649f0efc800de74935903d2851 (patch) | |
tree | cb31371973e4fa34191f99078cfa9ea40d7b6a70 /src | |
parent | 8adc99a9dedd74b6209bfa785545c68ddbfc7732 (diff) | |
download | persistence-client-library-944899b3120d81649f0efc800de74935903d2851.tar.gz |
Fixed a problem with file handle tree
Diffstat (limited to 'src')
-rw-r--r-- | src/persistence_client_library.c | 4 | ||||
-rw-r--r-- | src/persistence_client_library_backup_filelist.c | 9 | ||||
-rw-r--r-- | src/persistence_client_library_backup_filelist.h | 6 | ||||
-rw-r--r-- | src/persistence_client_library_db_access.c | 11 | ||||
-rw-r--r-- | src/persistence_client_library_db_access.h | 6 | ||||
-rw-r--r-- | src/persistence_client_library_file.c | 16 | ||||
-rw-r--r-- | src/persistence_client_library_handle.c | 514 | ||||
-rw-r--r-- | src/persistence_client_library_handle.h | 23 | ||||
-rw-r--r-- | src/persistence_client_library_tree_helper.c | 17 | ||||
-rw-r--r-- | src/persistence_client_library_tree_helper.h | 9 |
10 files changed, 439 insertions, 176 deletions
diff --git a/src/persistence_client_library.c b/src/persistence_client_library.c index 118953c..ace9d5f 100644 --- a/src/persistence_client_library.c +++ b/src/persistence_client_library.c @@ -282,6 +282,10 @@ static int private_pclDeinitLibrary(void) deliverToMainloop_NM(&data); // send quit command to dbus mainloop + deleteHandleTrees(); // delete allocated trees + deleteBackupTree(); + deleteNotifyTree(); + pthread_join(gMainLoopThread, (void**)&retval); // wait until the dbus mainloop has ended pthread_mutex_unlock(&gDbusPendingRegMtx); diff --git a/src/persistence_client_library_backup_filelist.c b/src/persistence_client_library_backup_filelist.c index fdb7718..d5b6447 100644 --- a/src/persistence_client_library_backup_filelist.c +++ b/src/persistence_client_library_backup_filelist.c @@ -44,6 +44,15 @@ static int need_backup_key(unsigned int key); static int pclRecoverFromBackup(int backupFd, const char* original); +void deleteBackupTree(void) +{ + if(gRb_tree_bl != NULL) + { + jsw_rbdelete(gRb_tree_bl); + gRb_tree_bl = NULL; + } +} + static void fillFileBackupCharTokenArray(unsigned int customConfigFileSize, char* fileMap) { unsigned int i=0; diff --git a/src/persistence_client_library_backup_filelist.h b/src/persistence_client_library_backup_filelist.h index 56e1d44..cf7f2ee 100644 --- a/src/persistence_client_library_backup_filelist.h +++ b/src/persistence_client_library_backup_filelist.h @@ -108,4 +108,10 @@ inline int pclBackupNeeded(const char* path); int pclGetPosixPermission(PersistencePermission_e permission); +/** + * @brief delete backup tree + */ +void deleteBackupTree(void); + + #endif /* PERS_BACKUP_BLACKLIST_H */ diff --git a/src/persistence_client_library_db_access.c b/src/persistence_client_library_db_access.c index 55f168a..c20f2b4 100644 --- a/src/persistence_client_library_db_access.c +++ b/src/persistence_client_library_db_access.c @@ -38,6 +38,17 @@ static int gHandlesDBCreated[DbTableSize][PersistenceDB_LastEntry] = { {0} }; /// tree to store notification information static jsw_rbtree_t *gNotificationTree = NULL; + +void deleteNotifyTree(void) +{ + if(gNotificationTree != NULL) + { + jsw_rbdelete(gNotificationTree); + gNotificationTree = NULL; + } +} + + static int database_get(PersistenceInfo_s* info, const char* dbPath, int dbType) { int arrayIdx = 0, handleDB = -1; diff --git a/src/persistence_client_library_db_access.h b/src/persistence_client_library_db_access.h index 86331ce..75f6c68 100644 --- a/src/persistence_client_library_db_access.h +++ b/src/persistence_client_library_db_access.h @@ -195,6 +195,12 @@ int pers_send_Notification_Signal(const char* key, PersistenceDbContext_s* conte void pers_rct_close_all(); +/** + * @brief delete notification tree + */ +void deleteNotifyTree(void); + + #ifdef __cplusplus } #endif diff --git a/src/persistence_client_library_file.c b/src/persistence_client_library_file.c index ca8c681..6f14777 100644 --- a/src/persistence_client_library_file.c +++ b/src/persistence_client_library_file.c @@ -111,6 +111,13 @@ int pclFileClose(int fd) } __sync_fetch_and_sub(&gOpenFdArray[fd], FileClosed); // set closed flag + + // remove form file tree; + if(remove_file_handle_data(fd) != 1) + { + DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("pclFileClose - Failed to remove from tree!"), DLT_INT(fd) ); + } + #if USE_FILECACHE if(get_file_cache_status(fd) == 1) { @@ -343,7 +350,6 @@ 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 } @@ -417,6 +423,8 @@ int pclFileOpen(unsigned int ldbid, const char* resource_id, unsigned int user_n { handle = pclFileOpenDefaultData(&dbContext, resource_id); set_file_user_id(handle, PCL_USER_DEFAULTDATA); + // as default data will be opened, use read/write permission and we don't need backup and csum path so use an empty string. + set_file_handle_data(handle, PersistencePermission_ReadWrite, "", "", NULL); } else { @@ -812,6 +820,12 @@ int pclFileReleasePath(int pathHandle) set_persistence_handle_close_idx(pathHandle); set_ossfile_file_path(pathHandle, NULL); + + if(remove_ossfile_handle_data(pathHandle) != 1) + { + DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("pclFileReleasePath - Failed to remove from tree!"), DLT_INT(pathHandle) ); + } + rval = 1; } else diff --git a/src/persistence_client_library_handle.c b/src/persistence_client_library_handle.c index 09311ae..d9e1a78 100644 --- a/src/persistence_client_library_handle.c +++ b/src/persistence_client_library_handle.c @@ -53,6 +53,28 @@ static int gFreeHandleArray[MaxPersHandle] = { [0 ...MaxPersHandle-1] = 0 }; static int gFreeHandleIdxHead = 0; +void deleteHandleTrees(void) +{ + if(gKeyHandleTree != NULL) + { + jsw_rbdelete(gKeyHandleTree); + gKeyHandleTree = NULL; + } + + if(gFileHandleTree != NULL) + { + jsw_rbdelete(gFileHandleTree); + gFileHandleTree = NULL; + } + + if(gOssFileHandleTree != NULL) + { + jsw_rbdelete(gOssFileHandleTree); + gOssFileHandleTree = NULL; + } +} + + int get_persistence_handle_idx() { int handle = 0; @@ -224,6 +246,28 @@ void clear_key_handle_array(int idx) } +int remove_file_handle_data(int idx) +{ + int rval = -1; + + if(pthread_mutex_lock(&gFileHandleAccessMtx) == 0) + { + if(gFileHandleTree != NULL) + { + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); // assign key and value to the rbtree item + if(item != NULL) + { + item->key = idx; + rval = jsw_rberase(gFileHandleTree, item); + } + } + + pthread_mutex_unlock(&gFileHandleAccessMtx); + } + + return rval; +} + int set_file_handle_data(int idx, PersistencePermission_e permission, const char* backup, const char* csumPath, char* filePath) { int rval = -1; @@ -240,24 +284,58 @@ int set_file_handle_data(int idx, PersistencePermission_e permission, const char item = malloc(sizeof(FileHandleTreeItem_s)); // assign key and value to the rbtree item if(item != NULL) { + FileHandleTreeItem_s* foundItem = NULL; item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gFileHandleTree, item); + if(foundItem == 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; + 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, PERS_ORG_MAX_LENGTH_PATH_FILENAME); - item->value.fileHandle.backupPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination + strncpy(item->value.fileHandle.backupPath, backup, PERS_ORG_MAX_LENGTH_PATH_FILENAME); + item->value.fileHandle.backupPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination - strncpy(item->value.fileHandle.csumPath, csumPath, PERS_ORG_MAX_LENGTH_PATH_FILENAME); - item->value.fileHandle.csumPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination + strncpy(item->value.fileHandle.csumPath, csumPath, PERS_ORG_MAX_LENGTH_PATH_FILENAME); + item->value.fileHandle.csumPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination - jsw_rbinsert(gFileHandleTree, item); + //debugFileItem("set_file_handle_data => insert", item); + jsw_rbinsert(gFileHandleTree, item); - free(item); + } + else + { + FileHandleTreeItem_s* newItem = malloc(sizeof(FileHandleTreeItem_s)); + if(newItem != NULL) + { + memcpy(newItem->value.payload , foundItem->value.payload, sizeof(FileHandleData_u) ); // duplicate value + + newItem->key = idx; + + newItem->value.fileHandle.permission = permission; + newItem->value.fileHandle.filePath = filePath; + + strncpy(newItem->value.fileHandle.backupPath, backup, PERS_ORG_MAX_LENGTH_PATH_FILENAME); + newItem->value.fileHandle.backupPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination + + strncpy(newItem->value.fileHandle.csumPath, csumPath, PERS_ORG_MAX_LENGTH_PATH_FILENAME); + newItem->value.fileHandle.csumPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination + + //debugFileItem("set_file_backup_status => erase => foundItem", foundItem); + jsw_rberase(gFileHandleTree, foundItem); + + //debugFileItem("set_file_backup_status => insert => newItem", newItem); + jsw_rbinsert(gFileHandleTree, newItem); + + free(newItem); + } + } + free(item); rval = 0; } @@ -285,6 +363,7 @@ int get_file_permission(int idx) if(foundItem != NULL) { permission = foundItem->value.fileHandle.permission; + //debugFileItem("get_file_permission => foundItem", foundItem); } else { @@ -315,6 +394,7 @@ char* get_file_backup_path(int idx) if(foundItem != NULL) { charPtr = foundItem->value.fileHandle.backupPath; + //debugFileItem("get_file_backup_path => foundItem", foundItem); } free(item); } @@ -341,6 +421,7 @@ char* get_file_checksum_path(int idx) if(foundItem != NULL) { charPtr = foundItem->value.fileHandle.csumPath; + //debugFileItem("get_file_checksum_path => foundItem", foundItem); } free(item); } @@ -355,44 +436,50 @@ void set_file_backup_status(int idx, int status) { if(pthread_mutex_lock(&gFileHandleAccessMtx) == 0) { - if(gFileHandleTree != NULL) + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + + if(gFileHandleTree == NULL) { - FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); - if(item != NULL) + gFileHandleTree = jsw_rbnew(fh_key_val_cmp, fh_key_val_dup, fh_key_val_rel); + } + + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gFileHandleTree, item); + if(foundItem == 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.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; + 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 + //debugFileItem("set_file_backup_status => insert => item", item); + jsw_rbinsert(gFileHandleTree, item); + } + else + { + FileHandleTreeItem_s* newItem = malloc(sizeof(FileHandleTreeItem_s)); + if(newItem != NULL) { - FileHandleTreeItem_s* newItem = malloc(sizeof(FileHandleTreeItem_s)); - if(newItem != NULL) - { - memcpy(newItem->value.payload , foundItem->value.payload, sizeof(FileHandleData_u) ); // duplicate value + memcpy(newItem->value.payload , foundItem->value.payload, sizeof(FileHandleData_u) ); // duplicate value - newItem->key = idx; - newItem->value.fileHandle.backupCreated = status; + newItem->key = idx; + newItem->value.fileHandle.backupCreated = status; - jsw_rberase(gFileHandleTree, foundItem); + //debugFileItem("set_file_backup_status => erase => foundItem", foundItem); + jsw_rberase(gFileHandleTree, foundItem); - jsw_rbinsert(gFileHandleTree, newItem); + //debugFileItem("set_file_backup_status => insert => newItem", newItem); + jsw_rbinsert(gFileHandleTree, newItem); - free(newItem); - } + free(newItem); } - free(item); } + free(item); } pthread_mutex_unlock(&gFileHandleAccessMtx); } @@ -414,6 +501,7 @@ int get_file_backup_status(int idx) if(foundItem != NULL) { backup = foundItem->value.fileHandle.backupCreated; + //debugFileItem("get_file_backup_status => foundItem", foundItem); } free(item); } @@ -427,47 +515,53 @@ void set_file_cache_status(int idx, int status) { if(pthread_mutex_lock(&gFileHandleAccessMtx) == 0) { - if(gFileHandleTree != NULL) + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + + if(gFileHandleTree == NULL) { - FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); - if(item != NULL) + gFileHandleTree = jsw_rbnew(fh_key_val_cmp, fh_key_val_dup, fh_key_val_rel); + } + + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gFileHandleTree, item); + if(foundItem == 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.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; + 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, PERS_ORG_MAX_LENGTH_PATH_FILENAME); - item->value.fileHandle.csumPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination - memset(item->value.fileHandle.backupPath, 0, PERS_ORG_MAX_LENGTH_PATH_FILENAME); - item->value.fileHandle.backupPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination + memset(item->value.fileHandle.csumPath , 0, PERS_ORG_MAX_LENGTH_PATH_FILENAME); + item->value.fileHandle.csumPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination + memset(item->value.fileHandle.backupPath, 0, PERS_ORG_MAX_LENGTH_PATH_FILENAME); + item->value.fileHandle.backupPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination - jsw_rbinsert(gFileHandleTree, item); - } - else + //debugFileItem("set_file_cache_status => insert => item", item); + jsw_rbinsert(gFileHandleTree, item); + } + else + { + FileHandleTreeItem_s* newItem = malloc(sizeof(FileHandleTreeItem_s)); + if(newItem != NULL) { - FileHandleTreeItem_s* newItem = malloc(sizeof(FileHandleTreeItem_s)); - if(newItem != 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); - } + memcpy(newItem->value.payload , foundItem->value.payload, sizeof(FileHandleData_u) ); // duplicate value + + //debugFileItem("set_file_cache_status => erase => foundItem", foundItem); + jsw_rberase(gFileHandleTree, foundItem); + + newItem->key = idx; + newItem->value.fileHandle.cacheStatus = status; + //debugFileItem("set_file_cache_status => insert => newItem", newItem); + jsw_rbinsert(gFileHandleTree, newItem); + free(newItem); } - free(item); } + free(item); } pthread_mutex_unlock(&gFileHandleAccessMtx); } @@ -503,49 +597,56 @@ void set_file_user_id(int idx, int userID) { if(pthread_mutex_lock(&gFileHandleAccessMtx) == 0) { - if(gFileHandleTree != NULL) + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + if(gFileHandleTree == NULL) { - FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); - if(item != NULL) + gFileHandleTree = jsw_rbnew(fh_key_val_cmp, fh_key_val_dup, fh_key_val_rel); + } + + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gFileHandleTree, item); + if(foundItem == 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.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; + 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, PERS_ORG_MAX_LENGTH_PATH_FILENAME); - item->value.fileHandle.csumPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination - memset(item->value.fileHandle.backupPath, 0, PERS_ORG_MAX_LENGTH_PATH_FILENAME); - item->value.fileHandle.backupPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination + memset(item->value.fileHandle.csumPath , 0, PERS_ORG_MAX_LENGTH_PATH_FILENAME); + item->value.fileHandle.csumPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination + memset(item->value.fileHandle.backupPath, 0, PERS_ORG_MAX_LENGTH_PATH_FILENAME); + item->value.fileHandle.backupPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination - jsw_rbinsert(gFileHandleTree, item); - } - else + //debugFileItem("set_file_user_id => insert", item); + jsw_rbinsert(gFileHandleTree, item); + } + else + { + FileHandleTreeItem_s* newItem = malloc(sizeof(FileHandleTreeItem_s)); + if(newItem != NULL) { - FileHandleTreeItem_s* newItem = malloc(sizeof(FileHandleTreeItem_s)); - if(newItem != NULL) - { - memcpy(newItem->value.payload , foundItem->value.payload, sizeof(FileHandleData_u) ); // duplicate value + memcpy(newItem->value.payload , foundItem->value.payload, sizeof(FileHandleData_u) ); // duplicate value - jsw_rberase(gFileHandleTree, foundItem); + //debugFileItem("set_file_user_id => erase", foundItem); + jsw_rberase(gFileHandleTree, foundItem); - newItem->key = idx; - newItem->value.fileHandle.userId = userID; - jsw_rbinsert(gFileHandleTree, newItem); + newItem->key = idx; + newItem->value.fileHandle.userId = userID; + //debugFileItem("set_file_user_id => insert", newItem); + jsw_rbinsert(gFileHandleTree, newItem); - free(newItem); - } + free(newItem); } - free(item); } + free(item); + free(foundItem); } + pthread_mutex_unlock(&gFileHandleAccessMtx); } } @@ -595,23 +696,62 @@ int set_ossfile_handle_data(int idx, PersistencePermission_e permission, int bac item = malloc(sizeof(FileHandleTreeItem_s)); // assign key and value to the rbtree item if(item != NULL) { + FileHandleTreeItem_s* foundItem = NULL; item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gOssFileHandleTree, item); + if(foundItem == NULL) + { - 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; + item->key = idx; - strncpy(item->value.fileHandle.backupPath, backup, PERS_ORG_MAX_LENGTH_PATH_FILENAME); - item->value.fileHandle.backupPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination - strncpy(item->value.fileHandle.csumPath, csumPath, PERS_ORG_MAX_LENGTH_PATH_FILENAME); - item->value.fileHandle.csumPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination + 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; - jsw_rbinsert(gOssFileHandleTree, item); + strncpy(item->value.fileHandle.backupPath, backup, PERS_ORG_MAX_LENGTH_PATH_FILENAME); + item->value.fileHandle.backupPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination + strncpy(item->value.fileHandle.csumPath, csumPath, PERS_ORG_MAX_LENGTH_PATH_FILENAME); + item->value.fileHandle.csumPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination + + jsw_rbinsert(gOssFileHandleTree, item); + + free(item); + rval = 0; + } + else + { + FileHandleTreeItem_s* newItem = malloc(sizeof(FileHandleTreeItem_s)); + if(newItem != NULL) + { + memcpy(newItem->value.payload , foundItem->value.payload, sizeof(FileHandleData_u) ); // duplicate value + + newItem->key = idx; + + newItem->value.fileHandle.permission = permission; + newItem->value.fileHandle.filePath = filePath; + + strncpy(newItem->value.fileHandle.backupPath, backup, PERS_ORG_MAX_LENGTH_PATH_FILENAME); + newItem->value.fileHandle.backupPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination + + strncpy(newItem->value.fileHandle.csumPath, csumPath, PERS_ORG_MAX_LENGTH_PATH_FILENAME); + newItem->value.fileHandle.csumPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination + + //debugFileItem("set_file_backup_status => erase => foundItem", foundItem); + jsw_rberase(gOssFileHandleTree, foundItem); + + //debugFileItem("set_file_backup_status => insert => newItem", newItem); + jsw_rbinsert(gOssFileHandleTree, newItem); + + free(newItem); + } + + } + } + else + { - free(item); - rval = 0; } pthread_mutex_unlock(&gOssFileHandleAccessMtx); } @@ -683,7 +823,6 @@ char* get_ossfile_file_path(int idx) char* charPtr = NULL; if(pthread_mutex_lock(&gOssFileHandleAccessMtx) == 0) { - if(gOssFileHandleTree != NULL) { FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); @@ -708,48 +847,51 @@ void set_ossfile_file_path(int idx, char* file) { if(pthread_mutex_lock(&gOssFileHandleAccessMtx) == 0) { - if(gFileHandleTree != NULL) + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + + if(gOssFileHandleTree == NULL) { - FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); - if(item != NULL) + gOssFileHandleTree = jsw_rbnew(fh_key_val_cmp, fh_key_val_dup, fh_key_val_rel); + } + + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gOssFileHandleTree, item); + if(foundItem == 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.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, PERS_ORG_MAX_LENGTH_PATH_FILENAME); - item->value.fileHandle.csumPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination + 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, PERS_ORG_MAX_LENGTH_PATH_FILENAME); + item->value.fileHandle.csumPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination - memset(item->value.fileHandle.backupPath, 0, PERS_ORG_MAX_LENGTH_PATH_FILENAME); - item->value.fileHandle.backupPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination + memset(item->value.fileHandle.backupPath, 0, PERS_ORG_MAX_LENGTH_PATH_FILENAME); + item->value.fileHandle.backupPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME-1] = '\0'; // Ensures 0-Termination - jsw_rbinsert(gFileHandleTree, item); - } - else + jsw_rbinsert(gOssFileHandleTree, item); + } + else + { + FileHandleTreeItem_s* newItem = malloc(sizeof(FileHandleTreeItem_s)); + if(newItem != NULL) { - FileHandleTreeItem_s* newItem = malloc(sizeof(FileHandleTreeItem_s)); - if(newItem != NULL) - { - memcpy(newItem->value.payload , foundItem->value.payload, sizeof(FileHandleData_u) ); // duplicate value + memcpy(newItem->value.payload , foundItem->value.payload, sizeof(FileHandleData_u) ); // duplicate value - jsw_rberase(gFileHandleTree, foundItem); + jsw_rberase(gOssFileHandleTree, foundItem); - newItem->key = idx; - newItem->value.fileHandle.filePath = file; - jsw_rbinsert(gFileHandleTree, newItem); + newItem->key = idx; + newItem->value.fileHandle.filePath = file; + jsw_rbinsert(gOssFileHandleTree, newItem); - free(newItem); - } + free(newItem); } - free(item); } + free(item); } pthread_mutex_unlock(&gOssFileHandleAccessMtx); } @@ -786,43 +928,46 @@ void set_ossfile_backup_status(int idx, int status) { if(pthread_mutex_lock(&gOssFileHandleAccessMtx) == 0) { - if(gOssFileHandleTree != NULL) + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); + + if(gOssFileHandleTree == NULL) { - FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); - if(item != NULL) + gOssFileHandleTree = jsw_rbnew(fh_key_val_cmp, fh_key_val_dup, fh_key_val_rel); + } + + if(item != NULL) + { + FileHandleTreeItem_s* foundItem = NULL; + item->key = idx; + foundItem = (FileHandleTreeItem_s*)jsw_rbfind(gOssFileHandleTree, item); + if(foundItem == 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.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; + 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 + jsw_rbinsert(gOssFileHandleTree, item); + } + else + { + FileHandleTreeItem_s* newItem = malloc(sizeof(FileHandleTreeItem_s)); + if(newItem != NULL) { - FileHandleTreeItem_s* newItem = malloc(sizeof(FileHandleTreeItem_s)); - if(newItem != NULL) - { - memcpy(newItem->value.payload , foundItem->value.payload, sizeof(FileHandleData_u) ); // duplicate value + memcpy(newItem->value.payload , foundItem->value.payload, sizeof(FileHandleData_u) ); // duplicate value - newItem->key = idx; - newItem->value.fileHandle.backupCreated = status; + newItem->key = idx; + newItem->value.fileHandle.backupCreated = status; - jsw_rberase(gFileHandleTree, foundItem); + jsw_rberase(gOssFileHandleTree, foundItem); - jsw_rbinsert(gFileHandleTree, newItem); - free(newItem); - } + jsw_rbinsert(gOssFileHandleTree, newItem); + free(newItem); } - free(item); } + free(item); } pthread_mutex_unlock(&gOssFileHandleAccessMtx); } @@ -853,3 +998,26 @@ int get_ossfile_backup_status(int idx) } return rval; } + + +int remove_ossfile_handle_data(int idx) +{ + int rval = -1; + + if(pthread_mutex_lock(&gOssFileHandleAccessMtx) == 0) + { + if(gOssFileHandleTree != NULL) + { + FileHandleTreeItem_s* item = malloc(sizeof(FileHandleTreeItem_s)); // assign key and value to the rbtree item + if(item != NULL) + { + item->key = idx; + rval = jsw_rberase(gOssFileHandleTree, item); + } + } + + pthread_mutex_unlock(&gOssFileHandleAccessMtx); + } + + return rval; +} diff --git a/src/persistence_client_library_handle.h b/src/persistence_client_library_handle.h index a9085f0..881a8df 100644 --- a/src/persistence_client_library_handle.h +++ b/src/persistence_client_library_handle.h @@ -64,10 +64,17 @@ extern int gOpenHandleArray[MaxPersHandle]; //---------------------------------------------------------------- //---------------------------------------------------------------- + +/** + * @brief delete handle trees + */ +void deleteHandleTrees(void); + + /** * @brief get persistence handle * - * @return a new handle or 0 if an error occured or EPERS_MAXHANDLE if max no of handles is reached + * @return a new handle or 0 if an error occurred or EPERS_MAXHANDLE if max no of handles is reached */ int get_persistence_handle_idx(); @@ -146,6 +153,13 @@ int set_file_handle_data(int idx, PersistencePermission_e permission, const char /** + * @brief remove file handle from file tree + * + * @param idx the index + */ +int remove_file_handle_data(int idx); + +/** * @brief set data to the key handle * * @param idx the index @@ -336,5 +350,12 @@ void set_ossfile_backup_status(int idx, int status); int get_ossfile_backup_status(int idx); +/** + * @brief remove file handle from ass file tree + * + * @param idx the index + */ +int remove_ossfile_handle_data(int idx); + #endif /* PERSISTENCY_CLIENT_LIBRARY_HANDLE_H */ diff --git a/src/persistence_client_library_tree_helper.c b/src/persistence_client_library_tree_helper.c index 83bb895..393817e 100644 --- a/src/persistence_client_library_tree_helper.c +++ b/src/persistence_client_library_tree_helper.c @@ -19,7 +19,22 @@ #include "persistence_client_library_tree_helper.h" - +#if 0 +void debugFileItem(const char* prefix, FileHandleTreeItem_s* item) +{ + printf("-----------------------------------------\n"); + printf(" prefix : %s\n", prefix); + printf(" key : %d\n", item->key ); + printf(" backupCreated : %d\n", item->value.fileHandle.backupCreated ); + printf(" backupPath : %s\n", item->value.fileHandle.backupPath ); + printf(" csumPath : %s\n", item->value.fileHandle.csumPath ); + printf(" filePath : %s\n", item->value.fileHandle.filePath ); + printf(" cacheStatus : %d\n", item->value.fileHandle.cacheStatus ); + printf(" permission : %d\n", item->value.fileHandle.permission ); + printf(" userId : %d\n", item->value.fileHandle.userId ); + printf("-----------------------------------------\n"); +} +#endif /** * File handle helper functions */ diff --git a/src/persistence_client_library_tree_helper.h b/src/persistence_client_library_tree_helper.h index f42e635..1ae9c73 100644 --- a/src/persistence_client_library_tree_helper.h +++ b/src/persistence_client_library_tree_helper.h @@ -169,5 +169,14 @@ void* key_val_dup(void *p); void key_val_rel(void *p); +#if 0 +/** + * @brief Release function for key tree item + * + * @param prefix a string prefix to identify the item + * @param item the item to debug + */ +void debugFileItem(const char* prefix, FileHandleTreeItem_s* item); +#endif #endif /* PERSISTENCE_CLIENT_LIBRARY_TREE_HELPER_H */ |