RESTinio
sendfile_defs_win.hpp
Go to the documentation of this file.
1 /*
2  restinio
3 */
4 
5 /*!
6  Sendfile routine definitions (win implementation).
7 
8  @since v.0.4.3
9 */
10 
11 #pragma once
12 
13 #if defined(RESTINIO_ASIO_HAS_WINDOWS_OVERLAPPED_PTR)
14 
15 #include <cstdio>
16 
17 namespace restinio
18 {
19 
20 /** @name Aliases for sendfile operation.
21  */
22 ///@{
23 using file_descriptor_t = HANDLE;
24 using file_offset_t = std::uint64_t;
25 using file_size_t = std::uint64_t;
26 ///@}
27 
28 /** @name File operations.
29  * @brief A minimal set of file operations.
30  *
31  * Incapsulates details of windows API for a set of file operations neccessary
32  * for sendfile_t class implementation.
33  */
34 ///@{
35 //! Get file descriptor which stands for null.
36 inline file_descriptor_t null_file_descriptor(){ return INVALID_HANDLE_VALUE; }
37 
38 //! Open file.
39 inline file_descriptor_t
40 open_file( const char * file_path )
41 {
42  file_descriptor_t file_descriptor =
43  // We don't support Unicode on Windows, so call Ansi-version of
44  // CreateFile directly.
45  ::CreateFileA(
46  file_path,
47  GENERIC_READ,
48  FILE_SHARE_READ,
49  0,
50  OPEN_EXISTING,
51  FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
52  0 );
53 
54  if( null_file_descriptor() == file_descriptor )
55  {
56  throw exception_t{
57  fmt::format( "unable to openfile '{}': error({})", file_path, GetLastError() ) };
58  }
59 
60  return file_descriptor;
61 }
62 
63 //! Get file meta.
64 template < typename META >
65 META
66 get_file_meta( file_descriptor_t fd )
67 {
68  file_size_t fsize = 0;
69  std::chrono::system_clock::time_point flastmodified;
70 
71  if( null_file_descriptor() != fd )
72  {
73  LARGE_INTEGER file_size;
74  // Obtain file size:
75  if( GetFileSizeEx( fd, &file_size ) )
76  {
77  fsize = static_cast< file_size_t >( file_size.QuadPart );
78  }
79  else
80  {
81  throw exception_t{
82  fmt::format( "unable to get file size: error code:{}", GetLastError() ) };
83  }
84 
85  FILETIME ftWrite;
86  if( GetFileTime( fd, NULL, NULL, &ftWrite ) )
87  {
88  // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724284(v=vs.85).aspx
89 
90  // Microseconds between 1601-01-01 00:00:00 UTC and 1970-01-01 00:00:00 UTC
91  constexpr std::uint64_t nanosec100_in_microsec = 10;
92  constexpr std::uint64_t epoch_difference_in_microsec =
93  11644473600ULL * 1000 *1000;
94 
95  // First convert 100-ns intervals to microseconds, then adjust for the
96  // epoch difference
97  ULARGE_INTEGER ull;
98  ull.LowPart = ftWrite.dwLowDateTime;
99  ull.HighPart = ftWrite.dwHighDateTime;
100 
101  flastmodified =
102  std::chrono::system_clock::time_point{
103  std::chrono::microseconds(
104  ull.QuadPart / nanosec100_in_microsec - epoch_difference_in_microsec ) };
105  }
106  else
107  {
108  throw exception_t{
109  fmt::format(
110  "unable to get file last modification: error code:{}",
111  GetLastError() ) };
112  }
113  }
114 
115  return META{ fsize, flastmodified};
116 }
117 
118 //! Close file by its descriptor.
119 inline void
120 close_file( file_descriptor_t fd )
121 {
122  CloseHandle( fd );
123 }
124 ///@}
125 
126 } /* namespace restinio */
127 
128 #else // #if defined(RESTINIO_ASIO_HAS_WINDOWS_OVERLAPPED_PTR)
129 
130 #include <restinio/sendfile_defs_default.hpp>
131 
132 #endif // #if defined(RESTINIO_ASIO_HAS_WINDOWS_OVERLAPPED_PTR)