Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules_mobile / core / src / file_output.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file        file_output.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of file output
21  */
22 #include <stddef.h>
23 #include <dpl/file_output.h>
24 #include <dpl/binary_queue.h>
25 #include <dpl/scoped_free.h>
26 #include <dpl/log/log.h>
27 #include <unistd.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <errno.h>
31
32 namespace DPL {
33 FileOutput::FileOutput() :
34     m_fd(-1)
35 {}
36
37 FileOutput::FileOutput(const std::string& fileName) :
38     m_fd(-1)
39 {
40     Open(fileName);
41 }
42
43 FileOutput::~FileOutput()
44 {
45     Close();
46 }
47
48 void FileOutput::Open(const std::string& fileName)
49 {
50     // Open non-blocking
51     int fd =
52         TEMP_FAILURE_RETRY(open(fileName.c_str(), O_WRONLY | O_CREAT |
53                                 O_TRUNC |
54                                 O_NONBLOCK, 0664));
55
56     // Throw an exception if an error occurred
57     if (fd == -1) {
58         ThrowMsg(Exception::OpenFailed, fileName);
59     }
60
61     // Close if any existing
62     Close();
63
64     // Save new descriptor
65     m_fd = fd;
66
67     LogPedantic("Opened file: " << fileName);
68 }
69
70 void FileOutput::Close()
71 {
72     if (m_fd == -1) {
73         return;
74     }
75
76     if (TEMP_FAILURE_RETRY(close(m_fd)) == -1) {
77         Throw(Exception::CloseFailed);
78     }
79
80     m_fd = -1;
81
82     LogPedantic("Closed file");
83 }
84
85 size_t FileOutput::Write(const BinaryQueue &buffer, size_t bufferSize)
86 {
87     // Adjust write size
88     if (bufferSize > buffer.Size()) {
89         bufferSize = buffer.Size();
90     }
91
92     // FIXME: User write visitor to write !
93     // WriteVisitor visitor
94
95     ScopedFree<void> flattened(malloc(bufferSize));
96     buffer.Flatten(flattened.Get(), bufferSize);
97
98     LogPedantic("Trying to write " << bufferSize << " bytes");
99
100     ssize_t result = TEMP_FAILURE_RETRY(write(m_fd, flattened.Get(), bufferSize));
101
102     LogPedantic("Wrote " << result << " bytes to file");
103
104     if (result > 0) {
105         // Successfuly written some bytes
106         return static_cast<size_t>(result);
107     } else if (result == 0) {
108         // This is abnormal result
109         ThrowMsg(CommonException::InternalError,
110                  "Invalid write result, 0 bytes written");
111     } else {
112         // Interpret error result
113         // FIXME: Handle errno
114         Throw(AbstractOutput::Exception::WriteFailed);
115     }
116 }
117
118 WaitableHandle FileOutput::WaitableWriteHandle() const
119 {
120     return static_cast<WaitableHandle>(m_fd);
121 }
122 } // namespace DPL