/// environment variable for max key value data
const char *pDataSize = getenv("PERS_MAX_KEY_VAL_DATA_SIZE");
- /// blacklist path environment variable
- const char *pBlacklistPath = getenv("PERS_BLACKLIST_PATH");
+ char blacklistPath[DbPathMaxLen] = {0};
#if USE_FILECACHE
DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("Using the filecache!!!"));
gMaxKeyValDataSize = atoi(pDataSize);
}
- if(pBlacklistPath == NULL)
- {
- pBlacklistPath = "/etc/pclBackupBlacklist.txt"; // default path
- }
+ // Assemble backup blacklist path
+ sprintf(blacklistPath, "%s%s/%s", CACHEPREFIX, appName, gBackupFilename);
- if(readBlacklistConfigFile(pBlacklistPath) == -1)
+ if(readBlacklistConfigFile(blacklistPath) == -1)
{
- DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("pclInitLibrary -> failed to access blacklist:"), DLT_STRING(pBlacklistPath));
+ DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("pclInitLibrary -> failed to access blacklist:"), DLT_STRING(blacklistPath));
}
if(setup_dbus_mainloop() == -1)
static void createAndStoreFileNames()
{
- int i= 0, j =0;
- char path[128];
- const char* gFilePostFix = ".pers";
- const char* gKeyPathFormat = "/%s/%s/%s/%s/%s%s";
+ int i= 0;
+ char path[128] = {0};
key_value_s* item;
// creat new tree
if(gRb_tree_bl != NULL)
{
-
- for(i=0; i<128; i++)
- {
- // assemble path
- snprintf(path, 128, gKeyPathFormat, gpTokenArray[j+2], // storage type
- gpTokenArray[j+3], // policy id
- gpTokenArray[j+4], // profileID
- gpTokenArray[j], // application id
- gpTokenArray[j+1], // filename
- gFilePostFix); // file postfix
-
- // asign key and value to the rbtree item
- item = malloc(sizeof(key_value_s));
- if(item != NULL)
- {
- 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);
- }
- j+=5;
- if(gpTokenArray[j] == NULL)
- {
- break;
- }
+ while( i < TOKENARRAYSIZE )
+ {
+ if(gpTokenArray[i+1] != 0 )
+ {
+ memset(path, 0, sizeof(path));
+ snprintf(path, 128, "%s", gpTokenArray[i]); // storage type
+
+ // asign key and value to the rbtree item
+ item = malloc(sizeof(key_value_s));
+ 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;
+ }
}
}
if(filename != NULL)
{
-
memset(&buffer, 0, sizeof(buffer));
status = stat(filename, &buffer);
if(status != -1)
int need_backup_key(unsigned int key)
{
- int rval = 1;
+ int rval = CREATE_BACKUP;
key_value_s* item = NULL;
key_value_s* foundItem = NULL;
foundItem = (key_value_s*)jsw_rbfind(gRb_tree_bl, item);
if(foundItem != NULL)
{
- rval = 0;
+ rval = DONT_CREATE_BACKUP;
}
free(item);
}
if(item!=NULL)
free(item);
- rval = -1;
- DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("need_backup_key ==> item or gRb_tree_bl is NULL"));
+ rval = CREATE_BACKUP;
}
return rval;
#include <stdlib.h>
-// define PERS_ORG_ROOT_PATH comes form persistence common object
-
-/// cached path location
-#define CACHEPREFIX PERS_ORG_ROOT_PATH "/mnt-c/"
-/// write through path location
-#define WTPREFIX PERS_ORG_ROOT_PATH "/mnt-wt/"
/// path for the backup location
const char* gBackupPrefix = PERS_ORG_ROOT_PATH "/mnt-backup/";
// path prefix for local cached files: /Data/mnt_c/<appId>/<user>/<seat>/<resource>
const char* gLocalCacheFilePath = CACHEPREFIX "%s"PERS_ORG_USER_FOLDER_NAME_"%d"PERS_ORG_SEAT_FOLDER_NAME_"%d/%s";
+const char* gBackupFilename = "BackupFileList.info";
const char* gChangeSignal = "PersistenceResChange";
const char* gDeleteSignal = "PersistenceResDelete";
/// constant definitions
enum _PersistenceConstantDef
{
+ /// flag to identify if a backup should NOT be created
+ DONT_CREATE_BACKUP = 0,
+ /// flag to identify if a backup should be created
+ CREATE_BACKUP = 1,
/// flag to identify that resource a not file
ResIsNoFile = 0,
/// flag to identify that resource a file
defaultMaxKeyValDataSize = PERS_DB_MAX_SIZE_KEY_DATA
};
+
+// define PERS_ORG_ROOT_PATH comes form persistence common object
+
+/// cached path location
+#define CACHEPREFIX PERS_ORG_ROOT_PATH "/mnt-c/"
+/// write through path location
+#define WTPREFIX PERS_ORG_ROOT_PATH "/mnt-wt/"
+
/// path for the backup location
extern const char* gBackupPrefix;
/// backup filename postfix
/// path prefix for local cached files: /Data/mnt_c/<appId>/<user>/>userno>/<seat>/>seatno>/<resource>
extern const char* gLocalCacheFilePath;
+// backup blacklist filename
+extern const char* gBackupFilename;
+
/// application id
extern char gAppId[MaxAppNameLen];
int pclFileGetDefaultData(int handle, const char* resource_id, int policy);
+char* get_raw_string(char* dbKey)
+{
+ char* keyPtr = NULL;
+ int cnt = 0, i = 0;
+
+ for(i=0; i<DbKeyMaxLen; i++)
+ {
+ if(dbKey[i] == '/')
+ {
+ cnt++;
+ }
+
+ if(cnt >= 5) // stop after the 5th '/' has been found
+ {
+ break;
+ }
+ keyPtr = dbKey+i;
+ }
+ return ++keyPtr;
+}
+
+
int pclFileClose(int fd)
{
int rval = EPERS_NOT_INITIALIZED;
}
-
int pclFileOpen(unsigned int ldbid, const char* resource_id, unsigned int user_no, unsigned int seat_no)
{
int handle = EPERS_NOT_INITIALIZED;
if(gPclInitialized >= PCLinitialized)
{
int shared_DB = 0;
+ int wantBackup = 1;
PersistenceInfo_s dbContext;
- char dbKey[DbKeyMaxLen] = {0}; // database key
- char dbPath[DbPathMaxLen] = {0}; // database location
+ char dbKey[DbKeyMaxLen] = {0}; // database key
+ char dbPath[DbPathMaxLen] = {0}; // database location
char backupPath[DbPathMaxLen] = {0}; // backup file
- char csumPath[DbPathMaxLen] = {0}; // checksum file
+ char csumPath[DbPathMaxLen] = {0}; // checksum file
//DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclFileOpen: "), DLT_INT(ldbid), DLT_STRING(resource_id) );
// file will be opened writable, so check about data consistency
if( (dbContext.configKey.permission != PersistencePermission_ReadOnly)
- && pclBackupNeeded(dbPath) )
+ && (pclBackupNeeded(get_raw_string(dbKey)) == CREATE_BACKUP))
{
+ wantBackup = 0;
if((handle = pclVerifyConsistency(dbPath, backupPath, csumPath, flags)) == -1)
{
DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pclFileOpen: error => file inconsistent, recovery N O T possible!"));
}
else
{
- DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("pclFileOpen: No Backup => file is read only OR is in blacklist!"));
+ if(dbContext.configKey.permission == PersistencePermission_ReadOnly)
+ DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("pclFileOpen: No Backup => file is READ ONLY!"), DLT_STRING(dbKey));
+ else
+ DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("pclFileOpen: No Backup => file is in backup blacklist!"), DLT_STRING(dbKey));
+
}
#if USE_FILECACHE
if(dbContext.configKey.permission != PersistencePermission_ReadOnly)
{
- if(set_file_handle_data(handle, dbContext.configKey.permission, 0 /*backupCreated*/, backupPath, csumPath, NULL) != -1)
+ if(set_file_handle_data(handle, dbContext.configKey.permission, backupPath, csumPath, NULL) != -1)
{
+ set_file_backup_status(handle, wantBackup);
__sync_fetch_and_add(&gOpenFdArray[handle], FileOpen); // set open flag
}
else
if(handle != -1)
{
-
- if(set_file_handle_data(handle, PersistencePermission_ReadWrite, 0 /*backupCreated*/, backupPath, csumPath, NULL) != -1)
+ if(set_file_handle_data(handle, PersistencePermission_ReadWrite, backupPath, csumPath, NULL) != -1)
{
+ set_file_backup_status(handle, 1);
__sync_fetch_and_add(&gOpenFdArray[handle], FileOpen); // set open flag
}
else
}
else
{
+ DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("pclFileWriteData: Failed to write ==> read only file!"), DLT_STRING(get_file_backup_path(fd)));
size = EPERS_RESOURCE_READ_ONLY;
}
}
// file will be opened writable, so check about data consistency
if( dbContext.configKey.permission != PersistencePermission_ReadOnly
- && pclBackupNeeded(dbPath) )
+ && pclBackupNeeded( get_raw_string(dbPath)) == CREATE_BACKUP)
{
snprintf(backupPath, DbPathMaxLen-1, "%s%s", dbPath, gBackupPostfix);
snprintf(csumPath, DbPathMaxLen-1, "%s%s", dbPath, gBackupCsPostfix);
if((handle = pclVerifyConsistency(dbPath, backupPath, csumPath, flags)) == -1)
{
- DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pclFileOpen: error => file inconsistent, recovery N O T possible!"));
+ DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pclFileCreatePath: error => file inconsistent, recovery N O T possible!"));
return -1;
}
// we don't need the file handle here
if(handle > 0)
close(handle);
}
+ else
+ {
+ DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("pclFileCreatePath: No Backup => read only OR in blacklist!"), DLT_STRING(dbKey));
+ }
handle = get_persistence_handle_idx();
}
return rval;
-}// getDefault
-
+}
}
-int set_file_handle_data(int idx, PersistencePermission_e permission, int backupCreated,
- const char* backup, const char* csumPath, char* filePath)
+int set_file_handle_data(int idx, PersistencePermission_e permission, const char* backup, const char* csumPath, char* filePath)
{
int rval = 0;
{
strcpy(gFileHandleArray[idx].backupPath, backup);
strcpy(gFileHandleArray[idx].csumPath, csumPath);
- gFileHandleArray[idx].backupCreated = backupCreated;
+ gFileHandleArray[idx].backupCreated = 0; // set to 0 by default
gFileHandleArray[idx].permission = permission;
gFileHandleArray[idx].filePath = filePath; // check to do if this works
}
* @param filePath the path to the file
*
*/
-int set_file_handle_data(int idx, PersistencePermission_e permission, int backupCreated,
- const char* backup, const char* csumPath, char* filePath);
+int set_file_handle_data(int idx, PersistencePermission_e permission, const char* backup, const char* csumPath, char* filePath);
/**
} PersistenceRCT_e;
-
PersistenceRCT_e get_table_id(int ldbid, int* groupId)
{
PersistenceRCT_e rctType = PersistenceRCT_LastEntry;
gResource_table[i] = -1;
}
-// status: OK
+
int get_resource_cfg_table(PersistenceRCT_e rct, int group)
{
int arrayIdx = 0;
-// status: OK
int get_db_path_and_key(PersistenceInfo_s* dbContext, const char* resource_id, char dbKey[], char dbPath[])
{
int storePolicy = PersistenceStorage_LastEntry;
char* gRecovChecksum = "608a3b5d"; // generated with http://www.tools4noobs.com/online_php_functions/crc32/
+
void data_teardown(void)
{
printf("* * * tear down * * *\n"); // nothing
+// creat blacklist file, if this does not exist
+void data_setupBlacklist(void)
+{
+
+/// backup info
+char gBackupInfo[] = {
+"/media/doNotBackupMe.txt_START\n\
+/media/doNotBackupMe_01.txt\n\
+/media/doNotBackupMe_02.txt\n\
+/media/doNotBackupMe_03.txt\n\
+/media/doNotBackupMe_04.txt\n\
+/media/iDontWantDoBeBackuped_01.txt\n\
+/media/iDontWantDoBeBackuped_02.txt\n\
+/media/iDontWantDoBeBackuped_03.txt\n\
+/media/iDontWantDoBeBackuped_04.txt\n\
+/media/iDontWantDoBeBackuped_05.txt_END\n"
+};
+
+ const char* backupBlacklist = "/Data/mnt-c/lt-persistence_client_library_test/BackupFileList.info";
+
+ if(access(backupBlacklist, F_OK) == -1)
+ {
+ int handle = open(backupBlacklist, O_CREAT|O_RDWR|O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
+
+ write(handle, gBackupInfo, strlen(gBackupInfo));
+ close(handle);
+ }
+
+ printf("Finished: ==> data_setupBlacklist\n");
+}
+
+
/*
* Test the file interface:
* - open file
X_TEST_REPORT_TYPE(GOOD);
int fd = 0, i = 0, idx = 0;
- int size = 0, ret = 0;
+ int size = 0, ret = 0, avail = 100;
int writeSize = 16*1024;
+ int fdArray[10] = {0};
unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
unsigned char buffer[READ_SIZE] = {0};
+ unsigned char wBuffer[READ_SIZE] = {0};
const char* refBuffer = "/Data/mnt-wt/lt-persistence_client_library_test/user/1/seat/1/media";
char* writeBuffer;
char* fileMap = NULL;
+ printf("test_DataFile ==> S T A R T\n");
ret = pclInitLibrary(gTheAppId, shutdownReg);
x_fail_unless(ret <= 1, "Failed to init PCL");
#if 1
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);
+ fdArray[1] = pclFileOpen(0xFF, "media/doNotBackupMe.txt_START", 1, 2);
+ fdArray[2] = pclFileOpen(0xFF, "media/doNotBackupMe.txt_START", 20, 10);
+ fdArray[3] = pclFileOpen(0xFF, "media/doNotBackupMe.txt_START", 200, 100);
+
+ fdArray[4] = pclFileOpen(0xFF, "media/doNotBackupMe_01.txt", 2, 1);
+ fdArray[5] = pclFileOpen(0xFF, "media/doNotBackupMe_02.txt", 2, 1);
+ fdArray[6] = pclFileOpen(0xFF, "media/doNotBackupMe_03.txt", 2, 1);
+ fdArray[7] = pclFileOpen(0xFF, "media/doNotBackupMe_04.txt", 2, 1);
+
+ fdArray[8] = pclFileOpen(0xFF, "media/iDontWantDoBeBackuped_04.txt", 2, 1);
+ fdArray[9] = pclFileOpen(0xFF, "media/iDontWantDoBeBackuped_05.txt_END", 2, 1);
+
+ for(i=0; i<10; i++)
+ {
+ snprintf( (char*)wBuffer, 1024, "Test - %d", i);
+ pclFileWriteData(fdArray[i], wBuffer, strlen( (char*)wBuffer));
+ }
+
+ //
+ // test if backup blacklist works correctly
+ //
+ avail = access("/Data/mnt-backup/lt-persistence_client_library_test/user/1/seat/1/media/doNotBackupMe.txt_START~", F_OK);
+ x_fail_unless(avail == -1, "1. Failed backup => backup available, but should not");
+
+
+ avail = access("/Data/mnt-backup/lt-persistence_client_library_test/user/1/seat/2/media/doNotBackupMe.txt_START~", F_OK);
+ x_fail_unless(avail == -1, "2. Failed backup => backup available, but should not");
+
+ avail = access("/Data/mnt-backup/lt-persistence_client_library_test/user/20/seat/10/media/doNotBackupMe.txt_START~", F_OK);
+ x_fail_unless(avail == -1, "3. Failed backup => backup available, but should not");
+
+ avail = access("/Data/mnt-backup/lt-persistence_client_library_test/user/200/seat/100/media/doNotBackupMe.txt_START~", F_OK);
+ x_fail_unless(avail == -1, "4. Failed backup => backup available, but should not");
+
+ avail = access("/Data/mnt-backup/lt-persistence_client_library_test/user/2/seat/1/media/doNotBackupMe_01.txt~", F_OK);
+ x_fail_unless(avail == -1, "5. Failed backup => backup available, but should not");
+
+ avail = access("/Data/mnt-backup/lt-persistence_client_library_test/user/2/seat/1/media/doNotBackupMe_02.txt~", F_OK);
+ x_fail_unless(avail == -1, "6. Failed backup => backup available, but should not");
+
+ avail = access("/Data/mnt-backup/lt-persistence_client_library_test/user/2/seat/1/media/doNotBackupMe_03.txt~", F_OK);
+ x_fail_unless(avail == -1, "7. Failed backup => backup available, but should not");
+
+ avail = access("/Data/mnt-backup/lt-persistence_client_library_test/user/2/seat/1/media/doNotBackupMe_04.txt~", F_OK);
+ x_fail_unless(avail == -1, "8. Failed backup => backup available, but should not");
+
+ avail = access("/Data/mnt-backup/lt-persistence_client_library_test/user/2/seat/1/media/iDontWantDoBeBackuped_04.txt~", F_OK);
+ x_fail_unless(avail == -1, "9. Failed backup => backup available, but should not");
+
+ avail = access("/Data/mnt-backup/lt-persistence_client_library_test/user/2/seat/1/media/iDontWantDoBeBackuped_05.txt_END~", F_OK);
+ x_fail_unless(avail == -1, "10. Failed backup => backup available, but should not");
+
+ for(i=0; i<10; i++)
+ {
+ pclFileClose(fdArray[i]);
+ }
+
free(writeBuffer);
#endif
pclDeinitLibrary();
// verify the backup creation:
handle = open(path, O_RDWR);
+ printf("Path: %s | handle: %d\n", path, handle);
x_fail_unless(handle != -1, "Could not open file ==> failed to access backup file");
rval = read(handle, rBuffer, 1024);
// test multiple handles
handleArray[0] = pclFileOpen(0xFF, "media/mediaDB_write_01.db", 1, 1);
- printf("** **** **** **** ** handleArray[0] => %d\n", handleArray[0]);
x_fail_unless(handle1 != -1, "Could not open file ==> /media/mediaDB_write_01.db");
handleArray[1] = pclFileOpen(0xFF, "media/mediaDB_write_02.db", 1, 1);
suite_add_tcase(s, tc_ReadConfDefault);
suite_add_tcase(s, tc_persDataFile);
+ tcase_add_checked_fixture(tc_persDataFile, data_setupBlacklist, data_teardown);
+
suite_add_tcase(s, tc_persDataFileBackupCreation);
tcase_add_checked_fixture(tc_persDataFileBackupCreation, data_setupBackup, data_teardown);
+
suite_add_tcase(s, tc_persDataFileRecovery);
tcase_add_checked_fixture(tc_persDataFileRecovery, data_setupRecovery, data_teardown);