RESTinio
sendfile_defs_posix.hpp
Go to the documentation of this file.
1 /*
2  restinio
3 */
4 
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 
30 using file_descriptor_t = int;
32 using file_offset_t = std::int64_t;
33 using file_size_t = std::uint64_t;
35 
42 
45 constexpr file_descriptor_t null_file_descriptor(){ return -1; }
46 
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 
65 template < typename META >
66 META
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 
90  const std::chrono::system_clock::time_point
91  last_modified{
92 #if defined( RESTINIO_MACOS_TARGET )
93  std::chrono::seconds( file_stat.st_mtimespec.tv_sec ) +
94  std::chrono::microseconds( file_stat.st_mtimespec.tv_nsec / 1000 )
95 #else
96  std::chrono::seconds( file_stat.st_mtim.tv_sec ) +
97  std::chrono::microseconds( file_stat.st_mtim.tv_nsec / 1000 )
98 #endif
99  };
100 
101  return META{ static_cast< file_size_t >( file_stat.st_size ), last_modified };
102 }
103 
105 inline void
107 {
108  ::close( fd );
109 }
111 
112 } /* namespace restinio */
restinio::file_size_t
std::uint64_t file_size_t
Definition: sendfile_defs_default.hpp:23
restinio::file_descriptor_t
std::FILE * file_descriptor_t
Definition: sendfile_defs_default.hpp:21
restinio::file_offset_t
std::int64_t file_offset_t
Definition: sendfile_defs_default.hpp:22
restinio::close_file
void close_file(file_descriptor_t fd)
Close file by its descriptor.
Definition: sendfile_defs_default.hpp:89
restinio::open_file
file_descriptor_t open_file(const char *file_path)
Open file.
Definition: sendfile_defs_default.hpp:40
restinio::get_file_meta
META get_file_meta(file_descriptor_t fd)
Get file size.
Definition: sendfile_defs_default.hpp:55
restinio::null_file_descriptor
constexpr file_descriptor_t null_file_descriptor()
Get file descriptor which stands for null.
Definition: sendfile_defs_default.hpp:36
restinio
Definition: asio_include.hpp:21