2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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.
25 #include <unistd.h> //sync()
27 #include "MsgStorageTypes.h"
29 #include "MsgException.h"
30 #include "MsgUtilFile.h"
31 #include "MsgMmsTypes.h"
32 #include "MsgInternalTypes.h"
33 #include "MsgDrmWrapper.h"
36 /*==================================================================================================
37 FUNCTION IMPLEMENTATION
38 ==================================================================================================*/
39 // File operation wrappers
40 FILE *MsgOpenFile(const char *filepath, const char *opt)
42 if (!filepath || !opt) {
43 MSG_FATAL("Null parameter");
47 MSG_DEBUG("[FILE] filepath : [%s], opt [%s]", filepath, opt);
52 pFile = fopen(filepath, opt);
53 MSG_DEBUG("[FILE] pFile [%p]", pFile);
54 } catch (exception &e) {
55 MSG_FATAL("%s", e.what());
62 void MsgCloseFile(FILE *pFile)
65 MSG_FATAL("NULL parameter");
69 MSG_DEBUG("[FILE] pFile [%p]", pFile);
73 } catch (exception &e) {
74 MSG_FATAL("%s", e.what());
78 int MsgFseek(FILE *pFile, long int offset, int origin)
81 MSG_FATAL("pFile NULL");
87 MSG_DEBUG("[FILE] pFile [%p], offset [%d], origin [%d]", pFile, offset, origin);
90 ret = fseek(pFile, offset, origin); // return 0, if success.
91 } catch (exception &e) {
92 MSG_FATAL("%s", e.what());
99 size_t MsgWriteFile(const char *pData, size_t size, size_t count, FILE *pFile)
101 if (!pData || !pFile) {
102 MSG_FATAL("pData or pFile NULL");
108 MSG_DEBUG("[FILE] pData [%p], size [%d], count [%d], pFile [%p]", pData, size, count, pFile);
111 nWrite = fwrite(pData, size, count, pFile);
112 } catch (exception &e) {
113 MSG_FATAL("%s", e.what());
119 size_t MsgReadFile(void *pData, size_t size, size_t count, FILE *pFile)
121 if (!pData || !pFile) {
122 MSG_FATAL("pData or pFile NULL");
129 nRead = fread(pData, size, count, pFile);
130 } catch (exception &e) {
131 MSG_FATAL("%s", e.what());
137 long int MsgFtell(FILE *pFile)
140 MSG_FATAL("pFile NULL");
144 long int ret = -1L; // -1L return if error occured.
148 } catch (exception &e) {
149 MSG_FATAL("%s", e.what());
155 int MsgFflush(FILE *pFile)
158 MSG_FATAL("pFile NULL");
165 ret = fflush(pFile); // return 0 if success
166 } catch (exception &e) {
167 MSG_FATAL("%s", e.what());
173 int MsgFsync(FILE *pFile)
176 MSG_FATAL("pFile NULL");
183 ret = fdatasync(pFile->_fileno); // return 0 if success
184 } catch (exception &e) {
185 MSG_FATAL("%s", e.what());
191 bool MsgCreateFileName(char *pFileName)
193 if (pFileName == NULL) {
194 MSG_DEBUG("[ERROR] pFileName is NULL");
201 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
202 MSG_DEBUG("clock_gettime() error: %s", strerror(errno));
206 // Create Random Number
207 srandom((unsigned int)ts.tv_nsec);
209 MSG_DEBUG("ts.tv_nsec : %d", ts.tv_nsec);
211 // between 1 - 1000000000
212 snprintf(pFileName, MSG_FILENAME_LEN_MAX, "MSG_%lu.DATA", random()%1000000000+1);
213 } catch (exception& e) {
214 MSG_FATAL("%s", e.what());
222 bool MsgOpenAndReadFile(const char *pFileName, char **ppData, int *pDataSize)
224 MSG_DEBUG("MsgOpenAndReadFile");
228 char fullPath[MAX_FULL_PATH_SIZE] = {0};
230 snprintf(fullPath, MAX_FULL_PATH_SIZE, MSG_IPC_DATA_PATH"%s", pFileName);
231 MSG_DEBUG("open file name: %s", fullPath);
234 pFile = MsgOpenFile(fullPath, "rb");
237 MSG_DEBUG("File Open Error: %s", strerror(errno));
241 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
243 MSG_DEBUG("File Read Error: %s", strerror(errno));
247 int FileSize = MsgFtell(pFile);
250 MSG_DEBUG("Filesize is error : %d", FileSize);
256 *ppData = new char[FileSize];
258 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
260 MSG_DEBUG("File seek Error: %s", strerror(errno));
264 if (MsgReadFile(*ppData, sizeof(char), FileSize, pFile) != (size_t)FileSize) {
266 MSG_DEBUG("File Read Error: %s", strerror(errno));
270 *pDataSize = FileSize;
278 bool MsgReadFileForDecode(FILE *pFile, char *pBuf, int length, int *nSize)
282 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
283 MSG_DEBUG("File Seek Error: %s", strerror(errno));
288 *nSize = MsgReadFile(pBuf, sizeof(char), length, pFile);
295 bool MsgWriteIpcFile(const char *pFileName, const char *pData, int DataSize)
298 MSG_DEBUG("NULL parameter, pFileName [%p], pData [%p]", pFileName, pData);
302 char fullPath[MAX_FULL_PATH_SIZE] = {0};
304 snprintf(fullPath, MAX_FULL_PATH_SIZE, MSG_IPC_DATA_PATH"%s", pFileName);
306 FILE *pFile = MsgOpenFile(fullPath, "wb+");
309 MSG_DEBUG("File Open Error: %s", strerror(errno));
313 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
315 MSG_DEBUG("File Seek Error: %s", strerror(errno));
319 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
321 MSG_DEBUG("File Write Error: %s", strerror(errno));
328 if (chmod(fullPath, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP ) != 0) {
329 MSG_DEBUG("File chmod Error: %s", strerror(errno));
332 if (chown(fullPath, 0, 6502 ) != 0) {
333 MSG_DEBUG("File chown Error: %s", strerror(errno));
339 int MsgReadSmilFile(const char *pFileName, char **ppData)
342 MSG_DEBUG("pFileName is NULL");
347 char fullPath[MAX_FULL_PATH_SIZE] = {0};
349 snprintf(fullPath, MAX_FULL_PATH_SIZE, MSG_SMIL_FILE_PATH"%s", pFileName);
351 MSG_DEBUG("open file name: %s", fullPath);
353 FILE *pFile = MsgOpenFile(fullPath, "rb");
356 MSG_DEBUG("File Open Error: %s", strerror(errno));
360 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
362 MSG_DEBUG("File Seek Error: %s", strerror(errno));
366 int FileSize = MsgFtell(pFile);
369 MSG_DEBUG("Filesize is error : %d", FileSize);
374 *ppData = new char[FileSize + 1];
376 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
378 MSG_DEBUG("File Sead Error: %s", strerror(errno));
382 if (MsgReadFile(*ppData, sizeof(char), FileSize, pFile) != (size_t)FileSize) {
384 MSG_DEBUG("File Read Error: %s", strerror(errno));
388 ppData[FileSize] = '\0';
398 bool MsgWriteSmilFile(const char *pFilePath,char *pData, int DataSize)
401 MSG_DEBUG("pFilePath is NULL");
405 if (mkdir(MSG_SMIL_FILE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
406 if (errno == EEXIST) {
407 MSG_DEBUG("The %s already exists", MSG_SMIL_FILE_PATH);
409 MSG_DEBUG("Error while mkdir %s", MSG_SMIL_FILE_PATH);
413 FILE *pFile = MsgOpenFile(pFilePath, "wb+");
416 MSG_DEBUG("File Open Error: %s", strerror(errno));
420 if (MsgFseek(pFile, 0L, SEEK_SET) < 0) {
422 MSG_DEBUG("File Seek Error: %s", strerror(errno));
426 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
428 MSG_DEBUG("File Write Error: %s", strerror(errno));
439 void MsgDeleteFile(const char *pFileName)
442 MSG_FATAL("pFileName is NULL");
446 if (strlen(pFileName) == 0) {
447 MSG_FATAL("pFileName has zero length");
451 char fullPath[MAX_FULL_PATH_SIZE] = {0};
454 snprintf(fullPath, MAX_FULL_PATH_SIZE, MSG_IPC_DATA_PATH"%s", pFileName);
456 MSG_DEBUG("%s", fullPath);
458 if (remove(fullPath) != 0)
459 MSG_FATAL("File Delete Error [%s]: %s", fullPath, strerror(errno));
460 } catch (exception &e) {
461 MSG_FATAL ("%s", e.what());
467 void MsgDeleteSmilFile(const char *pFileName)
470 MSG_FATAL("pFileName NULL");
475 char fullPath[MAX_FULL_PATH_SIZE] = {0};
477 snprintf(fullPath, MAX_FULL_PATH_SIZE, MSG_SMIL_FILE_PATH"%s", pFileName);
479 if (remove(fullPath) != 0)
480 MSG_FATAL("File Delete Error [%s]: %s", fullPath, strerror(errno));
481 } catch (exception &e) {
482 MSG_FATAL("%s", e.what());
487 bool MsgGetFileSize(const char *pFilePath, int *nSize)
490 MSG_FATAL("pFileName NULL");
496 pFile = MsgOpenFile(pFilePath, "rb");
499 MSG_DEBUG("File Open error: %s", strerror(errno));
503 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
505 MSG_FATAL("File Read Error: %s", strerror(errno));
509 *nSize = MsgFtell(pFile);
517 FILE *MsgOpenMMSFile(char *pFileName)
522 MSG_DEBUG("pFileName NULL: %s", strerror(errno));
526 len = strlen(pFileName);
528 for (int i = 0; i < len; i++) {
529 switch (pFileName[i]) {
536 MSG_DEBUG("pFileName = %s", pFileName);
538 char fullPath[MAX_FULL_PATH_SIZE+1] = {0};
540 snprintf(fullPath, MAX_FULL_PATH_SIZE+1, "%s.mms", pFileName);
542 FILE *pFile = MsgOpenFile(fullPath, "wb+");
545 MSG_FATAL("File Open Error: %s", strerror(errno));
549 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
551 MSG_DEBUG("File Read Error: %s", strerror(errno));
559 bool MsgWriteDataFromEncodeBuffer(FILE *pFile, char *pInBuffer, int *pPtr, int maxLen, int *pOffset )
561 if (!pFile || !pPtr || !pInBuffer || !pOffset) {
562 MSG_FATAL(" NULL parameter passed");
566 MSG_DEBUG("MsgWriteDataFromEncodeBuffer:");
567 MSG_DEBUG("pInBuffer %x", pInBuffer);
568 MSG_DEBUG("pPtr %d", (*pPtr));
569 MSG_DEBUG("before to fwite %x", pFile);
571 if (MsgWriteFile(pInBuffer, sizeof(char), (*pPtr), pFile) != (size_t)(*pPtr)) {
572 MSG_FATAL("MsgWriteFile failed");
576 MSG_DEBUG("after to fwite \n");
580 memset( pInBuffer, 0, maxLen );
584 if (MsgFseek(pFile, 0L, SEEK_END) < 0) {
585 MSG_FATAL("MsgFseek failed");
589 *pOffset = MsgFtell(pFile);
591 if (*pOffset == -1L) {
592 MSG_FATAL("MsgFtell failed");
600 bool MsgOpenCreateAndOverwriteFile(char *pFullPath, char *pBuff, int TotalLength)
605 file_mode = (S_IRUSR | S_IWUSR);
607 if ((pFile = MsgOpenFile(pFullPath, "wb+")) == NULL) {
608 MSG_FATAL("MsgOpenFile errer");
612 if (MsgWriteFile(pBuff, sizeof(char), TotalLength, pFile) != (size_t)TotalLength) {
613 MsgCloseFile( pFile );
617 MsgFsync(pFile); //file is written to device immediately, it prevents missing file data from unexpected power off
621 if (chmod(pFullPath, file_mode) < 0)
622 MSG_FATAL("File chmod Error: %s", strerror(errno));
628 char *MsgOpenAndReadMmsFile( const char *szFilePath, int offset, int size, int *npSize )
634 if (szFilePath == NULL) {
635 MSG_DEBUG("MsgOpenAndReadMmsFile: [ERROR] szFilePath id NULL");
641 pFile = MsgOpenFile( szFilePath, "rb" );
644 MSG_DEBUG("MsgOpenAndReadMmsFile: [ERROR] Can't open filepath", strerror(errno));
649 if (MsgGetFileSize(szFilePath, & readSize) == false) {
650 MSG_DEBUG("MsgGetFileSize: failed");
657 if (readSize > FM_READ_WRITE_BUFFER_MAX) {
658 MSG_DEBUG("MsgOpenAndReadMmsFile: File size tried to read too big");
662 pData = (char *)malloc(readSize + 1);
663 if ( NULL == pData ) {
664 MSG_DEBUG( "MsgOpenAndReadMmsFile: [ERROR] pData MemAlloc Fail", strerror(errno) );
667 memset( pData, 0, readSize + 1 );
669 if (MsgFseek( pFile, offset, SEEK_SET) < 0) {
670 MSG_DEBUG( "MsgOpenAndReadMmsFile: [ERROR] FmSeekFile failed", strerror(errno) );
674 *npSize = MsgReadFile(pData, sizeof(char), readSize, pFile);
680 *(pData + (*npSize)) = '\0';
694 MsgCloseFile( pFile );
701 // it is equivalent to "rm -rf pDirPath"
702 int MsgRmRf(char *pDirPath)
707 dir = opendir(pDirPath);
710 MSG_FATAL("error opendir: %s", strerror(errno));
714 int size = strlen(pDirPath) + 256;
716 char *path = (char*)malloc(size);
719 MSG_DEBUG("path is NULL");
725 while ((d = readdir(dir)) != NULL) {
726 if (d->d_type == DT_DIR) {
727 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
729 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
734 if (rmdir(path) != 0) {
741 MSG_FATAL("error rmdir: %s", strerror(errno));
746 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
748 if (MsgDrmIsDrmFile(path))
749 MsgDrmUnregisterFile(path);
751 if (remove(path) != 0) {
758 MSG_FATAL("error remove: %s", strerror(errno));
774 int MsgGetFileSize(const char *pFileName)
776 struct stat file_stat;
778 if (lstat(pFileName, &file_stat)) {
779 MSG_FATAL("error lstat: %s", strerror(errno));
783 return file_stat.st_size;
787 // it is equivalent to "du dir_path"
788 unsigned int MsgDu(const char *pDirPath)
793 dir = opendir(pDirPath);
796 MSG_FATAL("error opendir: %s", strerror(errno));
800 int size = strlen(pDirPath) + 256;
801 char *path = (char*)malloc(size);
804 unsigned int totalFileSize = 0;
806 while ((d = readdir(dir)) != NULL) {
807 if( d->d_type == DT_DIR) {
808 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
810 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
813 unsigned int dirSize = MsgDu(path);
816 MSG_FATAL("error MsgDu");
820 totalFileSize += dirSize;
822 snprintf(path, size, "%s/%s", pDirPath, d->d_name);
823 int fileSize = MsgGetFileSize(path);
826 MSG_FATAL("error MsgGetFileSize");
830 totalFileSize += fileSize;
838 return totalFileSize;
842 bool MsgAppendFile(const char *pFilePath, const char *pData, int DataSize)
845 MSG_FATAL("NULL check error, pFileName %p, pData %p", pFilePath, pData);
849 char fullPath[MAX_FULL_PATH_SIZE] = {0};
851 snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s", pFilePath);
853 FILE *pFile = MsgOpenFile(fullPath, "a+");
856 MSG_FATAL("File Open Error: %s", strerror(errno));
860 if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
862 MSG_FATAL("File Sead Error: %s", strerror(errno));
866 if (MsgWriteFile(pData, sizeof(char), DataSize, pFile) != (size_t)DataSize) {
868 MSG_FATAL("File Write Error: %s", strerror(errno));
872 MsgFsync(pFile); //file is written to device immediately, it prevents missing file data from unexpected power off
880 struct dirent *d = NULL;
883 dir = opendir(MSG_DATA_PATH);
886 MSG_FATAL("error opendir: %s", strerror(errno));
890 // Remove temporal Mms folder (/opt/data/msg-service/msgdata/*.dir)
891 while ((d = readdir(dir)) != NULL) {
892 if (d->d_type == DT_DIR) {
893 if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
896 if(strstr(d->d_name, ".dir") != NULL) {
897 char filePath[MSG_FILEPATH_LEN_MAX] = {0,};
898 snprintf(filePath, MSG_FILEPATH_LEN_MAX, MSG_DATA_PATH"%s", d->d_name);