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 file exists
23inline bool file_exists(const std::string &filename)
24{
25 if (filename.empty())
26 return false;
27 struct stat buffer;
28 return ::stat(filename.c_str(), &buffer) == 0;
29}
30
37inline bool file_exists_nonempty(const std::string &filename)
38{
39 if (filename.empty())
40 return false;
41 struct stat s;
42 if (::stat(filename.c_str(), &s) != 0)
43 return false;
44 return s.st_size > 0;
45}
46
47// Return true if dirname is a directory
48inline bool is_directory(const std::string &pathname, const bool follow_symlinks = false)
49{
50 if (pathname.empty())
51 return false;
52 struct stat sb;
53 if (follow_symlinks)
54 return ::stat(pathname.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode);
55 else
56 return ::lstat(pathname.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode);
57}
58
59// Return file modification time (in seconds since unix epoch) or 0 on error
60inline time_t file_mod_time(const std::string &filename)
61{
62 struct stat buffer;
63 if (::stat(filename.c_str(), &buffer) != 0)
64 return 0;
65 else
66 return buffer.st_mtime;
67}
68
69// Return file modification time from a struct stat
70inline std::uint64_t stat_mod_time_nanoseconds(const struct stat &s)
71{
72 typedef std::uint64_t T;
73#if defined(__APPLE__)
74 return T(s.st_mtimespec.tv_sec) * T(1000000000) + T(s.st_mtimespec.tv_nsec);
75#else
76 return T(s.st_mtim.tv_sec) * T(1000000000) + T(s.st_mtim.tv_nsec);
77#endif
78}
79
80// Return file modification time from a file path (in nanoseconds since unix epoch) or 0 on error
81inline std::uint64_t file_mod_time_nanoseconds(const char *filename)
82{
83 struct stat s;
84 if (::stat(filename, &s) == 0)
86 else
87 return 0;
88}
89
90// Return file modification time from a file path (in nanoseconds since unix epoch) or 0 on error
91inline std::uint64_t file_mod_time_nanoseconds(const std::string &filename)
92{
93 return file_mod_time_nanoseconds(filename.c_str());
94}
95
96// Return file modification time from a file descriptor (in nanoseconds since unix epoch) or 0 on error
97inline std::uint64_t fd_mod_time_nanoseconds(const int fd)
98{
99 struct stat s;
100 if (::fstat(fd, &s) == 0)
102 else
103 return 0;
104}
105
106// Return file modification time (in milliseconds since unix epoch) or 0 on error
107inline std::uint64_t file_mod_time_milliseconds(const std::string &filename)
108{
109 return file_mod_time_nanoseconds(filename) / std::uint64_t(1000000);
110}
111
112} // namespace openvpn
113
114#endif
Support deferred server-side state creation when client connects.
Definition ovpncli.cpp:95
std::uint64_t stat_mod_time_nanoseconds(const struct stat &s)
Definition stat.hpp:70
std::uint64_t file_mod_time_milliseconds(const std::string &filename)
Definition stat.hpp:107
time_t file_mod_time(const std::string &filename)
Definition stat.hpp:60
bool is_directory(const std::string &pathname, const bool follow_symlinks=false)
Definition stat.hpp:48
bool file_exists_nonempty(const std::string &filename)
Definition stat.hpp:37
std::uint64_t file_mod_time_nanoseconds(const char *filename)
Definition stat.hpp:81
std::uint64_t fd_mod_time_nanoseconds(const int fd)
Definition stat.hpp:97
bool file_exists(const std::string &filename)
Definition stat.hpp:23