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)
58 MSG_WARN("dstPath is NULL");
65 MSG_DEBUG("=================[RESULT]");
66 MSG_DEBUG("error_code [%d]", error);
67 MSG_DEBUG("request id [%s]", request_id);
68 MSG_DEBUG("width [%d], height [%d]", thumb_width, thumb_height);
69 MSG_DEBUG("raw_data [0x%x], size [%d]", *thumb_data, thumb_size);
72 ret = image_util_encode_jpeg(thumb_data, thumb_width, thumb_height, IMAGE_UTIL_COLORSPACE_BGRA8888, 100, (char *)user_data);
73 if (ret != IMAGE_UTIL_ERROR_NONE)
74 MSG_WARN("image_util_encode_jpeg() is failed");
82 /*==================================================================================================
83 FUNCTION IMPLEMENTATION
84 ==================================================================================================*/
85 bool MakeThumbnail(char *srcPath, char *dstPath)
87 if (srcPath == NULL || dstPath == NULL) {
88 MSG_SEC_DEBUG("Invalid Param src = %p, dst = %p", srcPath, dstPath);
92 if (MsgAccessFile(srcPath, R_OK) == false) {
93 MSG_SEC_DEBUG("not exist source file [%s]", srcPath);
101 int ret = THUMBNAIL_UTIL_ERROR_NONE;
104 thumbnail_util_create(&thumb_h);
105 thumbnail_util_set_path(thumb_h, srcPath);
107 ret = thumbnail_util_extract(thumb_h, thumbnail_completed_cb, dstPath, &req_id);
108 thumbnail_util_destroy(thumb_h);
114 if (ret != THUMBNAIL_UTIL_ERROR_NONE) {
115 MSG_ERR("thumbnail_util_extract is failed");
120 time_ret = g_cv.timedwait(g_mx.pMutex(), 5);
124 if (time_ret == ETIMEDOUT) {
125 MSG_ERR("@@ WAKE by timeout@@");
129 if (MsgAccessFile(dstPath, F_OK) == false) {
130 MSG_SEC_DEBUG("not exist result file [%s]", dstPath);
134 MSG_SEC_DEBUG("Make thumbnail: success [%s]", dstPath);
138 /* File operation wrappers */
139 FILE *MsgOpenFile(const char *filepath, const char *opt)
141 if (!filepath || !opt) {
142 MSG_FATAL("Null parameter");
146 MSG_SEC_DEBUG("[FILE] filepath : [%s], opt [%s]", filepath, opt);
151 pFile = fopen(filepath, opt);
152 MSG_DEBUG("[FILE] pFile [%p]", pFile);
153 } catch (exception &e) {
154 MSG_FATAL("%s", e.what());
161 void MsgCloseFile(FILE *pFile)
164 MSG_FATAL("NULL parameter");
168 MSG_DEBUG("[FILE] pFile [%p]", pFile);
172 } catch (exception &e) {
173 MSG_FATAL("%s", e.what());
177 int MsgFseek(FILE *pFile, long int offset, int origin)
180 MSG_FATAL("pFile NULL");
186 MSG_DEBUG("[FILE] pFile [%p], offset [%d], origin [%d]", pFile, offset, origin);
189 ret = fseek(pFile, offset, origin); /* return 0, if success. */
190 } catch (exception &e) {
191 MSG_FATAL("%s", e.what());
198 size_t MsgWriteFile(const char *pData, size_t size, size_t count, FILE *pFile)
200 if (!pData || !pFile) {
201 MSG_FATAL("pData or pFile NULL");
207 MSG_DEBUG("[FILE] pData [%p], size [%d], count [%d], pFile [%p]", pData, size, count, pFile);
210 nWrite = fwrite(pData, size, count, pFile);
211 } catch (exception &e) {
212 MSG_FATAL("%s", e.what());
218 size_t MsgReadFile(void *pData, size_t size, size_t count, FILE *pFile)
220 if (!pData || !pFile) {
221 MSG_FATAL("pData or pFile NULL");
228 nRead = fread(pData, size, count, pFile);
229 } catch (exception &e) {
230 MSG_FATAL("%s", e.what());
236 long int MsgFtell(FILE *pFile)
239 MSG_FATAL("pFile NULL");
243 long int ret = -1L; /* -1L return if error occured. */
247 } catch (exception &e) {
248 MSG_FATAL("%s", e.what());
254 int MsgFflush(FILE *pFile)
257 MSG_FATAL("pFile NULL");
264 ret = fflush(pFile); /* return 0 if success */
265 } catch (exception &e) {
266 MSG_FATAL("%s", e.what());
272 int MsgFsync(FILE *pFile)
275 MSG_FATAL("pFile NULL");
282 ret = fdatasync(pFile->_fileno); /* return 0 if success */
283 } catch (exception &e) {
284 MSG_FATAL("%s", e.what());
290 bool MsgCreateFileName(char *pFileName)
292 if (pFileName == NULL) {
293 MSG_DEBUG("[ERROR] pFileName is NULL");
300 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
301 MSG_DEBUG("clock_gettime() error: %s", g_strerror(errno));
305 /* Create Random Number */
306 srandom((unsigned int)ts.tv_nsec);
308 MSG_DEBUG("ts.tv_nsec : %d", ts.tv_nsec);
310 /* between 1 - 1000000000 */
311 snprintf(pFileName, MSG_FILENAME_LEN_MAX, "MSG_%lu.DATA", random()%1000000000+1);
312 } catch (exception& e) {
313 MSG_FATAL("%s", e.what());
321 bool MsgOpenAndReadFile(const char *pFileName, char **ppData, int *pDataSize)
323 if (!pFileName || !ppData || !pDataSize) {
324 MSG_ERR("Invalid params!! pFileName=%x, ppData=%x, pDataSize=%x", pFileName, ppData, pDataSize);
328 MSG_DEBUG("MsgOpenAndReadFile");
332 char fullPath[MAX_FULL_PATH_SIZE] = {0};
334 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
335 MSG_SEC_DEBUG("open file name: %s", fullPath);
337 pFile = MsgOpenFile(fullPath, "rb");
340 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
344 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
346 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
350 int FileSize = MsgFtell(pFile);
353 MSG_DEBUG("Filesize is error : %d", FileSize);
359 *ppData = new char[FileSize+1];
360 memset(*ppData, 0x00, (FileSize+1));
362 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
364 MSG_DEBUG("File seek Error: %s", g_strerror(errno));
368 if (MsgReadFile(*ppData, sizeof(char), FileSize, pFile) != (size_t)FileSize) {
370 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
374 *pDataSize = FileSize;
382 bool MsgReadFileForDecode(FILE *pFile, char *pBuf, int length, int *nSize)
386 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
387 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
392 *nSize = MsgReadFile(pBuf, sizeof(char), length, pFile);
399 bool MsgWriteIpcFile(const char *pFileName, const char *pData, int DataSize)
402 MSG_DEBUG("NULL parameter, pFileName [%p], pData [%p]", pFileName, pData);
406 char fullPath[MAX_FULL_PATH_SIZE] = {0};
408 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
410 FILE *pFile = MsgOpenFile(fullPath, "wb+");
413 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
417 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
419 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
423 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
425 MSG_DEBUG("File Write Error: %s", g_strerror(errno));
435 int MsgReadSmilFile(const char *pFileName, char **ppData)
438 MSG_DEBUG("pFileName is NULL");
443 char fullPath[MAX_FULL_PATH_SIZE] = {0};
445 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_SMIL_FILE_PATH, pFileName);
447 MSG_SEC_DEBUG("open file name: %s", fullPath);
449 FILE *pFile = MsgOpenFile(fullPath, "rb");
452 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
456 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
458 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
462 int FileSize = MsgFtell(pFile);
465 MSG_DEBUG("Filesize is error : %d", FileSize);
470 *ppData = new char[FileSize + 1];
471 memset(*ppData, 0x00, (FileSize+1));
473 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
475 MSG_DEBUG("File Sead Error: %s", g_strerror(errno));
479 if (MsgReadFile(*ppData, sizeof(char), FileSize, pFile) != (size_t)FileSize) {
481 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
485 /* ppData[FileSize] = '\0'; */
494 bool MsgWriteSmilFile(const char *pFilePath, char *pData, int DataSize)
497 MSG_DEBUG("pFilePath is NULL");
502 if (mkdir(MSG_SMIL_FILE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
503 if (errno == EEXIST) {
504 MSG_SEC_DEBUG("The %s already exists", MSG_SMIL_FILE_PATH);
506 MSG_SEC_DEBUG("Error while mkdir %s", MSG_SMIL_FILE_PATH);
511 FILE *pFile = MsgOpenFile(pFilePath, "wb+");
514 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
518 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
520 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
524 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
526 MSG_DEBUG("File Write Error: %s", g_strerror(errno));
537 void MsgDeleteFile(const char *pFileName)
540 MSG_FATAL("pFileName is NULL");
544 if (strlen(pFileName) == 0) {
545 MSG_FATAL("pFileName has zero length");
549 char fullPath[MAX_FULL_PATH_SIZE] = {0};
552 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
554 MSG_SEC_DEBUG("%s", fullPath);
556 if (remove(fullPath) != 0)
557 MSG_SEC_ERR("File Delete Error [%s]: %s", fullPath, g_strerror(errno));
558 } catch (exception &e) {
559 MSG_FATAL("%s", e.what());
564 void MsgDeleteSmilFile(const char *pFileName)
567 MSG_FATAL("pFileName NULL");
572 char fullPath[MAX_FULL_PATH_SIZE] = {0};
574 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_SMIL_FILE_PATH, pFileName);
576 if (remove(fullPath) != 0)
577 MSG_SEC_ERR("File Delete Error [%s]: %s", fullPath, g_strerror(errno));
578 } catch (exception &e) {
579 MSG_FATAL("%s", e.what());
584 bool MsgGetFileSize(const char *pFilePath, int *nSize)
587 MSG_FATAL("pFileName NULL");
593 pFile = MsgOpenFile(pFilePath, "rb");
596 MSG_DEBUG("File Open error: %s", g_strerror(errno));
600 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
602 MSG_FATAL("File Read Error: %s", g_strerror(errno));
606 *nSize = MsgFtell(pFile);
614 FILE *MsgOpenMMSFile(char *pFileName)
619 MSG_DEBUG("pFileName NULL: %s", g_strerror(errno));
623 len = strlen(pFileName);
625 for (int i = 0; i < len; i++) {
626 switch (pFileName[i]) {
633 MSG_SEC_DEBUG("pFileName = %s", pFileName);
635 char fullPath[MAX_FULL_PATH_SIZE+1] = {0};
637 snprintf(fullPath, MAX_FULL_PATH_SIZE+1, "%s.mms", pFileName);
639 FILE *pFile = MsgOpenFile(fullPath, "wb+");
642 MSG_ERR("File Open Error: %s", g_strerror(errno));
646 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
648 MSG_ERR("File Read Error: %s", g_strerror(errno));
656 bool MsgWriteDataFromEncodeBuffer(FILE *pFile, char *pInBuffer, int *pPtr, int maxLen, int *pOffset )
658 if (!pFile || !pPtr || !pInBuffer || !pOffset) {
659 MSG_FATAL(" NULL parameter passed");
663 MSG_DEBUG("MsgWriteDataFromEncodeBuffer:");
664 MSG_DEBUG("pInBuffer %x", pInBuffer);
665 MSG_DEBUG("pPtr %d", (*pPtr));
666 MSG_DEBUG("before to fwite %x", pFile);
668 if (MsgWriteFile(pInBuffer, sizeof(char), (*pPtr), pFile) != (size_t)(*pPtr)) {
669 MSG_FATAL("MsgWriteFile failed");
673 MSG_DEBUG("after to fwite \n");
677 memset(pInBuffer, 0, maxLen);
681 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
682 MSG_FATAL("MsgFseek failed");
686 *pOffset = MsgFtell(pFile);
688 if (*pOffset == -1L) {
689 MSG_FATAL("MsgFtell failed");
697 bool MsgOpenCreateAndOverwriteFile(char *pFullPath, char *pBuff, int TotalLength)
701 if ((pFile = MsgOpenFile(pFullPath, "wb+")) == NULL) {
702 MSG_FATAL("MsgOpenFile errer");
706 if (MsgWriteFile(pBuff, sizeof(char), TotalLength, pFile) != (size_t)TotalLength) {
711 MsgFsync(pFile); /* file is written to device immediately, it prevents missing file data from unexpected power off */
719 char *MsgOpenAndReadMmsFile(const char *szFilePath, int offset, int size, int *npSize)
725 if (szFilePath == NULL) {
726 MSG_ERR("szFilePath id NULL");
732 pFile = MsgOpenFile(szFilePath, "rb");
735 MSG_ERR("Can't open file: %s", g_strerror(errno));
740 if (MsgGetFileSize(szFilePath, & readSize) == false) {
741 MSG_DEBUG("MsgGetFileSize: failed");
747 /* restore Kies backup data size greater than FM_READ_WRITE_BUFFER_MAX */
749 if (readSize > FM_READ_WRITE_BUFFER_MAX) {
750 MSG_DEBUG("MsgOpenAndReadMmsFile: File size tried to read too big");
755 pData = (char *)calloc(1, readSize + 1);
756 if ( NULL == pData ) {
757 MSG_ERR("pData MemAlloc Fail : %s", g_strerror(errno) );
761 if (MsgFseek(pFile, offset, SEEK_SET) < 0) {
762 MSG_ERR("FmSeekFile failed : %s", g_strerror(errno) );
766 *npSize = MsgReadFile(pData, sizeof(char), readSize, pFile);
772 *(pData + (*npSize)) = '\0';
793 /* it is equivalent to "rm -rf pDirPath" */
794 int MsgRmRf(char *pDirPath)
796 struct dirent *d = NULL;
800 dir = opendir(pDirPath);
803 MSG_FATAL("error opendir: %s", g_strerror(errno));
807 int size = strlen(pDirPath) + 256;
809 char *path = (char*)malloc(size);
812 MSG_DEBUG("path is NULL");
819 for (readdir_r(dir, &entry, &d); d != NULL; readdir_r(dir, &entry, &d)) {
820 if (d->d_type == DT_DIR) {
821 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
823 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
828 if (rmdir(path) != 0) {
834 MSG_FATAL("error rmdir: %s", g_strerror(errno));
839 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
841 if (MsgDrmIsDrmFile(path))
842 MsgDrmUnregisterFile(path);
844 if (remove(path) != 0) {
850 MSG_FATAL("error remove: %s", g_strerror(errno));
867 int MsgGetFileSize(const char *pFileName)
869 struct stat file_stat;
871 if (lstat(pFileName, &file_stat)) {
872 MSG_FATAL("error lstat: %s", g_strerror(errno));
876 return file_stat.st_size;
880 /* it is equivalent to "du dir_path" */
881 unsigned int MsgDu(const char *pDirPath)
883 struct dirent *d = NULL;
887 dir = opendir(pDirPath);
890 MSG_FATAL("error opendir: %s", g_strerror(errno));
894 int size = strlen(pDirPath) + 256;
895 char *path = (char*)malloc(size);
903 unsigned int totalFileSize = 0;
905 for (readdir_r(dir, &entry, &d); d != NULL; readdir_r(dir, &entry, &d)) {
906 if(d->d_type == DT_DIR) {
907 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
909 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
912 unsigned int dirSize = MsgDu(path);
915 MSG_FATAL("error MsgDu");
921 totalFileSize += dirSize;
923 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
924 int fileSize = MsgGetFileSize(path);
927 MSG_FATAL("error MsgGetFileSize");
933 totalFileSize += fileSize;
942 return totalFileSize;
946 bool MsgAppendFile(const char *pFilePath, const char *pData, int DataSize)
949 MSG_FATAL("NULL check error, pFileName %p, pData %p", pFilePath, pData);
953 char fullPath[MAX_FULL_PATH_SIZE] = {0};
955 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s", pFilePath);
957 FILE *pFile = MsgOpenFile(fullPath, "a+");
960 MSG_FATAL("File Open Error: %s", g_strerror(errno));
964 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
966 MSG_FATAL("File Sead Error: %s", g_strerror(errno));
970 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
972 MSG_FATAL("File Write Error: %s", g_strerror(errno));
976 MsgFsync(pFile); /*file is written to device immediately, it prevents missing file data from unexpected power off */
984 struct dirent *d = NULL;
988 dir = opendir(MSG_DATA_PATH);
991 MSG_FATAL("error opendir: %s", g_strerror(errno));
995 /* Remove temporal Mms folder */
996 for (readdir_r(dir, &entry, &d); d != NULL; readdir_r(dir, &entry, &d)) {
997 if (d->d_type == DT_DIR) {
998 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
1001 if(strstr(d->d_name, ".dir") != NULL) {
1002 char filePath[MSG_FILEPATH_LEN_MAX] = {0, };
1003 snprintf(filePath, MSG_FILEPATH_LEN_MAX, "%s%s", MSG_DATA_PATH, d->d_name);
1015 /* mode : R_OK, W_OK, X_OK, or the existence test F_OK. */
1016 bool MsgAccessFile(const char *filepath, int mode)
1019 if (filepath == NULL) {
1020 MSG_DEBUG("filepath is NULL");
1024 MSG_SEC_DEBUG("request access path = %s, mode = %d", filepath, mode);
1026 ret = access(filepath, mode);
1030 MSG_DEBUG("Fail to access file, ret = %d", ret);
1038 bool MsgChmod(const char *filepath, int mode)
1041 struct stat lstat_info;
1042 struct stat fstat_info;
1045 if (lstat(filepath, &lstat_info) == -1) {
1046 MSG_SEC_DEBUG("No such file as [%s].", filepath);
1050 fd = open(filepath, O_RDONLY);
1053 MSG_SEC_DEBUG("Fail to open [%s].", filepath);
1057 if (fstat(fd, &fstat_info) == -1) {
1058 MSG_SEC_DEBUG("Fail to fstat [%s].", filepath);
1063 if (lstat_info.st_mode == fstat_info.st_mode &&
1064 lstat_info.st_ino == fstat_info.st_ino &&
1065 lstat_info.st_dev == fstat_info.st_dev) {
1066 if (fchmod(fd, mode) < 0) {
1067 MSG_SEC_DEBUG("Fail to fchmod [%s].", filepath);
1079 bool MsgChown(const char *filepath, int uid, int gid)
1081 struct stat lstat_info;
1082 struct stat fstat_info;
1085 if (lstat(filepath, &lstat_info) == -1) {
1086 MSG_SEC_INFO("No such file as [%s].", filepath);
1090 fd = open(filepath, O_RDONLY);
1093 MSG_SEC_INFO("Fail to open [%s].", filepath);
1097 if (fstat(fd, &fstat_info) == -1) {
1098 MSG_SEC_INFO("Fail to fstat [%s].", filepath);
1103 if (lstat_info.st_mode == fstat_info.st_mode &&
1104 lstat_info.st_ino == fstat_info.st_ino &&
1105 lstat_info.st_dev == fstat_info.st_dev) {
1106 if (fchown(fd, uid, gid) < 0) {
1107 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 bool MsgScanFile(char *filePath)
1230 if (filePath == NULL) {
1231 MSG_DEBUG("Invalid Parameter src = %p", filePath);
1235 msg_error_t ret = media_content_connect();
1237 if (ret == MEDIA_CONTENT_ERROR_NONE) {
1238 if (media_content_scan_file(filePath) != MEDIA_CONTENT_ERROR_NONE) {
1239 MSG_ERR("media_content_scan_file() is failed , %d", ret);
1240 media_content_disconnect();
1244 ret = media_content_disconnect();
1246 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1247 MSG_ERR("media_content_disconnect is failed , %d", ret);
1251 MSG_ERR("media_content_connect is failed , %d", ret);
1259 void MsgGetMimeType(char *filePath, char *mimeType, int size)
1261 aul_get_mime_from_file(filePath, mimeType, size);
1265 int MsgTcsScanFile(const char *filepath, int *bLevel)
1269 TCSScanResult result;
1270 TCSDetected* pDetected;
1272 int ret_b_level = -1;
1274 if (MsgAccessFile(filepath, R_OK) == false) {
1275 MSG_SEC_DEBUG("not exist source file [%s]", filepath);
1279 MSG_SEC_DEBUG("Scanning file name : %s\n", filepath);
1281 hLib = TCSLibraryOpen();
1282 if(hLib == INVALID_TCSLIB_HANDLE) {
1283 MSG_DEBUG("TCSLibraryOpen error\n");
1287 rtn = TCSScanFile(hLib, filepath, TCS_DTYPE_UNKNOWN, TCS_SA_SCANONLY, 1, &result);
1290 MSG_DEBUG("Detected malware number: %d\n", result.iNumDetected);
1291 i = result.iNumDetected;
1292 pDetected = result.pDList;
1293 while(i && pDetected)
1297 MSG_SEC_DEBUG(" +-- Malware [%d] Name: %s\n", i, pDetected->pszName);
1298 MSG_DEBUG(" +-- Malware [%d] uAction: %u : 0x%04x\n", i, pDetected->uAction, pDetected->uAction);
1300 temp_b_level = (pDetected->uAction & 0xFF00) >> 8;
1301 MSG_DEBUG(" +-- Malware [%d] Behavior level: %u\n", i, temp_b_level);
1303 if (ret_b_level == -1 || ret_b_level < temp_b_level) {
1304 ret_b_level = temp_b_level;
1307 temp_s_class = (pDetected->uAction & 0x00FF);
1308 MSG_DEBUG(" +-- Malware [%d] Severity class: %u\n", i, temp_s_class);
1310 pDetected = pDetected->pNext;
1314 result.pfFreeResult(&result);
1316 MSG_DEBUG("TCSScanFile fail: err = %d\n", rtn);
1319 TCSLibraryClose(hLib);
1322 *bLevel = ret_b_level;
1333 /* In msg-service.spec file use libacl-devel. Because acl get not process open API */
1334 /* So have to set TARGET_LINK_LIBRARIES(acl) in makefile */
1335 /* After GPL-3.0, not supply shell commend (setfacl, getfacl) by license issue, use the API */
1338 const char *priv_read =
1341 "group:priv_message_read:rwx\n"
1347 acl = acl_from_text(priv_read);
1349 MSG_ERR("%s: `%s': %s\n", MSG_IPC_DATA_PATH, priv_read, g_strerror(errno));
1353 ret = acl_check(acl, NULL);
1356 if (ret == ACL_DUPLICATE_ERROR) {
1357 MSG_DEBUG("Already Set ACL");
1360 MSG_ERR("acl_check Fail : [%d],[%s]", ret, acl_error(ret));
1364 ret = acl_set_file((const char *)MSG_IPC_DATA_PATH, ACL_TYPE_ACCESS, acl);
1366 MSG_ERR("acl_set_file Fail : [%d][%s]", ret, g_strerror(errno));