1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/glue/FileUtils.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,218 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef mozilla_FileUtils_h 1.10 +#define mozilla_FileUtils_h 1.11 + 1.12 +#include "nscore.h" // nullptr 1.13 + 1.14 +#if defined(XP_UNIX) 1.15 +# include <unistd.h> 1.16 +#elif defined(XP_WIN) 1.17 +# include <io.h> 1.18 +#endif 1.19 +#include "prio.h" 1.20 + 1.21 +#include "mozilla/Scoped.h" 1.22 +#include "nsIFile.h" 1.23 +#include <errno.h> 1.24 +#include <limits.h> 1.25 + 1.26 +namespace mozilla { 1.27 + 1.28 +#if defined(XP_WIN) 1.29 +typedef void* filedesc_t; 1.30 +typedef const wchar_t* pathstr_t; 1.31 +#else 1.32 +typedef int filedesc_t; 1.33 +typedef const char* pathstr_t; 1.34 +#endif 1.35 + 1.36 +/** 1.37 + * ScopedCloseFD is a RAII wrapper for POSIX file descriptors 1.38 + * 1.39 + * Instances |close()| their fds when they go out of scope. 1.40 + */ 1.41 +struct ScopedCloseFDTraits 1.42 +{ 1.43 + typedef int type; 1.44 + static type empty() { return -1; } 1.45 + static void release(type fd) { 1.46 + if (fd != -1) { 1.47 + while ((close(fd) == -1) && (errno == EINTR)) { 1.48 + ; 1.49 + } 1.50 + } 1.51 + } 1.52 +}; 1.53 +typedef Scoped<ScopedCloseFDTraits> ScopedClose; 1.54 + 1.55 +#if !defined(XPCOM_GLUE) 1.56 + 1.57 +/** 1.58 + * AutoFDClose is a RAII wrapper for PRFileDesc. 1.59 + * 1.60 + * Instances |PR_Close| their fds when they go out of scope. 1.61 + **/ 1.62 +struct ScopedClosePRFDTraits 1.63 +{ 1.64 + typedef PRFileDesc* type; 1.65 + static type empty() { return nullptr; } 1.66 + static void release(type fd) { 1.67 + if (fd != nullptr) { 1.68 + PR_Close(fd); 1.69 + } 1.70 + } 1.71 +}; 1.72 +typedef Scoped<ScopedClosePRFDTraits> AutoFDClose; 1.73 + 1.74 +/* RAII wrapper for FILE descriptors */ 1.75 +struct ScopedCloseFileTraits 1.76 +{ 1.77 + typedef FILE *type; 1.78 + static type empty() { return nullptr; } 1.79 + static void release(type f) { 1.80 + if (f) { 1.81 + fclose(f); 1.82 + } 1.83 + } 1.84 +}; 1.85 +typedef Scoped<ScopedCloseFileTraits> ScopedCloseFile; 1.86 + 1.87 +/** 1.88 + * Fallocate efficiently and continuously allocates files via fallocate-type APIs. 1.89 + * This is useful for avoiding fragmentation. 1.90 + * On sucess the file be padded with zeros to grow to aLength. 1.91 + * 1.92 + * @param aFD file descriptor. 1.93 + * @param aLength length of file to grow to. 1.94 + * @return true on success. 1.95 + */ 1.96 +NS_COM_GLUE bool fallocate(PRFileDesc *aFD, int64_t aLength); 1.97 + 1.98 +/** 1.99 + * Use readahead to preload shared libraries into the file cache before loading. 1.100 + * WARNING: This function should not be used without a telemetry field trial 1.101 + * demonstrating a clear performance improvement! 1.102 + * 1.103 + * @param aFile nsIFile representing path to shared library 1.104 + */ 1.105 +NS_COM_GLUE void ReadAheadLib(nsIFile* aFile); 1.106 + 1.107 +/** 1.108 + * Use readahead to preload a file into the file cache before reading. 1.109 + * WARNING: This function should not be used without a telemetry field trial 1.110 + * demonstrating a clear performance improvement! 1.111 + * 1.112 + * @param aFile nsIFile representing path to shared library 1.113 + * @param aOffset Offset into the file to begin preloading 1.114 + * @param aCount Number of bytes to preload (SIZE_MAX implies file size) 1.115 + * @param aOutFd Pointer to file descriptor. If specified, ReadAheadFile will 1.116 + * return its internal, opened file descriptor instead of closing it. 1.117 + */ 1.118 +NS_COM_GLUE void ReadAheadFile(nsIFile* aFile, const size_t aOffset = 0, 1.119 + const size_t aCount = SIZE_MAX, 1.120 + filedesc_t* aOutFd = nullptr); 1.121 + 1.122 +#endif // !defined(XPCOM_GLUE) 1.123 + 1.124 +/** 1.125 + * Use readahead to preload shared libraries into the file cache before loading. 1.126 + * WARNING: This function should not be used without a telemetry field trial 1.127 + * demonstrating a clear performance improvement! 1.128 + * 1.129 + * @param aFilePath path to shared library 1.130 + */ 1.131 +NS_COM_GLUE void ReadAheadLib(pathstr_t aFilePath); 1.132 + 1.133 +/** 1.134 + * Use readahead to preload a file into the file cache before loading. 1.135 + * WARNING: This function should not be used without a telemetry field trial 1.136 + * demonstrating a clear performance improvement! 1.137 + * 1.138 + * @param aFilePath path to shared library 1.139 + * @param aOffset Offset into the file to begin preloading 1.140 + * @param aCount Number of bytes to preload (SIZE_MAX implies file size) 1.141 + * @param aOutFd Pointer to file descriptor. If specified, ReadAheadFile will 1.142 + * return its internal, opened file descriptor instead of closing it. 1.143 + */ 1.144 +NS_COM_GLUE void ReadAheadFile(pathstr_t aFilePath, const size_t aOffset = 0, 1.145 + const size_t aCount = SIZE_MAX, 1.146 + filedesc_t* aOutFd = nullptr); 1.147 + 1.148 +/** 1.149 + * Use readahead to preload a file into the file cache before reading. 1.150 + * When this function exits, the file pointer is guaranteed to be in the same 1.151 + * position it was in before this function was called. 1.152 + * WARNING: This function should not be used without a telemetry field trial 1.153 + * demonstrating a clear performance improvement! 1.154 + * 1.155 + * @param aFd file descriptor opened for read access 1.156 + * (on Windows, file must be opened with FILE_FLAG_SEQUENTIAL_SCAN) 1.157 + * @param aOffset Offset into the file to begin preloading 1.158 + * @param aCount Number of bytes to preload (SIZE_MAX implies file size) 1.159 + */ 1.160 +NS_COM_GLUE void ReadAhead(filedesc_t aFd, const size_t aOffset = 0, 1.161 + const size_t aCount = SIZE_MAX); 1.162 + 1.163 + 1.164 +/* Define ReadSysFile() only on GONK to avoid unnecessary lubxul bloat. 1.165 +Also define it in debug builds, so that unit tests for it can be written 1.166 +and run in non-GONK builds. */ 1.167 +#if (defined(MOZ_WIDGET_GONK) || defined(DEBUG)) && defined(XP_UNIX) 1.168 + 1.169 +#ifndef ReadSysFile_PRESENT 1.170 +#define ReadSysFile_PRESENT 1.171 +#endif /* ReadSysFile_PRESENT */ 1.172 + 1.173 +#define MOZ_TEMP_FAILURE_RETRY(exp) (__extension__({ \ 1.174 + typeof (exp) _rc; \ 1.175 + do { \ 1.176 + _rc = (exp); \ 1.177 + } while (_rc == -1 && errno == EINTR); \ 1.178 + _rc; \ 1.179 +})) 1.180 + 1.181 +/** 1.182 + * Read the contents of a file. 1.183 + * This function is intended for reading a single-lined text files from 1.184 + * /sys/. If the file ends with a newline ('\n') then it will be discarded. 1.185 + * The output buffer will always be '\0'-terminated on successful completion. 1.186 + * If aBufSize == 0, then this function will return true if the file exists 1.187 + * and is readable (it will not attempt to read anything from it). 1.188 + * On failure the contents of aBuf after this call will be undefined and the 1.189 + * value of the global variable errno will be set accordingly. 1.190 + * @return true on success, notice that less than requested bytes could have 1.191 + * been read if the file was smaller 1.192 + */ 1.193 +bool 1.194 +ReadSysFile( 1.195 + const char* aFilename, 1.196 + char* aBuf, 1.197 + size_t aBufSize); 1.198 + 1.199 +/** 1.200 + * Parse the contents of a file, assuming it contains a decimal integer. 1.201 + * @return true on success 1.202 + */ 1.203 +bool 1.204 +ReadSysFile( 1.205 + const char* aFilename, 1.206 + int* aVal); 1.207 + 1.208 +/** 1.209 + * Parse the contents of a file, assuming it contains a boolean value 1.210 + * (either 0 or 1). 1.211 + * @return true on success 1.212 + */ 1.213 +bool 1.214 +ReadSysFile( 1.215 + const char* aFilename, 1.216 + bool* aVal); 1.217 + 1.218 +#endif /* (MOZ_WIDGET_GONK || DEBUG) && XP_UNIX */ 1.219 + 1.220 +} // namespace mozilla 1.221 +#endif