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>
29 #include <acl/libacl.h>
31 #include <media_content.h>
32 #include <thumbnail_util.h>
33 #include <image_util.h>
35 #include <TCSErrorCodes.h>
37 #include "MsgStorageTypes.h"
39 #include "MsgException.h"
40 #include "MsgUtilFile.h"
41 #include "MsgMmsTypes.h"
42 #include "MsgInternalTypes.h"
43 #include "MsgDrmWrapper.h"
53 void thumbnail_completed_cb(thumbnail_util_error_e error, const char *request_id,
54 int thumb_width, int thumb_height,
55 unsigned char *thumb_data, int thumb_size, void *user_data)
62 MSG_WARN("dstPath is NULL");
68 MSG_DEBUG("=================[RESULT]");
69 MSG_DEBUG("error_code [%d]", error);
70 MSG_DEBUG("request id [%s]", request_id);
71 MSG_DEBUG("width [%d], height [%d]", thumb_width, thumb_height);
72 MSG_DEBUG("raw_data [0x%x], size [%d]", *thumb_data, thumb_size);
75 ret = image_util_encode_jpeg(thumb_data, thumb_width, thumb_height, IMAGE_UTIL_COLORSPACE_BGRA8888, 100, (char *)user_data);
76 if (ret != IMAGE_UTIL_ERROR_NONE)
77 MSG_WARN("image_util_encode_jpeg() is failed");
85 /*==================================================================================================
86 FUNCTION IMPLEMENTATION
87 ==================================================================================================*/
88 bool MakeThumbnail(char *srcPath, char *dstPath)
90 if (srcPath == NULL || dstPath == NULL) {
91 MSG_SEC_DEBUG("Invalid Param src = %p, dst = %p", srcPath, dstPath);
95 if (MsgAccessFile(srcPath, R_OK) == false) {
96 MSG_SEC_DEBUG("not exist source file [%s]", srcPath);
104 int ret = THUMBNAIL_UTIL_ERROR_NONE;
107 thumbnail_util_create(&thumb_h);
108 thumbnail_util_set_path(thumb_h, srcPath);
110 ret = thumbnail_util_extract(thumb_h, thumbnail_completed_cb, dstPath, &req_id);
111 thumbnail_util_destroy(thumb_h);
117 if (ret != THUMBNAIL_UTIL_ERROR_NONE) {
118 MSG_ERR("thumbnail_util_extract is failed");
123 time_ret = g_cv.timedwait(g_mx.pMsgMutex(), 5);
127 if (time_ret == ETIMEDOUT) {
128 MSG_ERR("@@ WAKE by timeout@@");
132 if (MsgAccessFile(dstPath, F_OK) == false) {
133 MSG_SEC_DEBUG("not exist result file [%s]", dstPath);
137 MSG_SEC_DEBUG("Make thumbnail: success [%s]", dstPath);
141 /* File operation wrappers */
142 FILE *MsgOpenFile(const char *filepath, const char *opt)
144 if (!filepath || !opt) {
145 MSG_FATAL("Null parameter");
149 MSG_SEC_DEBUG("[FILE] filepath : [%s], opt [%s]", filepath, opt);
154 pFile = fopen(filepath, opt);
155 MSG_DEBUG("[FILE] pFile [%p]", pFile);
156 } catch (exception &e) {
157 MSG_FATAL("%s", e.what());
164 void MsgCloseFile(FILE *pFile)
167 MSG_FATAL("NULL parameter");
171 MSG_DEBUG("[FILE] pFile [%p]", pFile);
175 } catch (exception &e) {
176 MSG_FATAL("%s", e.what());
180 int MsgFseek(FILE *pFile, long int offset, int origin)
183 MSG_FATAL("pFile NULL");
189 MSG_DEBUG("[FILE] pFile [%p], offset [%d], origin [%d]", pFile, offset, origin);
192 ret = fseek(pFile, offset, origin); /* return 0, if success. */
193 } catch (exception &e) {
194 MSG_FATAL("%s", e.what());
201 size_t MsgWriteFile(const char *pData, size_t size, size_t count, FILE *pFile)
203 if (!pData || !pFile) {
204 MSG_FATAL("pData or pFile NULL");
210 MSG_DEBUG("[FILE] pData [%p], size [%d], count [%d], pFile [%p]", pData, size, count, pFile);
213 nWrite = fwrite(pData, size, count, pFile);
214 } catch (exception &e) {
215 MSG_FATAL("%s", e.what());
221 size_t MsgReadFile(void *pData, size_t size, size_t count, FILE *pFile)
223 if (!pData || !pFile) {
224 MSG_FATAL("pData or pFile NULL");
231 nRead = fread(pData, size, count, pFile);
232 } catch (exception &e) {
233 MSG_FATAL("%s", e.what());
239 long int MsgFtell(FILE *pFile)
242 MSG_FATAL("pFile NULL");
246 long int ret = -1L; /* -1L return if error occured. */
250 } catch (exception &e) {
251 MSG_FATAL("%s", e.what());
257 int MsgFflush(FILE *pFile)
260 MSG_FATAL("pFile NULL");
267 ret = fflush(pFile); /* return 0 if success */
268 } catch (exception &e) {
269 MSG_FATAL("%s", e.what());
275 int MsgFsync(FILE *pFile)
278 MSG_FATAL("pFile NULL");
285 ret = fdatasync(pFile->_fileno); /* return 0 if success */
286 } catch (exception &e) {
287 MSG_FATAL("%s", e.what());
293 bool MsgCreateFileName(char *pFileName)
295 if (pFileName == NULL) {
296 MSG_DEBUG("[ERROR] pFileName is NULL");
303 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
304 MSG_DEBUG("clock_gettime() error: %s", g_strerror(errno));
308 /* Create Random Number */
309 srandom((unsigned int)ts.tv_nsec);
311 MSG_DEBUG("ts.tv_nsec : %d", ts.tv_nsec);
313 /* between 1 - 1000000000 */
314 snprintf(pFileName, MSG_FILENAME_LEN_MAX, "MSG_%lu.DATA", random()%1000000000+1);
315 } catch (exception& e) {
316 MSG_FATAL("%s", e.what());
324 bool MsgOpenAndReadFile(const char *pFileName, char **ppData, int *pDataSize)
326 if (!pFileName || !ppData || !pDataSize) {
327 MSG_ERR("Invalid params!! pFileName=%x, ppData=%x, pDataSize=%x", pFileName, ppData, pDataSize);
331 MSG_DEBUG("MsgOpenAndReadFile");
335 char fullPath[MAX_FULL_PATH_SIZE] = {0};
337 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
338 MSG_SEC_DEBUG("open file name: %s", fullPath);
340 pFile = MsgOpenFile(fullPath, "rb");
343 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
347 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
349 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
353 int FileSize = MsgFtell(pFile);
356 MSG_DEBUG("Filesize is error : %d", FileSize);
362 *ppData = new char[FileSize+1];
363 memset(*ppData, 0x00, (FileSize+1));
365 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
367 MSG_DEBUG("File seek Error: %s", g_strerror(errno));
371 if (MsgReadFile(*ppData, sizeof(char), FileSize, pFile) != (size_t)FileSize) {
373 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
377 *pDataSize = FileSize;
385 bool MsgReadFileForDecode(FILE *pFile, char *pBuf, int length, int *nSize)
389 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
390 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
395 *nSize = MsgReadFile(pBuf, sizeof(char), length, pFile);
402 bool MsgWriteIpcFile(const char *pFileName, const char *pData, int DataSize)
405 MSG_DEBUG("NULL parameter, pFileName [%p], pData [%p]", pFileName, pData);
409 char fullPath[MAX_FULL_PATH_SIZE] = {0};
411 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
413 FILE *pFile = MsgOpenFile(fullPath, "wb+");
416 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
420 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
422 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
426 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
428 MSG_DEBUG("File Write Error: %s", g_strerror(errno));
438 int MsgReadSmilFile(const char *pFileName, char **ppData)
441 MSG_DEBUG("pFileName is NULL");
446 char fullPath[MAX_FULL_PATH_SIZE] = {0};
448 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_SMIL_FILE_PATH, pFileName);
450 MSG_SEC_DEBUG("open file name: %s", fullPath);
452 FILE *pFile = MsgOpenFile(fullPath, "rb");
455 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
459 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
461 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
465 int FileSize = MsgFtell(pFile);
468 MSG_DEBUG("Filesize is error : %d", FileSize);
473 *ppData = new char[FileSize + 1];
474 memset(*ppData, 0x00, (FileSize+1));
476 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
478 MSG_DEBUG("File Sead Error: %s", g_strerror(errno));
482 if (MsgReadFile(*ppData, sizeof(char), FileSize, pFile) != (size_t)FileSize) {
484 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
488 /* ppData[FileSize] = '\0'; */
497 bool MsgWriteSmilFile(const char *pFilePath, char *pData, int DataSize)
500 MSG_DEBUG("pFilePath is NULL");
505 if (mkdir(MSG_SMIL_FILE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
506 if (errno == EEXIST) {
507 MSG_SEC_DEBUG("The %s already exists", MSG_SMIL_FILE_PATH);
509 MSG_SEC_DEBUG("Error while mkdir %s", MSG_SMIL_FILE_PATH);
514 FILE *pFile = MsgOpenFile(pFilePath, "wb+");
517 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
521 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
523 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
527 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
529 MSG_DEBUG("File Write Error: %s", g_strerror(errno));
540 void MsgDeleteFile(const char *pFileName)
543 MSG_FATAL("pFileName is NULL");
547 if (strlen(pFileName) == 0) {
548 MSG_FATAL("pFileName has zero length");
552 char fullPath[MAX_FULL_PATH_SIZE] = {0};
555 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
557 MSG_SEC_DEBUG("%s", fullPath);
559 if (remove(fullPath) != 0)
560 MSG_SEC_ERR("File Delete Error [%s]: %s", fullPath, g_strerror(errno));
561 } catch (exception &e) {
562 MSG_FATAL("%s", e.what());
567 void MsgDeleteSmilFile(const char *pFileName)
570 MSG_FATAL("pFileName NULL");
575 char fullPath[MAX_FULL_PATH_SIZE] = {0};
577 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_SMIL_FILE_PATH, pFileName);
579 if (remove(fullPath) != 0)
580 MSG_SEC_ERR("File Delete Error [%s]: %s", fullPath, g_strerror(errno));
581 } catch (exception &e) {
582 MSG_FATAL("%s", e.what());
587 bool MsgGetFileSize(const char *pFilePath, int *nSize)
590 MSG_FATAL("pFileName NULL");
596 pFile = MsgOpenFile(pFilePath, "rb");
599 MSG_DEBUG("File Open error: %s", g_strerror(errno));
603 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
605 MSG_FATAL("File Read Error: %s", g_strerror(errno));
609 *nSize = MsgFtell(pFile);
617 FILE *MsgOpenMMSFile(char *pFileName)
622 MSG_DEBUG("pFileName NULL: %s", g_strerror(errno));
626 len = strlen(pFileName);
628 for (int i = 0; i < len; i++) {
629 switch (pFileName[i]) {
636 MSG_SEC_DEBUG("pFileName = %s", pFileName);
638 char fullPath[MAX_FULL_PATH_SIZE+1] = {0};
640 snprintf(fullPath, MAX_FULL_PATH_SIZE+1, "%s.mms", pFileName);
642 FILE *pFile = MsgOpenFile(fullPath, "wb+");
645 MSG_ERR("File Open Error: %s", g_strerror(errno));
649 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
651 MSG_ERR("File Read Error: %s", g_strerror(errno));
659 bool MsgWriteDataFromEncodeBuffer(FILE *pFile, char *pInBuffer, int *pPtr, int maxLen, int *pOffset )
661 if (!pFile || !pPtr || !pInBuffer || !pOffset) {
662 MSG_FATAL(" NULL parameter passed");
666 MSG_DEBUG("MsgWriteDataFromEncodeBuffer:");
667 MSG_DEBUG("pInBuffer %x", pInBuffer);
668 MSG_DEBUG("pPtr %d", (*pPtr));
669 MSG_DEBUG("before to fwite %x", pFile);
671 if (MsgWriteFile(pInBuffer, sizeof(char), (*pPtr), pFile) != (size_t)(*pPtr)) {
672 MSG_FATAL("MsgWriteFile failed");
676 MSG_DEBUG("after to fwite \n");
680 memset(pInBuffer, 0, maxLen);
684 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
685 MSG_FATAL("MsgFseek failed");
689 *pOffset = MsgFtell(pFile);
691 if (*pOffset == -1L) {
692 MSG_FATAL("MsgFtell failed");
700 bool MsgOpenCreateAndOverwriteFile(char *pFullPath, char *pBuff, int TotalLength)
704 if ((pFile = MsgOpenFile(pFullPath, "wb+")) == NULL) {
705 MSG_FATAL("MsgOpenFile errer");
709 if (MsgWriteFile(pBuff, sizeof(char), TotalLength, pFile) != (size_t)TotalLength) {
714 MsgFsync(pFile); /* file is written to device immediately, it prevents missing file data from unexpected power off */
722 char *MsgOpenAndReadMmsFile(const char *szFilePath, int offset, int size, int *npSize)
728 if (szFilePath == NULL) {
729 MSG_ERR("szFilePath id NULL");
735 pFile = MsgOpenFile(szFilePath, "rb");
738 MSG_ERR("Can't open file: %s", g_strerror(errno));
743 if (MsgGetFileSize(szFilePath, & readSize) == false) {
744 MSG_DEBUG("MsgGetFileSize: failed");
750 /* restore Kies backup data size greater than FM_READ_WRITE_BUFFER_MAX */
752 if (readSize > FM_READ_WRITE_BUFFER_MAX) {
753 MSG_DEBUG("MsgOpenAndReadMmsFile: File size tried to read too big");
758 pData = (char *)calloc(1, readSize + 1);
759 if ( NULL == pData ) {
760 MSG_ERR("pData MemAlloc Fail : %s", g_strerror(errno) );
764 if (MsgFseek(pFile, offset, SEEK_SET) < 0) {
765 MSG_ERR("FmSeekFile failed : %s", g_strerror(errno) );
769 *npSize = MsgReadFile(pData, sizeof(char), readSize, pFile);
775 *(pData + (*npSize)) = '\0';
796 /* it is equivalent to "rm -rf pDirPath" */
797 int MsgRmRf(char *pDirPath)
799 struct dirent *d = NULL;
803 dir = opendir(pDirPath);
806 MSG_FATAL("error opendir: %s", g_strerror(errno));
810 int size = strlen(pDirPath) + 256;
812 char *path = (char*)malloc(size);
815 MSG_DEBUG("path is NULL");
822 for (readdir_r(dir, &entry, &d); d != NULL; readdir_r(dir, &entry, &d)) {
823 if (d->d_type == DT_DIR) {
824 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
826 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
831 if (rmdir(path) != 0) {
837 MSG_FATAL("error rmdir: %s", g_strerror(errno));
842 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
844 if (MsgDrmIsDrmFile(path))
845 MsgDrmUnregisterFile(path);
847 if (remove(path) != 0) {
853 MSG_FATAL("error remove: %s", g_strerror(errno));
870 int MsgGetFileSize(const char *pFileName)
872 struct stat file_stat;
874 if (lstat(pFileName, &file_stat)) {
875 MSG_FATAL("error lstat: %s", g_strerror(errno));
879 return file_stat.st_size;
883 /* it is equivalent to "du dir_path" */
884 unsigned int MsgDu(const char *pDirPath)
886 struct dirent *d = NULL;
890 dir = opendir(pDirPath);
893 MSG_FATAL("error opendir: %s", g_strerror(errno));
897 int size = strlen(pDirPath) + 256;
898 char *path = (char*)malloc(size);
906 unsigned int totalFileSize = 0;
908 for (readdir_r(dir, &entry, &d); d != NULL; readdir_r(dir, &entry, &d)) {
909 if(d->d_type == DT_DIR) {
910 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
912 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
915 unsigned int dirSize = MsgDu(path);
918 MSG_FATAL("error MsgDu");
924 totalFileSize += dirSize;
926 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
927 int fileSize = MsgGetFileSize(path);
930 MSG_FATAL("error MsgGetFileSize");
936 totalFileSize += fileSize;
945 return totalFileSize;
949 bool MsgAppendFile(const char *pFilePath, const char *pData, int DataSize)
952 MSG_FATAL("NULL check error, pFileName %p, pData %p", pFilePath, pData);
956 char fullPath[MAX_FULL_PATH_SIZE] = {0};
958 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s", pFilePath);
960 FILE *pFile = MsgOpenFile(fullPath, "a+");
963 MSG_FATAL("File Open Error: %s", g_strerror(errno));
967 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
969 MSG_FATAL("File Sead Error: %s", g_strerror(errno));
973 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
975 MSG_FATAL("File Write Error: %s", g_strerror(errno));
979 MsgFsync(pFile); /*file is written to device immediately, it prevents missing file data from unexpected power off */
987 struct dirent *d = NULL;
991 dir = opendir(MSG_DATA_PATH);
994 MSG_FATAL("error opendir: %s", g_strerror(errno));
998 /* Remove temporal Mms folder */
999 for (readdir_r(dir, &entry, &d); d != NULL; readdir_r(dir, &entry, &d)) {
1000 if (d->d_type == DT_DIR) {
1001 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
1004 if(strstr(d->d_name, ".dir") != NULL) {
1005 char filePath[MSG_FILEPATH_LEN_MAX] = {0, };
1006 snprintf(filePath, MSG_FILEPATH_LEN_MAX, "%s%s", MSG_DATA_PATH, d->d_name);
1018 /* mode : R_OK, W_OK, X_OK, or the existence test F_OK. */
1019 bool MsgAccessFile(const char *filepath, int mode)
1022 if (filepath == NULL) {
1023 MSG_DEBUG("filepath is NULL");
1027 MSG_SEC_DEBUG("request access path = %s, mode = %d", filepath, mode);
1029 ret = access(filepath, mode);
1033 MSG_DEBUG("Fail to access file, ret = %d", ret);
1041 bool MsgChmod(const char *filepath, int mode)
1044 struct stat lstat_info;
1045 struct stat fstat_info;
1048 if (lstat(filepath, &lstat_info) == -1) {
1049 MSG_SEC_DEBUG("No such file as [%s].", filepath);
1053 fd = open(filepath, O_RDONLY);
1056 MSG_SEC_DEBUG("Fail to open [%s].", filepath);
1060 if (fstat(fd, &fstat_info) == -1) {
1061 MSG_SEC_DEBUG("Fail to fstat [%s].", filepath);
1066 if (lstat_info.st_mode == fstat_info.st_mode &&
1067 lstat_info.st_ino == fstat_info.st_ino &&
1068 lstat_info.st_dev == fstat_info.st_dev) {
1069 if (fchmod(fd, mode) < 0) {
1070 MSG_SEC_DEBUG("Fail to fchmod [%s].", filepath);
1082 bool MsgChown(const char *filepath, int uid, int gid)
1084 struct stat lstat_info;
1085 struct stat fstat_info;
1088 if (lstat(filepath, &lstat_info) == -1) {
1089 MSG_SEC_INFO("No such file as [%s].", filepath);
1093 fd = open(filepath, O_RDONLY);
1096 MSG_SEC_INFO("Fail to open [%s].", filepath);
1100 if (fstat(fd, &fstat_info) == -1) {
1101 MSG_SEC_INFO("Fail to fstat [%s].", filepath);
1106 if (lstat_info.st_mode == fstat_info.st_mode &&
1107 lstat_info.st_ino == fstat_info.st_ino &&
1108 lstat_info.st_dev == fstat_info.st_dev) {
1109 if (fchown(fd, uid, gid) < 0) {
1110 MSG_SEC_INFO("Fail to fchown [%s].", filepath);
1121 bool MsgCreateFile(const char *pFilePath, char *pData, int DataSize)
1124 MSG_DEBUG("pFilePath is NULL");
1128 FILE *pFile = MsgOpenFile(pFilePath, "wb+");
1130 if (pFile == NULL) {
1131 MSG_ERR("File Open Error: %s", g_strerror(errno));
1135 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
1136 MsgCloseFile(pFile);
1137 MSG_ERR("File Seek Error: %s", g_strerror(errno));
1141 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
1142 MsgCloseFile(pFile);
1143 MSG_ERR("File Write Error: %s", g_strerror(errno));
1149 MsgCloseFile(pFile);
1154 char *MsgGetDirName(char *file_path)
1156 return g_path_get_dirname(file_path);
1160 char *MsgGetFileName(char *file_path)
1162 return g_path_get_basename(file_path);
1166 int MsgCheckFilepathSmack(const char *app_smack_label, char *file_path)
1168 int err = MSG_SUCCESS;
1170 char *path_smack_label = NULL;
1171 char *dir_smack_label = NULL;
1172 char *dir_name = NULL;
1174 if (!file_path || file_path[0] == '\0') {
1179 if (stat(file_path, &st) != 0) {
1180 MSG_SEC_ERR("stat(%s, &st) != 0", file_path);
1181 return MSG_ERR_PERMISSION_DENIED;
1183 if (S_ISDIR(st.st_mode)) {
1184 MSG_ERR("S_ISDIR(st.st_mode)");
1185 return MSG_ERR_INVALID_PARAMETER;
1188 dir_name = MsgGetDirName(file_path);
1189 if (!dir_name || !g_strcmp0(dir_name, file_path)) {
1190 MSG_SEC_ERR("!dir_name || !g_strcmp0(dir_name, %s)", file_path);
1191 err = MSG_ERR_INVALID_PARAMETER;
1195 smack_getlabel(dir_name, &dir_smack_label, SMACK_LABEL_ACCESS);
1196 if (dir_smack_label == NULL) {
1197 MSG_ERR("smack_getlabel failed (dir_smack_label)");
1198 err = MSG_ERR_PERMISSION_DENIED;
1202 if (smack_have_access(app_smack_label, dir_smack_label, "RX") < 1) {
1203 MSG_ERR("smack_have_access failed (dir_smack_label)");
1204 err = MSG_ERR_PERMISSION_DENIED;
1208 smack_getlabel(file_path, &path_smack_label, SMACK_LABEL_ACCESS);
1209 if (path_smack_label == NULL) {
1210 MSG_ERR("smack_getlabel failed (path_smack_label)");
1211 err = MSG_ERR_PERMISSION_DENIED;
1215 if (smack_have_access(app_smack_label, path_smack_label, "R") < 1) {
1216 MSG_ERR("smack_have_access failed (path_smack_label)");
1217 err = MSG_ERR_PERMISSION_DENIED;
1221 MSG_DEBUG("smack_have_access pass successfully");
1224 MSG_FREE(path_smack_label);
1225 MSG_FREE(dir_smack_label);
1231 bool MsgScanFile(char *filePath)
1233 if (filePath == NULL) {
1234 MSG_DEBUG("Invalid Parameter src = %p", filePath);
1238 msg_error_t ret = media_content_connect();
1240 if (ret == MEDIA_CONTENT_ERROR_NONE) {
1241 if (media_content_scan_file(filePath) != MEDIA_CONTENT_ERROR_NONE) {
1242 MSG_ERR("media_content_scan_file() is failed , %d", ret);
1243 media_content_disconnect();
1247 ret = media_content_disconnect();
1249 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1250 MSG_ERR("media_content_disconnect is failed , %d", ret);
1254 MSG_ERR("media_content_connect is failed , %d", ret);
1262 void MsgGetMimeType(char *filePath, char *mimeType, int size)
1264 aul_get_mime_from_file(filePath, mimeType, size);
1268 int MsgTcsScanFile(const char *filepath, int *bLevel)
1272 TCSScanResult result;
1273 TCSDetected* pDetected;
1275 int ret_b_level = -1;
1277 if (MsgAccessFile(filepath, R_OK) == false) {
1278 MSG_SEC_DEBUG("not exist source file [%s]", filepath);
1282 MSG_SEC_DEBUG("Scanning file name : %s\n", filepath);
1284 hLib = TCSLibraryOpen();
1285 if(hLib == INVALID_TCSLIB_HANDLE) {
1286 MSG_DEBUG("TCSLibraryOpen error\n");
1290 rtn = TCSScanFile(hLib, filepath, TCS_DTYPE_UNKNOWN, TCS_SA_SCANONLY, 1, &result);
1293 MSG_DEBUG("Detected malware number: %d\n", result.iNumDetected);
1294 i = result.iNumDetected;
1295 pDetected = result.pDList;
1296 while(i && pDetected)
1300 MSG_SEC_DEBUG(" +-- Malware [%d] Name: %s\n", i, pDetected->pszName);
1301 MSG_DEBUG(" +-- Malware [%d] uAction: %u : 0x%04x\n", i, pDetected->uAction, pDetected->uAction);
1303 temp_b_level = (pDetected->uAction & 0xFF00) >> 8;
1304 MSG_DEBUG(" +-- Malware [%d] Behavior level: %u\n", i, temp_b_level);
1306 if (ret_b_level == -1 || ret_b_level < temp_b_level) {
1307 ret_b_level = temp_b_level;
1310 temp_s_class = (pDetected->uAction & 0x00FF);
1311 MSG_DEBUG(" +-- Malware [%d] Severity class: %u\n", i, temp_s_class);
1313 pDetected = pDetected->pNext;
1317 result.pfFreeResult(&result);
1319 MSG_DEBUG("TCSScanFile fail: err = %d\n", rtn);
1322 TCSLibraryClose(hLib);
1325 *bLevel = ret_b_level;
1336 /* In msg-service.spec file use libacl-devel. Because acl get not process open API */
1337 /* So have to set TARGET_LINK_LIBRARIES(acl) in makefile */
1338 /* After GPL-3.0, not supply shell commend (setfacl, getfacl) by license issue, use the API */
1341 const char *priv_read =
1344 "group:priv_message_read:rwx\n"
1350 acl = acl_from_text(priv_read);
1352 MSG_ERR("%s: `%s': %s\n", MSG_IPC_DATA_PATH, priv_read, g_strerror(errno));
1356 ret = acl_check(acl, NULL);
1359 if (ret == ACL_DUPLICATE_ERROR) {
1360 MSG_DEBUG("Already Set ACL");
1363 MSG_ERR("acl_check Fail : [%d],[%s]", ret, acl_error(ret));
1367 ret = acl_set_file((const char *)MSG_IPC_DATA_PATH, ACL_TYPE_ACCESS, acl);
1369 MSG_ERR("acl_set_file Fail : [%d][%s]", ret, g_strerror(errno));