Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules / 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/free_deleter.h>
26 #include <memory>
27 #include <dpl/log/wrt_log.h>
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <errno.h>
32
33 namespace DPL {
34 FileOutput::FileOutput() :
35     m_fd(-1)
36 {}
37
38 FileOutput::FileOutput(const std::string& fileName) :
39     m_fd(-1)
40 {
41     Open(fileName);
42 }
43
44 FileOutput::~FileOutput()
45 {
46     Close();
47 }
48
49 void FileOutput::Open(const std::string& fileName)
50 {
51     // Open non-blocking
52     int fd =
53         TEMP_FAILURE_RETRY(open(fileName.c_str(), O_WRONLY | O_CREAT |
54                                 O_TRUNC |
55                                 O_NONBLOCK, 0664));
56
57     // Throw an exception if an error occurred
58     if (fd == -1) {
59         ThrowMsg(Exception::OpenFailed, fileName);
60     }
61
62     // Close if any existing
63     Close();
64
65     // Save new descriptor
66     m_fd = fd;
67
68     WrtLogD("Opened file: %s", fileName.c_str());
69 }
70
71 void FileOutput::Close()
72 {
73     if (m_fd == -1) {
74         return;
75     }
76
77     if (TEMP_FAILURE_RETRY(close(m_fd)) == -1) {
78         Throw(Exception::CloseFailed);
79     }
80
81     m_fd = -1;
82
83     WrtLogD("Closed file");
84 }
85
86 size_t FileOutput::Write(const BinaryQueue &buffer, size_t bufferSize)
87 {
88     // Adjust write size
89     if (bufferSize > buffer.Size()) {
90         bufferSize = buffer.Size();
91     }
92
93     // FIXME: User write visitor to write !
94     // WriteVisitor visitor
95
96     std::unique_ptr<void,free_deleter> flattened(malloc(bufferSize));
97     buffer.Flatten(flattened.get(), bufferSize);
98
99     WrtLogD("Trying to write %u bytes", bufferSize);
100
101     ssize_t result = TEMP_FAILURE_RETRY(write(m_fd, flattened.get(), bufferSize));
102
103     WrtLogD("Wrote %u bytes to file", result);
104
105     if (result > 0) {
106         // Successfuly written some bytes
107         return static_cast<size_t>(result);
108     } else if (result == 0) {
109         // This is abnormal result
110         ThrowMsg(CommonException::InternalError,
111                  "Invalid write result, 0 bytes written");
112     } else {
113         // Interpret error result
114         // FIXME: Handle errno
115         Throw(AbstractOutput::Exception::WriteFailed);
116     }
117 }
118
119 WaitableHandle FileOutput::WaitableWriteHandle() const
120 {
121     return static_cast<WaitableHandle>(m_fd);
122 }
123 } // namespace DPL