2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file file_output.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of file output
23 #include <dpl/file_output.h>
24 #include <dpl/binary_queue.h>
25 #include <dpl/free_deleter.h>
27 #include <dpl/log/wrt_log.h>
34 FileOutput::FileOutput() :
38 FileOutput::FileOutput(const std::string& fileName) :
44 FileOutput::~FileOutput()
49 void FileOutput::Open(const std::string& fileName)
53 TEMP_FAILURE_RETRY(open(fileName.c_str(), O_WRONLY | O_CREAT |
57 // Throw an exception if an error occurred
59 ThrowMsg(Exception::OpenFailed, fileName);
62 // Close if any existing
65 // Save new descriptor
68 WrtLogD("Opened file: %s", fileName.c_str());
71 void FileOutput::Close()
77 if (TEMP_FAILURE_RETRY(close(m_fd)) == -1) {
78 Throw(Exception::CloseFailed);
83 WrtLogD("Closed file");
86 size_t FileOutput::Write(const BinaryQueue &buffer, size_t bufferSize)
89 if (bufferSize > buffer.Size()) {
90 bufferSize = buffer.Size();
93 // FIXME: User write visitor to write !
94 // WriteVisitor visitor
96 std::unique_ptr<void,free_deleter> flattened(malloc(bufferSize));
97 buffer.Flatten(flattened.get(), bufferSize);
99 WrtLogD("Trying to write %u bytes", bufferSize);
101 ssize_t result = TEMP_FAILURE_RETRY(write(m_fd, flattened.get(), bufferSize));
103 WrtLogD("Wrote %u bytes to file", result);
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");
113 // Interpret error result
114 // FIXME: Handle errno
115 Throw(AbstractOutput::Exception::WriteFailed);
119 WaitableHandle FileOutput::WaitableWriteHandle() const
121 return static_cast<WaitableHandle>(m_fd);