Changed the deprecated thumbnail_util APIs in msg-service
[platform/core/messaging/msg-service.git] / utils / MsgUtilFile.cpp
index f1b7901..9b00350 100755 (executable)
@@ -1,17 +1,17 @@
 /*
-* Copyright 2012-2013  Samsung Electronics Co., Ltd
-*
-* Licensed under the Flora License, Version 1.1 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-*    http://floralicense.org/license/
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
 */
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <sys/stat.h>
+#include <sys/smack.h>
 #include <string.h>
 #include <dirent.h>
-#include <unistd.h>    //sync()
+#include <unistd.h>
+#include <fcntl.h>
+#include <libgen.h>
+#include <dlfcn.h>
+
+#include <thumbnail_util.h>
+#include <image_util.h>
 
 #include "MsgStorageTypes.h"
 #include "MsgDebug.h"
 #include "MsgMmsTypes.h"
 #include "MsgInternalTypes.h"
 #include "MsgDrmWrapper.h"
+#include "MsgMutex.h"
+
+extern "C" {
+       #include <aul.h>
+}
+
+#define PATH_LIBCSR_CLIENT "/usr/lib/libcsr-client.so.2"
+#define THUMB_WIDTH 320
+#define THUMB_HEIGHT 240
 
+MsgMutex g_mx;
+MsgCndVar g_cv;
 
 /*==================================================================================================
                                      FUNCTION IMPLEMENTATION
 ==================================================================================================*/
