1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_FileUtils_h
8 #define mozilla_FileUtils_h
10 #include "nscore.h" // nullptr
20 #include "mozilla/Scoped.h"
28 typedef void* filedesc_t
;
29 typedef const wchar_t* pathstr_t
;
31 typedef int filedesc_t
;
32 typedef const char* pathstr_t
;
36 * ScopedCloseFD is a RAII wrapper for POSIX file descriptors
38 * Instances |close()| their fds when they go out of scope.
40 struct ScopedCloseFDTraits
{
42 static type
empty() { return -1; }
43 static void release(type aFd
) {
45 while (close(aFd
) == -1 && errno
== EINTR
) {
50 typedef Scoped
<ScopedCloseFDTraits
> ScopedClose
;
52 #if defined(MOZILLA_INTERNAL_API)
55 * AutoFDClose is a RAII wrapper for PRFileDesc.
57 * Instances |PR_Close| their fds when they go out of scope.
59 struct ScopedClosePRFDTraits
{
60 typedef PRFileDesc
* type
;
61 static type
empty() { return nullptr; }
62 static void release(type aFd
) {
68 typedef Scoped
<ScopedClosePRFDTraits
> AutoFDClose
;
70 /* RAII wrapper for FILE descriptors */
71 struct ScopedCloseFileTraits
{
73 static type
empty() { return nullptr; }
74 static void release(type aFile
) {
80 typedef Scoped
<ScopedCloseFileTraits
> ScopedCloseFile
;
83 * Fallocate efficiently and continuously allocates files via fallocate-type
84 * APIs. This is useful for avoiding fragmentation. On sucess the file be padded
85 * with zeros to grow to aLength.
87 * @param aFD file descriptor.
88 * @param aLength length of file to grow to.
89 * @return true on success.
91 bool fallocate(PRFileDesc
* aFD
, int64_t aLength
);
94 * Use readahead to preload shared libraries into the file cache before loading.
95 * WARNING: This function should not be used without a telemetry field trial
96 * demonstrating a clear performance improvement!
98 * @param aFile nsIFile representing path to shared library
100 void ReadAheadLib(nsIFile
* aFile
);
103 * Use readahead to preload a file into the file cache before reading.
104 * WARNING: This function should not be used without a telemetry field trial
105 * demonstrating a clear performance improvement!
107 * @param aFile nsIFile representing path to shared library
108 * @param aOffset Offset into the file to begin preloading
109 * @param aCount Number of bytes to preload (SIZE_MAX implies file size)
110 * @param aOutFd Pointer to file descriptor. If specified, ReadAheadFile will
111 * return its internal, opened file descriptor instead of closing it.
113 void ReadAheadFile(nsIFile
* aFile
, const size_t aOffset
= 0,
114 const size_t aCount
= SIZE_MAX
,
115 filedesc_t
* aOutFd
= nullptr);
118 * Wrappers for PR_GetLibraryName and PR_GetLibraryFilePathname.
120 PathString
GetLibraryName(pathstr_t aDirectory
, const char* aLib
);
121 PathString
GetLibraryFilePathname(pathstr_t aName
, PRFuncPtr aAddr
);
123 #endif // MOZILLA_INTERNAL_API
126 * Use readahead to preload shared libraries into the file cache before loading.
127 * WARNING: This function should not be used without a telemetry field trial
128 * demonstrating a clear performance improvement!
130 * @param aFilePath path to shared library
132 void ReadAheadLib(pathstr_t aFilePath
);
135 * Use readahead to preload a file into the file cache before loading.
136 * WARNING: This function should not be used without a telemetry field trial
137 * demonstrating a clear performance improvement!
139 * @param aFilePath path to shared library
140 * @param aOffset Offset into the file to begin preloading
141 * @param aCount Number of bytes to preload (SIZE_MAX implies file size)
142 * @param aOutFd Pointer to file descriptor. If specified, ReadAheadFile will
143 * return its internal, opened file descriptor instead of closing it.
145 void ReadAheadFile(pathstr_t aFilePath
, const size_t aOffset
= 0,
146 const size_t aCount
= SIZE_MAX
,
147 filedesc_t
* aOutFd
= nullptr);
150 * Use readahead to preload a file into the file cache before reading.
151 * When this function exits, the file pointer is guaranteed to be in the same
152 * position it was in before this function was called.
153 * WARNING: This function should not be used without a telemetry field trial
154 * demonstrating a clear performance improvement!
156 * @param aFd file descriptor opened for read access
157 * (on Windows, file must be opened with FILE_FLAG_SEQUENTIAL_SCAN)
158 * @param aOffset Offset into the file to begin preloading
159 * @param aCount Number of bytes to preload (SIZE_MAX implies file size)
161 void ReadAhead(filedesc_t aFd
, const size_t aOffset
= 0,
162 const size_t aCount
= SIZE_MAX
);
165 # define MOZ_TEMP_FAILURE_RETRY(exp) \
170 } while (_rc == -1 && errno == EINTR); \
175 } // namespace mozilla