8b090424afb5ae58023743e077e3e716043ba4bb
[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 <dpl/file_output.h>
23 #include <dpl/binary_queue.h>
24 #include <dpl/scoped_free.h>
25 #include <dpl/log/log.h>
26 #include <unistd.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <errno.h>
30
31 namespace DPL
32 {
33 FileOutput::FileOutput()
34     : m_fd(-1)
35 {
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 = TEMP_FAILURE_RETRY(open(fileName.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, 0664));
53
54     // Throw an exception if an error occurred
55     if (fd == -1)
56         ThrowMsg(Exception::OpenFailed, fileName);
57
58     // Close if any existing
59     Close();
60
61     // Save new descriptor
62     m_fd = fd;
63
64     LogPedantic("Opened file: " << fileName);
65 }
66
67 void FileOutput::Close()
68 {
69     if (m_fd == -1)
70         return;
71
72     if (TEMP_FAILURE_RETRY(close(m_fd)) == -1)
73         Throw(Exception::CloseFailed);
74
75     m_fd = -1;
76
77     LogPedantic("Closed file");
78 }
79
80 size_t FileOutput::Write(const BinaryQueue &buffer, size_t bufferSize)
81 {
82     // Adjust write size
83     if (bufferSize > buffer.Size())
84         bufferSize = buffer.Size();
85
86     // FIXME: User write visitor to write !
87     // WriteVisitor visitor
88
89     ScopedFree<void> flattened(malloc(bufferSize));
90     buffer.Flatten(flattened.Get(), bufferSize);
91
92     LogPedantic("Trying to write " << bufferSize << " bytes");
93
94     ssize_t result = TEMP_FAILURE_RETRY(write(m_fd, flattened.Get(), bufferSize));
95
96     LogPedantic("Wrote " << result << " bytes to file");
97
98     if (result > 0)
99     {
100         // Successfuly written some bytes
101         return static_cast<size_t>(result);
102     }
103     else if (result == 0)
104     {
105         // This is abnormal result
106         ThrowMsg(CommonException::InternalError, "Invalid write result, 0 bytes written");
107     }
108     else
109     {
110         // Interpret error result
111         // FIXME: Handle errno
112         Throw(AbstractOutput::Exception::WriteFailed);
113     }
114 }
115
116 WaitableHandle FileOutput::WaitableWriteHandle() const
117 {
118     return static_cast<WaitableHandle>(m_fd);
119 }
120 } // namespace DPL