-// File operation wrappers
+bool MakeThumbnail(char *srcPath, char *dstPath)
+{
+       if (srcPath == NULL || dstPath == NULL) {
+               MSG_SEC_DEBUG("Invalid Param src = %p, dst = %p", srcPath, dstPath);
+               return false;
+       }
+
+       if (MsgAccessFile(srcPath, R_OK) == false) {
+               MSG_SEC_DEBUG("not exist source file [%s]", srcPath);
+               return false;
+       }
+
+       g_mx.lock();
+
+       int time_ret = 0;
+
+       int ret = THUMBNAIL_UTIL_ERROR_NONE;
+       char *req_id = NULL;
+
+       ret = thumbnail_util_extract_to_file(srcPath, THUMB_WIDTH, THUMB_HEIGHT, dstPath);
+
+       if (req_id) {
+               g_free(req_id);
+               req_id = NULL;
+       }
+
+       if (ret != THUMBNAIL_UTIL_ERROR_NONE) {
+               MSG_ERR("thumbnail_util_extract_to_file is failed");
+               g_mx.unlock();
+               return false;
+       }
+
+       time_ret = g_cv.timedwait(g_mx.pMsgMutex(), 5);
+
+       g_mx.unlock();
+
+       if (time_ret == ETIMEDOUT) {
+               MSG_ERR("@@ WAKE by timeout@@");
+               return false;
+       }
+
+       if (MsgAccessFile(dstPath, F_OK) == false) {
+               MSG_SEC_DEBUG("not exist result file [%s]", dstPath);
+               return false;
+       }
+
+       MSG_SEC_DEBUG("Make thumbnail: success [%s]", dstPath);
+       return true;
+}
+
+/* File operation wrappers */
 FILE *MsgOpenFile(const char *filepath, const char *opt)
 {
        if (!filepath || !opt) {
@@ -44,7 +112,7 @@ FILE *MsgOpenFile(const char *filepath, const char *opt)
                return NULL;
        }
 
-       MSG_DEBUG("[FILE] filepath : [%s], opt [%s]", filepath, opt);
+       MSG_SEC_DEBUG("[FILE] filepath : [%s], opt [%s]", filepath, opt);
 
        FILE *pFile = NULL;
 
@@ -84,10 +152,10 @@ int MsgFseek(FILE *pFile, long int offset, int origin)
 
        int ret = -1;
 
-       MSG_DEBUG("[FILE] pFile [%p], offset [%d], origin [%d]", pFile, offset, origin);
+       MSG_DEBUG("[FILE] pFile [%p], offset [%ld], origin [%d]", pFile, offset, origin);
 
        try {
-               ret = fseek(pFile, offset, origin);             // return 0, if success.
+               ret = fseek(pFile, offset, origin); /* return 0, if success. */
        } catch (exception &e) {
                MSG_FATAL("%s", e.what());
                return -1;
@@ -105,7 +173,7 @@ size_t MsgWriteFile(const char *pData, size_t size, size_t count, FILE *pFile)
 
        size_t nWrite = 0;
 
-       MSG_DEBUG("[FILE] pData [%p], size [%d], count [%d], pFile [%p]", pData, size, count, pFile);
+       MSG_DEBUG("[FILE] pData [%p], size [%zu], count [%zu], pFile [%p]", pData, size, count, pFile);
 
        try {
                nWrite = fwrite(pData, size, count, pFile);
@@ -141,7 +209,7 @@ long int MsgFtell(FILE *pFile)
                return -1L;
        }
 
-       long int ret = -1L; // -1L return if error occured.
+       long int ret = -1L; /* -1L return if error occured. */
 
        try {
                ret = ftell(pFile);
@@ -162,7 +230,7 @@ int MsgFflush(FILE *pFile)
        int ret = -1;
 
        try {
-               ret = fflush(pFile);            // return 0 if success
+               ret = fflush(pFile); /* return 0 if success */
        } catch (exception &e) {
                MSG_FATAL("%s", e.what());
        }
@@ -180,7 +248,7 @@ int MsgFsync(FILE *pFile)
        int ret = -1;
 
        try {
-               ret = fdatasync(pFile->_fileno);        // return 0 if success
+               ret = fdatasync(pFile->_fileno); /* return 0 if success */
        } catch (exception &e) {
                MSG_FATAL("%s", e.what());
        }
@@ -199,16 +267,16 @@ bool MsgCreateFileName(char *pFileName)
 
        try {
                if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
-                       MSG_DEBUG("clock_gettime() error: %s", strerror(errno));
+                       MSG_DEBUG("clock_gettime() error: %s", g_strerror(errno));
                        return false;
                }
 
-               // Create Random Number
+               /* Create Random Number */
                srandom((unsigned int)ts.tv_nsec);
 
-               MSG_DEBUG("ts.tv_nsec : %d", ts.tv_nsec);
+               MSG_DEBUG("ts.tv_nsec : %ld", ts.tv_nsec);
 
-               // between 1 - 1000000000
+               /* between 1 - 1000000000 */
                snprintf(pFileName, MSG_FILENAME_LEN_MAX, "MSG_%lu.DATA", random()%1000000000+1);
        } catch (exception& e) {
                MSG_FATAL("%s", e.what());
@@ -221,26 +289,40 @@ bool MsgCreateFileName(char *pFileName)
 
 bool MsgOpenAndReadFile(const char *pFileName, char **ppData, int *pDataSize)
 {
+       if (!pFileName || !ppData || !pDataSize) {
+               MSG_ERR("Invalid params");
+               return false;
+       }
+
        MSG_DEBUG("MsgOpenAndReadFile");
 
        FILE *pFile = NULL;
 
        char fullPath[MAX_FULL_PATH_SIZE] = {0};
 
-       snprintf(fullPath, MAX_FULL_PATH_SIZE, MSG_IPC_DATA_PATH"%s", pFileName);
-       MSG_DEBUG("open file name: %s", fullPath);
+       snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
+       MSG_SEC_DEBUG("open file name: %s", fullPath);
 
+       struct stat st;
+       if (stat(fullPath, &st) != 0) {
+               MSG_SEC_ERR("stat(%s, &st) != 0", fullPath);
+               return false;
+       }
+       if (S_ISDIR(st.st_mode)) {
+               MSG_ERR("S_ISDIR(st.st_mode)");
+               return false;
+       }
 
        pFile = MsgOpenFile(fullPath, "rb");
 
        if (pFile == NULL) {
-               MSG_DEBUG("File Open Error: %s", strerror(errno));
+               MSG_DEBUG("File Open Error: %s", g_strerror(errno));
                return false;
        }
 
        if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
                MsgCloseFile(pFile);
-               MSG_DEBUG("File Read Error: %s", strerror(errno));
+               MSG_DEBUG("File Read Error: %s", g_strerror(errno));
                return false;
        }
 
@@ -258,13 +340,13 @@ bool MsgOpenAndReadFile(const char *pFileName, char **ppData, int *pDataSize)
 
        if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
                MsgCloseFile(pFile);
-               MSG_DEBUG("File seek Error: %s", strerror(errno));
+               MSG_DEBUG("File seek Error: %s", g_strerror(errno));
                return false;
        }
 
        if (MsgReadFile(*ppData, sizeof(char), FileSize, pFile) != (size_t)FileSize) {
                MsgCloseFile(pFile);
-               MSG_DEBUG("File Read Error: %s", strerror(errno));
+               MSG_DEBUG("File Read Error: %s", g_strerror(errno));
                return false;
        }
 
@@ -281,7 +363,7 @@ bool MsgReadFileForDecode(FILE *pFile, char *pBuf, int length, int *nSize)
        MSG_BEGIN();
 
        if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
-               MSG_DEBUG("File Seek Error: %s", strerror(errno));
+               MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
                MsgCloseFile(pFile);
                return false;
        }
@@ -302,38 +384,30 @@ bool MsgWriteIpcFile(const char *pFileName, const char *pData, int DataSize)
 
        char fullPath[MAX_FULL_PATH_SIZE] = {0};
 
-       snprintf(fullPath, MAX_FULL_PATH_SIZE, MSG_IPC_DATA_PATH"%s", pFileName);
+       snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
 
        FILE *pFile = MsgOpenFile(fullPath, "wb+");
 
        if (pFile == NULL) {
-               MSG_DEBUG("File Open Error: %s", strerror(errno));
+               MSG_DEBUG("File Open Error: %s", g_strerror(errno));
                return false;
        }
 
        if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
                MsgCloseFile(pFile);
-               MSG_DEBUG("File Seek Error: %s", strerror(errno));
+               MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
                return false;
        }
 
        if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
                MsgCloseFile(pFile);
