Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/FileUtils.h
Line
Count
Source (jump to first uncovered line)
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/. */
6
7
#ifndef mozilla_FileUtils_h
8
#define mozilla_FileUtils_h
9
10
#include "nscore.h" // nullptr
11
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"
19
20
#include "mozilla/Scoped.h"
21
#include "nsIFile.h"
22
#include <errno.h>
23
#include <limits.h>
24
25
namespace mozilla {
26
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
34
35
/**
36
 * ScopedCloseFD is a RAII wrapper for POSIX file descriptors
37
 *
38
 * Instances |close()| their fds when they go out of scope.
39
 */
40
struct ScopedCloseFDTraits
41
{
42
  typedef int type;
43
0
  static type empty() { return -1; }
44
  static void release(type aFd)
45
0
  {
46
0
    if (aFd != -1) {
47
0
      while (close(aFd) == -1 && errno == EINTR) {
48
0
      }
49
0
    }
50
0
  }
51
};
52
typedef Scoped<ScopedCloseFDTraits> ScopedClose;
53
54
#if defined(MOZILLA_INTERNAL_API)
55
56
/**
57
 * AutoFDClose is a RAII wrapper for PRFileDesc.
58
 *
59
 * Instances |PR_Close| their fds when they go out of scope.
60
 **/
61
struct ScopedClosePRFDTraits
62
{
63
  typedef PRFileDesc* type;
64
42
  static type empty() { return nullptr; }
65
  static void release(type aFd)
66
33
  {
67
33
    if (aFd) {
68
6
      PR_Close(aFd);
69
6
    }
70
33
  }
71
};
72
typedef Scoped<ScopedClosePRFDTraits> AutoFDClose;
73
74
/* RAII wrapper for FILE descriptors */
75
struct ScopedCloseFileTraits
76
{
77
  typedef FILE* type;
78
0
  static type empty() { return nullptr; }
79
  static void release(type aFile)
80
0
  {
81
0
    if (aFile) {
82
0
      fclose(aFile);
83
0
    }
84
0
  }
85
};
86
typedef Scoped<ScopedCloseFileTraits> ScopedCloseFile;
87
88
/**
89
 * Fallocate efficiently and continuously allocates files via fallocate-type APIs.
90
 * This is useful for avoiding fragmentation.
91
 * On sucess the file be padded with zeros to grow to aLength.
92
 *
93
 * @param aFD file descriptor.
94
 * @param aLength length of file to grow to.
95
 * @return true on success.
96
 */
97
bool fallocate(PRFileDesc* aFD, int64_t aLength);
98
99
/**
100
 * Use readahead to preload shared libraries into the file cache before loading.
101
 * WARNING: This function should not be used without a telemetry field trial
102
 *          demonstrating a clear performance improvement!
103
 *
104
 * @param aFile nsIFile representing path to shared library
105
 */
106
void ReadAheadLib(nsIFile* aFile);
107
108
/**
109
 * Use readahead to preload a file into the file cache before reading.
110
 * WARNING: This function should not be used without a telemetry field trial
111
 *          demonstrating a clear performance improvement!
112
 *
113
 * @param aFile nsIFile representing path to shared library
114
 * @param aOffset Offset into the file to begin preloading
115
 * @param aCount Number of bytes to preload (SIZE_MAX implies file size)
116
 * @param aOutFd Pointer to file descriptor. If specified, ReadAheadFile will
117
 *        return its internal, opened file descriptor instead of closing it.
118
 */
119
void ReadAheadFile(nsIFile* aFile, const size_t aOffset = 0,
120
                   const size_t aCount = SIZE_MAX,
121
                   filedesc_t* aOutFd = nullptr);
122
123
/*
124
 * Wrappers for PR_GetLibraryName and PR_GetLibraryFilePathname.
125
 */
126
PathString GetLibraryName(pathstr_t aDirectory, const char* aLib);
127
PathString GetLibraryFilePathname(pathstr_t aName, PRFuncPtr aAddr);
128
129
#endif // MOZILLA_INTERNAL_API
130
131
/**
132
 * Use readahead to preload shared libraries into the file cache before loading.
133
 * WARNING: This function should not be used without a telemetry field trial
134
 *          demonstrating a clear performance improvement!
135
 *
136
 * @param aFilePath path to shared library
137
 */
138
void ReadAheadLib(pathstr_t aFilePath);
139
140
/**
141
 * Use readahead to preload a file into the file cache before loading.
142
 * WARNING: This function should not be used without a telemetry field trial
143
 *          demonstrating a clear performance improvement!
144
 *
145
 * @param aFilePath path to shared library
146
 * @param aOffset Offset into the file to begin preloading
147
 * @param aCount Number of bytes to preload (SIZE_MAX implies file size)
148
 * @param aOutFd Pointer to file descriptor. If specified, ReadAheadFile will
149
 *        return its internal, opened file descriptor instead of closing it.
150
 */
151
void ReadAheadFile(pathstr_t aFilePath, const size_t aOffset = 0,
152
                   const size_t aCount = SIZE_MAX,
153
                   filedesc_t* aOutFd = nullptr);
154
155
/**
156
 * Use readahead to preload a file into the file cache before reading.
157
 * When this function exits, the file pointer is guaranteed to be in the same
158
 * position it was in before this function was called.
159
 * WARNING: This function should not be used without a telemetry field trial
160
 *          demonstrating a clear performance improvement!
161
 *
162
 * @param aFd file descriptor opened for read access
163
 * (on Windows, file must be opened with FILE_FLAG_SEQUENTIAL_SCAN)
164
 * @param aOffset Offset into the file to begin preloading
165
 * @param aCount Number of bytes to preload (SIZE_MAX implies file size)
166
 */
167
void ReadAhead(filedesc_t aFd, const size_t aOffset = 0,
168
               const size_t aCount = SIZE_MAX);
169
170
#if defined(XP_UNIX)
171
6
#define MOZ_TEMP_FAILURE_RETRY(exp) (__extension__({ \
172
6
  typeof (exp) _rc; \
173
6
  do { \
174
6
    _rc = (exp); \
175
6
  } while (_rc == -1 && errno == EINTR); \
176
6
  _rc; \
177
6
}))
178
#endif
179
180
} // namespace mozilla
181
182
#endif