Applied secure log for privacy protect
[platform/framework/native/image-core.git] / src / FMedia_MediaUtil.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file   FMedia_Util.cpp
20  * @brief  This file contains the utility APIs of Media namespace.
21  */
22
23 #include <limits.h>
24 #include <unique_ptr.h>
25 #include <FBaseSysLog.h>
26 #include <FIoFile.h>
27 #include "FMedia_MediaUtil.h"
28
29 using namespace Tizen::Base;
30 using namespace Tizen::Io;
31
32 namespace Tizen { namespace Media {
33
34 Tizen::Base::ByteBuffer*
35 _MediaUtil::FileToBufferN(const Tizen::Base::String &path, int maxSize)
36 {
37         File file;
38         std::unique_ptr<ByteBuffer> pBuf;
39         result r = E_SUCCESS;
40         int size = 0;
41
42         SysTryReturn(NID_MEDIA, !path.IsEmpty(), null, E_FILE_NOT_FOUND,
43                 "[E_FILE_NOT_FOUND] path is empty");
44         SysTryReturn(NID_MEDIA, path.GetLength() > 0 && path.GetLength() <= PATH_MAX, null, E_INVALID_ARG,
45                 "[E_INVALID_ARG] Given filePath length is zero or exceeds system limitations.");
46         SysSecureTryReturn(NID_MEDIA, File::IsFileExist(path), null, E_FILE_NOT_FOUND,
47                 "[E_FILE_NOT_FOUND] filePath:%ls", path.GetPointer());
48
49         r = file.Construct(path, L"rb");
50         SysSecureTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
51                 "[%s] file.Construct failed. File name (%ls)", GetErrorMessage(r), path.GetPointer());
52
53         file.Seek(FILESEEKPOSITION_END, 0);
54         size = file.Tell();
55         file.Seek(FILESEEKPOSITION_BEGIN, 0);
56
57         SysSecureTryReturn(NID_MEDIA, size > 0, null, E_DATA_NOT_FOUND,
58                 "[E_DATA_NOT_FOUND] File size is 0: %ls", path.GetPointer());
59
60         pBuf.reset(new (std::nothrow) ByteBuffer());
61         SysTryReturn(NID_MEDIA, pBuf.get() != null, null, E_OUT_OF_MEMORY,
62                 "[E_OUT_OF_MEMORY] Failed to create new ByteBuffer.");
63
64         if (maxSize == 0 || size <= maxSize) // read whole data
65         {
66                 r = pBuf->Construct(size);
67                 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ByteBuffer Construct failed for %d bytes.",
68                         GetErrorMessage(r), size);
69
70                 r = file.Read(*pBuf.get());
71                 SysSecureTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. File.Read failed: %ls",
72                         GetErrorMessage(r), path.GetPointer());
73         }
74         else
75         {
76                 r = pBuf->Construct(maxSize);
77                 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ByteBuffer Construct failed for %d bytes.",
78                         GetErrorMessage(r), maxSize);
79
80                 r = file.Read(*pBuf.get());
81                 SysSecureTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. File.Read failed: %ls",
82                         GetErrorMessage(r), path.GetPointer());
83         }
84
85         pBuf->Flip();
86
87         SetLastResult(E_SUCCESS);
88         return pBuf.release();
89 }
90
91 result
92 _MediaUtil::BufferToFile(const Tizen::Base::ByteBuffer &srcBuf,
93                                                  const Tizen::Base::String &dstPath, bool overwrite)
94 {
95         File file;
96         result r = E_SUCCESS;
97
98         // Check file exist
99         if (File::IsFileExist(dstPath))
100         {
101                 if (overwrite)
102                 {
103                         File::Remove(dstPath);
104                 }
105                 else
106                 {
107                         return E_FILE_ALREADY_EXIST;
108                 }
109         }
110
111         r = file.Construct(dstPath, "wb", true);
112         SysSecureTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated. File.Construct failed for %ls",
113                 GetErrorMessage(r), dstPath.GetPointer());
114         r = file.Write(srcBuf);
115         SysSecureTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated. File.Write failed for %ls",
116                 GetErrorMessage(r), dstPath.GetPointer());
117
118 CATCH:
119         return r;
120 }
121
122 }} // Tizen::Media