-               MSG_DEBUG("File Write Error: %s", strerror(errno));
+               MSG_DEBUG("File Write Error: %s", g_strerror(errno));
                return false;
        }
 
        MsgFflush(pFile);
        MsgCloseFile(pFile);
 
-       if (chmod(fullPath, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP ) != 0) {
-               MSG_DEBUG("File chmod Error: %s", strerror(errno));
-       }
-
-       if (chown(fullPath, 0, 6502 ) != 0) {
-               MSG_DEBUG("File chown Error: %s", strerror(errno));
-       }
-
        return true;
 }
 
@@ -347,20 +421,20 @@ int MsgReadSmilFile(const char *pFileName, char **ppData)
        int     nSize = 0;
        char fullPath[MAX_FULL_PATH_SIZE] = {0};
 
-       snprintf(fullPath, MAX_FULL_PATH_SIZE, MSG_SMIL_FILE_PATH"%s", pFileName);
+       snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_SMIL_FILE_PATH, pFileName);
 
-       MSG_DEBUG("open file name: %s", fullPath);
+       MSG_SEC_DEBUG("open file name: %s", fullPath);
 
        FILE *pFile = MsgOpenFile(fullPath, "rb");
 
        if (pFile == NULL) {
-               MSG_DEBUG("File Open Error: %s", strerror(errno));
+               MSG_DEBUG("File Open Error: %s", g_strerror(errno));
                return -1;
        }
 
        if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
                MsgCloseFile(pFile);
-               MSG_DEBUG("File Seek Error: %s", strerror(errno));
+               MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
                return -1;
        }
 
@@ -373,60 +447,62 @@ int MsgReadSmilFile(const char *pFileName, char **ppData)
        }
 
        *ppData = new char[FileSize + 1];
+       memset(*ppData, 0x00, (FileSize+1));
 
        if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
                MsgCloseFile(pFile);
-               MSG_DEBUG("File Sead Error: %s", strerror(errno));
+               MSG_DEBUG("File Sead Error: %s", g_strerror(errno));
                return -1;
        }
 
        if (MsgReadFile(*ppData, sizeof(char), FileSize, pFile) != (size_t)FileSize) {
                MsgCloseFile(pFile);
-               MSG_DEBUG("File Read Error: %s", strerror(errno));
+               MSG_DEBUG("File Read Error: %s", g_strerror(errno));
                return -1;
        }
 
-       ppData[FileSize] = '\0';
+       /* ppData[FileSize] = '\0'; */
 
        nSize = FileSize;
-
        MsgCloseFile(pFile);
 
        return nSize;
 }
 
 
-bool MsgWriteSmilFile(const char *pFilePath,char *pData, int DataSize)
+bool MsgWriteSmilFile(const char *pFilePath, char *pData, int DataSize)
 {
        if(!pFilePath) {
                MSG_DEBUG("pFilePath is NULL");
                return false;
        }
 
+#if 0
        if (mkdir(MSG_SMIL_FILE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
                if (errno == EEXIST) {
-                       MSG_DEBUG("The %s already exists", MSG_SMIL_FILE_PATH);
+                       MSG_SEC_DEBUG("The %s already exists", MSG_SMIL_FILE_PATH);
                } else {
-                       MSG_DEBUG("Error while mkdir %s", MSG_SMIL_FILE_PATH);
+                       MSG_SEC_DEBUG("Error while mkdir %s", MSG_SMIL_FILE_PATH);
                }
        }
+#endif
 
        FILE *pFile = MsgOpenFile(pFilePath, "wb+");
 
        if (pFile == NULL) {
-               MSG_DEBUG("File Open Error: %s", strerror(errno));
+               MSG_DEBUG("File Open Error: %s", g_strerror(errno));
                return false;
        }
 
        if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
                MsgCloseFile(pFile);
-               MSG_DEBUG("File Seek Error: %s", strerror(errno));
+               MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
                return false;
        }
 
        if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
                MsgCloseFile(pFile);
-               MSG_DEBUG("File Write Error: %s", strerror(errno));
+               MSG_DEBUG("File Write Error: %s", g_strerror(errno));
                return false;
        }
 
@@ -452,22 +528,21 @@ void MsgDeleteFile(const char *pFileName)
        char fullPath[MAX_FULL_PATH_SIZE] = {0};
 
        try {
-               snprintf(fullPath, MAX_FULL_PATH_SIZE, MSG_IPC_DATA_PATH"%s", pFileName);
+               snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
 
-               MSG_DEBUG("%s", fullPath);
+               MSG_SEC_DEBUG("%s", fullPath);
 
                if (remove(fullPath) != 0)
-                       MSG_FATAL("File Delete Error [%s]: %s", fullPath, strerror(errno));
+                       MSG_SEC_ERR("File Delete Error [%s]: %s", fullPath, g_strerror(errno));
        } catch (exception &e) {
-               MSG_FATAL ("%s", e.what());
+               MSG_FATAL("%s", e.what());
        }
