Bug 1631807 [wpt PR 23140] - Test new Web IDL class string behavior, a=testonly
[gecko.git] / xpcom / glue / FileUtils.h
blobcfbd183f2ddbbc4d562fb6e33b891081ddbbd3ea
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
12 #if defined(XP_UNIX)
13 # include <unistd.h>
14 #elif defined(XP_WIN)
15 # include <io.h>
16 #endif
17 #include "prio.h"
18 #include "prlink.h"
20 #include "mozilla/Scoped.h"
21 #include "nsIFile.h"
22 #include <errno.h>
23 #include <limits.h>
25 namespace mozilla {
27 #if defined(XP_WIN)
28 typedef void* filedesc_t;
29 typedef const wchar_t* pathstr_t;
30 #else
31 typedef int filedesc_t;
32 typedef const char* pathstr_t;
33 #endif
35 /**
36 * ScopedCloseFD is a RAII wrapper for POSIX file descriptors
38 * Instances |close()| their fds when they go out of scope.
40 struct ScopedCloseFDTraits {
41 typedef int type;
42 static type empty() { return -1; }
43 static void release(type aFd) {
44 if (aFd != -1) {
45 while (close(aFd) == -1 && errno == EINTR) {
50 typedef Scoped<ScopedCloseFDTraits> ScopedClose;
52 #if defined(MOZILLA_INTERNAL_API)
54 /**
55 * AutoFDClose is a RAII wrapper for PRFileDesc.
57 * Instances |PR_Close| their fds when they go out of scope.
58 **/
59 struct ScopedClosePRFDTraits {
60 typedef PRFileDesc* type;
61 static type empty() { return nullptr; }
62 static void release(type aFd) {
63 if (aFd) {
64 PR_Close(aFd);
68 typedef Scoped<ScopedClosePRFDTraits> AutoFDClose;
70 /* RAII wrapper for FILE descriptors */
71 struct ScopedCloseFileTraits {
72 typedef FILE* type;
73 static type empty() { return nullptr; }
74 static void release(type aFile) {
75 if (aFile) {
76 fclose(aFile);
80 typedef Scoped<ScopedCloseFileTraits> ScopedCloseFile;
82 /**
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);
93 /**
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);
164 #if defined(XP_UNIX)
165 # define MOZ_TEMP_FAILURE_RETRY(exp) \
166 (__extension__({ \
167 typeof(exp) _rc; \
168 do { \
169 _rc = (exp); \
170 } while (_rc == -1 && errno == EINTR); \
171 _rc; \
173 #endif
175 } // namespace mozilla
177 #endif