2 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include <sys/smack.h>
31 #include <thumbnail_util.h>
32 #include <image_util.h>
34 #include "MsgStorageTypes.h"
36 #include "MsgException.h"
37 #include "MsgUtilFile.h"
38 #include "MsgMmsTypes.h"
39 #include "MsgInternalTypes.h"
40 #include "MsgDrmWrapper.h"
47 #define PATH_LIBCSR_CLIENT "/usr/lib/libcsr-client.so.2"
48 #define THUMB_WIDTH 320
49 #define THUMB_HEIGHT 240
54 /*==================================================================================================
55 FUNCTION IMPLEMENTATION
56 ==================================================================================================*/
57 bool MakeThumbnail(char *srcPath, char *dstPath)
59 if (srcPath == NULL || dstPath == NULL) {
60 MSG_SEC_DEBUG("Invalid Param src = %p, dst = %p", srcPath, dstPath);
64 if (MsgAccessFile(srcPath, R_OK) == false) {
65 MSG_SEC_DEBUG("not exist source file [%s]", srcPath);
73 int ret = THUMBNAIL_UTIL_ERROR_NONE;
75 ret = thumbnail_util_extract_to_file(srcPath, THUMB_WIDTH, THUMB_HEIGHT, dstPath);
77 if (ret != THUMBNAIL_UTIL_ERROR_NONE) {
78 MSG_ERR("thumbnail_util_extract_to_file is failed");
83 time_ret = g_cv.timedwait(g_mx.pMsgMutex(), 5);
87 if (time_ret == ETIMEDOUT) {
88 MSG_ERR("@@ WAKE by timeout@@");
92 if (MsgAccessFile(dstPath, F_OK) == false) {
93 MSG_SEC_DEBUG("not exist result file [%s]", dstPath);
97 MSG_SEC_DEBUG("Make thumbnail: success [%s]", dstPath);
101 /* File operation wrappers */
102 FILE *MsgOpenFile(const char *filepath, const char *opt)
104 if (!filepath || !opt) {
105 MSG_FATAL("Null parameter");
109 MSG_SEC_DEBUG("[FILE] filepath : [%s], opt [%s]", filepath, opt);
114 pFile = fopen(filepath, opt);
115 MSG_DEBUG("[FILE] pFile [%p]", pFile);
116 } catch (exception &e) {
117 MSG_FATAL("%s", e.what());
124 void MsgCloseFile(FILE *pFile)
127 MSG_FATAL("NULL parameter");
131 MSG_DEBUG("[FILE] pFile [%p]", pFile);
135 } catch (exception &e) {
136 MSG_FATAL("%s", e.what());
140 int MsgFseek(FILE *pFile, long int offset, int origin)
143 MSG_FATAL("pFile NULL");
149 MSG_DEBUG("[FILE] pFile [%p], offset [%ld], origin [%d]", pFile, offset, origin);
152 ret = fseek(pFile, offset, origin); /* return 0, if success. */
153 } catch (exception &e) {
154 MSG_FATAL("%s", e.what());
161 size_t MsgWriteFile(const char *pData, size_t size, size_t count, FILE *pFile)
163 if (!pData || !pFile) {
164 MSG_FATAL("pData or pFile NULL");
170 MSG_DEBUG("[FILE] pData [%p], size [%zu], count [%zu], pFile [%p]", pData, size, count, pFile);
173 nWrite = fwrite(pData, size, count, pFile);
174 } catch (exception &e) {
175 MSG_FATAL("%s", e.what());
181 size_t MsgReadFile(void *pData, size_t size, size_t count, FILE *pFile)
183 if (!pData || !pFile) {
184 MSG_FATAL("pData or pFile NULL");
191 nRead = fread(pData, size, count, pFile);
192 } catch (exception &e) {
193 MSG_FATAL("%s", e.what());
199 long int MsgFtell(FILE *pFile)
202 MSG_FATAL("pFile NULL");
206 long int ret = -1L; /* -1L return if error occured. */
210 } catch (exception &e) {
211 MSG_FATAL("%s", e.what());
217 int MsgFflush(FILE *pFile)
220 MSG_FATAL("pFile NULL");
227 ret = fflush(pFile); /* return 0 if success */
228 } catch (exception &e) {
229 MSG_FATAL("%s", e.what());
235 int MsgFsync(FILE *pFile)
238 MSG_FATAL("pFile NULL");
245 ret = fdatasync(pFile->_fileno); /* return 0 if success */
246 } catch (exception &e) {
247 MSG_FATAL("%s", e.what());
253 bool MsgCreateFileName(char *pFileName)
255 if (pFileName == NULL) {
256 MSG_DEBUG("[ERROR] pFileName is NULL");
263 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
264 MSG_DEBUG("clock_gettime() error: %s", g_strerror(errno));
268 /* Create Random Number */
269 srandom((unsigned int)ts.tv_nsec);
271 MSG_DEBUG("ts.tv_nsec : %ld", ts.tv_nsec);
273 /* between 1 - 1000000000 */
274 snprintf(pFileName, MSG_FILENAME_LEN_MAX, "MSG_%lu.DATA", random()%1000000000+1);
275 } catch (exception& e) {
276 MSG_FATAL("%s", e.what());
284 bool MsgOpenAndReadFile(const char *pFileName, char **ppData, int *pDataSize)
286 if (!pFileName || !ppData || !pDataSize) {
287 MSG_ERR("Invalid params");
291 MSG_DEBUG("MsgOpenAndReadFile");
295 char fullPath[MAX_FULL_PATH_SIZE] = {0};
297 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
298 MSG_SEC_DEBUG("open file name: %s", fullPath);
301 if (stat(fullPath, &st) != 0) {
302 MSG_SEC_ERR("stat(%s, &st) != 0", fullPath);
305 if (S_ISDIR(st.st_mode)) {
306 MSG_ERR("S_ISDIR(st.st_mode)");
310 pFile = MsgOpenFile(fullPath, "rb");
313 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
317 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
319 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
323 int FileSize = MsgFtell(pFile);
326 MSG_DEBUG("Filesize is error : %d", FileSize);
332 *ppData = new char[FileSize+1];
333 memset(*ppData, 0x00, (FileSize+1));
335 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
337 MSG_DEBUG("File seek Error: %s", g_strerror(errno));
341 if (MsgReadFile(*ppData, sizeof(char), FileSize, pFile) != (size_t)FileSize) {
343 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
347 *pDataSize = FileSize;
355 bool MsgReadFileForDecode(FILE *pFile, char *pBuf, int length, int *nSize)
359 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
360 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
365 *nSize = MsgReadFile(pBuf, sizeof(char), length, pFile);
372 bool MsgWriteIpcFile(const char *pFileName, const char *pData, int DataSize)
375 MSG_DEBUG("NULL parameter, pFileName [%p], pData [%p]", pFileName, pData);
379 char fullPath[MAX_FULL_PATH_SIZE] = {0};
381 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
383 FILE *pFile = MsgOpenFile(fullPath, "wb+");
386 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
390 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
392 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
396 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
398 MSG_DEBUG("File Write Error: %s", g_strerror(errno));
408 int MsgReadSmilFile(const char *pFileName, char **ppData)
411 MSG_DEBUG("pFileName is NULL");
416 char fullPath[MAX_FULL_PATH_SIZE] = {0};
418 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_SMIL_FILE_PATH, pFileName);
420 MSG_SEC_DEBUG("open file name: %s", fullPath);
422 FILE *pFile = MsgOpenFile(fullPath, "rb");
425 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
429 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
431 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
435 int FileSize = MsgFtell(pFile);
438 MSG_DEBUG("Filesize is error : %d", FileSize);
443 *ppData = new char[FileSize + 1];
444 memset(*ppData, 0x00, (FileSize+1));
446 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
448 MSG_DEBUG("File Sead Error: %s", g_strerror(errno));
452 if (MsgReadFile(*ppData, sizeof(char), FileSize, pFile) != (size_t)FileSize) {
454 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
458 /* ppData[FileSize] = '\0'; */
467 bool MsgWriteSmilFile(const char *pFilePath, char *pData, int DataSize)
470 MSG_DEBUG("pFilePath is NULL");
475 if (mkdir(MSG_SMIL_FILE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
476 if (errno == EEXIST) {
477 MSG_SEC_DEBUG("The %s already exists", MSG_SMIL_FILE_PATH);
479 MSG_SEC_DEBUG("Error while mkdir %s", MSG_SMIL_FILE_PATH);
484 FILE *pFile = MsgOpenFile(pFilePath, "wb+");
487 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
491 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
493 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
497 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
499 MSG_DEBUG("File Write Error: %s", g_strerror(errno));
510 void MsgDeleteFile(const char *pFileName)
513 MSG_FATAL("pFileName is NULL");
517 if (strlen(pFileName) == 0) {
518 MSG_FATAL("pFileName has zero length");
522 char fullPath[MAX_FULL_PATH_SIZE] = {0};
525 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
527 MSG_SEC_DEBUG("%s", fullPath);
529 if (remove(fullPath) != 0)
530 MSG_SEC_ERR("File Delete Error [%s]: %s", fullPath, g_strerror(errno));
531 } catch (exception &e) {
532 MSG_FATAL("%s", e.what());
537 void MsgDeleteSmilFile(const char *pFileName)
540 MSG_FATAL("pFileName NULL");
545 char fullPath[MAX_FULL_PATH_SIZE] = {0};
547 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_SMIL_FILE_PATH, pFileName);
549 if (remove(fullPath) != 0)
550 MSG_SEC_ERR("File Delete Error [%s]: %s", fullPath, g_strerror(errno));
551 } catch (exception &e) {
552 MSG_FATAL("%s", e.what());
557 bool MsgGetFileSize(const char *pFilePath, int *nSize)
560 MSG_FATAL("pFileName NULL");
566 pFile = MsgOpenFile(pFilePath, "rb");
569 MSG_DEBUG("File Open error: %s", g_strerror(errno));
573 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
575 MSG_FATAL("File Read Error: %s", g_strerror(errno));
579 *nSize = MsgFtell(pFile);
587 FILE *MsgOpenMMSFile(char *pFileName)
592 MSG_DEBUG("pFileName NULL: %s", g_strerror(errno));
596 len = strlen(pFileName);
598 for (int i = 0; i < len; i++) {
599 switch (pFileName[i]) {
606 MSG_SEC_DEBUG("pFileName = %s", pFileName);
608 char fullPath[MAX_FULL_PATH_SIZE+1] = {0};
610 snprintf(fullPath, MAX_FULL_PATH_SIZE+1, "%s.mms", pFileName);
612 FILE *pFile = MsgOpenFile(fullPath, "wb+");
615 MSG_ERR("File Open Error: %s", g_strerror(errno));
619 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
621 MSG_ERR("File Read Error: %s", g_strerror(errno));
629 bool MsgWriteDataFromEncodeBuffer(FILE *pFile, char *pInBuffer, int *pPtr, int maxLen, int *pOffset )
631 if (!pFile || !pPtr || !pInBuffer || !pOffset) {
632 MSG_FATAL(" NULL parameter passed");
636 MSG_DEBUG("MsgWriteDataFromEncodeBuffer:");
637 MSG_DEBUG("pInBuffer %s", pInBuffer);
638 MSG_DEBUG("pPtr %d", (*pPtr));
639 MSG_DEBUG("before to fwite %p", pFile);
641 if (MsgWriteFile(pInBuffer, sizeof(char), (*pPtr), pFile) != (size_t)(*pPtr)) {
642 MSG_FATAL("MsgWriteFile failed");
646 MSG_DEBUG("after to fwite \n");
650 memset(pInBuffer, 0, maxLen);
654 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
655 MSG_FATAL("MsgFseek failed");
659 *pOffset = MsgFtell(pFile);
661 if (*pOffset == -1L) {
662 MSG_FATAL("MsgFtell failed");
670 bool MsgOpenCreateAndOverwriteFile(char *pFullPath, char *pBuff, int TotalLength)
674 if ((pFile = MsgOpenFile(pFullPath, "wb+")) == NULL) {
675 MSG_FATAL("MsgOpenFile errer");
679 if (MsgWriteFile(pBuff, sizeof(char), TotalLength, pFile) != (size_t)TotalLength) {
684 MsgFsync(pFile); /* file is written to device immediately, it prevents missing file data from unexpected power off */
692 char *MsgOpenAndReadMmsFile(const char *szFilePath, int offset, int size, int *npSize)
698 if (szFilePath == NULL) {
699 MSG_ERR("szFilePath id NULL");
705 pFile = MsgOpenFile(szFilePath, "rb");
708 MSG_ERR("Can't open file: %s", g_strerror(errno));
713 if (MsgGetFileSize(szFilePath, & readSize) == false) {
714 MSG_DEBUG("MsgGetFileSize: failed");
720 /* restore Kies backup data size greater than FM_READ_WRITE_BUFFER_MAX */
722 if (readSize > FM_READ_WRITE_BUFFER_MAX) {
723 MSG_DEBUG("MsgOpenAndReadMmsFile: File size tried to read too big");
728 pData = (char *)calloc(1, readSize + 1);
729 if ( NULL == pData ) {
730 MSG_ERR("pData MemAlloc Fail : %s", g_strerror(errno) );
734 if (MsgFseek(pFile, offset, SEEK_SET) < 0) {
735 MSG_ERR("FmSeekFile failed : %s", g_strerror(errno) );
739 *npSize = MsgReadFile(pData, sizeof(char), readSize, pFile);
745 *(pData + (*npSize)) = '\0';
766 /* it is equivalent to "rm -rf pDirPath" */
767 int MsgRmRf(char *pDirPath)
769 struct dirent *d = NULL;
772 dir = opendir(pDirPath);
775 MSG_FATAL("error opendir: %s", g_strerror(errno));
779 int size = strlen(pDirPath) + 256;
781 char *path = (char*)malloc(size);
784 MSG_DEBUG("path is NULL");
792 while ((d = readdir(dir)) != NULL) {
793 if (d->d_type == DT_DIR) {
794 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
796 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
801 if (rmdir(path) != 0) {
807 MSG_FATAL("error rmdir: %s", g_strerror(errno));
812 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
814 if (MsgDrmIsDrmFile(path))
815 MsgDrmUnregisterFile(path);
817 if (remove(path) != 0) {
823 MSG_FATAL("error remove: %s", g_strerror(errno));
843 int MsgGetFileSize(const char *pFileName)
845 struct stat file_stat;
847 if (lstat(pFileName, &file_stat)) {
848 MSG_FATAL("error lstat: %s", g_strerror(errno));
852 return file_stat.st_size;
856 /* it is equivalent to "du dir_path" */
857 unsigned int MsgDu(const char *pDirPath)
859 struct dirent *d = NULL;
862 dir = opendir(pDirPath);
865 MSG_FATAL("error opendir: %s", g_strerror(errno));
869 int size = strlen(pDirPath) + 256;
870 char *path = (char*)malloc(size);
878 unsigned int totalFileSize = 0;
881 while ((d = readdir(dir)) != NULL) {
882 if(d->d_type == DT_DIR) {
883 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
885 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
888 unsigned int dirSize = MsgDu(path);
891 MSG_FATAL("error MsgDu");
897 totalFileSize += dirSize;
899 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
900 int fileSize = MsgGetFileSize(path);
903 MSG_FATAL("error MsgGetFileSize");
909 totalFileSize += fileSize;
921 return totalFileSize;
925 bool MsgAppendFile(const char *pFilePath, const char *pData, int DataSize)
928 MSG_FATAL("NULL check error, pFileName %p, pData %p", pFilePath, pData);
932 char fullPath[MAX_FULL_PATH_SIZE] = {0};
934 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s", pFilePath);
936 FILE *pFile = MsgOpenFile(fullPath, "a+");
939 MSG_FATAL("File Open Error: %s", g_strerror(errno));
943 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
945 MSG_FATAL("File Sead Error: %s", g_strerror(errno));
949 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
951 MSG_FATAL("File Write Error: %s", g_strerror(errno));
955 MsgFsync(pFile); /*file is written to device immediately, it prevents missing file data from unexpected power off */
963 struct dirent *d = NULL;
966 dir = opendir(MSG_DATA_PATH);
969 MSG_FATAL("error opendir: %s", g_strerror(errno));
973 /* Remove temporal Mms folder */
974 while ((d = readdir(dir)) != NULL) {
975 if (d->d_type == DT_DIR) {
976 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
979 if(strstr(d->d_name, ".dir") != NULL) {
980 char filePath[MSG_FILEPATH_LEN_MAX] = {0, };
981 snprintf(filePath, MSG_FILEPATH_LEN_MAX, "%s%s", MSG_DATA_PATH, d->d_name);
993 /* mode : R_OK, W_OK, X_OK, or the existence test F_OK. */
994 bool MsgAccessFile(const char *filepath, int mode)
997 if (filepath == NULL) {
998 MSG_DEBUG("filepath is NULL");
1002 MSG_SEC_DEBUG("request access path = %s, mode = %d", filepath, mode);
1004 ret = access(filepath, mode);
1008 MSG_DEBUG("Fail to access file, ret = %d", ret);
1016 bool MsgChmod(const char *filepath, int mode)
1019 struct stat lstat_info;
1020 struct stat fstat_info;
1023 if (lstat(filepath, &lstat_info) == -1) {
1024 MSG_SEC_DEBUG("No such file as [%s].", filepath);
1028 fd = open(filepath, O_RDONLY);
1031 MSG_SEC_DEBUG("Fail to open [%s].", filepath);
1035 if (fstat(fd, &fstat_info) == -1) {
1036 MSG_SEC_DEBUG("Fail to fstat [%s].", filepath);
1041 if (lstat_info.st_mode == fstat_info.st_mode &&
1042 lstat_info.st_ino == fstat_info.st_ino &&
1043 lstat_info.st_dev == fstat_info.st_dev) {
1044 if (fchmod(fd, mode) < 0) {
1045 MSG_SEC_DEBUG("Fail to fchmod [%s].", filepath);
1057 bool MsgChown(const char *filepath, int uid, int gid)
1059 struct stat lstat_info;
1060 struct stat fstat_info;
1063 if (lstat(filepath, &lstat_info) == -1) {
1064 MSG_SEC_INFO("No such file as [%s].", filepath);
1068 fd = open(filepath, O_RDONLY);
1071 MSG_SEC_INFO("Fail to open [%s].", filepath);
1075 if (fstat(fd, &fstat_info) == -1) {
1076 MSG_SEC_INFO("Fail to fstat [%s].", filepath);
1081 if (lstat_info.st_mode == fstat_info.st_mode &&
1082 lstat_info.st_ino == fstat_info.st_ino &&
1083 lstat_info.st_dev == fstat_info.st_dev) {
1084 if (fchown(fd, uid, gid) < 0) {
1085 MSG_SEC_INFO("Fail to fchown [%s].", filepath);
1095 bool MsgCreateFile(const char *pFilePath, char *pData, int DataSize)
1098 MSG_DEBUG("pFilePath is NULL");
1102 FILE *pFile = MsgOpenFile(pFilePath, "wb+");
1104 if (pFile == NULL) {
1105 MSG_ERR("File Open Error: %s", g_strerror(errno));
1109 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
1110 MsgCloseFile(pFile);
1111 MSG_ERR("File Seek Error: %s", g_strerror(errno));
1115 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
1116 MsgCloseFile(pFile);
1117 MSG_ERR("File Write Error: %s", g_strerror(errno));
1123 MsgCloseFile(pFile);
1128 char *MsgGetDirName(char *file_path)
1130 return g_path_get_dirname(file_path);
1134 char *MsgGetFileName(char *file_path)
1136 return g_path_get_basename(file_path);
1140 int MsgCheckFilepathSmack(const char *app_smack_label, char *file_path)
1142 int err = MSG_SUCCESS;
1144 char *path_smack_label = NULL;
1145 char *dir_smack_label = NULL;
1146 char *dir_name = NULL;
1148 if (!file_path || file_path[0] == '\0') {
1153 if (stat(file_path, &st) != 0) {
1154 MSG_SEC_ERR("stat(%s, &st) != 0", file_path);
1155 return MSG_ERR_PERMISSION_DENIED;
1157 if (S_ISDIR(st.st_mode)) {
1158 MSG_ERR("S_ISDIR(st.st_mode)");
1159 return MSG_ERR_INVALID_PARAMETER;
1162 dir_name = MsgGetDirName(file_path);
1163 if (!dir_name || !g_strcmp0(dir_name, file_path)) {
1164 MSG_SEC_ERR("!dir_name || !g_strcmp0(dir_name, %s)", file_path);
1165 err = MSG_ERR_INVALID_PARAMETER;
1169 smack_getlabel(dir_name, &dir_smack_label, SMACK_LABEL_ACCESS);
1170 if (dir_smack_label == NULL) {
1171 MSG_ERR("smack_getlabel failed (dir_smack_label)");
1172 err = MSG_ERR_PERMISSION_DENIED;
1176 if (smack_have_access(app_smack_label, dir_smack_label, "RX") < 1) {
1177 MSG_ERR("smack_have_access failed (dir_smack_label)");
1178 err = MSG_ERR_PERMISSION_DENIED;
1182 smack_getlabel(file_path, &path_smack_label, SMACK_LABEL_ACCESS);
1183 if (path_smack_label == NULL) {
1184 MSG_ERR("smack_getlabel failed (path_smack_label)");
1185 err = MSG_ERR_PERMISSION_DENIED;
1189 if (smack_have_access(app_smack_label, path_smack_label, "R") < 1) {
1190 MSG_ERR("smack_have_access failed (path_smack_label)");
1191 err = MSG_ERR_PERMISSION_DENIED;
1195 MSG_DEBUG("smack_have_access pass successfully");
1198 MSG_FREE(path_smack_label);
1199 MSG_FREE(dir_smack_label);
1205 void MsgGetMimeType(char *filePath, char *mimeType, int size)
1207 aul_get_mime_from_file(filePath, mimeType, size);
1210 int MsgTcsScanFile(const char *filepath, int *bLevel)
1214 int (*_csr_cs_context_create)(void **handle);
1215 int (*_csr_cs_scan_file)(void *handle, const char *filepath, void **malware);
1216 int (*_csr_cs_context_destroy)(void *handle);
1217 int (*_csr_cs_malware_get_severity)(void *malware, void *severity);
1218 int (*_csr_cs_malware_get_name)(void *malware, char **name);
1220 if (MsgAccessFile(filepath, R_OK) == false) {
1221 MSG_SEC_DEBUG("not exist source file [%s]", filepath);
1225 MSG_SEC_DEBUG("Scanning file name : %s\n", filepath);
1227 void *lib_handle = NULL;
1228 lib_handle = dlopen(PATH_LIBCSR_CLIENT, RTLD_LAZY);
1230 MSG_ERR("Unable to open %s", PATH_LIBCSR_CLIENT);
1234 _csr_cs_context_create = (int(*)(void**))dlsym(lib_handle, "csr_cs_context_create");
1235 _csr_cs_scan_file = (int(*)(void*, const char*, void**))dlsym(lib_handle, "csr_cs_scan_file");
1236 _csr_cs_context_destroy = (int(*)(void*))dlsym(lib_handle, "csr_cs_context_destroy");
1237 _csr_cs_malware_get_severity = (int(*)(void*, void*))dlsym(lib_handle, "csr_cs_malware_get_severity");
1238 _csr_cs_malware_get_name = (int(*)(void*, char**))dlsym(lib_handle, "csr_cs_malware_get_name");
1241 if (!_csr_cs_context_create || !_csr_cs_scan_file || !_csr_cs_context_destroy
1242 || !_csr_cs_malware_get_severity || !_csr_cs_malware_get_name) {
1243 MSG_ERR("Failed to load CSR symbols");
1245 dlclose(lib_handle);
1249 void *csr_handle = NULL;
1250 ret = _csr_cs_context_create(&csr_handle);
1252 MSG_DEBUG("csr_cs_context_create error: err = %d\n", ret);
1254 dlclose(lib_handle);
1259 void *detected = NULL;
1260 ret = _csr_cs_scan_file(csr_handle, filepath, &detected);
1262 MSG_DEBUG("csr_cs_scan_file fail: err = %d\n", ret);
1266 if (NULL == detected) {
1267 MSG_DEBUG("Nothing detected");
1271 int severity = 0x01; /* CSR_CS_SEVERITY_LOW */
1272 ret = _csr_cs_malware_get_severity(detected, &severity);
1274 MSG_DEBUG("csr_cs_malware_get_severity error: err = %d\n", ret);
1276 MSG_DEBUG(" +-- Malware Severity class: %d\n", severity);
1279 ret = _csr_cs_malware_get_name(detected, &name);
1281 MSG_DEBUG("csr_cs_malware_get_name error: err = %d\n", ret);
1283 MSG_SEC_DEBUG(" +-- Malware Name: [%s]\n", name);
1293 ret = _csr_cs_context_destroy(csr_handle);
1295 MSG_DEBUG("csr_cs_context_destroy error: err = %d\n", ret);
1298 dlclose(lib_handle);