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