added creation date and a simple hash on the file name for to improve the uniqueness...
[profile/ivi/dlt-daemon.git] / src / lib / dlt_filetransfer.c
index 359ad2c..ce18f52 100644 (file)
@@ -77,6 +77,30 @@ unsigned long getFilesize(const char* file){
        return (unsigned long)st.st_size;
 }
 
+/** A simple Hash function for C-strings
+ * @param str input string. E.g. a file path.
+ * @param hash start and result value for hash computation
+ *
+ */
+void stringHash(const char* str, unsigned long *hash )
+{
+    if (!str || !hash)
+        return;
+   unsigned int len = strlen(str);
+
+   unsigned int i = 0;
+   if (len <= 0){
+    return;
+   }
+
+   for(i = 0; i < len;  i++)
+   {
+      *hash = 53 * *hash  + str[i];
+   }
+
+}
+
+
 //!Get some information about the file serial number of a file
 /** See stat(2) for more informations.
  * @param file Absolute file path
@@ -84,8 +108,14 @@ unsigned long getFilesize(const char* file){
  */
 unsigned long getFileSerialNumber(const char* file){
        struct stat st;
+       unsigned long ret;
        stat(file, &st);
-       return (unsigned long)st.st_ino;
+       ret = st.st_ino;
+       ret = ret << (sizeof(ret)*8)/2;
+       ret |= st.st_size;
+    ret ^= st.st_ctime;
+    stringHash(file, &ret);
+       return ret;
 }
 
 //!Returns the creation date of a file
@@ -297,6 +327,40 @@ int dlt_user_log_file_packagesCount(DltContext *fileContext, const char *filenam
  * See the Mainpages.c for more informations.
  * @param fileContext Specific context to log the file to dlt
  * @param filename Absolute file path
+ * @param alias Alias for the file. An alternative name to show in the receiving end
+ * @return Returns 0 if everything was okey. If there was a failure a value < 0 will be returned.
+ */
+int dlt_user_log_file_header_alias(DltContext *fileContext,const char *filename, const char *alias){
+
+       if(isFile(filename))
+       {
+               DLT_LOG(*fileContext,DLT_LOG_INFO,
+                                       DLT_STRING("FLST"),
+                                       DLT_UINT(getFileSerialNumber(filename)),
+                                       DLT_STRING(alias),
+                                       DLT_UINT(getFilesize(filename)),
+                                       DLT_STRING(getFileCreationDate2(filename));
+                                       DLT_UINT(dlt_user_log_file_packagesCount(fileContext,filename)),
+                                       DLT_UINT(BUFFER_SIZE),
+                                       DLT_STRING("FLST")
+                               );
+
+               return 0;
+       }
+       else
+       {
+               dlt_user_log_file_errorMessage(fileContext,filename, DLT_FILETRANSFER_ERROR_FILE_HEAD);
+               return DLT_FILETRANSFER_ERROR_FILE_HEAD;
+       }
+}
+
+//!Transfer the head of the file as a dlt logs.
+/**The head of the file must be logged to dlt because the head contains inforamtion about the file serial number,
+ * the file name, the file size, package number the file have and the buffer size.
+ * All these informations are needed from the plugin of the dlt viewer.
+ * See the Mainpages.c for more informations.
+ * @param fileContext Specific context to log the file to dlt
+ * @param filename Absolute file path
  * @return Returns 0 if everything was okey. If there was a failure a value < 0 will be returned.
  */
 int dlt_user_log_file_header(DltContext *fileContext,const char *filename){
@@ -357,6 +421,7 @@ int dlt_user_log_file_data(DltContext *fileContext,const char *filename, int pac
                                DLT_STRING("for File:"),
                                DLT_STRING(filename)
                        );
+                       fclose(file);
                        return DLT_FILETRANSFER_ERROR_FILE_DATA;
                }