Silicium
file_size.hpp
Go to the documentation of this file.
1 #ifndef SILICIUM_FILE_SIZE_HPP
2 #define SILICIUM_FILE_SIZE_HPP
3 
5 #include <silicium/error_or.hpp>
7 
8 #ifndef _WIN32
9 # include <sys/stat.h>
10 #endif
11 
12 namespace Si
13 {
16  inline Si::error_or<Si::optional<boost::uint64_t>> file_size(Si::native_file_descriptor file)
17  {
18 #ifdef _WIN32
19  LARGE_INTEGER size;
20  if (!GetFileSizeEx(file, &size))
21  {
22  return get_last_error();
23  }
24  assert(size.QuadPart >= 0);
25  return static_cast<boost::uint64_t>(size.QuadPart);
26 #else
27  struct stat buffer;
28  if (fstat(file, &buffer) < 0)
29  {
30  return get_last_error();
31  }
32  if ((buffer.st_mode & S_IFMT) != S_IFREG)
33  {
34  return Si::none;
35  }
36  return static_cast<boost::uint64_t>(buffer.st_size);
37 #endif
38  }
39 }
40 
41 #endif
Si::error_or< Si::optional< boost::uint64_t > > file_size(Si::native_file_descriptor file)
Definition: file_size.hpp:16
Definition: absolute_path.hpp:19
Definition: error_or.hpp:48
SILICIUM_USE_RESULT boost::system::error_code get_last_error()
Definition: get_last_error.hpp:16