Libosmium  2.15.1
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 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2019 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS 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 
59 namespace 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  assert(std::numeric_limits<off_t>::max() >= std::numeric_limits<size_t>::max() || new_size <= std::numeric_limits<off_t>::max());
185  if (::ftruncate(fd, static_cast<off_t>(new_size)) != 0) {
186 #endif
187  throw std::system_error{errno, std::system_category(), "Could not resize file"};
188  }
189  }
190 
194  inline std::size_t get_pagesize() {
195 #ifdef _WIN32
196  // Windows implementation
197  SYSTEM_INFO si;
198  GetSystemInfo(&si);
199  return si.dwPageSize;
200 #else
201  // Unix implementation
202  return static_cast<std::size_t>(::sysconf(_SC_PAGESIZE));
203 #endif
204  }
205 
212  inline std::size_t file_offset(int fd) {
213 #ifdef _MSC_VER
214  osmium::detail::disable_invalid_parameter_handler diph;
215  // https://msdn.microsoft.com/en-us/library/1yee101t.aspx
216  const auto offset = _lseeki64(fd, 0, SEEK_CUR);
217 #else
218  const auto offset = ::lseek(fd, 0, SEEK_CUR);
219 #endif
220  if (offset == -1) {
221  return 0;
222  }
223  return static_cast<std::size_t>(offset);
224  }
225 
229  inline bool isatty(int fd) {
230 #ifdef _MSC_VER
231  osmium::detail::disable_invalid_parameter_handler diph;
232  // https://msdn.microsoft.com/en-us/library/f4s0ddew.aspx
233  return _isatty(fd) != 0;
234 #else
235  return ::isatty(fd) != 0;
236 #endif
237  }
238 
239  } // namespace util
240 
241 } // namespace osmium
242 
243 #endif // OSMIUM_UTIL_FILE_HPP
std::size_t file_size(int fd)
Definition: file.hpp:109
bool isatty(int fd)
Definition: file.hpp:229
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: attr.hpp:333
std::size_t get_pagesize()
Definition: file.hpp:194
std::size_t file_offset(int fd)
Definition: file.hpp:212
void resize_file(int fd, std::size_t new_size)
Definition: file.hpp:177