Additional Include: stddef.h
[framework/web/wrt-commons.git] / modules / core / src / file_input.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        named_input_pipe.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of named input pipe
21  */
22 #include <stddef.h>
23 #include <dpl/file_input.h>
24 #include <dpl/binary_queue.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 namespace // anonymous
34 {
35 const size_t DEFAULT_READ_BUFFER_SIZE = 4096;
36 } // namespace anonymous
37
38 FileInput::FileInput()
39     : m_fd(-1)
40 {
41 }
42
43 FileInput::FileInput(const std::string& fileName)
44     : m_fd(-1)
45 {
46     Open(fileName);
47 }
48
49 FileInput::~FileInput()
50 {
51     Close();
52 }
53
54 void FileInput::Open(const std::string& fileName)
55 {
56     // Open non-blocking
57     int fd = TEMP_FAILURE_RETRY(open(fileName.c_str(), O_RDONLY | O_NONBLOCK));
58
59     // Throw an exception if an error occurred
60     if (fd == -1)
61         ThrowMsg(Exception::OpenFailed, fileName);
62
63     // Close if any existing
64     Close();
65
66     // Save new descriptor
67     m_fd = fd;
68
69     LogPedantic("Opened file: " << fileName);
70 }
71
72 void FileInput::Close()
73 {
74     if (m_fd == -1)
75         return;
76
77     if (TEMP_FAILURE_RETRY(close(m_fd)) == -1)
78         Throw(Exception::CloseFailed);
79
80     m_fd = -1;
81
82     LogPedantic("Closed file");
83 }
84
85 BinaryQueueAutoPtr FileInput::Read(size_t size)
86 {
87     size_t bytesToRead = size > DEFAULT_READ_BUFFER_SIZE ? DEFAULT_READ_BUFFER_SIZE : size;
88
89     // Malloc default read buffer size
90     // It is unmanaged, so it can be then attached directly to binary queue
91     void *buffer = malloc(bytesToRead);
92
93     if (buffer == NULL)
94         throw std::bad_alloc();
95
96     LogPedantic("Trying to read " << bytesToRead << " bytes");
97
98     ssize_t result = TEMP_FAILURE_RETRY(read(m_fd, buffer, bytesToRead));
99
100     LogPedantic("Read " << result << " bytes from file");
101
102     if (result > 0)
103     {
104         // Succedded to read socket data
105         BinaryQueueAutoPtr binaryQueue(new BinaryQueue());
106
107         // Append unmanaged memory
108         binaryQueue->AppendUnmanaged(buffer, result, &BinaryQueue::BufferDeleterFree, NULL);
109
110         // Return buffer
111         return binaryQueue;
112     }
113     else if (result == 0)
114     {
115         // Socket was gracefuly closed
116         free(buffer);
117
118         // Return empty buffer
119         return BinaryQueueAutoPtr(new BinaryQueue());
120     }
121     else
122     {
123         // Must first save errno value, because it may be altered
124         int lastErrno = errno;
125
126         // Free buffer
127         free(buffer);
128
129         // Interpret error result
130         (void)lastErrno;
131
132         // FIXME: Handle specific errno
133         Throw(AbstractInput::Exception::ReadFailed);
134     }
135 }
136
137 WaitableHandle FileInput::WaitableReadHandle() const
138 {
139     return static_cast<WaitableHandle>(m_fd);
140 }
141 } // namespace DPL