BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdls_filesystemutil_uniximputil.h
Go to the documentation of this file.
1/// @file bdls_filesystemutil_uniximputil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdls_filesystemutil_uniximputil.h -*-C++-*-
8#ifndef INCLUDED_BDLS_FILESYSTEMUTIL_UNIXIMPUTIL
9#define INCLUDED_BDLS_FILESYSTEMUTIL_UNIXIMPUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdls_filesystemutil_uniximputil bdls_filesystemutil_uniximputil
15/// @brief Provide testable `bdls::FilesystemUtil` operations for some Unixes.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdls
19/// @{
20/// @addtogroup bdls_filesystemutil_uniximputil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdls_filesystemutil_uniximputil-purpose"> Purpose</a>
25/// * <a href="#bdls_filesystemutil_uniximputil-classes"> Classes </a>
26/// * <a href="#bdls_filesystemutil_uniximputil-description"> Description </a>
27///
28/// # Purpose {#bdls_filesystemutil_uniximputil-purpose}
29/// Provide testable `bdls::FilesystemUtil` operations for some Unixes.
30///
31/// # Classes {#bdls_filesystemutil_uniximputil-classes}
32///
33/// - bdls::FilesystemUtil_UnixImpUtil: testable file-system utilities
34///
35/// @see bdls_filesystemutil, bdls_filesystemutil_unixplatform
36///
37/// # Description {#bdls_filesystemutil_uniximputil-description}
38/// This subordinate component to @ref bdls_filesystemutil provides a
39/// utility `struct` template, `bdls::FilesystemUtil_UnixImpUtil` for
40/// implementing some of `bdls::FilesystemUtil`s functions on Unix platforms.
41/// `bdls::FilesystemUtil_UnixImpUtil` accesses Unix functions and types through
42/// its template parameter, `UNIX_INTERFACE`, in order to allow tests to supply
43/// mock Unix interfaces.
44/// @}
45/** @} */
46/** @} */
47
48/** @addtogroup bdl
49 * @{
50 */
51/** @addtogroup bdls
52 * @{
53 */
54/** @addtogroup bdls_filesystemutil_uniximputil
55 * @{
56 */
57
58#include <bdlt_datetime.h>
59#include <bdlt_epochutil.h>
60#include <bdlt_timeunitratio.h>
61
62#include <bsls_assert.h>
63
64
65namespace bdls {
66
67 // =================================
68 // struct FilesystemUtil_UnixImpUtil
69 // =================================
70
71/// This component-private utility `struct` provides a namespace for a suite
72/// of functions that `FilesystemUtil` uses as implementation details.
73/// These functions have a `UNIX_INTERFACE` template parameter, which
74/// provides access to the entities that large-file environment Unix
75/// systems declare, and that the function implementations need.
76///
77///
78/// \note Note that, on some Unix platforms and some build configurations, the
79/// `stat` struct does not have an `st_mtime` field, and `st_mtime` is a
80/// macro that emulates the access of the field. Similarly, on some Unix
81/// platforms and some build configurations, the `stat` structure does not
82/// have an `st_mtim` field or the `st_mtim` struct does not have a
83/// `tv_nsec` (or, for some versions of Solaris, `__tv_nsec`) field, in
84/// which case the @ref get_st_mtim_nsec function returns zero. For more
85/// information, please see the specification of the `sys/stat.h` header
86/// from IEEE Std
87/// 1003.1-2017, which provides information about the evolution of the
88/// `stat` struct in the POSIX specification
89/// (https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/).
90///
91/// The program is ill-formed unless the specified `UNIX_INTERFACE` is a
92/// class type that meets the following requirements:
93///
94/// * `UNIX_INTERFACE::off_t` is a type alias to the `off_t` type provided
95/// by the `sys/types.h` header.
96/// * `UNIX_INTERFACE::stat` is a type alias to the `stat` type provided
97/// by the `sys/stat.h` header.
98/// * `UNIX_INTERFACE::time_t` is a type alias to the `time_t` type provided
99/// by the `sys/types.h` header.
100/// * `UNIX_INTERFACE::get_st_mtim_nsec` is a public, static member
101/// function that has `long (const stat& stat)` type and whose contract
102/// is to return the value of the `st_mtim.tv_nsec` field of the
103/// specified `stat` struct.
104/// * `UNIX_INTERFACE::get_st_mtime` is a public, static member function
105/// that has `time_t (const stat& stat)` type and whose contract is to
106/// return the value of the `st_mtime` field of the specified `stat`
107/// struct.
108/// * `UNIX_INTERFACE::get_st_size` is a public, static member function
109/// that has `off_t (const stat& stat)` type and whose contract is to
110/// return the value of the `st_size` field of the specified `stat` struct.
111///
112/// \note Note that this function is required in order to
113/// access the data members of a `stat` struct in a manner consistent
114/// with the requirements of @ref get_st_mtime .
115/// * `UNIX_INTERFACE::fstat` is a public, static member function that has
116/// `int (int fildes, stat *buf)` type and whose contract is to return
117/// the result of `::fstat(fildes, buf)`, where `::fstat` is the
118/// function provided by the `sys/stat.h` header.
119///
120/// See @ref bdls_filesystemutil_uniximputil
121template <class UNIX_INTERFACE>
123
124 // TYPES
125
126 /// `FileDescriptor` is an alias for the operating system's native
127 /// file descriptor / file handle type.
128 typedef int FileDescriptor;
129
130 /// `Offset` is an alias for a signed integral type, and represents the
131 /// offset of a location in a file.
132 typedef typename UNIX_INTERFACE::off_t Offset;
133
134 private:
135 // PRIVATE TYPES
136
137 /// `off_t` is an alias to the `off_t` type provided by the
138 /// `sys/types.h` header. It is a signed integral type used to represent quantities of bytes.
139 ///
140 /// \note Note that, depending on the build
141 /// configuration, this type may have 32 or 64 bits.
142 typedef typename UNIX_INTERFACE::off_t off_t;
143
144 /// `stat` is an alias to the `stat` `struct` provided by the
145 /// `sys/stat.h` header.
146 typedef typename UNIX_INTERFACE::stat stat;
147
148 /// `time_t` is an alias to the `time_t` type provided by the
149 /// `sys/types.h` header. It represents a time point as number of
150 /// seconds since January 1st 1970 in Coordinated Universal Time.
151 typedef typename UNIX_INTERFACE::time_t time_t;
152
153 // PRIVATE CLASS METHODS
154
155 /// Return the value of the `st_mtim.nsec` field of the specified `stat`
156 /// struct.
157 static long get_st_mtim_nsec(const stat& stat);
158
159 /// Return the value of the `st_mtime` data member of the specified
160 /// `stat`.
161 static time_t get_st_mtime(const stat& stat);
162
163 /// Return the value of the `st_size` data member of the specified `stat` struct.
164 ///
165 /// \note Note that this function is provided in order to
166 /// create a consistent interface for accessing the data members of a
167 /// `stat` struct with @ref get_st_mtime .
168 static off_t get_st_size(const stat& stat);
169
170 /// Invoke and return the result of `::fstat(fildes, buf)` with the
171 /// specified `fildes` and `buf`, where `::fstat` is the function
172 /// provided by the `sys/stat.h` header.
173 static int fstat(int fildes, stat *buf);
174
175 public:
176 // CLASS METHODS
177
178 /// Return the size, in bytes, of the file with the specified
179 /// `descriptor`, or a negative value if an error occurs.
180 static Offset getFileSize(FileDescriptor descriptor);
181
182 /// Load into the specified `time` the last modification time of the
183 /// file with the specified `descriptor`, as reported by the filesystem. Return 0 on success, and a non-zero value otherwise.
184 ///
185 /// \note Note that the
186 /// time is reported in UTC.
188 FileDescriptor descriptor);
189};
190
191// ============================================================================
192// INLINE DEFINITIONS
193// ============================================================================
194
195 // ---------------------------------
196 // struct FilesystemUtil_UnixImpUtil
197 // ---------------------------------
198
199// PRIVATE CLASS METHODS
200template <class UNIX_INTERFACE>
201long
203{
204 return UNIX_INTERFACE::get_st_mtim_nsec(stat);
205}
206
207template <class UNIX_INTERFACE>
208typename UNIX_INTERFACE::time_t
209FilesystemUtil_UnixImpUtil<UNIX_INTERFACE>::get_st_mtime(const stat& stat)
210{
211 return UNIX_INTERFACE::get_st_mtime(stat);
212}
213
214template <class UNIX_INTERFACE>
215typename UNIX_INTERFACE::off_t
216FilesystemUtil_UnixImpUtil<UNIX_INTERFACE>::get_st_size(const stat& stat)
217{
218 return UNIX_INTERFACE::get_st_size(stat);
219}
220
221template <class UNIX_INTERFACE>
222int FilesystemUtil_UnixImpUtil<UNIX_INTERFACE>::fstat(int fildes, stat *buf)
223{
224 return UNIX_INTERFACE::fstat(fildes, buf);
225}
226
227// CLASS METHODS
228template <class UNIX_INTERFACE>
231 FileDescriptor descriptor)
232{
233 stat statResult;
234 const int rc = fstat(descriptor, &statResult);
235 if (0 != rc) {
236 return -1; // RETURN
237 }
238
239 return get_st_size(statResult);
240}
241
242template <class UNIX_INTERFACE>
244 bdlt::Datetime *time,
245 FileDescriptor descriptor)
246{
247 stat statResult;
248 int rc = fstat(descriptor, &statResult);
249 if (0 != rc) {
250 return -1; // RETURN
251 }
252
254 rc = result.addSecondsIfValid(get_st_mtime(statResult));
255 if (0 != rc) {
256 return -1; // RETURN
257 }
258
259 long nanoseconds = get_st_mtim_nsec(statResult);
260 BSLS_ASSERT_SAFE((0 <= nanoseconds) &&
261 (nanoseconds <
263 rc = result.addMicrosecondsIfValid(
265 if (0 != rc) {
266 return -1; // RETURN
267 }
268
269 *time = result;
270 return 0;
271}
272
273} // close package namespace
274
275
276#endif
277
278// ----------------------------------------------------------------------------
279// Copyright 2020 Bloomberg Finance L.P.
280//
281// Licensed under the Apache License, Version 2.0 (the "License");
282// you may not use this file except in compliance with the License.
283// You may obtain a copy of the License at
284//
285// http://www.apache.org/licenses/LICENSE-2.0
286//
287// Unless required by applicable law or agreed to in writing, software
288// distributed under the License is distributed on an "AS IS" BASIS,
289// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
290// See the License for the specific language governing permissions and
291// limitations under the License.
292// ----------------------------- END-OF-FILE ----------------------------------
293
294/** @} */
295/** @} */
296/** @} */
Definition bdlt_datetime.h:330
int addMicrosecondsIfValid(bsls::Types::Int64 microseconds)
Definition bdlt_datetime.h:2173
int addSecondsIfValid(bsls::Types::Int64 seconds)
Definition bdlt_datetime.h:2110
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1917
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdls_fdstreambuf.h:412
Definition bdls_filesystemutil_uniximputil.h:122
static Offset getFileSize(FileDescriptor descriptor)
Definition bdls_filesystemutil_uniximputil.h:230
UNIX_INTERFACE::off_t Offset
Definition bdls_filesystemutil_uniximputil.h:132
static int getLastModificationTime(bdlt::Datetime *time, FileDescriptor descriptor)
Definition bdls_filesystemutil_uniximputil.h:243
int FileDescriptor
Definition bdls_filesystemutil_uniximputil.h:128
static const Datetime & epoch()
Definition bdlt_epochutil.h:397
static const bsls::Types::Int64 k_NANOSECONDS_PER_SECOND
Definition bdlt_timeunitratio.h:219
static const int k_NANOSECONDS_PER_MICROSECOND_32
Definition bdlt_timeunitratio.h:298