-
 }
 
 
 void MsgDeleteSmilFile(const char *pFileName)
 {
-       if (!pFileName ) {
+       if (!pFileName) {
                MSG_FATAL("pFileName NULL");
                return;
        }
@@ -475,10 +550,10 @@ void MsgDeleteSmilFile(const char *pFileName)
        try {
                char fullPath[MAX_FULL_PATH_SIZE] = {0};
 
-               snprintf(fullPath, MAX_FULL_PATH_SIZE, MSG_SMIL_FILE_PATH"%s", pFileName);
+               snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_SMIL_FILE_PATH, pFileName);
 
                if (remove(fullPath) != 0)
-                       MSG_FATAL("File Delete Error [%s]: %s", fullPath, strerror(errno));
+                       MSG_SEC_ERR("File Delete Error [%s]: %s", fullPath, g_strerror(errno));
        } catch (exception &e) {
                MSG_FATAL("%s", e.what());
        }
@@ -497,13 +572,13 @@ bool MsgGetFileSize(const char *pFilePath, int *nSize)
        pFile = MsgOpenFile(pFilePath, "rb");
 
        if (!pFile) {
-               MSG_DEBUG("File Open error: %s", strerror(errno));
+               MSG_DEBUG("File Open error: %s", g_strerror(errno));
                return false;
        }
 
        if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
                MsgCloseFile(pFile);
-               MSG_FATAL("File Read Error: %s", strerror(errno));
+               MSG_FATAL("File Read Error: %s", g_strerror(errno));
                return false;
        }
 
@@ -520,7 +595,7 @@ FILE *MsgOpenMMSFile(char *pFileName)
        int len;
 
        if (!pFileName) {
-               MSG_DEBUG("pFileName NULL: %s", strerror(errno));
+               MSG_DEBUG("pFileName NULL: %s", g_strerror(errno));
                return NULL;
        }
 
@@ -534,7 +609,7 @@ FILE *MsgOpenMMSFile(char *pFileName)
                }
        }
 
-       MSG_DEBUG("pFileName = %s", pFileName);
+       MSG_SEC_DEBUG("pFileName = %s", pFileName);
 
        char fullPath[MAX_FULL_PATH_SIZE+1] = {0};
 
@@ -543,13 +618,13 @@ FILE *MsgOpenMMSFile(char *pFileName)
        FILE *pFile = MsgOpenFile(fullPath, "wb+");
 
        if (pFile == NULL) {
-               MSG_FATAL("File Open Error: %s", strerror(errno));
+               MSG_ERR("File Open Error: %s", g_strerror(errno));
                return NULL;
        }
 
        if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
                MsgCloseFile(pFile);
-               MSG_DEBUG("File Read Error: %s", strerror(errno));
+               MSG_ERR("File Read Error: %s", g_strerror(errno));
                return NULL;
        }
 
@@ -565,9 +640,9 @@ bool MsgWriteDataFromEncodeBuffer(FILE *pFile, char *pInBuffer, int *pPtr, int m
        }
 
        MSG_DEBUG("MsgWriteDataFromEncodeBuffer:");
-       MSG_DEBUG("pInBuffer %x", pInBuffer);
+       MSG_DEBUG("pInBuffer %s", pInBuffer);
        MSG_DEBUG("pPtr %d",  (*pPtr));
