Merge "Changing the return value description of IsBitSet(), IsProbablePrimeNumber...
[platform/framework/native/appfw.git] / src / io / FIo_FileUtil.cpp
index 3276dd9..0edf5d5 100644 (file)
@@ -1,5 +1,4 @@
 //
-// Open Service Platform
 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
 //
 // Licensed under the Apache License, Version 2.0 (the License);
@@ -55,7 +54,6 @@ namespace Tizen { namespace Io
 
 static const int _BASE_YEAR = 1900;
 static const int _MAX_COPY_BYTES = 4096;
-static const int _MAX_OPENMODE_LENGTH = 3;
 
 //Holds app path prefixes
 static const char* filePathAppPrefix[] =
@@ -104,7 +102,7 @@ _FileUtil::Remove(const String& filePath)
 
        if (_FileUtil::IsFileExist(pFilePath.get()) == false)
        {
-               SysLog(NID_IO, "[E_FILE_NOT_FOUND] File(%s) does not exist.", pFilePath.get());
+               SysSecureLogException(NID_IO, E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] File (%s) does not exist.", pFilePath.get());
                return E_FILE_NOT_FOUND;
        }
 
@@ -217,6 +215,7 @@ _FileUtil::Copy(const String& srcFilePath, const String& destFilePath, bool fail
        ssize_t writtenBytes = -1;
        ssize_t remainingBytes = -1;
        char* pBuffer = null;
+       char* pCopyBuf = null;
        result r = E_SUCCESS;
 
        unique_ptr<char[]> pSrcpath(_StringConverter::CopyToCharArrayN(srcFilePath));
@@ -256,11 +255,12 @@ _FileUtil::Copy(const String& srcFilePath, const String& destFilePath, bool fail
        pBuffer = new (std::nothrow) char[_MAX_COPY_BYTES];
        SysTryCatch(NID_IO, pBuffer != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
 
+       pCopyBuf = pBuffer;
        do
        {
                do
                {
-                       readBytes = read(srcFd, pBuffer, _MAX_COPY_BYTES);
+                       readBytes = read(srcFd, pCopyBuf, _MAX_COPY_BYTES);
                }
                while (readBytes < 0 && errno == EINTR);
                if (readBytes < 0)
@@ -278,7 +278,7 @@ _FileUtil::Copy(const String& srcFilePath, const String& destFilePath, bool fail
 RETRY:
                do
                {
-                       writtenBytes = write(dstFd, pBuffer, remainingBytes);
+                       writtenBytes = write(dstFd, pCopyBuf, remainingBytes);
                }
                while (writtenBytes < 0 && errno == EINTR);
                if (writtenBytes < 0)
@@ -291,7 +291,7 @@ RETRY:
                else if (writtenBytes < remainingBytes)
                {
                        remainingBytes = remainingBytes - writtenBytes;
-                       pBuffer = const_cast< char* >(pBuffer) + writtenBytes;
+                       pCopyBuf = const_cast< char* >(pCopyBuf) + writtenBytes;
                        goto RETRY;
                }
        }
@@ -308,7 +308,7 @@ CATCH:
                fsync(dstFd);
                close(dstFd);
        }
-       delete pBuffer;
+       delete[] pBuffer;
 
        return r;
 }
@@ -321,7 +321,6 @@ _FileUtil::GetAttributes(const String& filePath, FileAttributes& attribute)
        off64_t fileSize = 0;
        unsigned long attr = 0;
        result r = E_SUCCESS;
-       struct tm* pTm = null;
        String fileName = L"";
        bool hidden = false;
 
@@ -330,10 +329,11 @@ _FileUtil::GetAttributes(const String& filePath, FileAttributes& attribute)
                           GetErrorMessage(GetLastResult()));
 
        struct stat64 statbuf;
-       if (int ret = stat64(pFilePath.get(), &statbuf) == -1)
+       if (stat64(pFilePath.get(), &statbuf) == -1)
        {
                r = __ConvertNativeErrorToResult(errno);
-               SysLogException(NID_IO, r, "[%s] Failed to get file (%s) status.", GetErrorMessage(r), pFilePath.get());
+               SysSecureLogException(NID_IO, r, "[%s] Failed to get file (%s) status. errno: %d (%s)",
+                               GetErrorMessage(r), pFilePath.get(), errno, strerror(errno));
                return r;
        }
 
@@ -343,13 +343,13 @@ _FileUtil::GetAttributes(const String& filePath, FileAttributes& attribute)
        // attributes
        attr = statbuf.st_mode;
 
-       pTm = localtime(&statbuf.st_mtime);
-       SysTryReturnResult(NID_IO, pTm != null, E_SYSTEM, "Failed to call localtime() (%s).", strerror(errno));
+       struct tm resultTm;
+       struct tm* pTm = localtime_r(&statbuf.st_mtime, &resultTm);
+       SysTryReturnResult(NID_IO, pTm != null, E_SYSTEM, "Failed to get local time (%s).", strerror(errno));
+
        r = dateTime.SetValue(_BASE_YEAR + pTm->tm_year, 1 + pTm->tm_mon, pTm->tm_mday, pTm->tm_hour, pTm->tm_min, pTm->tm_sec);
        SysTryReturn(NID_IO, (!IsFailed(r)), r, r, "[%s] Failed to set DateTime.", GetErrorMessage(r));
 
-       pTm = localtime(&statbuf.st_mtime);
-       SysTryReturnResult(NID_IO, pTm != null, E_SYSTEM, "Failed to call localtime() (%s).", strerror(errno));
        r = modifiedTime.SetValue(_BASE_YEAR + pTm->tm_year, 1 + pTm->tm_mon, pTm->tm_mday, pTm->tm_hour, pTm->tm_min, pTm->tm_sec);
        SysTryReturn(NID_IO, (!IsFailed(r)), r, r, "[%s] Failed to set DateTime.", GetErrorMessage(r));
 
@@ -371,7 +371,8 @@ _FileUtil::GetFileName(const String& filePath)
        int pos = -1;
 
        result r = filePath.LastIndexOf(L'/', filePath.GetLength() - 1, pos);
-       SysTryReturn(NID_IO, !IsFailed(r), fileName, E_INVALID_ARG, "[E_INVALID_ARG] The file path is invalid.");
+       SysTryReturn(NID_IO, r == E_SUCCESS || r == E_OBJ_NOT_FOUND, fileName, E_INVALID_ARG,
+                       "[E_INVALID_ARG] The file path is invalid.");
 
        r = filePath.SubString(pos + 1, fileName);
        SysTryReturn(NID_IO, !IsFailed(r), fileName, E_INVALID_ARG, "[E_INVALID_ARG] The file path is invalid.");
@@ -389,7 +390,8 @@ _FileUtil::GetFileExtension(const String& filePath)
        int pos = -1;
 
        result r = filePath.LastIndexOf(L'/', filePath.GetLength() - 1, pos);
-       SysTryReturn(NID_IO, !IsFailed(r), extName, E_INVALID_ARG, "[E_INVALID_ARG] The file path is invalid.");
+       SysTryReturn(NID_IO, r == E_SUCCESS || r == E_OBJ_NOT_FOUND, extName, E_INVALID_ARG,
+                       "[E_INVALID_ARG] The file path is invalid.");
 
        String fileName;
        r = filePath.SubString(pos + 1, fileName);