OpenVPN 3 Core Library
Loading...
Searching...
No Matches
stat.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#ifndef OPENVPN_COMMON_STAT_H
13#define OPENVPN_COMMON_STAT_H
14
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <unistd.h>
18#include <cstdint> // for std::uint64_t
19
20namespace openvpn {
21
22// Return true if dirname is a directory
23inline bool is_directory(const std::string &pathname, const bool follow_symlinks = false)
24{
25 if (pathname.empty())
26 return false;
27 struct stat sb;
28 if (follow_symlinks)
29 return ::stat(pathname.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode);
30 else
31 return ::lstat(pathname.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode);
32}
33
34// Return file modification time (in seconds since unix epoch) or 0 on error
35inline time_t file_mod_time(const std::string &filename)
36{
37 struct stat buffer;
38 if (::stat(filename.c_str(), &buffer) != 0)
39 return 0;
40 else
41 return buffer.st_mtime;
42}
43
44// Return file modification time from a struct stat
45inline std::uint64_t stat_mod_time_nanoseconds(const struct stat &s)
46{
47 typedef std::uint64_t T;
48#if defined(__APPLE__)
49 return T(s.st_mtimespec.tv_sec) * T(1000000000) + T(s.st_mtimespec.tv_nsec);
50#else
51 return T(s.st_mtim.tv_sec) * T(1000000000) + T(s.st_mtim.tv_nsec);
52#endif
53}
54
55// Return file modification time from a file path (in nanoseconds since unix epoch) or 0 on error
56inline std::uint64_t file_mod_time_nanoseconds(const char *filename)
57{
58 struct stat s;
59 if (::stat(filename, &s) == 0)
61 else
62 return 0;
63}
64
65// Return file modification time from a file path (in nanoseconds since unix epoch) or 0 on error
66inline std::uint64_t file_mod_time_nanoseconds(const std::string &filename)
67{
68 return file_mod_time_nanoseconds(filename.c_str());
69}
70
71// Return file modification time from a file descriptor (in nanoseconds since unix epoch) or 0 on error
72inline std::uint64_t fd_mod_time_nanoseconds(const int fd)
73{
74 struct stat s;
75 if (::fstat(fd, &s) == 0)
77 else
78 return 0;
79}
80
81// Return file modification time (in milliseconds since unix epoch) or 0 on error
82inline std::uint64_t file_mod_time_milliseconds(const std::string &filename)
83{
84 return file_mod_time_nanoseconds(filename) / std::uint64_t(1000000);
85}
86
87} // namespace openvpn
88
89#endif
std::uint64_t stat_mod_time_nanoseconds(const struct stat &s)
Definition stat.hpp:45
std::uint64_t file_mod_time_milliseconds(const std::string &filename)
Definition stat.hpp:82
time_t file_mod_time(const std::string &filename)
Definition stat.hpp:35
bool is_directory(const std::string &pathname, const bool follow_symlinks=false)
Definition stat.hpp:23
std::uint64_t file_mod_time_nanoseconds(const char *filename)
Definition stat.hpp:56
std::uint64_t fd_mod_time_nanoseconds(const int fd)
Definition stat.hpp:72