Libosmium  2.20.0
Fast and flexible C++ library for working with OpenStreetMap data
file.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_UTIL_FILE_HPP
2#define OSMIUM_UTIL_FILE_HPP
3
4/*
5
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
36#include <cassert>
37#include <cerrno>
38#include <cstddef>
39#include <cstdio>
40#include <limits>
41#include <string>
42#include <sys/stat.h>
43#include <sys/types.h>
44#include <system_error>
45
46#ifdef _WIN32
47# ifndef WIN32_LEAN_AND_MEAN
48# define WIN32_LEAN_AND_MEAN // Prevent winsock.h inclusion; avoid winsock2.h conflict
49# endif
50# include <crtdbg.h>
51# include <io.h>
52# include <windows.h>
53#endif
54
55#ifndef _MSC_VER
56# include <unistd.h>
57#endif
58
59namespace osmium {
60
61#ifdef _MSC_VER
62 namespace detail {
63
64 // Disable parameter validation on Windows and reenable it
65 // automatically when scope closes.
66 // https://docs.microsoft.com/en-us/cpp/c-runtime-library/parameter-validation
67 class disable_invalid_parameter_handler {
68
69 static void invalid_parameter_handler(
70 const wchar_t* expression,
71 const wchar_t* function,
72 const wchar_t* file,
73 unsigned int line,
74 uintptr_t pReserved
75 ) {
76 // do nothing
77 }
78
79 _invalid_parameter_handler old_handler;
80 int old_report_mode;
81
82 public:
83
84 disable_invalid_parameter_handler() :
85 old_handler(_set_thread_local_invalid_parameter_handler(invalid_parameter_handler)),
86 old_report_mode(_CrtSetReportMode(_CRT_ASSERT, 0)) {
87 }
88
89 ~disable_invalid_parameter_handler() {
90 _CrtSetReportMode(_CRT_ASSERT, old_report_mode);
91 _set_thread_local_invalid_parameter_handler(old_handler);
92 }
93
94 }; // class disable_invalid_parameter_handler
95
96 } // namespace detail
97#endif
98
99 inline namespace util {
100
109 inline std::size_t file_size(int fd) {
110#ifdef _MSC_VER
111 // Windows implementation
112 osmium::detail::disable_invalid_parameter_handler diph;
113 // https://msdn.microsoft.com/en-us/library/dfbc2kec.aspx
114 const auto size = ::_filelengthi64(fd);
115 if (size < 0) {
116 throw std::system_error{errno, std::system_category(), "Could not get file size"};
117 }
118 return static_cast<std::size_t>(size);
119#else
120 // Unix implementation
121 struct stat s; // NOLINT clang-tidy
122 if (::fstat(fd, &s) != 0) {
123 throw std::system_error{errno, std::system_category(), "Could not get file size"};
124 }
125 return static_cast<std::size_t>(s.st_size);
126#endif
127 }
128
138 inline std::size_t file_size(const char* name) {
139#ifdef _MSC_VER
140 // Windows implementation
141 osmium::detail::disable_invalid_parameter_handler diph;
142 // https://msdn.microsoft.com/en-us/library/14h5k7ff.aspx
143 struct _stat64 s{};
144 if (::_stati64(name, &s) != 0) {
145 throw std::system_error{errno, std::system_category(), std::string{"Could not get file size of file '"} + name + "'"};
146 }
147#else
148 // Unix implementation
149 struct stat s; // NOLINT clang-tidy
150 if (::stat(name, &s) != 0) {
151 throw std::system_error{errno, std::system_category(), std::string{"Could not get file size of file '"} + name + "'"};
152 }
153#endif
154 return static_cast<std::size_t>(s.st_size);
155 }
156
165 inline std::size_t file_size(const std::string& name) {
166 return file_size(name.c_str());
167 }
168
177 inline void resize_file(int fd, std::size_t new_size) {
178#ifdef _MSC_VER
179 osmium::detail::disable_invalid_parameter_handler diph;
180 assert(new_size <= static_cast<std::size_t>(std::numeric_limits<__int64>::max()));
181 // https://msdn.microsoft.com/en-us/library/whx354w1.aspx
182 if (::_chsize_s(fd, static_cast<__int64>(new_size)) != 0) {
183#else
184 if (::ftruncate(fd, static_cast<off_t>(new_size)) != 0) {
185#endif
186 throw std::system_error{errno, std::system_category(), "Could not resize file"};
187 }
188 }
189
193 inline std::size_t get_pagesize() noexcept {
194#ifdef _WIN32
195 // Windows implementation
196 SYSTEM_INFO si;
197 GetSystemInfo(&si);
198 return si.dwPageSize;
199#else
200 // Unix implementation
201 return static_cast<std::size_t>(::sysconf(_SC_PAGESIZE));
202#endif
203 }
204
211 inline std::size_t file_offset(int fd) noexcept {
212#ifdef _MSC_VER
213 osmium::detail::disable_invalid_parameter_handler diph;
214 // https://msdn.microsoft.com/en-us/library/1yee101t.aspx
215 const auto offset = _lseeki64(fd, 0, SEEK_CUR);
216#else
217 const auto offset = ::lseek(fd, 0, SEEK_CUR);
218#endif
219 if (offset == -1) {
220 return 0;
221 }
222 return static_cast<std::size_t>(offset);
223 }
224
230 inline bool isatty(int fd) noexcept {
231#ifdef _MSC_VER
232 osmium::detail::disable_invalid_parameter_handler diph;
233 // https://msdn.microsoft.com/en-us/library/f4s0ddew.aspx
234 return _isatty(fd) != 0;
235#else
236 return ::isatty(fd) != 0;
237#endif
238 }
239
240 } // namespace util
241
242} // namespace osmium
243
244#endif // OSMIUM_UTIL_FILE_HPP
Definition: attr.hpp:342
void resize_file(int fd, std::size_t new_size)
Definition: file.hpp:177
bool isatty(int fd) noexcept
Definition: file.hpp:230
std::size_t file_offset(int fd) noexcept
Definition: file.hpp:211
std::size_t file_size(int fd)
Definition: file.hpp:109
std::size_t get_pagesize() noexcept
Definition: file.hpp:193
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53