-       MSG_DEBUG("before to fwite %x", pFile);
+       MSG_DEBUG("before to fwite %p", pFile);
 
        if (MsgWriteFile(pInBuffer, sizeof(char), (*pPtr), pFile) != (size_t)(*pPtr)) {
                MSG_FATAL("MsgWriteFile failed");
@@ -578,7 +653,7 @@ bool MsgWriteDataFromEncodeBuffer(FILE *pFile, char *pInBuffer, int *pPtr, int m
 
        MsgFflush(pFile);
 
-       memset( pInBuffer, 0, maxLen );
+       memset(pInBuffer, 0, maxLen);
 
        *pPtr = 0;
 
@@ -601,9 +676,6 @@ bool MsgWriteDataFromEncodeBuffer(FILE *pFile, char *pInBuffer, int *pPtr, int m
 bool MsgOpenCreateAndOverwriteFile(char *pFullPath, char *pBuff, int TotalLength)
 {
        FILE *pFile = NULL ;
-       mode_t file_mode;
-
-       file_mode = (S_IRUSR | S_IWUSR);
 
        if ((pFile = MsgOpenFile(pFullPath, "wb+")) == NULL) {
                MSG_FATAL("MsgOpenFile errer");
@@ -611,42 +683,39 @@ bool MsgOpenCreateAndOverwriteFile(char *pFullPath, char *pBuff, int TotalLength
        }
 
        if (MsgWriteFile(pBuff, sizeof(char), TotalLength, pFile) != (size_t)TotalLength) {
-               MsgCloseFile( pFile );
+               MsgCloseFile(pFile);
                return false;
        }
 
-       MsgFsync(pFile);        //file is written to device immediately, it prevents missing file data from unexpected power off
+       MsgFsync(pFile);        /* file is written to device immediately, it prevents missing file data from unexpected power off */
        MsgFflush(pFile);
        MsgCloseFile(pFile);
 
-       if (chmod(pFullPath, file_mode) < 0)
-               MSG_FATAL("File chmod Error: %s", strerror(errno));
-
        return true;
 }
 
 
-char *MsgOpenAndReadMmsFile( const char *szFilePath, int offset, int size, int *npSize )
+char *MsgOpenAndReadMmsFile(const char *szFilePath, int offset, int size, int *npSize)
 {
        FILE *pFile = NULL;
        char *pData = NULL;
        int     readSize = 0;
 
        if (szFilePath == NULL) {
-               MSG_DEBUG("MsgOpenAndReadMmsFile: [ERROR] szFilePath id NULL");
+               MSG_ERR("szFilePath id NULL");
                goto __CATCH;
        }
 
        *npSize = 0;
 
-       pFile = MsgOpenFile( szFilePath, "rb" );
+       pFile = MsgOpenFile(szFilePath, "rb");
 
        if (pFile == NULL) {
-               MSG_DEBUG("MsgOpenAndReadMmsFile: [ERROR] Can't open filepath", strerror(errno));
+               MSG_ERR("Can't open file: %s", g_strerror(errno));
                goto __CATCH;
        }
 
-       if( size == -1 ) {
+       if (size == -1) {
                if (MsgGetFileSize(szFilePath, & readSize) == false) {
                        MSG_DEBUG("MsgGetFileSize: failed");
                        goto __CATCH;
@@ -654,21 +723,22 @@ char *MsgOpenAndReadMmsFile( const char *szFilePath, int offset, int size, int *
        } else {
                readSize = size;
        }
-
+/* restore Kies backup data size greater than FM_READ_WRITE_BUFFER_MAX */
+#if 0
        if (readSize > FM_READ_WRITE_BUFFER_MAX) {
                MSG_DEBUG("MsgOpenAndReadMmsFile: File size tried to read too big");
                goto __CATCH;
        }
+#endif
 
-       pData = (char *)malloc(readSize + 1);
+       pData = (char *)calloc(1, readSize + 1);
        if ( NULL == pData ) {
-               MSG_DEBUG( "MsgOpenAndReadMmsFile: [ERROR] pData MemAlloc Fail", strerror(errno) );
+               MSG_ERR("pData MemAlloc Fail : %s", g_strerror(errno) );
                goto __CATCH;
        }
-       memset( pData, 0, readSize + 1 );
 
-       if (MsgFseek( pFile, offset, SEEK_SET) < 0) {
-               MSG_DEBUG( "MsgOpenAndReadMmsFile: [ERROR] FmSeekFile failed", strerror(errno) );
+       if (MsgFseek(pFile, offset, SEEK_SET) < 0) {
+               MSG_ERR("FmSeekFile failed : %s", g_strerror(errno) );
                goto __CATCH;
        }
 
@@ -685,30 +755,30 @@ char *MsgOpenAndReadMmsFile( const char *szFilePath, int offset, int size, int *
 __CATCH:
 
        if (pData) {
-               free( pData );
+               free(pData);
                pData = NULL;
        }
 
        *npSize = 0;
 
        if (pFile != NULL) {
-               MsgCloseFile( pFile );
+               MsgCloseFile(pFile);
                pFile = NULL;
        }
 
        return NULL;
 }
 
-// it is equivalent to "rm -rf pDirPath"
+/* it is equivalent to "rm -rf pDirPath" */
 int MsgRmRf(char *pDirPath)
 {
-       struct dirent *d;
+       struct dirent *d = NULL;
        DIR *dir;
 
        dir = opendir(pDirPath);
 
        if (dir == NULL) {
-               MSG_FATAL("error opendir: %s", strerror(errno));
+               MSG_FATAL("error opendir: %s", g_strerror(errno));
                return -1;
        }
 
@@ -724,6 +794,7 @@ int MsgRmRf(char *pDirPath)
 
        bzero(path, size);
 
+       errno = 0;
        while ((d = readdir(dir)) != NULL) {
                if (d->d_type == DT_DIR) {
                        snprintf(path, size, "%s/%s", pDirPath, d->d_name);
@@ -734,13 +805,12 @@ int MsgRmRf(char *pDirPath)
                        MsgRmRf(path);
 
                        if (rmdir(path) != 0) {
-
                                if (path != NULL)
                                        free(path);
 
                                closedir(dir);
 
-                               MSG_FATAL("error rmdir: %s", strerror(errno));
+                               MSG_FATAL("error rmdir: %s", g_strerror(errno));
 
                                return -1;
                        }
@@ -751,17 +821,17 @@ int MsgRmRf(char *pDirPath)
                                MsgDrmUnregisterFile(path);
 
                        if (remove(path) != 0) {
-
                                if (path != NULL)
                                        free(path);
 
                                closedir(dir);
 
-                               MSG_FATAL("error remove: %s", strerror(errno));
+                               MSG_FATAL("error remove: %s", g_strerror(errno));
 
                                return -1;
                        }
                }
+               d = NULL;
        }
 
        closedir(dir);
@@ -769,6 +839,9 @@ int MsgRmRf(char *pDirPath)
        if (path != NULL)
                free(path);
 
+       if (errno != 0)
+               return -1;
+
        return 0;
 }
 
@@ -778,7 +851,7 @@ int MsgGetFileSize(const char *pFileName)
        struct stat file_stat;
 
        if (lstat(pFileName, &file_stat)) {
-               MSG_FATAL("error lstat: %s", strerror(errno));
+               MSG_FATAL("error lstat: %s", g_strerror(errno));
                return -1;
        }
 
@@ -786,27 +859,33 @@ int MsgGetFileSize(const char *pFileName)
 }
 
 
-// it is equivalent to "du dir_path"
+/* it is equivalent to "du dir_path" */
 unsigned int MsgDu(const char *pDirPath)
 {
-       struct dirent *d;
+       struct dirent *d = NULL;
        DIR *dir;
 
        dir = opendir(pDirPath);
 
        if (dir == NULL) {
-               MSG_FATAL("error opendir: %s", strerror(errno));
+               MSG_FATAL("error opendir: %s", g_strerror(errno));
                return -1;
        }
 
        int size = strlen(pDirPath) + 256;
        char *path = (char*)malloc(size);
+       if (path == NULL) {
+               closedir(dir);
+               return -1;
+       }
+
        bzero(path, size);
 
        unsigned int totalFileSize = 0;
 
+       errno = 0;
        while ((d = readdir(dir)) != NULL) {
-               if( d->d_type == DT_DIR) {
+               if(d->d_type == DT_DIR) {
                        snprintf(path, size, "%s/%s", pDirPath, d->d_name);
 
                        if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
@@ -835,12 +914,16 @@ unsigned int MsgDu(const char *pDirPath)
 
                        totalFileSize += fileSize;
                }
+               d = NULL;
        }
 
        closedir(dir);
 
        free(path);
 
+       if (errno != 0)
+               return 0;
+
        return totalFileSize;
 }
 
@@ -859,23 +942,23 @@ bool MsgAppendFile(const char *pFilePath, const char *pData, int DataSize)
        FILE *pFile = MsgOpenFile(fullPath, "a+");
 
        if (pFile == NULL) {
-               MSG_FATAL("File Open Error: %s", strerror(errno));
+               MSG_FATAL("File Open Error: %s", g_strerror(errno));
                return false;
        }
 
        if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
                MsgCloseFile(pFile);
-               MSG_FATAL("File Sead Error: %s", strerror(errno));
+               MSG_FATAL("File Sead Error: %s", g_strerror(errno));
                return false;
        }
 
        if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
                MsgCloseFile(pFile);
-               MSG_FATAL("File Write Error: %s", strerror(errno));
+               MSG_FATAL("File Write Error: %s", g_strerror(errno));
                return false;
        }
 
-       MsgFsync(pFile);        //file is written to device immediately, it prevents missing file data from unexpected power off
+       MsgFsync(pFile);        /*file is written to device immediately, it prevents missing file data from unexpected power off */
        MsgFflush(pFile);
        MsgCloseFile(pFile);
        return true;
@@ -889,30 +972,31 @@ void MsgMmsInitDir()
        dir = opendir(MSG_DATA_PATH);
 
        if (dir == NULL) {
-               MSG_FATAL("error opendir: %s", strerror(errno));
+               MSG_FATAL("error opendir: %s", g_strerror(errno));
                return;
        }
 
-       // Remove temporal Mms folder (/opt/usr/data/msg-service/msgdata/*.dir)
+       /* Remove temporal Mms folder */
        while ((d = readdir(dir)) != NULL) {
                if (d->d_type == DT_DIR) {
                        if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
                                continue;
 
                        if(strstr(d->d_name, ".dir") != NULL) {
-                               char filePath[MSG_FILEPATH_LEN_MAX] = {0,};
-                               snprintf(filePath, MSG_FILEPATH_LEN_MAX, MSG_DATA_PATH"%s", d->d_name);
+                               char filePath[MSG_FILEPATH_LEN_MAX] = {0, };
+                               snprintf(filePath, MSG_FILEPATH_LEN_MAX, "%s%s", MSG_DATA_PATH, d->d_name);
 
                                MsgRmRf(filePath);
                                rmdir(filePath);
                        }
                }
+               d = NULL;
        }
 
        closedir(dir);
 }
 
-//mode : R_OK, W_OK, X_OK, or the existence test F_OK.
+/* mode : R_OK, W_OK, X_OK, or the existence test F_OK. */
 bool MsgAccessFile(const char *filepath, int mode)
 {
        int ret;
@@ -921,7 +1005,7 @@ bool MsgAccessFile(const char *filepath, int mode)
                return false;
        }
 
-       MSG_DEBUG("request access path = %s, mode = %d", filepath, mode);
+       MSG_SEC_DEBUG("request access path = %s, mode = %d", filepath, mode);
 
        ret = access(filepath, mode);
 
@@ -933,3 +1017,294 @@ bool MsgAccessFile(const char *filepath, int mode)
 
        return true;
 }
+
+
+bool MsgChmod(const char *filepath, int mode)
+{
+#if 0
+       struct stat lstat_info;
+       struct stat fstat_info;
+       int fd;
+
+       if (lstat(filepath, &lstat_info) == -1) {
+               MSG_SEC_DEBUG("No such file as [%s].", filepath);
+               return false;
+       }
+
+       fd = open(filepath, O_RDONLY);
+
+       if (fd == -1) {
+               MSG_SEC_DEBUG("Fail to open [%s].", filepath);
+               return false;
+       }
+
+       if (fstat(fd, &fstat_info) == -1) {
+               MSG_SEC_DEBUG("Fail to fstat [%s].", filepath);
+               close(fd);
+               return false;
+       }
+
+       if (lstat_info.st_mode == fstat_info.st_mode &&
+                       lstat_info.st_ino == fstat_info.st_ino  &&
+                       lstat_info.st_dev == fstat_info.st_dev) {
+               if (fchmod(fd, mode) < 0) {
+                       MSG_SEC_DEBUG("Fail to fchmod [%s].", filepath);
+                       close(fd);
+                       return false;
+               }
+       }
+
+       close(fd);
+#endif
+       return true;
+}
+
+
+bool MsgChown(const char *filepath, int uid, int gid)
+{
+       struct stat lstat_info;
+       struct stat fstat_info;
+       int fd;
+
+       if (lstat(filepath, &lstat_info) == -1) {
+               MSG_SEC_INFO("No such file as [%s].", filepath);
+               return false;
+       }
+
+       fd = open(filepath, O_RDONLY);
+
+       if (fd == -1) {
+               MSG_SEC_INFO("Fail to open [%s].", filepath);
+               return false;
+       }
+
+       if (fstat(fd, &fstat_info) == -1) {
+               MSG_SEC_INFO("Fail to fstat [%s].", filepath);
+               close(fd);
+               return false;
+       }
+
+       if (lstat_info.st_mode == fstat_info.st_mode &&
+                       lstat_info.st_ino == fstat_info.st_ino  &&
+                       lstat_info.st_dev == fstat_info.st_dev) {
+               if (fchown(fd, uid, gid) < 0) {
+                       MSG_SEC_INFO("Fail to fchown [%s].", filepath);
+                       close(fd);
+                       return false;
+               }
+       }
+
+       close(fd);
+       return true;
+}
+
+bool MsgCreateFile(const char *pFilePath, char *pData, int DataSize)
+{
+       if(!pFilePath) {
+               MSG_DEBUG("pFilePath is NULL");
+               return false;
+       }
+
+       FILE *pFile = MsgOpenFile(pFilePath, "wb+");
+
+       if (pFile == NULL) {
+               MSG_ERR("File Open Error: %s", g_strerror(errno));
+               return false;
+       }
+
+       if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
+               MsgCloseFile(pFile);
+               MSG_ERR("File Seek Error: %s", g_strerror(errno));
+               return false;
+       }
+
+       if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
+               MsgCloseFile(pFile);
+               MSG_ERR("File Write Error: %s", g_strerror(errno));
+               return false;
+       }
+
+       MsgFflush(pFile);
+       MsgFsync(pFile);
+       MsgCloseFile(pFile);
+
+       return true;
+}
+
+char *MsgGetDirName(char *file_path)
+{
+       return g_path_get_dirname(file_path);
+}
+
+
+char *MsgGetFileName(char *file_path)
+{
+       return g_path_get_basename(file_path);
+}
+
+
+int MsgCheckFilepathSmack(const char *app_smack_label, char *file_path)
+{
+       int err = MSG_SUCCESS;
+
+       char *path_smack_label = NULL;
+       char *dir_smack_label = NULL;
+       char *dir_name = NULL;
+
+       if (!file_path || file_path[0] == '\0') {
+               return MSG_SUCCESS;
+       }
+
+       struct stat st;
+       if (stat(file_path, &st) != 0) {
+               MSG_SEC_ERR("stat(%s, &st) != 0", file_path);
+               return MSG_ERR_PERMISSION_DENIED;
+       }
+       if (S_ISDIR(st.st_mode)) {
+               MSG_ERR("S_ISDIR(st.st_mode)");
+               return MSG_ERR_INVALID_PARAMETER;
+       }
+
+       dir_name = MsgGetDirName(file_path);
+       if (!dir_name || !g_strcmp0(dir_name, file_path)) {
+               MSG_SEC_ERR("!dir_name || !g_strcmp0(dir_name, %s)", file_path);
+               err = MSG_ERR_INVALID_PARAMETER;
+               goto __RETURN;
+       }
+
+       smack_getlabel(dir_name, &dir_smack_label, SMACK_LABEL_ACCESS);
+       if (dir_smack_label == NULL) {
+               MSG_ERR("smack_getlabel failed (dir_smack_label)");
+               err = MSG_ERR_PERMISSION_DENIED;
+               goto __RETURN;
+       }
+
+       if (smack_have_access(app_smack_label, dir_smack_label, "RX") < 1) {
+               MSG_ERR("smack_have_access failed (dir_smack_label)");
+               err = MSG_ERR_PERMISSION_DENIED;
+               goto __RETURN;
+       }
+
+       smack_getlabel(file_path, &path_smack_label, SMACK_LABEL_ACCESS);
+       if (path_smack_label == NULL) {
+               MSG_ERR("smack_getlabel failed (path_smack_label)");
+               err = MSG_ERR_PERMISSION_DENIED;
+               goto __RETURN;
+       }
+
+       if (smack_have_access(app_smack_label, path_smack_label, "R") < 1) {
+               MSG_ERR("smack_have_access failed (path_smack_label)");
+               err = MSG_ERR_PERMISSION_DENIED;
+               goto __RETURN;
+       }
+
+       MSG_DEBUG("smack_have_access pass successfully");
+
+__RETURN:
+       MSG_FREE(path_smack_label);
+       MSG_FREE(dir_smack_label);
+       MSG_FREE(dir_name);
+       return err;
+}
+
+
+void MsgGetMimeType(char *filePath, char *mimeType, int size)
+{
+       aul_get_mime_from_file(filePath, mimeType, size);
+}
+
+int MsgTcsScanFile(const char *filepath, int *bLevel)
+{
+       MSG_BEGIN();
+
+       int (*_csr_cs_context_create)(void **handle);
+       int (*_csr_cs_scan_file)(void *handle, const char *filepath, void **malware);
+       int (*_csr_cs_context_destroy)(void *handle);
+       int (*_csr_cs_malware_get_severity)(void *malware, void *severity);
+       int (*_csr_cs_malware_get_name)(void *malware, char **name);
+
+       if (MsgAccessFile(filepath, R_OK) == false) {
+               MSG_SEC_DEBUG("not exist source file [%s]", filepath);
+               return -1;
+       }
+
+       MSG_SEC_DEBUG("Scanning file name : %s\n", filepath);
+
+       void *lib_handle = NULL;
+       lib_handle = dlopen(PATH_LIBCSR_CLIENT, RTLD_LAZY);
+       if (!lib_handle) {
+               MSG_ERR("Unable to open %s", PATH_LIBCSR_CLIENT);
+               return 0;
+       }
+
+       _csr_cs_context_create = (int(*)(void**))dlsym(lib_handle, "csr_cs_context_create");
+       _csr_cs_scan_file = (int(*)(void*, const char*, void**))dlsym(lib_handle, "csr_cs_scan_file");
+       _csr_cs_context_destroy = (int(*)(void*))dlsym(lib_handle, "csr_cs_context_destroy");
+       _csr_cs_malware_get_severity = (int(*)(void*, void*))dlsym(lib_handle, "csr_cs_malware_get_severity");
+       _csr_cs_malware_get_name = (int(*)(void*, char**))dlsym(lib_handle, "csr_cs_malware_get_name");
+
+       int ret = 0;
+       if (!_csr_cs_context_create || !_csr_cs_scan_file || !_csr_cs_context_destroy
+                       || !_csr_cs_malware_get_severity || !_csr_cs_malware_get_name) {
+               MSG_ERR("Failed to load CSR symbols");
+               if (lib_handle)
+                               dlclose(lib_handle);
+               return -1;
+       }
+
+       void *csr_handle = NULL;
+       ret = _csr_cs_context_create(&csr_handle);
+       if (ret != 0) {
+               MSG_DEBUG("csr_cs_context_create error: err = %d\n", ret);
+               if (lib_handle)
+                               dlclose(lib_handle);
+               return -1;
+       }
+
+       do {
+               void *detected = NULL;
+               ret = _csr_cs_scan_file(csr_handle, filepath, &detected);
+               if (ret != 0) {
+                       MSG_DEBUG("csr_cs_scan_file fail: err = %d\n", ret);
+                       break;
+               }
+
+               if (NULL == detected) {
+                       MSG_DEBUG("Nothing detected");
+                       break;
+               }
+
+               int severity = 0x01; /* CSR_CS_SEVERITY_LOW */
+               ret = _csr_cs_malware_get_severity(detected, &severity);
+               if (ret != 0) {
+                       MSG_DEBUG("csr_cs_malware_get_severity error: err = %d\n", ret);
+               }
+               MSG_DEBUG(" +-- Malware Severity class: %d\n", severity);
+
+               char *name = NULL;
+               ret = _csr_cs_malware_get_name(detected, &name);
+               if (ret != 0) {
+                       MSG_DEBUG("csr_cs_malware_get_name error: err = %d\n", ret);
+               }
+               MSG_SEC_DEBUG(" +-- Malware Name: [%s]\n", name);
+
+               if (name)
+                       free(name);
+
+               if (bLevel)
+                       *bLevel = severity;
+
+       } while (0);
+
+       ret = _csr_cs_context_destroy(csr_handle);
+       if (ret != 0)
+               MSG_DEBUG("csr_cs_context_destroy error: err = %d\n", ret);
+
+       if (lib_handle)
+               dlclose(lib_handle);
+
+       MSG_END();
+       return 0;
+}
+
+