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;
76 ret = thumbnail_util_extract_to_file(srcPath, THUMB_WIDTH, THUMB_HEIGHT, dstPath);
83 if (ret != THUMBNAIL_UTIL_ERROR_NONE) {
84 MSG_ERR("thumbnail_util_extract_to_file is failed");
89 time_ret = g_cv.timedwait(g_mx.pMsgMutex(), 5);
93 if (time_ret == ETIMEDOUT) {
94 MSG_ERR("@@ WAKE by timeout@@");
98 if (MsgAccessFile(dstPath, F_OK) == false) {
99 MSG_SEC_DEBUG("not exist result file [%s]", dstPath);
103 MSG_SEC_DEBUG("Make thumbnail: success [%s]", dstPath);
107 /* File operation wrappers */
108 FILE *MsgOpenFile(const char *filepath, const char *opt)
110 if (!filepath || !opt) {
111 MSG_FATAL("Null parameter");
115 MSG_SEC_DEBUG("[FILE] filepath : [%s], opt [%s]", filepath, opt);
120 pFile = fopen(filepath, opt);
121 MSG_DEBUG("[FILE] pFile [%p]", pFile);
122 } catch (exception &e) {
123 MSG_FATAL("%s", e.what());
130 void MsgCloseFile(FILE *pFile)
133 MSG_FATAL("NULL parameter");
137 MSG_DEBUG("[FILE] pFile [%p]", pFile);
141 } catch (exception &e) {
142 MSG_FATAL("%s", e.what());
146 int MsgFseek(FILE *pFile, long int offset, int origin)
149 MSG_FATAL("pFile NULL");
155 MSG_DEBUG("[FILE] pFile [%p], offset [%ld], origin [%d]", pFile, offset, origin);
158 ret = fseek(pFile, offset, origin); /* return 0, if success. */
159 } catch (exception &e) {
160 MSG_FATAL("%s", e.what());
167 size_t MsgWriteFile(const char *pData, size_t size, size_t count, FILE *pFile)
169 if (!pData || !pFile) {
170 MSG_FATAL("pData or pFile NULL");
176 MSG_DEBUG("[FILE] pData [%p], size [%zu], count [%zu], pFile [%p]", pData, size, count, pFile);
179 nWrite = fwrite(pData, size, count, pFile);
180 } catch (exception &e) {
181 MSG_FATAL("%s", e.what());
187 size_t MsgReadFile(void *pData, size_t size, size_t count, FILE *pFile)
189 if (!pData || !pFile) {
190 MSG_FATAL("pData or pFile NULL");
197 nRead = fread(pData, size, count, pFile);
198 } catch (exception &e) {
199 MSG_FATAL("%s", e.what());
205 long int MsgFtell(FILE *pFile)
208 MSG_FATAL("pFile NULL");
212 long int ret = -1L; /* -1L return if error occured. */
216 } catch (exception &e) {
217 MSG_FATAL("%s", e.what());
223 int MsgFflush(FILE *pFile)
226 MSG_FATAL("pFile NULL");
233 ret = fflush(pFile); /* return 0 if success */
234 } catch (exception &e) {
235 MSG_FATAL("%s", e.what());
241 int MsgFsync(FILE *pFile)
244 MSG_FATAL("pFile NULL");
251 ret = fdatasync(pFile->_fileno); /* return 0 if success */
252 } catch (exception &e) {
253 MSG_FATAL("%s", e.what());
259 bool MsgCreateFileName(char *pFileName)
261 if (pFileName == NULL) {
262 MSG_DEBUG("[ERROR] pFileName is NULL");
269 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
270 MSG_DEBUG("clock_gettime() error: %s", g_strerror(errno));
274 /* Create Random Number */
275 srandom((unsigned int)ts.tv_nsec);
277 MSG_DEBUG("ts.tv_nsec : %ld", ts.tv_nsec);
279 /* between 1 - 1000000000 */
280 snprintf(pFileName, MSG_FILENAME_LEN_MAX, "MSG_%lu.DATA", random()%1000000000+1);
281 } catch (exception& e) {
282 MSG_FATAL("%s", e.what());
290 bool MsgOpenAndReadFile(const char *pFileName, char **ppData, int *pDataSize)
292 if (!pFileName || !ppData || !pDataSize) {
293 MSG_ERR("Invalid params");
297 MSG_DEBUG("MsgOpenAndReadFile");
301 char fullPath[MAX_FULL_PATH_SIZE] = {0};
303 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
304 MSG_SEC_DEBUG("open file name: %s", fullPath);
307 if (stat(fullPath, &st) != 0) {
308 MSG_SEC_ERR("stat(%s, &st) != 0", fullPath);
311 if (S_ISDIR(st.st_mode)) {
312 MSG_ERR("S_ISDIR(st.st_mode)");
316 pFile = MsgOpenFile(fullPath, "rb");
319 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
323 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
325 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
329 int FileSize = MsgFtell(pFile);
332 MSG_DEBUG("Filesize is error : %d", FileSize);
338 *ppData = new char[FileSize+1];
339 memset(*ppData, 0x00, (FileSize+1));
341 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
343 MSG_DEBUG("File seek Error: %s", g_strerror(errno));
347 if (MsgReadFile(*ppData, sizeof(char), FileSize, pFile) != (size_t)FileSize) {
349 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
353 *pDataSize = FileSize;
361 bool MsgReadFileForDecode(FILE *pFile, char *pBuf, int length, int *nSize)
365 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
366 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
371 *nSize = MsgReadFile(pBuf, sizeof(char), length, pFile);
378 bool MsgWriteIpcFile(const char *pFileName, const char *pData, int DataSize)
381 MSG_DEBUG("NULL parameter, pFileName [%p], pData [%p]", pFileName, pData);
385 char fullPath[MAX_FULL_PATH_SIZE] = {0};
387 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
389 FILE *pFile = MsgOpenFile(fullPath, "wb+");
392 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
396 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
398 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
402 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
404 MSG_DEBUG("File Write Error: %s", g_strerror(errno));
414 int MsgReadSmilFile(const char *pFileName, char **ppData)
417 MSG_DEBUG("pFileName is NULL");
422 char fullPath[MAX_FULL_PATH_SIZE] = {0};
424 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_SMIL_FILE_PATH, pFileName);
426 MSG_SEC_DEBUG("open file name: %s", fullPath);
428 FILE *pFile = MsgOpenFile(fullPath, "rb");
431 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
435 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
437 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
441 int FileSize = MsgFtell(pFile);
444 MSG_DEBUG("Filesize is error : %d", FileSize);
449 *ppData = new char[FileSize + 1];
450 memset(*ppData, 0x00, (FileSize+1));
452 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
454 MSG_DEBUG("File Sead Error: %s", g_strerror(errno));
458 if (MsgReadFile(*ppData, sizeof(char), FileSize, pFile) != (size_t)FileSize) {
460 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
464 /* ppData[FileSize] = '\0'; */
473 bool MsgWriteSmilFile(const char *pFilePath, char *pData, int DataSize)
476 MSG_DEBUG("pFilePath is NULL");
481 if (mkdir(MSG_SMIL_FILE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
482 if (errno == EEXIST) {
483 MSG_SEC_DEBUG("The %s already exists", MSG_SMIL_FILE_PATH);
485 MSG_SEC_DEBUG("Error while mkdir %s", MSG_SMIL_FILE_PATH);
490 FILE *pFile = MsgOpenFile(pFilePath, "wb+");
493 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
497 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
499 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
503 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
505 MSG_DEBUG("File Write Error: %s", g_strerror(errno));
516 void MsgDeleteFile(const char *pFileName)
519 MSG_FATAL("pFileName is NULL");
523 if (strlen(pFileName) == 0) {
524 MSG_FATAL("pFileName has zero length");
528 char fullPath[MAX_FULL_PATH_SIZE] = {0};
531 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
533 MSG_SEC_DEBUG("%s", fullPath);
535 if (remove(fullPath) != 0)
536 MSG_SEC_ERR("File Delete Error [%s]: %s", fullPath, g_strerror(errno));
537 } catch (exception &e) {
538 MSG_FATAL("%s", e.what());
543 void MsgDeleteSmilFile(const char *pFileName)
546 MSG_FATAL("pFileName NULL");
551 char fullPath[MAX_FULL_PATH_SIZE] = {0};
553 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_SMIL_FILE_PATH, pFileName);
555 if (remove(fullPath) != 0)
556 MSG_SEC_ERR("File Delete Error [%s]: %s", fullPath, g_strerror(errno));
557 } catch (exception &e) {
558 MSG_FATAL("%s", e.what());
563 bool MsgGetFileSize(const char *pFilePath, int *nSize)
566 MSG_FATAL("pFileName NULL");
572 pFile = MsgOpenFile(pFilePath, "rb");
575 MSG_DEBUG("File Open error: %s", g_strerror(errno));
579 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
581 MSG_FATAL("File Read Error: %s", g_strerror(errno));
585 *nSize = MsgFtell(pFile);
593 FILE *MsgOpenMMSFile(char *pFileName)
598 MSG_DEBUG("pFileName NULL: %s", g_strerror(errno));
602 len = strlen(pFileName);
604 for (int i = 0; i < len; i++) {
605 switch (pFileName[i]) {
612 MSG_SEC_DEBUG("pFileName = %s", pFileName);
614 char fullPath[MAX_FULL_PATH_SIZE+1] = {0};
616 snprintf(fullPath, MAX_FULL_PATH_SIZE+1, "%s.mms", pFileName);
618 FILE *pFile = MsgOpenFile(fullPath, "wb+");
621 MSG_ERR("File Open Error: %s", g_strerror(errno));
625 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
627 MSG_ERR("File Read Error: %s", g_strerror(errno));
635 bool MsgWriteDataFromEncodeBuffer(FILE *pFile, char *pInBuffer, int *pPtr, int maxLen, int *pOffset )
637 if (!pFile || !pPtr || !pInBuffer || !pOffset) {
638 MSG_FATAL(" NULL parameter passed");
642 MSG_DEBUG("MsgWriteDataFromEncodeBuffer:");
643 MSG_DEBUG("pInBuffer %s", pInBuffer);
644 MSG_DEBUG("pPtr %d", (*pPtr));
645 MSG_DEBUG("before to fwite %p", pFile);
647 if (MsgWriteFile(pInBuffer, sizeof(char), (*pPtr), pFile) != (size_t)(*pPtr)) {
648 MSG_FATAL("MsgWriteFile failed");
652 MSG_DEBUG("after to fwite \n");
656 memset(pInBuffer, 0, maxLen);
660 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
661 MSG_FATAL("MsgFseek failed");
665 *pOffset = MsgFtell(pFile);
667 if (*pOffset == -1L) {
668 MSG_FATAL("MsgFtell failed");
676 bool MsgOpenCreateAndOverwriteFile(char *pFullPath, char *pBuff, int TotalLength)
680 if ((pFile = MsgOpenFile(pFullPath, "wb+")) == NULL) {
681 MSG_FATAL("MsgOpenFile errer");
685 if (MsgWriteFile(pBuff, sizeof(char), TotalLength, pFile) != (size_t)TotalLength) {
690 MsgFsync(pFile); /* file is written to device immediately, it prevents missing file data from unexpected power off */
698 char *MsgOpenAndReadMmsFile(const char *szFilePath, int offset, int size, int *npSize)
704 if (szFilePath == NULL) {
705 MSG_ERR("szFilePath id NULL");
711 pFile = MsgOpenFile(szFilePath, "rb");
714 MSG_ERR("Can't open file: %s", g_strerror(errno));
719 if (MsgGetFileSize(szFilePath, & readSize) == false) {
720 MSG_DEBUG("MsgGetFileSize: failed");
726 /* restore Kies backup data size greater than FM_READ_WRITE_BUFFER_MAX */
728 if (readSize > FM_READ_WRITE_BUFFER_MAX) {
729 MSG_DEBUG("MsgOpenAndReadMmsFile: File size tried to read too big");
734 pData = (char *)calloc(1, readSize + 1);
735 if ( NULL == pData ) {
736 MSG_ERR("pData MemAlloc Fail : %s", g_strerror(errno) );
740 if (MsgFseek(pFile, offset, SEEK_SET) < 0) {
741 MSG_ERR("FmSeekFile failed : %s", g_strerror(errno) );
745 *npSize = MsgReadFile(pData, sizeof(char), readSize, pFile);
751 *(pData + (*npSize)) = '\0';
772 /* it is equivalent to "rm -rf pDirPath" */
773 int MsgRmRf(char *pDirPath)
775 struct dirent *d = NULL;
778 dir = opendir(pDirPath);
781 MSG_FATAL("error opendir: %s", g_strerror(errno));
785 int size = strlen(pDirPath) + 256;
787 char *path = (char*)malloc(size);
790 MSG_DEBUG("path is NULL");
798 while ((d = readdir(dir)) != NULL) {
799 if (d->d_type == DT_DIR) {
800 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
802 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
807 if (rmdir(path) != 0) {
813 MSG_FATAL("error rmdir: %s", g_strerror(errno));
818 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
820 if (MsgDrmIsDrmFile(path))
821 MsgDrmUnregisterFile(path);
823 if (remove(path) != 0) {
829 MSG_FATAL("error remove: %s", g_strerror(errno));
849 int MsgGetFileSize(const char *pFileName)
851 struct stat file_stat;
853 if (lstat(pFileName, &file_stat)) {
854 MSG_FATAL("error lstat: %s", g_strerror(errno));
858 return file_stat.st_size;
862 /* it is equivalent to "du dir_path" */
863 unsigned int MsgDu(const char *pDirPath)
865 struct dirent *d = NULL;
868 dir = opendir(pDirPath);
871 MSG_FATAL("error opendir: %s", g_strerror(errno));
875 int size = strlen(pDirPath) + 256;
876 char *path = (char*)malloc(size);
884 unsigned int totalFileSize = 0;
887 while ((d = readdir(dir)) != NULL) {
888 if(d->d_type == DT_DIR) {
889 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
891 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
894 unsigned int dirSize = MsgDu(path);
897 MSG_FATAL("error MsgDu");
903 totalFileSize += dirSize;
905 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
906 int fileSize = MsgGetFileSize(path);
909 MSG_FATAL("error MsgGetFileSize");
915 totalFileSize += fileSize;
927 return totalFileSize;
931 bool MsgAppendFile(const char *pFilePath, const char *pData, int DataSize)
934 MSG_FATAL("NULL check error, pFileName %p, pData %p", pFilePath, pData);
938 char fullPath[MAX_FULL_PATH_SIZE] = {0};
940 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s", pFilePath);
942 FILE *pFile = MsgOpenFile(fullPath, "a+");
945 MSG_FATAL("File Open Error: %s", g_strerror(errno));
949 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
951 MSG_FATAL("File Sead Error: %s", g_strerror(errno));
955 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
957 MSG_FATAL("File Write Error: %s", g_strerror(errno));
961 MsgFsync(pFile); /*file is written to device immediately, it prevents missing file data from unexpected power off */
969 struct dirent *d = NULL;
972 dir = opendir(MSG_DATA_PATH);
975 MSG_FATAL("error opendir: %s", g_strerror(errno));
979 /* Remove temporal Mms folder */
980 while ((d = readdir(dir)) != NULL) {
981 if (d->d_type == DT_DIR) {
982 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
985 if(strstr(d->d_name, ".dir") != NULL) {
986 char filePath[MSG_FILEPATH_LEN_MAX] = {0, };
987 snprintf(filePath, MSG_FILEPATH_LEN_MAX, "%s%s", MSG_DATA_PATH, d->d_name);
999 /* mode : R_OK, W_OK, X_OK, or the existence test F_OK. */
1000 bool MsgAccessFile(const char *filepath, int mode)
1003 if (filepath == NULL) {
1004 MSG_DEBUG("filepath is NULL");
1008 MSG_SEC_DEBUG("request access path = %s, mode = %d", filepath, mode);
1010 ret = access(filepath, mode);
1014 MSG_DEBUG("Fail to access file, ret = %d", ret);
1022 bool MsgChmod(const char *filepath, int mode)
1025 struct stat lstat_info;
1026 struct stat fstat_info;
1029 if (lstat(filepath, &lstat_info) == -1) {
1030 MSG_SEC_DEBUG("No such file as [%s].", filepath);
1034 fd = open(filepath, O_RDONLY);
1037 MSG_SEC_DEBUG("Fail to open [%s].", filepath);
1041 if (fstat(fd, &fstat_info) == -1) {
1042 MSG_SEC_DEBUG("Fail to fstat [%s].", filepath);
1047 if (lstat_info.st_mode == fstat_info.st_mode &&
1048 lstat_info.st_ino == fstat_info.st_ino &&
1049 lstat_info.st_dev == fstat_info.st_dev) {
1050 if (fchmod(fd, mode) < 0) {
1051 MSG_SEC_DEBUG("Fail to fchmod [%s].", filepath);
1063 bool MsgChown(const char *filepath, int uid, int gid)
1065 struct stat lstat_info;
1066 struct stat fstat_info;
1069 if (lstat(filepath, &lstat_info) == -1) {
1070 MSG_SEC_INFO("No such file as [%s].", filepath);
1074 fd = open(filepath, O_RDONLY);
1077 MSG_SEC_INFO("Fail to open [%s].", filepath);
1081 if (fstat(fd, &fstat_info) == -1) {
1082 MSG_SEC_INFO("Fail to fstat [%s].", filepath);
1087 if (lstat_info.st_mode == fstat_info.st_mode &&
1088 lstat_info.st_ino == fstat_info.st_ino &&
1089 lstat_info.st_dev == fstat_info.st_dev) {
1090 if (fchown(fd, uid, gid) < 0) {
1091 MSG_SEC_INFO("Fail to fchown [%s].", filepath);
1101 bool MsgCreateFile(const char *pFilePath, char *pData, int DataSize)
1104 MSG_DEBUG("pFilePath is NULL");
1108 FILE *pFile = MsgOpenFile(pFilePath, "wb+");
1110 if (pFile == NULL) {
1111 MSG_ERR("File Open Error: %s", g_strerror(errno));
1115 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
1116 MsgCloseFile(pFile);
1117 MSG_ERR("File Seek Error: %s", g_strerror(errno));
1121 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
1122 MsgCloseFile(pFile);
1123 MSG_ERR("File Write Error: %s", g_strerror(errno));
1129 MsgCloseFile(pFile);
1134 char *MsgGetDirName(char *file_path)
1136 return g_path_get_dirname(file_path);
1140 char *MsgGetFileName(char *file_path)
1142 return g_path_get_basename(file_path);
1146 int MsgCheckFilepathSmack(const char *app_smack_label, char *file_path)
1148 int err = MSG_SUCCESS;
1150 char *path_smack_label = NULL;
1151 char *dir_smack_label = NULL;
1152 char *dir_name = NULL;
1154 if (!file_path || file_path[0] == '\0') {
1159 if (stat(file_path, &st) != 0) {
1160 MSG_SEC_ERR("stat(%s, &st) != 0", file_path);
1161 return MSG_ERR_PERMISSION_DENIED;
1163 if (S_ISDIR(st.st_mode)) {
1164 MSG_ERR("S_ISDIR(st.st_mode)");
1165 return MSG_ERR_INVALID_PARAMETER;
1168 dir_name = MsgGetDirName(file_path);
1169 if (!dir_name || !g_strcmp0(dir_name, file_path)) {
1170 MSG_SEC_ERR("!dir_name || !g_strcmp0(dir_name, %s)", file_path);
1171 err = MSG_ERR_INVALID_PARAMETER;
1175 smack_getlabel(dir_name, &dir_smack_label, SMACK_LABEL_ACCESS);
1176 if (dir_smack_label == NULL) {
1177 MSG_ERR("smack_getlabel failed (dir_smack_label)");
1178 err = MSG_ERR_PERMISSION_DENIED;
1182 if (smack_have_access(app_smack_label, dir_smack_label, "RX") < 1) {
1183 MSG_ERR("smack_have_access failed (dir_smack_label)");
1184 err = MSG_ERR_PERMISSION_DENIED;
1188 smack_getlabel(file_path, &path_smack_label, SMACK_LABEL_ACCESS);
1189 if (path_smack_label == NULL) {
1190 MSG_ERR("smack_getlabel failed (path_smack_label)");
1191 err = MSG_ERR_PERMISSION_DENIED;
1195 if (smack_have_access(app_smack_label, path_smack_label, "R") < 1) {
1196 MSG_ERR("smack_have_access failed (path_smack_label)");
1197 err = MSG_ERR_PERMISSION_DENIED;
1201 MSG_DEBUG("smack_have_access pass successfully");
1204 MSG_FREE(path_smack_label);
1205 MSG_FREE(dir_smack_label);
1211 void MsgGetMimeType(char *filePath, char *mimeType, int size)
1213 aul_get_mime_from_file(filePath, mimeType, size);
1216 int MsgTcsScanFile(const char *filepath, int *bLevel)
1220 int (*_csr_cs_context_create)(void **handle);
1221 int (*_csr_cs_scan_file)(void *handle, const char *filepath, void **malware);
1222 int (*_csr_cs_context_destroy)(void *handle);
1223 int (*_csr_cs_malware_get_severity)(void *malware, void *severity);
1224 int (*_csr_cs_malware_get_name)(void *malware, char **name);
1226 if (MsgAccessFile(filepath, R_OK) == false) {
1227 MSG_SEC_DEBUG("not exist source file [%s]", filepath);
1231 MSG_SEC_DEBUG("Scanning file name : %s\n", filepath);
1233 void *lib_handle = NULL;
1234 lib_handle = dlopen(PATH_LIBCSR_CLIENT, RTLD_LAZY);
1236 MSG_ERR("Unable to open %s", PATH_LIBCSR_CLIENT);
1240 _csr_cs_context_create = (int(*)(void**))dlsym(lib_handle, "csr_cs_context_create");
1241 _csr_cs_scan_file = (int(*)(void*, const char*, void**))dlsym(lib_handle, "csr_cs_scan_file");
1242 _csr_cs_context_destroy = (int(*)(void*))dlsym(lib_handle, "csr_cs_context_destroy");
1243 _csr_cs_malware_get_severity = (int(*)(void*, void*))dlsym(lib_handle, "csr_cs_malware_get_severity");
1244 _csr_cs_malware_get_name = (int(*)(void*, char**))dlsym(lib_handle, "csr_cs_malware_get_name");
1247 if (!_csr_cs_context_create || !_csr_cs_scan_file || !_csr_cs_context_destroy
1248 || !_csr_cs_malware_get_severity || !_csr_cs_malware_get_name) {
1249 MSG_ERR("Failed to load CSR symbols");
1251 dlclose(lib_handle);
1255 void *csr_handle = NULL;
1256 ret = _csr_cs_context_create(&csr_handle);
1258 MSG_DEBUG("csr_cs_context_create error: err = %d\n", ret);
1260 dlclose(lib_handle);
1265 void *detected = NULL;
1266 ret = _csr_cs_scan_file(csr_handle, filepath, &detected);
1268 MSG_DEBUG("csr_cs_scan_file fail: err = %d\n", ret);
1272 if (NULL == detected) {
1273 MSG_DEBUG("Nothing detected");
1277 int severity = 0x01; /* CSR_CS_SEVERITY_LOW */
1278 ret = _csr_cs_malware_get_severity(detected, &severity);
1280 MSG_DEBUG("csr_cs_malware_get_severity error: err = %d\n", ret);
1282 MSG_DEBUG(" +-- Malware Severity class: %d\n", severity);
1285 ret = _csr_cs_malware_get_name(detected, &name);
1287 MSG_DEBUG("csr_cs_malware_get_name error: err = %d\n", ret);
1289 MSG_SEC_DEBUG(" +-- Malware Name: [%s]\n", name);
1299 ret = _csr_cs_context_destroy(csr_handle);
1301 MSG_DEBUG("csr_cs_context_destroy error: err = %d\n", ret);
1304 dlclose(lib_handle);