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 <TCSErrorCodes.h>
35 #include "MsgStorageTypes.h"
37 #include "MsgException.h"
38 #include "MsgUtilFile.h"
39 #include "MsgMmsTypes.h"
40 #include "MsgInternalTypes.h"
41 #include "MsgDrmWrapper.h"
51 void thumbnail_completed_cb(thumbnail_util_error_e error, const char *request_id,
52 int thumb_width, int thumb_height,
53 unsigned char *thumb_data, int thumb_size, void *user_data)
60 MSG_WARN("dstPath is NULL");
66 MSG_DEBUG("=================[RESULT]");
67 MSG_DEBUG("error_code [%d]", error);
68 MSG_DEBUG("request id [%s]", request_id);
69 MSG_DEBUG("width [%d], height [%d]", thumb_width, thumb_height);
70 MSG_DEBUG("raw_data [0x%x], size [%d]", *thumb_data, thumb_size);
73 ret = image_util_encode_jpeg(thumb_data, thumb_width, thumb_height, IMAGE_UTIL_COLORSPACE_BGRA8888, 100, (char *)user_data);
74 if (ret != IMAGE_UTIL_ERROR_NONE)
75 MSG_WARN("image_util_encode_jpeg() is failed");
83 /*==================================================================================================
84 FUNCTION IMPLEMENTATION
85 ==================================================================================================*/
86 bool MakeThumbnail(char *srcPath, char *dstPath)
88 if (srcPath == NULL || dstPath == NULL) {
89 MSG_SEC_DEBUG("Invalid Param src = %p, dst = %p", srcPath, dstPath);
93 if (MsgAccessFile(srcPath, R_OK) == false) {
94 MSG_SEC_DEBUG("not exist source file [%s]", srcPath);
102 int ret = THUMBNAIL_UTIL_ERROR_NONE;
105 thumbnail_util_create(&thumb_h);
106 thumbnail_util_set_path(thumb_h, srcPath);
108 ret = thumbnail_util_extract(thumb_h, thumbnail_completed_cb, dstPath, &req_id);
109 thumbnail_util_destroy(thumb_h);
115 if (ret != THUMBNAIL_UTIL_ERROR_NONE) {
116 MSG_ERR("thumbnail_util_extract is failed");
121 time_ret = g_cv.timedwait(g_mx.pMsgMutex(), 5);
125 if (time_ret == ETIMEDOUT) {
126 MSG_ERR("@@ WAKE by timeout@@");
130 if (MsgAccessFile(dstPath, F_OK) == false) {
131 MSG_SEC_DEBUG("not exist result file [%s]", dstPath);
135 MSG_SEC_DEBUG("Make thumbnail: success [%s]", dstPath);
139 /* File operation wrappers */
140 FILE *MsgOpenFile(const char *filepath, const char *opt)
142 if (!filepath || !opt) {
143 MSG_FATAL("Null parameter");
147 MSG_SEC_DEBUG("[FILE] filepath : [%s], opt [%s]", filepath, opt);
152 pFile = fopen(filepath, opt);
153 MSG_DEBUG("[FILE] pFile [%p]", pFile);
154 } catch (exception &e) {
155 MSG_FATAL("%s", e.what());
162 void MsgCloseFile(FILE *pFile)
165 MSG_FATAL("NULL parameter");
169 MSG_DEBUG("[FILE] pFile [%p]", pFile);
173 } catch (exception &e) {
174 MSG_FATAL("%s", e.what());
178 int MsgFseek(FILE *pFile, long int offset, int origin)
181 MSG_FATAL("pFile NULL");
187 MSG_DEBUG("[FILE] pFile [%p], offset [%d], origin [%d]", pFile, offset, origin);
190 ret = fseek(pFile, offset, origin); /* return 0, if success. */
191 } catch (exception &e) {
192 MSG_FATAL("%s", e.what());
199 size_t MsgWriteFile(const char *pData, size_t size, size_t count, FILE *pFile)
201 if (!pData || !pFile) {
202 MSG_FATAL("pData or pFile NULL");
208 MSG_DEBUG("[FILE] pData [%p], size [%d], count [%d], pFile [%p]", pData, size, count, pFile);
211 nWrite = fwrite(pData, size, count, pFile);
212 } catch (exception &e) {
213 MSG_FATAL("%s", e.what());
219 size_t MsgReadFile(void *pData, size_t size, size_t count, FILE *pFile)
221 if (!pData || !pFile) {
222 MSG_FATAL("pData or pFile NULL");
229 nRead = fread(pData, size, count, pFile);
230 } catch (exception &e) {
231 MSG_FATAL("%s", e.what());
237 long int MsgFtell(FILE *pFile)
240 MSG_FATAL("pFile NULL");
244 long int ret = -1L; /* -1L return if error occured. */
248 } catch (exception &e) {
249 MSG_FATAL("%s", e.what());
255 int MsgFflush(FILE *pFile)
258 MSG_FATAL("pFile NULL");
265 ret = fflush(pFile); /* return 0 if success */
266 } catch (exception &e) {
267 MSG_FATAL("%s", e.what());
273 int MsgFsync(FILE *pFile)
276 MSG_FATAL("pFile NULL");
283 ret = fdatasync(pFile->_fileno); /* return 0 if success */
284 } catch (exception &e) {
285 MSG_FATAL("%s", e.what());
291 bool MsgCreateFileName(char *pFileName)
293 if (pFileName == NULL) {
294 MSG_DEBUG("[ERROR] pFileName is NULL");
301 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
302 MSG_DEBUG("clock_gettime() error: %s", g_strerror(errno));
306 /* Create Random Number */
307 srandom((unsigned int)ts.tv_nsec);
309 MSG_DEBUG("ts.tv_nsec : %d", ts.tv_nsec);
311 /* between 1 - 1000000000 */
312 snprintf(pFileName, MSG_FILENAME_LEN_MAX, "MSG_%lu.DATA", random()%1000000000+1);
313 } catch (exception& e) {
314 MSG_FATAL("%s", e.what());
322 bool MsgOpenAndReadFile(const char *pFileName, char **ppData, int *pDataSize)
324 if (!pFileName || !ppData || !pDataSize) {
325 MSG_ERR("Invalid params!! pFileName=%x, ppData=%x, pDataSize=%x", pFileName, ppData, pDataSize);
329 MSG_DEBUG("MsgOpenAndReadFile");
333 char fullPath[MAX_FULL_PATH_SIZE] = {0};
335 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
336 MSG_SEC_DEBUG("open file name: %s", fullPath);
338 pFile = MsgOpenFile(fullPath, "rb");
341 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
345 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
347 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
351 int FileSize = MsgFtell(pFile);
354 MSG_DEBUG("Filesize is error : %d", FileSize);
360 *ppData = new char[FileSize+1];
361 memset(*ppData, 0x00, (FileSize+1));
363 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
365 MSG_DEBUG("File seek Error: %s", g_strerror(errno));
369 if (MsgReadFile(*ppData, sizeof(char), FileSize, pFile) != (size_t)FileSize) {
371 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
375 *pDataSize = FileSize;
383 bool MsgReadFileForDecode(FILE *pFile, char *pBuf, int length, int *nSize)
387 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
388 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
393 *nSize = MsgReadFile(pBuf, sizeof(char), length, pFile);
400 bool MsgWriteIpcFile(const char *pFileName, const char *pData, int DataSize)
403 MSG_DEBUG("NULL parameter, pFileName [%p], pData [%p]", pFileName, pData);
407 char fullPath[MAX_FULL_PATH_SIZE] = {0};
409 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
411 FILE *pFile = MsgOpenFile(fullPath, "wb+");
414 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
418 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
420 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
424 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
426 MSG_DEBUG("File Write Error: %s", g_strerror(errno));
436 int MsgReadSmilFile(const char *pFileName, char **ppData)
439 MSG_DEBUG("pFileName is NULL");
444 char fullPath[MAX_FULL_PATH_SIZE] = {0};
446 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_SMIL_FILE_PATH, pFileName);
448 MSG_SEC_DEBUG("open file name: %s", fullPath);
450 FILE *pFile = MsgOpenFile(fullPath, "rb");
453 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
457 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
459 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
463 int FileSize = MsgFtell(pFile);
466 MSG_DEBUG("Filesize is error : %d", FileSize);
471 *ppData = new char[FileSize + 1];
472 memset(*ppData, 0x00, (FileSize+1));
474 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
476 MSG_DEBUG("File Sead Error: %s", g_strerror(errno));
480 if (MsgReadFile(*ppData, sizeof(char), FileSize, pFile) != (size_t)FileSize) {
482 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
486 /* ppData[FileSize] = '\0'; */
495 bool MsgWriteSmilFile(const char *pFilePath, char *pData, int DataSize)
498 MSG_DEBUG("pFilePath is NULL");
503 if (mkdir(MSG_SMIL_FILE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
504 if (errno == EEXIST) {
505 MSG_SEC_DEBUG("The %s already exists", MSG_SMIL_FILE_PATH);
507 MSG_SEC_DEBUG("Error while mkdir %s", MSG_SMIL_FILE_PATH);
512 FILE *pFile = MsgOpenFile(pFilePath, "wb+");
515 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
519 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
521 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
525 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
527 MSG_DEBUG("File Write Error: %s", g_strerror(errno));
538 void MsgDeleteFile(const char *pFileName)
541 MSG_FATAL("pFileName is NULL");
545 if (strlen(pFileName) == 0) {
546 MSG_FATAL("pFileName has zero length");
550 char fullPath[MAX_FULL_PATH_SIZE] = {0};
553 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
555 MSG_SEC_DEBUG("%s", fullPath);
557 if (remove(fullPath) != 0)
558 MSG_SEC_ERR("File Delete Error [%s]: %s", fullPath, g_strerror(errno));
559 } catch (exception &e) {
560 MSG_FATAL("%s", e.what());
565 void MsgDeleteSmilFile(const char *pFileName)
568 MSG_FATAL("pFileName NULL");
573 char fullPath[MAX_FULL_PATH_SIZE] = {0};
575 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_SMIL_FILE_PATH, pFileName);
577 if (remove(fullPath) != 0)
578 MSG_SEC_ERR("File Delete Error [%s]: %s", fullPath, g_strerror(errno));
579 } catch (exception &e) {
580 MSG_FATAL("%s", e.what());
585 bool MsgGetFileSize(const char *pFilePath, int *nSize)
588 MSG_FATAL("pFileName NULL");
594 pFile = MsgOpenFile(pFilePath, "rb");
597 MSG_DEBUG("File Open error: %s", g_strerror(errno));
601 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
603 MSG_FATAL("File Read Error: %s", g_strerror(errno));
607 *nSize = MsgFtell(pFile);
615 FILE *MsgOpenMMSFile(char *pFileName)
620 MSG_DEBUG("pFileName NULL: %s", g_strerror(errno));
624 len = strlen(pFileName);
626 for (int i = 0; i < len; i++) {
627 switch (pFileName[i]) {
634 MSG_SEC_DEBUG("pFileName = %s", pFileName);
636 char fullPath[MAX_FULL_PATH_SIZE+1] = {0};
638 snprintf(fullPath, MAX_FULL_PATH_SIZE+1, "%s.mms", pFileName);
640 FILE *pFile = MsgOpenFile(fullPath, "wb+");
643 MSG_ERR("File Open Error: %s", g_strerror(errno));
647 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
649 MSG_ERR("File Read Error: %s", g_strerror(errno));
657 bool MsgWriteDataFromEncodeBuffer(FILE *pFile, char *pInBuffer, int *pPtr, int maxLen, int *pOffset )
659 if (!pFile || !pPtr || !pInBuffer || !pOffset) {
660 MSG_FATAL(" NULL parameter passed");
664 MSG_DEBUG("MsgWriteDataFromEncodeBuffer:");
665 MSG_DEBUG("pInBuffer %x", pInBuffer);
666 MSG_DEBUG("pPtr %d", (*pPtr));
667 MSG_DEBUG("before to fwite %x", pFile);
669 if (MsgWriteFile(pInBuffer, sizeof(char), (*pPtr), pFile) != (size_t)(*pPtr)) {
670 MSG_FATAL("MsgWriteFile failed");
674 MSG_DEBUG("after to fwite \n");
678 memset(pInBuffer, 0, maxLen);
682 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
683 MSG_FATAL("MsgFseek failed");
687 *pOffset = MsgFtell(pFile);
689 if (*pOffset == -1L) {
690 MSG_FATAL("MsgFtell failed");
698 bool MsgOpenCreateAndOverwriteFile(char *pFullPath, char *pBuff, int TotalLength)
702 if ((pFile = MsgOpenFile(pFullPath, "wb+")) == NULL) {
703 MSG_FATAL("MsgOpenFile errer");
707 if (MsgWriteFile(pBuff, sizeof(char), TotalLength, pFile) != (size_t)TotalLength) {
712 MsgFsync(pFile); /* file is written to device immediately, it prevents missing file data from unexpected power off */
720 char *MsgOpenAndReadMmsFile(const char *szFilePath, int offset, int size, int *npSize)
726 if (szFilePath == NULL) {
727 MSG_ERR("szFilePath id NULL");
733 pFile = MsgOpenFile(szFilePath, "rb");
736 MSG_ERR("Can't open file: %s", g_strerror(errno));
741 if (MsgGetFileSize(szFilePath, & readSize) == false) {
742 MSG_DEBUG("MsgGetFileSize: failed");
748 /* restore Kies backup data size greater than FM_READ_WRITE_BUFFER_MAX */
750 if (readSize > FM_READ_WRITE_BUFFER_MAX) {
751 MSG_DEBUG("MsgOpenAndReadMmsFile: File size tried to read too big");
756 pData = (char *)calloc(1, readSize + 1);
757 if ( NULL == pData ) {
758 MSG_ERR("pData MemAlloc Fail : %s", g_strerror(errno) );
762 if (MsgFseek(pFile, offset, SEEK_SET) < 0) {
763 MSG_ERR("FmSeekFile failed : %s", g_strerror(errno) );
767 *npSize = MsgReadFile(pData, sizeof(char), readSize, pFile);
773 *(pData + (*npSize)) = '\0';
794 /* it is equivalent to "rm -rf pDirPath" */
795 int MsgRmRf(char *pDirPath)
797 struct dirent *d = NULL;
801 dir = opendir(pDirPath);
804 MSG_FATAL("error opendir: %s", g_strerror(errno));
808 int size = strlen(pDirPath) + 256;
810 char *path = (char*)malloc(size);
813 MSG_DEBUG("path is NULL");
820 for (readdir_r(dir, &entry, &d); d != NULL; readdir_r(dir, &entry, &d)) {
821 if (d->d_type == DT_DIR) {
822 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
824 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
829 if (rmdir(path) != 0) {
835 MSG_FATAL("error rmdir: %s", g_strerror(errno));
840 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
842 if (MsgDrmIsDrmFile(path))
843 MsgDrmUnregisterFile(path);
845 if (remove(path) != 0) {
851 MSG_FATAL("error remove: %s", g_strerror(errno));
868 int MsgGetFileSize(const char *pFileName)
870 struct stat file_stat;
872 if (lstat(pFileName, &file_stat)) {
873 MSG_FATAL("error lstat: %s", g_strerror(errno));
877 return file_stat.st_size;
881 /* it is equivalent to "du dir_path" */
882 unsigned int MsgDu(const char *pDirPath)
884 struct dirent *d = NULL;
888 dir = opendir(pDirPath);
891 MSG_FATAL("error opendir: %s", g_strerror(errno));
895 int size = strlen(pDirPath) + 256;
896 char *path = (char*)malloc(size);
904 unsigned int totalFileSize = 0;
906 for (readdir_r(dir, &entry, &d); d != NULL; readdir_r(dir, &entry, &d)) {
907 if(d->d_type == DT_DIR) {
908 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
910 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
913 unsigned int dirSize = MsgDu(path);
916 MSG_FATAL("error MsgDu");
922 totalFileSize += dirSize;
924 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
925 int fileSize = MsgGetFileSize(path);
928 MSG_FATAL("error MsgGetFileSize");
934 totalFileSize += fileSize;
943 return totalFileSize;
947 bool MsgAppendFile(const char *pFilePath, const char *pData, int DataSize)
950 MSG_FATAL("NULL check error, pFileName %p, pData %p", pFilePath, pData);
954 char fullPath[MAX_FULL_PATH_SIZE] = {0};
956 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s", pFilePath);
958 FILE *pFile = MsgOpenFile(fullPath, "a+");
961 MSG_FATAL("File Open Error: %s", g_strerror(errno));
965 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
967 MSG_FATAL("File Sead Error: %s", g_strerror(errno));
971 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
973 MSG_FATAL("File Write Error: %s", g_strerror(errno));
977 MsgFsync(pFile); /*file is written to device immediately, it prevents missing file data from unexpected power off */
985 struct dirent *d = NULL;
989 dir = opendir(MSG_DATA_PATH);
992 MSG_FATAL("error opendir: %s", g_strerror(errno));
996 /* Remove temporal Mms folder */
997 for (readdir_r(dir, &entry, &d); d != NULL; readdir_r(dir, &entry, &d)) {
998 if (d->d_type == DT_DIR) {
999 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
1002 if(strstr(d->d_name, ".dir") != NULL) {
1003 char filePath[MSG_FILEPATH_LEN_MAX] = {0, };
1004 snprintf(filePath, MSG_FILEPATH_LEN_MAX, "%s%s", MSG_DATA_PATH, d->d_name);
1016 /* mode : R_OK, W_OK, X_OK, or the existence test F_OK. */
1017 bool MsgAccessFile(const char *filepath, int mode)
1020 if (filepath == NULL) {
1021 MSG_DEBUG("filepath is NULL");
1025 MSG_SEC_DEBUG("request access path = %s, mode = %d", filepath, mode);
1027 ret = access(filepath, mode);
1031 MSG_DEBUG("Fail to access file, ret = %d", ret);
1039 bool MsgChmod(const char *filepath, int mode)
1042 struct stat lstat_info;
1043 struct stat fstat_info;
1046 if (lstat(filepath, &lstat_info) == -1) {
1047 MSG_SEC_DEBUG("No such file as [%s].", filepath);
1051 fd = open(filepath, O_RDONLY);
1054 MSG_SEC_DEBUG("Fail to open [%s].", filepath);
1058 if (fstat(fd, &fstat_info) == -1) {
1059 MSG_SEC_DEBUG("Fail to fstat [%s].", filepath);
1064 if (lstat_info.st_mode == fstat_info.st_mode &&
1065 lstat_info.st_ino == fstat_info.st_ino &&
1066 lstat_info.st_dev == fstat_info.st_dev) {
1067 if (fchmod(fd, mode) < 0) {
1068 MSG_SEC_DEBUG("Fail to fchmod [%s].", filepath);
1080 bool MsgChown(const char *filepath, int uid, int gid)
1082 struct stat lstat_info;
1083 struct stat fstat_info;
1086 if (lstat(filepath, &lstat_info) == -1) {
1087 MSG_SEC_INFO("No such file as [%s].", filepath);
1091 fd = open(filepath, O_RDONLY);
1094 MSG_SEC_INFO("Fail to open [%s].", filepath);
1098 if (fstat(fd, &fstat_info) == -1) {
1099 MSG_SEC_INFO("Fail to fstat [%s].", filepath);
1104 if (lstat_info.st_mode == fstat_info.st_mode &&
1105 lstat_info.st_ino == fstat_info.st_ino &&
1106 lstat_info.st_dev == fstat_info.st_dev) {
1107 if (fchown(fd, uid, gid) < 0) {
1108 MSG_SEC_INFO("Fail to fchown [%s].", filepath);
1118 bool MsgCreateFile(const char *pFilePath, char *pData, int DataSize)
1121 MSG_DEBUG("pFilePath is NULL");
1125 FILE *pFile = MsgOpenFile(pFilePath, "wb+");
1127 if (pFile == NULL) {
1128 MSG_ERR("File Open Error: %s", g_strerror(errno));
1132 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
1133 MsgCloseFile(pFile);
1134 MSG_ERR("File Seek Error: %s", g_strerror(errno));
1138 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
1139 MsgCloseFile(pFile);
1140 MSG_ERR("File Write Error: %s", g_strerror(errno));
1146 MsgCloseFile(pFile);
1151 char *MsgGetDirName(char *file_path)
1153 return g_path_get_dirname(file_path);
1157 char *MsgGetFileName(char *file_path)
1159 return g_path_get_basename(file_path);
1163 int MsgCheckFilepathSmack(const char *app_smack_label, char *file_path)
1165 int err = MSG_SUCCESS;
1167 char *path_smack_label = NULL;
1168 char *dir_smack_label = NULL;
1169 char *dir_name = NULL;
1171 if (!file_path || file_path[0] == '\0') {
1176 if (stat(file_path, &st) != 0) {
1177 MSG_SEC_ERR("stat(%s, &st) != 0", file_path);
1178 return MSG_ERR_PERMISSION_DENIED;
1180 if (S_ISDIR(st.st_mode)) {
1181 MSG_ERR("S_ISDIR(st.st_mode)");
1182 return MSG_ERR_INVALID_PARAMETER;
1185 dir_name = MsgGetDirName(file_path);
1186 if (!dir_name || !g_strcmp0(dir_name, file_path)) {
1187 MSG_SEC_ERR("!dir_name || !g_strcmp0(dir_name, %s)", file_path);
1188 err = MSG_ERR_INVALID_PARAMETER;
1192 smack_getlabel(dir_name, &dir_smack_label, SMACK_LABEL_ACCESS);
1193 if (dir_smack_label == NULL) {
1194 MSG_ERR("smack_getlabel failed (dir_smack_label)");
1195 err = MSG_ERR_PERMISSION_DENIED;
1199 if (smack_have_access(app_smack_label, dir_smack_label, "RX") < 1) {
1200 MSG_ERR("smack_have_access failed (dir_smack_label)");
1201 err = MSG_ERR_PERMISSION_DENIED;
1205 smack_getlabel(file_path, &path_smack_label, SMACK_LABEL_ACCESS);
1206 if (path_smack_label == NULL) {
1207 MSG_ERR("smack_getlabel failed (path_smack_label)");
1208 err = MSG_ERR_PERMISSION_DENIED;
1212 if (smack_have_access(app_smack_label, path_smack_label, "R") < 1) {
1213 MSG_ERR("smack_have_access failed (path_smack_label)");
1214 err = MSG_ERR_PERMISSION_DENIED;
1218 MSG_DEBUG("smack_have_access pass successfully");
1221 MSG_FREE(path_smack_label);
1222 MSG_FREE(dir_smack_label);
1228 void MsgGetMimeType(char *filePath, char *mimeType, int size)
1230 aul_get_mime_from_file(filePath, mimeType, size);
1234 int MsgTcsScanFile(const char *filepath, int *bLevel)
1238 TCSScanResult result;
1239 TCSDetected* pDetected;
1241 int ret_b_level = -1;
1243 if (MsgAccessFile(filepath, R_OK) == false) {
1244 MSG_SEC_DEBUG("not exist source file [%s]", filepath);
1248 MSG_SEC_DEBUG("Scanning file name : %s\n", filepath);
1250 hLib = TCSLibraryOpen();
1251 if(hLib == INVALID_TCSLIB_HANDLE) {
1252 MSG_DEBUG("TCSLibraryOpen error\n");
1256 rtn = TCSScanFile(hLib, filepath, TCS_DTYPE_UNKNOWN, TCS_SA_SCANONLY, 1, &result);
1259 MSG_DEBUG("Detected malware number: %d\n", result.iNumDetected);
1260 i = result.iNumDetected;
1261 pDetected = result.pDList;
1262 while(i && pDetected)
1266 MSG_SEC_DEBUG(" +-- Malware [%d] Name: %s\n", i, pDetected->pszName);
1267 MSG_DEBUG(" +-- Malware [%d] uAction: %u : 0x%04x\n", i, pDetected->uAction, pDetected->uAction);
1269 temp_b_level = (pDetected->uAction & 0xFF00) >> 8;
1270 MSG_DEBUG(" +-- Malware [%d] Behavior level: %u\n", i, temp_b_level);
1272 if (ret_b_level == -1 || ret_b_level < temp_b_level) {
1273 ret_b_level = temp_b_level;
1276 temp_s_class = (pDetected->uAction & 0x00FF);
1277 MSG_DEBUG(" +-- Malware [%d] Severity class: %u\n", i, temp_s_class);
1279 pDetected = pDetected->pNext;
1283 result.pfFreeResult(&result);
1285 MSG_DEBUG("TCSScanFile fail: err = %d\n", rtn);
1288 TCSLibraryClose(hLib);
1291 *bLevel = ret_b_level;