Libosmium  2.2.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 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2015 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 <cerrno>
37 #include <cstdio>
38 #include <system_error>
39 #include <sys/stat.h>
40 #include <sys/types.h>
41 
42 #ifdef _WIN32
43 # include <io.h>
44 # include <windows.h>
45 #endif
46 
47 #ifndef _MSC_VER
48 # include <unistd.h>
49 #else
50 // https://msdn.microsoft.com/en-us/library/whx354w1.aspx
51 # define ftruncate _chsize_s
52 #endif
53 
54 namespace osmium {
55 
56  namespace util {
57 
66  inline size_t file_size(int fd) {
67 #ifdef _MSC_VER
68  // Windows implementation
69  // https://msdn.microsoft.com/en-us/library/dfbc2kec.aspx
70  auto size = ::_filelengthi64(fd);
71  if (size == -1L) {
72  throw std::system_error(errno, std::system_category(), "_filelengthi64 failed");
73  }
74  return size_t(size);
75 #else
76  // Unix implementation
77  struct stat s;
78  if (::fstat(fd, &s) != 0) {
79  throw std::system_error(errno, std::system_category(), "fstat failed");
80  }
81  return size_t(s.st_size);
82 #endif
83  }
84 
93  inline void resize_file(int fd, size_t new_size) {
94  if (::ftruncate(fd, new_size) != 0) {
95  throw std::system_error(errno, std::system_category(), "ftruncate failed");
96  }
97  }
98 
102  inline size_t get_pagesize() {
103 #ifdef _WIN32
104  // Windows implementation
105  SYSTEM_INFO si;
106  GetSystemInfo(&si);
107  return si.dwPageSize;
108 #else
109  // Unix implementation
110  return ::sysconf(_SC_PAGESIZE);
111 #endif
112  }
113 
114  } // namespace util
115 
116 } // namespace osmium
117 
118 #endif // OSMIUM_UTIL_FILE_HPP
size_t file_size(int fd)
Definition: file.hpp:66
size_t get_pagesize()
Definition: file.hpp:102
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
void resize_file(int fd, size_t new_size)
Definition: file.hpp:93