RESTinio
sendfile_defs_posix.hpp
Go to the documentation of this file.
1 /*
2  restinio
3 */
4 
5 /*!
6  Sendfile routine definitions (posix implementation).
7 
8  @since v.0.4.3
9 */
10 
11 #pragma once
12 
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 
17 #include <iostream>
18 
19 namespace restinio
20 {
21 
22 #if defined( __FreeBSD__ )
23  #define RESTINIO_FREEBSD_TARGET
24 #elif defined(__APPLE__) && defined( __MACH__ )
25  #define RESTINIO_MACOS_TARGET
26 #endif
27 
28 /** @name Aliases for sendfile operation.
29  */
30 ///@{
31 using file_descriptor_t = int;
32 using file_offset_t = std::int64_t;
33 using file_size_t = std::uint64_t;
34 ///@}
35 
36 /** @name File operations.
37  * @brief A minimal set of file operations.
38  *
39  * Incapsulates details of native API for a set of file operations neccessary
40  * for sendfile_t class implementation.
41  */
42 ///@{
43 
44 //! Get file descriptor which stands for null.
45 constexpr file_descriptor_t null_file_descriptor(){ return -1; }
46 
47 //! Open file.
48 inline file_descriptor_t
49 open_file( const char * file_path)
50 {
51 #if defined( RESTINIO_FREEBSD_TARGET ) || defined( RESTINIO_MACOS_TARGET )
52  file_descriptor_t file_descriptor = ::open( file_path, O_RDONLY );
53 #else
54  file_descriptor_t file_descriptor = ::open( file_path, O_RDONLY | O_LARGEFILE );
55 #endif
56  if( null_file_descriptor() == file_descriptor )
57  {
58  throw exception_t{
59  fmt::format( "unable to openfile '{}': {}", file_path, strerror( errno ) ) };
60  }
61  return file_descriptor;
62 }
63 
64 //! Get file meta.
65 template < typename META >
66 META
67 get_file_meta( file_descriptor_t fd )
68 {
69  if( null_file_descriptor() == fd )
70  {
71  throw exception_t{ "invalid file descriptor" };
72  }
73 
74 #if defined( RESTINIO_FREEBSD_TARGET ) || defined( RESTINIO_MACOS_TARGET )
75  struct stat file_stat;
76 
77  const auto fstat_rc = ::fstat( fd, &file_stat );
78 #else
79  struct stat64 file_stat;
80 
81  const auto fstat_rc = fstat64( fd, &file_stat );
82 #endif
83 
84  if( 0 != fstat_rc )
85  {
86  throw exception_t{
87  fmt::format( "unable to get file stat : {}", strerror( errno ) ) };
88  }
89 
92 #if defined( RESTINIO_MACOS_TARGET )
95 #else
98 #endif
99  };
100 
101  return META{ static_cast< file_size_t >( file_stat.st_size ), last_modified };
102 }
103 
104 //! Close file by its descriptor.
105 inline void
106 close_file( file_descriptor_t fd )
107 {
108  ::close( fd );
109 }
110 ///@}
111 
112 } /* namespace restinio */
constexpr file_descriptor_t null_file_descriptor()
Get file descriptor which stands for null.
void close_file(file_descriptor_t fd)
Close file by its descriptor.
META get_file_meta(file_descriptor_t fd)
Get file size.
file_descriptor_t open_file(const char *file_path)
Open file.
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, optional_t< Value_Type > >::type opt_value(const Parameter_Container &params, string_view_t key)
Gets the value of a parameter specified by key wrapped in optional_t<Value_Type> if parameter exists ...
Definition: value_or.hpp:64