upload tizen1.0 source
[framework/web/wrt-commons.git] / modules / core / src / file_input_mapping.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_mapping.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of file input mapping
21  */
22 #include <dpl/file_input_mapping.h>
23 #include <dpl/scoped_close.h>
24 #include <dpl/log/log.h>
25 #include <iomanip>
26 #include <sys/stat.h>
27 #include <sys/mman.h>
28 #include <fcntl.h>
29 #include <errno.h>
30
31 namespace DPL
32 {
33 FileInputMapping::FileInputMapping(const std::string &fileName)
34     : m_handle(-1),
35       m_size(0),
36       m_address(NULL)
37 {
38     // Open device and map it to user space
39     int file = TEMP_FAILURE_RETRY(open(fileName.c_str(), O_RDONLY));
40
41     if (file == -1)
42     {
43         int error = errno;
44         ThrowMsg(FileInputMapping::Exception::OpenFailed,
45                  "Failed to open file. errno = " << error);
46     }
47
48     // Scoped close on file
49     ScopedClose scopedClose(file);
50
51     // Calculate file size
52     off64_t size = lseek64(file, 0, SEEK_END);
53
54     if (size == static_cast<off64_t>(-1))
55     {
56         int error = errno;
57         ThrowMsg(FileInputMapping::Exception::OpenFailed,
58                  "Failed to seek file. errno = " << error);
59     }
60
61     // Map file to usespace
62     void *address = mmap(0, static_cast<size_t>(size),
63                          PROT_READ, MAP_SHARED, file, 0);
64
65     if (address == MAP_FAILED)
66     {
67         int error = errno;
68         ThrowMsg(FileInputMapping::Exception::OpenFailed,
69                  "Failed to map file. errno = " << error);
70     }
71
72     // Release scoped close
73     m_handle = scopedClose.Release();
74
75     // Save mapped up address
76     m_size = size;
77     m_address = static_cast<unsigned char *>(address);
78
79     LogPedantic("Created file mapping: " << fileName <<
80                 " of size: " << m_size <<
81                 " at address: " << std::hex << static_cast<void *>(m_address));
82 }
83
84 FileInputMapping::~FileInputMapping()
85 {
86     // Close mapping
87     if (munmap(m_address, static_cast<size_t>(m_size)) == -1)
88     {
89         int error = errno;
90         LogPedantic("Failed to munmap file. errno = " << error);
91     }
92
93     // Close file descriptor
94     if (TEMP_FAILURE_RETRY(close(m_handle)) == -1)
95     {
96         int error = errno;
97         LogPedantic("Failed to close file. errno = " << error);
98     }
99 }
100
101 off64_t FileInputMapping::GetSize() const
102 {
103     return m_size;
104 }
105
106 const unsigned char *FileInputMapping::GetAddress() const
107 {
108     return m_address;
109 }
110 } // namespace DPL