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>
30 #include <thumbnail_util.h>
31 #include <image_util.h>
33 #include "MsgStorageTypes.h"
35 #include "MsgException.h"
36 #include "MsgUtilFile.h"
37 #include "MsgMmsTypes.h"
38 #include "MsgInternalTypes.h"
39 #include "MsgDrmWrapper.h"
49 void thumbnail_completed_cb(thumbnail_util_error_e error, const char *request_id,
50 int thumb_width, int thumb_height,
51 unsigned char *thumb_data, int thumb_size, void *user_data)
58 MSG_WARN("dstPath is NULL");
64 MSG_DEBUG("=================[RESULT]");
65 MSG_DEBUG("error_code [%d]", error);
66 MSG_DEBUG("request id [%s]", request_id);
67 MSG_DEBUG("width [%d], height [%d]", thumb_width, thumb_height);
68 MSG_DEBUG("size [%d]", thumb_size);
71 ret = image_util_encode_jpeg(thumb_data, thumb_width, thumb_height, IMAGE_UTIL_COLORSPACE_BGRA8888, 100, (char *)user_data);
72 if (ret != IMAGE_UTIL_ERROR_NONE)
73 MSG_WARN("image_util_encode_jpeg() is failed");
81 /*==================================================================================================
82 FUNCTION IMPLEMENTATION
83 ==================================================================================================*/
84 bool MakeThumbnail(char *srcPath, char *dstPath)
86 if (srcPath == NULL || dstPath == NULL) {
87 MSG_SEC_DEBUG("Invalid Param src = %p, dst = %p", srcPath, dstPath);
91 if (MsgAccessFile(srcPath, R_OK) == false) {
92 MSG_SEC_DEBUG("not exist source file [%s]", srcPath);
100 int ret = THUMBNAIL_UTIL_ERROR_NONE;
103 thumbnail_util_create(&thumb_h);
104 thumbnail_util_set_path(thumb_h, srcPath);
106 ret = thumbnail_util_extract(thumb_h, thumbnail_completed_cb, dstPath, &req_id);
107 thumbnail_util_destroy(thumb_h);
113 if (ret != THUMBNAIL_UTIL_ERROR_NONE) {
114 MSG_ERR("thumbnail_util_extract is failed");
119 time_ret = g_cv.timedwait(g_mx.pMsgMutex(), 5);
123 if (time_ret == ETIMEDOUT) {
124 MSG_ERR("@@ WAKE by timeout@@");
128 if (MsgAccessFile(dstPath, F_OK) == false) {
129 MSG_SEC_DEBUG("not exist result file [%s]", dstPath);
133 MSG_SEC_DEBUG("Make thumbnail: success [%s]", dstPath);
137 /* File operation wrappers */
138 FILE *MsgOpenFile(const char *filepath, const char *opt)
140 if (!filepath || !opt) {
141 MSG_FATAL("Null parameter");
145 MSG_SEC_DEBUG("[FILE] filepath : [%s], opt [%s]", filepath, opt);
150 pFile = fopen(filepath, opt);
151 MSG_DEBUG("[FILE] pFile [%p]", pFile);
152 } catch (exception &e) {
153 MSG_FATAL("%s", e.what());
160 void MsgCloseFile(FILE *pFile)
163 MSG_FATAL("NULL parameter");
167 MSG_DEBUG("[FILE] pFile [%p]", pFile);
171 } catch (exception &e) {
172 MSG_FATAL("%s", e.what());
176 int MsgFseek(FILE *pFile, long int offset, int origin)
179 MSG_FATAL("pFile NULL");
185 MSG_DEBUG("[FILE] pFile [%p], offset [%d], origin [%d]", pFile, offset, origin);
188 ret = fseek(pFile, offset, origin); /* return 0, if success. */
189 } catch (exception &e) {
190 MSG_FATAL("%s", e.what());
197 size_t MsgWriteFile(const char *pData, size_t size, size_t count, FILE *pFile)
199 if (!pData || !pFile) {
200 MSG_FATAL("pData or pFile NULL");
206 MSG_DEBUG("[FILE] pData [%p], size [%d], count [%d], pFile [%p]", pData, size, count, pFile);
209 nWrite = fwrite(pData, size, count, pFile);
210 } catch (exception &e) {
211 MSG_FATAL("%s", e.what());
217 size_t MsgReadFile(void *pData, size_t size, size_t count, FILE *pFile)
219 if (!pData || !pFile) {
220 MSG_FATAL("pData or pFile NULL");
227 nRead = fread(pData, size, count, pFile);
228 } catch (exception &e) {
229 MSG_FATAL("%s", e.what());
235 long int MsgFtell(FILE *pFile)
238 MSG_FATAL("pFile NULL");
242 long int ret = -1L; /* -1L return if error occured. */
246 } catch (exception &e) {
247 MSG_FATAL("%s", e.what());
253 int MsgFflush(FILE *pFile)
256 MSG_FATAL("pFile NULL");
263 ret = fflush(pFile); /* return 0 if success */
264 } catch (exception &e) {
265 MSG_FATAL("%s", e.what());
271 int MsgFsync(FILE *pFile)
274 MSG_FATAL("pFile NULL");
281 ret = fdatasync(pFile->_fileno); /* return 0 if success */
282 } catch (exception &e) {
283 MSG_FATAL("%s", e.what());
289 bool MsgCreateFileName(char *pFileName)
291 if (pFileName == NULL) {
292 MSG_DEBUG("[ERROR] pFileName is NULL");
299 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
300 MSG_DEBUG("clock_gettime() error: %s", g_strerror(errno));
304 /* Create Random Number */
305 srandom((unsigned int)ts.tv_nsec);
307 MSG_DEBUG("ts.tv_nsec : %d", ts.tv_nsec);
309 /* between 1 - 1000000000 */
310 snprintf(pFileName, MSG_FILENAME_LEN_MAX, "MSG_%lu.DATA", random()%1000000000+1);
311 } catch (exception& e) {
312 MSG_FATAL("%s", e.what());
320 bool MsgOpenAndReadFile(const char *pFileName, char **ppData, int *pDataSize)
322 if (!pFileName || !ppData || !pDataSize) {
323 MSG_ERR("Invalid params!! pFileName=%x, ppData=%x, pDataSize=%x", pFileName, ppData, pDataSize);
327 MSG_DEBUG("MsgOpenAndReadFile");
331 char fullPath[MAX_FULL_PATH_SIZE] = {0};
333 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
334 MSG_SEC_DEBUG("open file name: %s", fullPath);
336 pFile = MsgOpenFile(fullPath, "rb");
339 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
343 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
345 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
349 int FileSize = MsgFtell(pFile);
352 MSG_DEBUG("Filesize is error : %d", FileSize);
358 *ppData = new char[FileSize+1];
359 memset(*ppData, 0x00, (FileSize+1));
361 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
363 MSG_DEBUG("File seek Error: %s", g_strerror(errno));
367 if (MsgReadFile(*ppData, sizeof(char), FileSize, pFile) != (size_t)FileSize) {
369 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
373 *pDataSize = FileSize;
381 bool MsgReadFileForDecode(FILE *pFile, char *pBuf, int length, int *nSize)
385 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
386 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
391 *nSize = MsgReadFile(pBuf, sizeof(char), length, pFile);
398 bool MsgWriteIpcFile(const char *pFileName, const char *pData, int DataSize)
401 MSG_DEBUG("NULL parameter, pFileName [%p], pData [%p]", pFileName, pData);
405 char fullPath[MAX_FULL_PATH_SIZE] = {0};
407 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
409 FILE *pFile = MsgOpenFile(fullPath, "wb+");
412 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
416 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
418 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
422 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
424 MSG_DEBUG("File Write Error: %s", g_strerror(errno));
434 int MsgReadSmilFile(const char *pFileName, char **ppData)
437 MSG_DEBUG("pFileName is NULL");
442 char fullPath[MAX_FULL_PATH_SIZE] = {0};
444 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_SMIL_FILE_PATH, pFileName);
446 MSG_SEC_DEBUG("open file name: %s", fullPath);
448 FILE *pFile = MsgOpenFile(fullPath, "rb");
451 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
455 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
457 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
461 int FileSize = MsgFtell(pFile);
464 MSG_DEBUG("Filesize is error : %d", FileSize);
469 *ppData = new char[FileSize + 1];
470 memset(*ppData, 0x00, (FileSize+1));
472 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
474 MSG_DEBUG("File Sead Error: %s", g_strerror(errno));
478 if (MsgReadFile(*ppData, sizeof(char), FileSize, pFile) != (size_t)FileSize) {
480 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
484 /* ppData[FileSize] = '\0'; */
493 bool MsgWriteSmilFile(const char *pFilePath, char *pData, int DataSize)
496 MSG_DEBUG("pFilePath is NULL");
501 if (mkdir(MSG_SMIL_FILE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
502 if (errno == EEXIST) {
503 MSG_SEC_DEBUG("The %s already exists", MSG_SMIL_FILE_PATH);
505 MSG_SEC_DEBUG("Error while mkdir %s", MSG_SMIL_FILE_PATH);
510 FILE *pFile = MsgOpenFile(pFilePath, "wb+");
513 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
517 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
519 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
523 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
525 MSG_DEBUG("File Write Error: %s", g_strerror(errno));
536 void MsgDeleteFile(const char *pFileName)
539 MSG_FATAL("pFileName is NULL");
543 if (strlen(pFileName) == 0) {
544 MSG_FATAL("pFileName has zero length");
548 char fullPath[MAX_FULL_PATH_SIZE] = {0};
551 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
553 MSG_SEC_DEBUG("%s", fullPath);
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 void MsgDeleteSmilFile(const char *pFileName)
566 MSG_FATAL("pFileName NULL");
571 char fullPath[MAX_FULL_PATH_SIZE] = {0};
573 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_SMIL_FILE_PATH, pFileName);
575 if (remove(fullPath) != 0)
576 MSG_SEC_ERR("File Delete Error [%s]: %s", fullPath, g_strerror(errno));
577 } catch (exception &e) {
578 MSG_FATAL("%s", e.what());
583 bool MsgGetFileSize(const char *pFilePath, int *nSize)
586 MSG_FATAL("pFileName NULL");
592 pFile = MsgOpenFile(pFilePath, "rb");
595 MSG_DEBUG("File Open error: %s", g_strerror(errno));
599 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
601 MSG_FATAL("File Read Error: %s", g_strerror(errno));
605 *nSize = MsgFtell(pFile);
613 FILE *MsgOpenMMSFile(char *pFileName)
618 MSG_DEBUG("pFileName NULL: %s", g_strerror(errno));
622 len = strlen(pFileName);
624 for (int i = 0; i < len; i++) {
625 switch (pFileName[i]) {
632 MSG_SEC_DEBUG("pFileName = %s", pFileName);
634 char fullPath[MAX_FULL_PATH_SIZE+1] = {0};
636 snprintf(fullPath, MAX_FULL_PATH_SIZE+1, "%s.mms", pFileName);
638 FILE *pFile = MsgOpenFile(fullPath, "wb+");
641 MSG_ERR("File Open Error: %s", g_strerror(errno));
645 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
647 MSG_ERR("File Read Error: %s", g_strerror(errno));
655 bool MsgWriteDataFromEncodeBuffer(FILE *pFile, char *pInBuffer, int *pPtr, int maxLen, int *pOffset )
657 if (!pFile || !pPtr || !pInBuffer || !pOffset) {
658 MSG_FATAL(" NULL parameter passed");
662 MSG_DEBUG("MsgWriteDataFromEncodeBuffer:");
663 MSG_DEBUG("pInBuffer %x", pInBuffer);
664 MSG_DEBUG("pPtr %d", (*pPtr));
665 MSG_DEBUG("before to fwite %x", pFile);
667 if (MsgWriteFile(pInBuffer, sizeof(char), (*pPtr), pFile) != (size_t)(*pPtr)) {
668 MSG_FATAL("MsgWriteFile failed");
672 MSG_DEBUG("after to fwite \n");
676 memset(pInBuffer, 0, maxLen);
680 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
681 MSG_FATAL("MsgFseek failed");
685 *pOffset = MsgFtell(pFile);
687 if (*pOffset == -1L) {
688 MSG_FATAL("MsgFtell failed");
696 bool MsgOpenCreateAndOverwriteFile(char *pFullPath, char *pBuff, int TotalLength)
700 if ((pFile = MsgOpenFile(pFullPath, "wb+")) == NULL) {
701 MSG_FATAL("MsgOpenFile errer");
705 if (MsgWriteFile(pBuff, sizeof(char), TotalLength, pFile) != (size_t)TotalLength) {
710 MsgFsync(pFile); /* file is written to device immediately, it prevents missing file data from unexpected power off */
718 char *MsgOpenAndReadMmsFile(const char *szFilePath, int offset, int size, int *npSize)
724 if (szFilePath == NULL) {
725 MSG_ERR("szFilePath id NULL");
731 pFile = MsgOpenFile(szFilePath, "rb");
734 MSG_ERR("Can't open file: %s", g_strerror(errno));
739 if (MsgGetFileSize(szFilePath, & readSize) == false) {
740 MSG_DEBUG("MsgGetFileSize: failed");
746 /* restore Kies backup data size greater than FM_READ_WRITE_BUFFER_MAX */
748 if (readSize > FM_READ_WRITE_BUFFER_MAX) {
749 MSG_DEBUG("MsgOpenAndReadMmsFile: File size tried to read too big");
754 pData = (char *)calloc(1, readSize + 1);
755 if ( NULL == pData ) {
756 MSG_ERR("pData MemAlloc Fail : %s", g_strerror(errno) );
760 if (MsgFseek(pFile, offset, SEEK_SET) < 0) {
761 MSG_ERR("FmSeekFile failed : %s", g_strerror(errno) );
765 *npSize = MsgReadFile(pData, sizeof(char), readSize, pFile);
771 *(pData + (*npSize)) = '\0';
792 /* it is equivalent to "rm -rf pDirPath" */
793 int MsgRmRf(char *pDirPath)
795 struct dirent *d = NULL;
799 dir = opendir(pDirPath);
802 MSG_FATAL("error opendir: %s", g_strerror(errno));
806 int size = strlen(pDirPath) + 256;
808 char *path = (char*)malloc(size);
811 MSG_DEBUG("path is NULL");
818 for (readdir_r(dir, &entry, &d); d != NULL; readdir_r(dir, &entry, &d)) {
819 if (d->d_type == DT_DIR) {
820 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
822 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
827 if (rmdir(path) != 0) {
833 MSG_FATAL("error rmdir: %s", g_strerror(errno));
838 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
840 if (MsgDrmIsDrmFile(path))
841 MsgDrmUnregisterFile(path);
843 if (remove(path) != 0) {
849 MSG_FATAL("error remove: %s", g_strerror(errno));
866 int MsgGetFileSize(const char *pFileName)
868 struct stat file_stat;
870 if (lstat(pFileName, &file_stat)) {
871 MSG_FATAL("error lstat: %s", g_strerror(errno));
875 return file_stat.st_size;
879 /* it is equivalent to "du dir_path" */
880 unsigned int MsgDu(const char *pDirPath)
882 struct dirent *d = NULL;
886 dir = opendir(pDirPath);
889 MSG_FATAL("error opendir: %s", g_strerror(errno));
893 int size = strlen(pDirPath) + 256;
894 char *path = (char*)malloc(size);
902 unsigned int totalFileSize = 0;
904 for (readdir_r(dir, &entry, &d); d != NULL; readdir_r(dir, &entry, &d)) {
905 if(d->d_type == DT_DIR) {
906 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
908 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
911 unsigned int dirSize = MsgDu(path);
914 MSG_FATAL("error MsgDu");
920 totalFileSize += dirSize;
922 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
923 int fileSize = MsgGetFileSize(path);
926 MSG_FATAL("error MsgGetFileSize");
932 totalFileSize += fileSize;
941 return totalFileSize;
945 bool MsgAppendFile(const char *pFilePath, const char *pData, int DataSize)
948 MSG_FATAL("NULL check error, pFileName %p, pData %p", pFilePath, pData);
952 char fullPath[MAX_FULL_PATH_SIZE] = {0};
954 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s", pFilePath);
956 FILE *pFile = MsgOpenFile(fullPath, "a+");
959 MSG_FATAL("File Open Error: %s", g_strerror(errno));
963 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
965 MSG_FATAL("File Sead Error: %s", g_strerror(errno));
969 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
971 MSG_FATAL("File Write Error: %s", g_strerror(errno));
975 MsgFsync(pFile); /*file is written to device immediately, it prevents missing file data from unexpected power off */
983 struct dirent *d = NULL;
987 dir = opendir(MSG_DATA_PATH);
990 MSG_FATAL("error opendir: %s", g_strerror(errno));
994 /* Remove temporal Mms folder */
995 for (readdir_r(dir, &entry, &d); d != NULL; readdir_r(dir, &entry, &d)) {
996 if (d->d_type == DT_DIR) {
997 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
1000 if(strstr(d->d_name, ".dir") != NULL) {
1001 char filePath[MSG_FILEPATH_LEN_MAX] = {0, };
1002 snprintf(filePath, MSG_FILEPATH_LEN_MAX, "%s%s", MSG_DATA_PATH, d->d_name);
1014 /* mode : R_OK, W_OK, X_OK, or the existence test F_OK. */
1015 bool MsgAccessFile(const char *filepath, int mode)
1018 if (filepath == NULL) {
1019 MSG_DEBUG("filepath is NULL");
1023 MSG_SEC_DEBUG("request access path = %s, mode = %d", filepath, mode);
1025 ret = access(filepath, mode);
1029 MSG_DEBUG("Fail to access file, ret = %d", ret);
1037 bool MsgChmod(const char *filepath, int mode)
1040 struct stat lstat_info;
1041 struct stat fstat_info;
1044 if (lstat(filepath, &lstat_info) == -1) {
1045 MSG_SEC_DEBUG("No such file as [%s].", filepath);
1049 fd = open(filepath, O_RDONLY);
1052 MSG_SEC_DEBUG("Fail to open [%s].", filepath);
1056 if (fstat(fd, &fstat_info) == -1) {
1057 MSG_SEC_DEBUG("Fail to fstat [%s].", filepath);
1062 if (lstat_info.st_mode == fstat_info.st_mode &&
1063 lstat_info.st_ino == fstat_info.st_ino &&
1064 lstat_info.st_dev == fstat_info.st_dev) {
1065 if (fchmod(fd, mode) < 0) {
1066 MSG_SEC_DEBUG("Fail to fchmod [%s].", filepath);
1078 bool MsgChown(const char *filepath, int uid, int gid)
1080 struct stat lstat_info;
1081 struct stat fstat_info;
1084 if (lstat(filepath, &lstat_info) == -1) {
1085 MSG_SEC_INFO("No such file as [%s].", filepath);
1089 fd = open(filepath, O_RDONLY);
1092 MSG_SEC_INFO("Fail to open [%s].", filepath);
1096 if (fstat(fd, &fstat_info) == -1) {
1097 MSG_SEC_INFO("Fail to fstat [%s].", filepath);
1102 if (lstat_info.st_mode == fstat_info.st_mode &&
1103 lstat_info.st_ino == fstat_info.st_ino &&
1104 lstat_info.st_dev == fstat_info.st_dev) {
1105 if (fchown(fd, uid, gid) < 0) {
1106 MSG_SEC_INFO("Fail to fchown [%s].", filepath);
1116 bool MsgCreateFile(const char *pFilePath, char *pData, int DataSize)
1119 MSG_DEBUG("pFilePath is NULL");
1123 FILE *pFile = MsgOpenFile(pFilePath, "wb+");
1125 if (pFile == NULL) {
1126 MSG_ERR("File Open Error: %s", g_strerror(errno));
1130 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
1131 MsgCloseFile(pFile);
1132 MSG_ERR("File Seek Error: %s", g_strerror(errno));
1136 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
1137 MsgCloseFile(pFile);
1138 MSG_ERR("File Write Error: %s", g_strerror(errno));
1144 MsgCloseFile(pFile);
1149 char *MsgGetDirName(char *file_path)
1151 return g_path_get_dirname(file_path);
1155 char *MsgGetFileName(char *file_path)
1157 return g_path_get_basename(file_path);
1161 int MsgCheckFilepathSmack(const char *app_smack_label, char *file_path)
1163 int err = MSG_SUCCESS;
1165 char *path_smack_label = NULL;
1166 char *dir_smack_label = NULL;
1167 char *dir_name = NULL;
1169 if (!file_path || file_path[0] == '\0') {
1174 if (stat(file_path, &st) != 0) {
1175 MSG_SEC_ERR("stat(%s, &st) != 0", file_path);
1176 return MSG_ERR_PERMISSION_DENIED;
1178 if (S_ISDIR(st.st_mode)) {
1179 MSG_ERR("S_ISDIR(st.st_mode)");
1180 return MSG_ERR_INVALID_PARAMETER;
1183 dir_name = MsgGetDirName(file_path);
1184 if (!dir_name || !g_strcmp0(dir_name, file_path)) {
1185 MSG_SEC_ERR("!dir_name || !g_strcmp0(dir_name, %s)", file_path);
1186 err = MSG_ERR_INVALID_PARAMETER;
1190 smack_getlabel(dir_name, &dir_smack_label, SMACK_LABEL_ACCESS);
1191 if (dir_smack_label == NULL) {
1192 MSG_ERR("smack_getlabel failed (dir_smack_label)");
1193 err = MSG_ERR_PERMISSION_DENIED;
1197 if (smack_have_access(app_smack_label, dir_smack_label, "RX") < 1) {
1198 MSG_ERR("smack_have_access failed (dir_smack_label)");
1199 err = MSG_ERR_PERMISSION_DENIED;
1203 smack_getlabel(file_path, &path_smack_label, SMACK_LABEL_ACCESS);
1204 if (path_smack_label == NULL) {
1205 MSG_ERR("smack_getlabel failed (path_smack_label)");
1206 err = MSG_ERR_PERMISSION_DENIED;
1210 if (smack_have_access(app_smack_label, path_smack_label, "R") < 1) {
1211 MSG_ERR("smack_have_access failed (path_smack_label)");
1212 err = MSG_ERR_PERMISSION_DENIED;
1216 MSG_DEBUG("smack_have_access pass successfully");
1219 MSG_FREE(path_smack_label);
1220 MSG_FREE(dir_smack_label);
1226 void MsgGetMimeType(char *filePath, char *mimeType, int size)
1228 aul_get_mime_from_file(filePath, mimeType, size);
1232 int MsgTcsScanFile(const char *filepath, int *bLevel)
1237 TCSScanResult result;
1238 TCSDetected* pDetected;
1240 int ret_b_level = -1;
1242 if (MsgAccessFile(filepath, R_OK) == false) {
1243 MSG_SEC_DEBUG("not exist source file [%s]", filepath);
1247 MSG_SEC_DEBUG("Scanning file name : %s\n", filepath);
1249 hLib = TCSLibraryOpen();
1250 if(hLib == INVALID_TCSLIB_HANDLE) {
1251 MSG_DEBUG("TCSLibraryOpen error\n");
1255 rtn = TCSScanFile(hLib, filepath, TCS_DTYPE_UNKNOWN, TCS_SA_SCANONLY, 1, &result);
1258 MSG_DEBUG("Detected malware number: %d\n", result.iNumDetected);
1259 i = result.iNumDetected;
1260 pDetected = result.pDList;
1261 while(i && pDetected)
1265 MSG_SEC_DEBUG(" +-- Malware [%d] Name: %s\n", i, pDetected->pszName);
1266 MSG_DEBUG(" +-- Malware [%d] uAction: %u : 0x%04x\n", i, pDetected->uAction, pDetected->uAction);
1268 temp_b_level = (pDetected->uAction & 0xFF00) >> 8;
1269 MSG_DEBUG(" +-- Malware [%d] Behavior level: %u\n", i, temp_b_level);
1271 if (ret_b_level == -1 || ret_b_level < temp_b_level) {
1272 ret_b_level = temp_b_level;
1275 temp_s_class = (pDetected->uAction & 0x00FF);
1276 MSG_DEBUG(" +-- Malware [%d] Severity class: %u\n", i, temp_s_class);
1278 pDetected = pDetected->pNext;
1282 result.pfFreeResult(&result);
1284 MSG_DEBUG("TCSScanFile fail: err = %d\n", rtn);
1287 TCSLibraryClose(hLib);
1290 *bLevel = ret_b_level;