2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file file_input_mapping.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of file input mapping
22 #include <dpl/file_input_mapping.h>
23 #include <dpl/scoped_close.h>
24 #include <dpl/log/log.h>
33 FileInputMapping::FileInputMapping(const std::string &fileName)
38 // Open device and map it to user space
39 int file = TEMP_FAILURE_RETRY(open(fileName.c_str(), O_RDONLY));
44 ThrowMsg(FileInputMapping::Exception::OpenFailed,
45 "Failed to open file. errno = " << error);
48 // Scoped close on file
49 ScopedClose scopedClose(file);
51 // Calculate file size
52 off64_t size = lseek64(file, 0, SEEK_END);
54 if (size == static_cast<off64_t>(-1))
57 ThrowMsg(FileInputMapping::Exception::OpenFailed,
58 "Failed to seek file. errno = " << error);
61 // Map file to usespace
62 void *address = mmap(0, static_cast<size_t>(size),
63 PROT_READ, MAP_SHARED, file, 0);
65 if (address == MAP_FAILED)
68 ThrowMsg(FileInputMapping::Exception::OpenFailed,
69 "Failed to map file. errno = " << error);
72 // Release scoped close
73 m_handle = scopedClose.Release();
75 // Save mapped up address
77 m_address = static_cast<unsigned char *>(address);
79 LogPedantic("Created file mapping: " << fileName <<
80 " of size: " << m_size <<
81 " at address: " << std::hex << static_cast<void *>(m_address));
84 FileInputMapping::~FileInputMapping()
87 if (munmap(m_address, static_cast<size_t>(m_size)) == -1)
90 LogPedantic("Failed to munmap file. errno = " << error);
93 // Close file descriptor
94 if (TEMP_FAILURE_RETRY(close(m_handle)) == -1)
97 LogPedantic("Failed to close file. errno = " << error);
101 off64_t FileInputMapping::GetSize() const
106 const unsigned char *FileInputMapping::GetAddress() const