Fixed PCL bugs: 137, 138, 141
authorIngo Huerner <ingo.huerner@xse.de>
Mon, 9 Dec 2013 08:27:45 +0000 (09:27 +0100)
committerIngo Huerner <ingo.huerner@xse.de>
Mon, 9 Dec 2013 08:27:45 +0000 (09:27 +0100)
include/persistence_client_library_error_def.h
include/persistence_client_library_file.h
include/persistence_client_library_key.h
src/persistence_client_library_file.c
src/persistence_client_library_key.c
src/persistence_client_library_prct_access.c
test/data/Data.tar.gz
test/persistence_client_library_test.c

index 8a4a63f..04bbf0f 100644 (file)
@@ -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
 }
index c357714..2cdbccd 100644 (file)
@@ -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);
index fa9fa16..52e4aa6 100644 (file)
@@ -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);
 
index b065df9..dada09e 100644 (file)
@@ -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;
+}
+
 
index acd3852..94f03d5 100644 (file)
@@ -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
index d8f169b..b27c125 100644 (file)
@@ -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)
       {
index 740b4da..37a9f76 100644 (file)
Binary files a/test/data/Data.tar.gz and b/test/data/Data.tar.gz differ
index 789c9cc..412042e 100644 (file)
@@ -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