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