merge commits of 2.2.1 to public
[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 <FIoDirectory.h>
28 #include "FMedia_MediaUtil.h"
29 #include "FBase_StringConverter.h"
30
31 using namespace Tizen::Base;
32 using namespace Tizen::Io;
33
34 namespace Tizen { namespace Media {
35
36 Tizen::Base::ByteBuffer*
37 _MediaUtil::FileToBufferN(const Tizen::Base::String &path, int maxSize)
38 {
39         File file;
40         std::unique_ptr<ByteBuffer> pBuf;
41         result r = E_SUCCESS;
42         int size = 0;
43
44         SysTryReturn(NID_MEDIA, !path.IsEmpty(), null, E_FILE_NOT_FOUND,
45                 "[E_FILE_NOT_FOUND] path is empty");
46         SysTryReturn(NID_MEDIA, path.GetLength() > 0 && path.GetLength() <= PATH_MAX, null, E_INVALID_ARG,
47                 "[E_INVALID_ARG] Given filePath length is zero or exceeds system limitations.");
48         SysSecureTryReturn(NID_MEDIA, File::IsFileExist(path), null, E_FILE_NOT_FOUND,
49                 "[E_FILE_NOT_FOUND] filePath:%ls", path.GetPointer());
50
51         r = file.Construct(path, L"rb");
52         SysSecureTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
53                 "[%s] file.Construct failed. File name (%ls)", GetErrorMessage(r), path.GetPointer());
54
55         file.Seek(FILESEEKPOSITION_END, 0);
56         size = file.Tell();
57         file.Seek(FILESEEKPOSITION_BEGIN, 0);
58
59         SysSecureTryReturn(NID_MEDIA, size > 0, null, E_DATA_NOT_FOUND,
60                 "[E_DATA_NOT_FOUND] File size is 0: %ls", path.GetPointer());
61
62         pBuf.reset(new (std::nothrow) ByteBuffer());
63         SysTryReturn(NID_MEDIA, pBuf.get() != null, null, E_OUT_OF_MEMORY,
64                 "[E_OUT_OF_MEMORY] Failed to create new ByteBuffer.");
65
66         if (maxSize == 0 || size <= maxSize) // read whole data
67         {
68                 r = pBuf->Construct(size);
69                 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ByteBuffer Construct failed for %d bytes.",
70                         GetErrorMessage(r), size);
71
72                 r = file.Read(*pBuf.get());
73                 SysSecureTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. File.Read failed: %ls",
74                         GetErrorMessage(r), path.GetPointer());
75         }
76         else
77         {
78                 r = pBuf->Construct(maxSize);
79                 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ByteBuffer Construct failed for %d bytes.",
80                         GetErrorMessage(r), maxSize);
81
82                 r = file.Read(*pBuf.get());
83                 SysSecureTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. File.Read failed: %ls",
84                         GetErrorMessage(r), path.GetPointer());
85         }
86
87         pBuf->Flip();
88
89         SetLastResult(E_SUCCESS);
90         return pBuf.release();
91 }
92
93 result
94 _MediaUtil::BufferToFile(const Tizen::Base::ByteBuffer &srcBuf,
95                                                  const Tizen::Base::String &dstPath, bool overwrite)
96 {
97         File file;
98         result r = E_SUCCESS;
99         String directoryName;
100         String fileName;
101         char* tempFileName = null;
102         std::unique_ptr<char[]> pDirectory;
103         std::unique_ptr<char[]> pFileName;
104
105         if (File::IsFileExist(dstPath) && overwrite == false)
106         {
107                 return E_FILE_ALREADY_EXIST;
108         }
109
110         String tmpPath = File::GetFileName(dstPath);
111
112         r = dstPath.SubString(0, dstPath.GetLength() - tmpPath.GetLength() -1, directoryName);
113         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagated. Faied to generate tempfile", GetErrorMessage(r));
114
115         r = dstPath.SubString(dstPath.GetLength() - tmpPath.GetLength(), fileName);
116         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagated. Faied to generate tempfile", GetErrorMessage(r));
117
118         pDirectory.reset(_StringConverter::CopyToCharArrayN(directoryName));
119         SysTryReturn(NID_MEDIA, pDirectory.get() != null, r, r, "[E_OUT_OF_MEMORY] Propagated.");
120
121         pFileName.reset(_StringConverter::CopyToCharArrayN(fileName));
122         SysTryReturn(NID_MEDIA, pFileName.get() != null, r, r, "[E_OUT_OF_MEMORY] Propagated.");
123
124         r = Directory::Create(directoryName, true);
125         SysSecureTryReturn(NID_MEDIA, (r == E_SUCCESS || r == E_FILE_ALREADY_EXIST), r, r, "[%s] Propagated. Faied to create directory for %ls",
126                                            GetErrorMessage(r), directoryName.GetPointer());
127
128         tempFileName = tempnam(pDirectory.get(), pFileName.get());
129         SysTryReturn(NID_MEDIA, tempFileName != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Propagated. Faied to generate tempfile");
130
131         tmpPath = String(tempFileName);
132
133         r = file.Construct(tmpPath, "wb", true);
134         SysSecureTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated. Temp File.Construct failed for %ls",
135                                           GetErrorMessage(r), tmpPath.GetPointer());
136
137         r = file.Write(srcBuf);
138         SysSecureTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated. Temp File.Write failed for %ls",
139                                           GetErrorMessage(r), tmpPath.GetPointer());
140
141         r = file.Flush();
142         SysSecureTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated. Temp File.Flush failed for %ls",
143                                           GetErrorMessage(r), tmpPath.GetPointer());
144
145         if (File::IsFileExist(dstPath))
146         {
147                 r = File::Remove(dstPath);
148                 SysSecureTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated. Original File Remove failed for %ls",
149                                           GetErrorMessage(r), tmpPath.GetPointer());
150         }
151
152         r = file.Move(tmpPath, dstPath);
153         SysSecureTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated. File.Move failed for %ls",
154                 GetErrorMessage(r), dstPath.GetPointer());
155
156         free(tempFileName);
157         return r;
158
159 CATCH:
160         if (tempFileName != null)
161         {
162                 free(tempFileName);
163         }
164         file.Remove(tmpPath);
165         return r;
166 }
167
168 }} // Tizen::Media