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"
52 void thumbnail_completed_cb(thumbnail_util_error_e error,
53 const char *request_id,
56 unsigned char *thumb_data,
65 MSG_WARN("dstPath is NULL");
71 MSG_DEBUG("=================[RESULT]");
72 MSG_DEBUG("error_code [%d]", error);
73 MSG_DEBUG("request id [%s]", request_id);
74 MSG_DEBUG("width [%d], height [%d]", thumb_width, thumb_height);
75 MSG_DEBUG("size [%d]", thumb_size);
78 image_util_encode_h eh = NULL;
81 unsigned long long ll_size_encode = 0;
83 ret = image_util_encode_create(IMAGE_UTIL_JPEG, &eh);
84 if (IMAGE_UTIL_ERROR_NONE != ret)
86 ret = image_util_encode_set_input_buffer(eh, thumb_data);
87 if (IMAGE_UTIL_ERROR_NONE != ret)
89 ret = image_util_encode_set_resolution(eh, thumb_width, thumb_height);
90 if (IMAGE_UTIL_ERROR_NONE != ret)
92 ret = image_util_encode_set_colorspace(eh, IMAGE_UTIL_COLORSPACE_BGRA8888);
93 if (IMAGE_UTIL_ERROR_NONE != ret)
95 ret = image_util_encode_set_quality(eh, 100);
96 if (IMAGE_UTIL_ERROR_NONE != ret)
98 ret = image_util_encode_set_output_path(eh, (char *)user_data);
99 if (IMAGE_UTIL_ERROR_NONE != ret)
101 ret = image_util_encode_run(eh, &ll_size_encode);
105 image_util_encode_destroy(eh);
107 if (ret != IMAGE_UTIL_ERROR_NONE)
108 MSG_WARN("image_util_encode_jpeg() is failed");
116 /*==================================================================================================
117 FUNCTION IMPLEMENTATION
118 ==================================================================================================*/
119 bool MakeThumbnail(char *srcPath, char *dstPath)
121 if (srcPath == NULL || dstPath == NULL) {
122 MSG_SEC_DEBUG("Invalid Param src = %p, dst = %p", srcPath, dstPath);
126 if (MsgAccessFile(srcPath, R_OK) == false) {
127 MSG_SEC_DEBUG("not exist source file [%s]", srcPath);
135 int ret = THUMBNAIL_UTIL_ERROR_NONE;
138 thumbnail_util_create(&thumb_h);
139 thumbnail_util_set_path(thumb_h, srcPath);
141 ret = thumbnail_util_extract(thumb_h, thumbnail_completed_cb, dstPath, &req_id);
142 thumbnail_util_destroy(thumb_h);
148 if (ret != THUMBNAIL_UTIL_ERROR_NONE) {
149 MSG_ERR("thumbnail_util_extract is failed");
154 time_ret = g_cv.timedwait(g_mx.pMsgMutex(), 5);
158 if (time_ret == ETIMEDOUT) {
159 MSG_ERR("@@ WAKE by timeout@@");
163 if (MsgAccessFile(dstPath, F_OK) == false) {
164 MSG_SEC_DEBUG("not exist result file [%s]", dstPath);
168 MSG_SEC_DEBUG("Make thumbnail: success [%s]", dstPath);
172 /* File operation wrappers */
173 FILE *MsgOpenFile(const char *filepath, const char *opt)
175 if (!filepath || !opt) {
176 MSG_FATAL("Null parameter");
180 MSG_SEC_DEBUG("[FILE] filepath : [%s], opt [%s]", filepath, opt);
185 pFile = fopen(filepath, opt);
186 MSG_DEBUG("[FILE] pFile [%p]", pFile);
187 } catch (exception &e) {
188 MSG_FATAL("%s", e.what());
195 void MsgCloseFile(FILE *pFile)
198 MSG_FATAL("NULL parameter");
202 MSG_DEBUG("[FILE] pFile [%p]", pFile);
206 } catch (exception &e) {
207 MSG_FATAL("%s", e.what());
211 int MsgFseek(FILE *pFile, long int offset, int origin)
214 MSG_FATAL("pFile NULL");
220 MSG_DEBUG("[FILE] pFile [%p], offset [%ld], origin [%d]", pFile, offset, origin);
223 ret = fseek(pFile, offset, origin); /* return 0, if success. */
224 } catch (exception &e) {
225 MSG_FATAL("%s", e.what());
232 size_t MsgWriteFile(const char *pData, size_t size, size_t count, FILE *pFile)
234 if (!pData || !pFile) {
235 MSG_FATAL("pData or pFile NULL");
241 MSG_DEBUG("[FILE] pData [%p], size [%zu], count [%zu], pFile [%p]", pData, size, count, pFile);
244 nWrite = fwrite(pData, size, count, pFile);
245 } catch (exception &e) {
246 MSG_FATAL("%s", e.what());
252 size_t MsgReadFile(void *pData, size_t size, size_t count, FILE *pFile)
254 if (!pData || !pFile) {
255 MSG_FATAL("pData or pFile NULL");
262 nRead = fread(pData, size, count, pFile);
263 } catch (exception &e) {
264 MSG_FATAL("%s", e.what());
270 long int MsgFtell(FILE *pFile)
273 MSG_FATAL("pFile NULL");
277 long int ret = -1L; /* -1L return if error occured. */
281 } catch (exception &e) {
282 MSG_FATAL("%s", e.what());
288 int MsgFflush(FILE *pFile)
291 MSG_FATAL("pFile NULL");
298 ret = fflush(pFile); /* return 0 if success */
299 } catch (exception &e) {
300 MSG_FATAL("%s", e.what());
306 int MsgFsync(FILE *pFile)
309 MSG_FATAL("pFile NULL");
316 ret = fdatasync(pFile->_fileno); /* return 0 if success */
317 } catch (exception &e) {
318 MSG_FATAL("%s", e.what());
324 bool MsgCreateFileName(char *pFileName)
326 if (pFileName == NULL) {
327 MSG_DEBUG("[ERROR] pFileName is NULL");
334 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
335 MSG_DEBUG("clock_gettime() error: %s", g_strerror(errno));
339 /* Create Random Number */
340 srandom((unsigned int)ts.tv_nsec);
342 MSG_DEBUG("ts.tv_nsec : %ld", ts.tv_nsec);
344 /* between 1 - 1000000000 */
345 snprintf(pFileName, MSG_FILENAME_LEN_MAX, "MSG_%lu.DATA", random()%1000000000+1);
346 } catch (exception& e) {
347 MSG_FATAL("%s", e.what());
355 bool MsgOpenAndReadFile(const char *pFileName, char **ppData, int *pDataSize)
357 if (!pFileName || !ppData || !pDataSize) {
358 MSG_ERR("Invalid params");
362 MSG_DEBUG("MsgOpenAndReadFile");
366 char fullPath[MAX_FULL_PATH_SIZE] = {0};
368 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
369 MSG_SEC_DEBUG("open file name: %s", fullPath);
372 if (stat(fullPath, &st) != 0) {
373 MSG_SEC_ERR("stat(%s, &st) != 0", fullPath);
376 if (S_ISDIR(st.st_mode)) {
377 MSG_ERR("S_ISDIR(st.st_mode)");
381 pFile = MsgOpenFile(fullPath, "rb");
384 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
388 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
390 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
394 int FileSize = MsgFtell(pFile);
397 MSG_DEBUG("Filesize is error : %d", FileSize);
403 *ppData = new char[FileSize+1];
404 memset(*ppData, 0x00, (FileSize+1));
406 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
408 MSG_DEBUG("File seek Error: %s", g_strerror(errno));
412 if (MsgReadFile(*ppData, sizeof(char), FileSize, pFile) != (size_t)FileSize) {
414 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
418 *pDataSize = FileSize;
426 bool MsgReadFileForDecode(FILE *pFile, char *pBuf, int length, int *nSize)
430 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
431 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
436 *nSize = MsgReadFile(pBuf, sizeof(char), length, pFile);
443 bool MsgWriteIpcFile(const char *pFileName, const char *pData, int DataSize)
446 MSG_DEBUG("NULL parameter, pFileName [%p], pData [%p]", pFileName, pData);
450 char fullPath[MAX_FULL_PATH_SIZE] = {0};
452 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
454 FILE *pFile = MsgOpenFile(fullPath, "wb+");
457 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
461 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
463 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
467 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
469 MSG_DEBUG("File Write Error: %s", g_strerror(errno));
479 int MsgReadSmilFile(const char *pFileName, char **ppData)
482 MSG_DEBUG("pFileName is NULL");
487 char fullPath[MAX_FULL_PATH_SIZE] = {0};
489 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_SMIL_FILE_PATH, pFileName);
491 MSG_SEC_DEBUG("open file name: %s", fullPath);
493 FILE *pFile = MsgOpenFile(fullPath, "rb");
496 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
500 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
502 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
506 int FileSize = MsgFtell(pFile);
509 MSG_DEBUG("Filesize is error : %d", FileSize);
514 *ppData = new char[FileSize + 1];
515 memset(*ppData, 0x00, (FileSize+1));
517 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
519 MSG_DEBUG("File Sead Error: %s", g_strerror(errno));
523 if (MsgReadFile(*ppData, sizeof(char), FileSize, pFile) != (size_t)FileSize) {
525 MSG_DEBUG("File Read Error: %s", g_strerror(errno));
529 /* ppData[FileSize] = '\0'; */
538 bool MsgWriteSmilFile(const char *pFilePath, char *pData, int DataSize)
541 MSG_DEBUG("pFilePath is NULL");
546 if (mkdir(MSG_SMIL_FILE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
547 if (errno == EEXIST) {
548 MSG_SEC_DEBUG("The %s already exists", MSG_SMIL_FILE_PATH);
550 MSG_SEC_DEBUG("Error while mkdir %s", MSG_SMIL_FILE_PATH);
555 FILE *pFile = MsgOpenFile(pFilePath, "wb+");
558 MSG_DEBUG("File Open Error: %s", g_strerror(errno));
562 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
564 MSG_DEBUG("File Seek Error: %s", g_strerror(errno));
568 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
570 MSG_DEBUG("File Write Error: %s", g_strerror(errno));
581 void MsgDeleteFile(const char *pFileName)
584 MSG_FATAL("pFileName is NULL");
588 if (strlen(pFileName) == 0) {
589 MSG_FATAL("pFileName has zero length");
593 char fullPath[MAX_FULL_PATH_SIZE] = {0};
596 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
598 MSG_SEC_DEBUG("%s", fullPath);
600 if (remove(fullPath) != 0)
601 MSG_SEC_ERR("File Delete Error [%s]: %s", fullPath, g_strerror(errno));
602 } catch (exception &e) {
603 MSG_FATAL("%s", e.what());
608 void MsgDeleteSmilFile(const char *pFileName)
611 MSG_FATAL("pFileName NULL");
616 char fullPath[MAX_FULL_PATH_SIZE] = {0};
618 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_SMIL_FILE_PATH, pFileName);
620 if (remove(fullPath) != 0)
621 MSG_SEC_ERR("File Delete Error [%s]: %s", fullPath, g_strerror(errno));
622 } catch (exception &e) {
623 MSG_FATAL("%s", e.what());
628 bool MsgGetFileSize(const char *pFilePath, int *nSize)
631 MSG_FATAL("pFileName NULL");
637 pFile = MsgOpenFile(pFilePath, "rb");
640 MSG_DEBUG("File Open error: %s", g_strerror(errno));
644 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
646 MSG_FATAL("File Read Error: %s", g_strerror(errno));
650 *nSize = MsgFtell(pFile);
658 FILE *MsgOpenMMSFile(char *pFileName)
663 MSG_DEBUG("pFileName NULL: %s", g_strerror(errno));
667 len = strlen(pFileName);
669 for (int i = 0; i < len; i++) {
670 switch (pFileName[i]) {
677 MSG_SEC_DEBUG("pFileName = %s", pFileName);
679 char fullPath[MAX_FULL_PATH_SIZE+1] = {0};
681 snprintf(fullPath, MAX_FULL_PATH_SIZE+1, "%s.mms", pFileName);
683 FILE *pFile = MsgOpenFile(fullPath, "wb+");
686 MSG_ERR("File Open Error: %s", g_strerror(errno));
690 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
692 MSG_ERR("File Read Error: %s", g_strerror(errno));
700 bool MsgWriteDataFromEncodeBuffer(FILE *pFile, char *pInBuffer, int *pPtr, int maxLen, int *pOffset )
702 if (!pFile || !pPtr || !pInBuffer || !pOffset) {
703 MSG_FATAL(" NULL parameter passed");
707 MSG_DEBUG("MsgWriteDataFromEncodeBuffer:");
708 MSG_DEBUG("pInBuffer %s", pInBuffer);
709 MSG_DEBUG("pPtr %d", (*pPtr));
710 MSG_DEBUG("before to fwite %p", pFile);
712 if (MsgWriteFile(pInBuffer, sizeof(char), (*pPtr), pFile) != (size_t)(*pPtr)) {
713 MSG_FATAL("MsgWriteFile failed");
717 MSG_DEBUG("after to fwite \n");
721 memset(pInBuffer, 0, maxLen);
725 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
726 MSG_FATAL("MsgFseek failed");
730 *pOffset = MsgFtell(pFile);
732 if (*pOffset == -1L) {
733 MSG_FATAL("MsgFtell failed");
741 bool MsgOpenCreateAndOverwriteFile(char *pFullPath, char *pBuff, int TotalLength)
745 if ((pFile = MsgOpenFile(pFullPath, "wb+")) == NULL) {
746 MSG_FATAL("MsgOpenFile errer");
750 if (MsgWriteFile(pBuff, sizeof(char), TotalLength, pFile) != (size_t)TotalLength) {
755 MsgFsync(pFile); /* file is written to device immediately, it prevents missing file data from unexpected power off */
763 char *MsgOpenAndReadMmsFile(const char *szFilePath, int offset, int size, int *npSize)
769 if (szFilePath == NULL) {
770 MSG_ERR("szFilePath id NULL");
776 pFile = MsgOpenFile(szFilePath, "rb");
779 MSG_ERR("Can't open file: %s", g_strerror(errno));
784 if (MsgGetFileSize(szFilePath, & readSize) == false) {
785 MSG_DEBUG("MsgGetFileSize: failed");
791 /* restore Kies backup data size greater than FM_READ_WRITE_BUFFER_MAX */
793 if (readSize > FM_READ_WRITE_BUFFER_MAX) {
794 MSG_DEBUG("MsgOpenAndReadMmsFile: File size tried to read too big");
799 pData = (char *)calloc(1, readSize + 1);
800 if ( NULL == pData ) {
801 MSG_ERR("pData MemAlloc Fail : %s", g_strerror(errno) );
805 if (MsgFseek(pFile, offset, SEEK_SET) < 0) {
806 MSG_ERR("FmSeekFile failed : %s", g_strerror(errno) );
810 *npSize = MsgReadFile(pData, sizeof(char), readSize, pFile);
816 *(pData + (*npSize)) = '\0';
837 /* it is equivalent to "rm -rf pDirPath" */
838 int MsgRmRf(char *pDirPath)
840 struct dirent *d = NULL;
843 dir = opendir(pDirPath);
846 MSG_FATAL("error opendir: %s", g_strerror(errno));
850 int size = strlen(pDirPath) + 256;
852 char *path = (char*)malloc(size);
855 MSG_DEBUG("path is NULL");
863 while ((d = readdir(dir)) != NULL) {
864 if (d->d_type == DT_DIR) {
865 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
867 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
872 if (rmdir(path) != 0) {
878 MSG_FATAL("error rmdir: %s", g_strerror(errno));
883 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
885 if (MsgDrmIsDrmFile(path))
886 MsgDrmUnregisterFile(path);
888 if (remove(path) != 0) {
894 MSG_FATAL("error remove: %s", g_strerror(errno));
914 int MsgGetFileSize(const char *pFileName)
916 struct stat file_stat;
918 if (lstat(pFileName, &file_stat)) {
919 MSG_FATAL("error lstat: %s", g_strerror(errno));
923 return file_stat.st_size;
927 /* it is equivalent to "du dir_path" */
928 unsigned int MsgDu(const char *pDirPath)
930 struct dirent *d = NULL;
933 dir = opendir(pDirPath);
936 MSG_FATAL("error opendir: %s", g_strerror(errno));
940 int size = strlen(pDirPath) + 256;
941 char *path = (char*)malloc(size);
949 unsigned int totalFileSize = 0;
952 while ((d = readdir(dir)) != NULL) {
953 if(d->d_type == DT_DIR) {
954 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
956 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
959 unsigned int dirSize = MsgDu(path);
962 MSG_FATAL("error MsgDu");
968 totalFileSize += dirSize;
970 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
971 int fileSize = MsgGetFileSize(path);
974 MSG_FATAL("error MsgGetFileSize");
980 totalFileSize += fileSize;
992 return totalFileSize;
996 bool MsgAppendFile(const char *pFilePath, const char *pData, int DataSize)
999 MSG_FATAL("NULL check error, pFileName %p, pData %p", pFilePath, pData);
1003 char fullPath[MAX_FULL_PATH_SIZE] = {0};
1005 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s", pFilePath);
1007 FILE *pFile = MsgOpenFile(fullPath, "a+");
1009 if (pFile == NULL) {
1010 MSG_FATAL("File Open Error: %s", g_strerror(errno));
1014 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
1015 MsgCloseFile(pFile);
1016 MSG_FATAL("File Sead Error: %s", g_strerror(errno));
1020 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
1021 MsgCloseFile(pFile);
1022 MSG_FATAL("File Write Error: %s", g_strerror(errno));
1026 MsgFsync(pFile); /*file is written to device immediately, it prevents missing file data from unexpected power off */
1028 MsgCloseFile(pFile);
1032 void MsgMmsInitDir()
1034 struct dirent *d = NULL;
1037 dir = opendir(MSG_DATA_PATH);
1040 MSG_FATAL("error opendir: %s", g_strerror(errno));
1044 /* Remove temporal Mms folder */
1045 while ((d = readdir(dir)) != NULL) {
1046 if (d->d_type == DT_DIR) {
1047 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
1050 if(strstr(d->d_name, ".dir") != NULL) {
1051 char filePath[MSG_FILEPATH_LEN_MAX] = {0, };
1052 snprintf(filePath, MSG_FILEPATH_LEN_MAX, "%s%s", MSG_DATA_PATH, d->d_name);
1064 /* mode : R_OK, W_OK, X_OK, or the existence test F_OK. */
1065 bool MsgAccessFile(const char *filepath, int mode)
1068 if (filepath == NULL) {
1069 MSG_DEBUG("filepath is NULL");
1073 MSG_SEC_DEBUG("request access path = %s, mode = %d", filepath, mode);
1075 ret = access(filepath, mode);
1079 MSG_DEBUG("Fail to access file, ret = %d", ret);
1087 bool MsgChmod(const char *filepath, int mode)
1090 struct stat lstat_info;
1091 struct stat fstat_info;
1094 if (lstat(filepath, &lstat_info) == -1) {
1095 MSG_SEC_DEBUG("No such file as [%s].", filepath);
1099 fd = open(filepath, O_RDONLY);
1102 MSG_SEC_DEBUG("Fail to open [%s].", filepath);
1106 if (fstat(fd, &fstat_info) == -1) {
1107 MSG_SEC_DEBUG("Fail to fstat [%s].", filepath);
1112 if (lstat_info.st_mode == fstat_info.st_mode &&
1113 lstat_info.st_ino == fstat_info.st_ino &&
1114 lstat_info.st_dev == fstat_info.st_dev) {
1115 if (fchmod(fd, mode) < 0) {
1116 MSG_SEC_DEBUG("Fail to fchmod [%s].", filepath);
1128 bool MsgChown(const char *filepath, int uid, int gid)
1130 struct stat lstat_info;
1131 struct stat fstat_info;
1134 if (lstat(filepath, &lstat_info) == -1) {
1135 MSG_SEC_INFO("No such file as [%s].", filepath);
1139 fd = open(filepath, O_RDONLY);
1142 MSG_SEC_INFO("Fail to open [%s].", filepath);
1146 if (fstat(fd, &fstat_info) == -1) {
1147 MSG_SEC_INFO("Fail to fstat [%s].", filepath);
1152 if (lstat_info.st_mode == fstat_info.st_mode &&
1153 lstat_info.st_ino == fstat_info.st_ino &&
1154 lstat_info.st_dev == fstat_info.st_dev) {
1155 if (fchown(fd, uid, gid) < 0) {
1156 MSG_SEC_INFO("Fail to fchown [%s].", filepath);
1166 bool MsgCreateFile(const char *pFilePath, char *pData, int DataSize)
1169 MSG_DEBUG("pFilePath is NULL");
1173 FILE *pFile = MsgOpenFile(pFilePath, "wb+");
1175 if (pFile == NULL) {
1176 MSG_ERR("File Open Error: %s", g_strerror(errno));
1180 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
1181 MsgCloseFile(pFile);
1182 MSG_ERR("File Seek Error: %s", g_strerror(errno));
1186 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
1187 MsgCloseFile(pFile);
1188 MSG_ERR("File Write Error: %s", g_strerror(errno));
1194 MsgCloseFile(pFile);
1199 char *MsgGetDirName(char *file_path)
1201 return g_path_get_dirname(file_path);
1205 char *MsgGetFileName(char *file_path)
1207 return g_path_get_basename(file_path);
1211 int MsgCheckFilepathSmack(const char *app_smack_label, char *file_path)
1213 int err = MSG_SUCCESS;
1215 char *path_smack_label = NULL;
1216 char *dir_smack_label = NULL;
1217 char *dir_name = NULL;
1219 if (!file_path || file_path[0] == '\0') {
1224 if (stat(file_path, &st) != 0) {
1225 MSG_SEC_ERR("stat(%s, &st) != 0", file_path);
1226 return MSG_ERR_PERMISSION_DENIED;
1228 if (S_ISDIR(st.st_mode)) {
1229 MSG_ERR("S_ISDIR(st.st_mode)");
1230 return MSG_ERR_INVALID_PARAMETER;
1233 dir_name = MsgGetDirName(file_path);
1234 if (!dir_name || !g_strcmp0(dir_name, file_path)) {
1235 MSG_SEC_ERR("!dir_name || !g_strcmp0(dir_name, %s)", file_path);
1236 err = MSG_ERR_INVALID_PARAMETER;
1240 smack_getlabel(dir_name, &dir_smack_label, SMACK_LABEL_ACCESS);
1241 if (dir_smack_label == NULL) {
1242 MSG_ERR("smack_getlabel failed (dir_smack_label)");
1243 err = MSG_ERR_PERMISSION_DENIED;
1247 if (smack_have_access(app_smack_label, dir_smack_label, "RX") < 1) {
1248 MSG_ERR("smack_have_access failed (dir_smack_label)");
1249 err = MSG_ERR_PERMISSION_DENIED;
1253 smack_getlabel(file_path, &path_smack_label, SMACK_LABEL_ACCESS);
1254 if (path_smack_label == NULL) {
1255 MSG_ERR("smack_getlabel failed (path_smack_label)");
1256 err = MSG_ERR_PERMISSION_DENIED;
1260 if (smack_have_access(app_smack_label, path_smack_label, "R") < 1) {
1261 MSG_ERR("smack_have_access failed (path_smack_label)");
1262 err = MSG_ERR_PERMISSION_DENIED;
1266 MSG_DEBUG("smack_have_access pass successfully");
1269 MSG_FREE(path_smack_label);
1270 MSG_FREE(dir_smack_label);
1276 void MsgGetMimeType(char *filePath, char *mimeType, int size)
1278 aul_get_mime_from_file(filePath, mimeType, size);
1281 int MsgTcsScanFile(const char *filepath, int *bLevel)
1285 int (*_csr_cs_context_create)(void **handle);
1286 int (*_csr_cs_scan_file)(void *handle, const char *filepath, void **malware);
1287 int (*_csr_cs_context_destroy)(void *handle);
1288 int (*_csr_cs_malware_get_severity)(void *malware, void *severity);
1289 int (*_csr_cs_malware_get_name)(void *malware, char **name);
1291 if (MsgAccessFile(filepath, R_OK) == false) {
1292 MSG_SEC_DEBUG("not exist source file [%s]", filepath);
1296 MSG_SEC_DEBUG("Scanning file name : %s\n", filepath);
1298 void *lib_handle = NULL;
1299 lib_handle = dlopen(PATH_LIBCSR_CLIENT, RTLD_LAZY);
1301 MSG_ERR("Unable to open %s", PATH_LIBCSR_CLIENT);
1305 _csr_cs_context_create = (int(*)(void**))dlsym(lib_handle, "csr_cs_context_create");
1306 _csr_cs_scan_file = (int(*)(void*, const char*, void**))dlsym(lib_handle, "csr_cs_scan_file");
1307 _csr_cs_context_destroy = (int(*)(void*))dlsym(lib_handle, "csr_cs_context_destroy");
1308 _csr_cs_malware_get_severity = (int(*)(void*, void*))dlsym(lib_handle, "csr_cs_malware_get_severity");
1309 _csr_cs_malware_get_name = (int(*)(void*, char**))dlsym(lib_handle, "csr_cs_malware_get_name");
1312 if (!_csr_cs_context_create || !_csr_cs_scan_file || !_csr_cs_context_destroy
1313 || !_csr_cs_malware_get_severity || !_csr_cs_malware_get_name) {
1314 MSG_ERR("Failed to load CSR symbols");
1316 dlclose(lib_handle);
1320 void *csr_handle = NULL;
1321 ret = _csr_cs_context_create(&csr_handle);
1323 MSG_DEBUG("csr_cs_context_create error: err = %d\n", ret);
1325 dlclose(lib_handle);
1330 void *detected = NULL;
1331 ret = _csr_cs_scan_file(csr_handle, filepath, &detected);
1333 MSG_DEBUG("csr_cs_scan_file fail: err = %d\n", ret);
1337 if (NULL == detected) {
1338 MSG_DEBUG("Nothing detected");
1342 int severity = 0x01; /* CSR_CS_SEVERITY_LOW */
1343 ret = _csr_cs_malware_get_severity(detected, &severity);
1345 MSG_DEBUG("csr_cs_malware_get_severity error: err = %d\n", ret);
1347 MSG_DEBUG(" +-- Malware Severity class: %d\n", severity);
1350 ret = _csr_cs_malware_get_name(detected, &name);
1352 MSG_DEBUG("csr_cs_malware_get_name error: err = %d\n", ret);
1354 MSG_SEC_DEBUG(" +-- Malware Name: [%s]\n", name);
1364 ret = _csr_cs_context_destroy(csr_handle);
1366 MSG_DEBUG("csr_cs_context_destroy error: err = %d\n", ret);
1369 dlclose(lib_handle);