2010-08-19 01:30:09 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-08-19 01:30:09 +04:00
|
|
|
|
|
|
|
#ifndef mozilla_FileUtils_h
|
|
|
|
#define mozilla_FileUtils_h
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
#include "nscore.h" // nullptr
|
2012-04-17 18:11:53 +04:00
|
|
|
|
2011-02-02 21:51:22 +03:00
|
|
|
#if defined(XP_UNIX) || defined(XP_OS2)
|
2010-11-09 05:48:59 +03:00
|
|
|
# include <unistd.h>
|
|
|
|
#elif defined(XP_WIN)
|
|
|
|
# include <io.h>
|
|
|
|
#endif
|
|
|
|
#include "prio.h"
|
|
|
|
|
2012-04-12 14:21:24 +04:00
|
|
|
#include "mozilla/Scoped.h"
|
2013-01-04 06:46:47 +04:00
|
|
|
#include <errno.h>
|
2012-04-12 14:21:24 +04:00
|
|
|
|
2010-08-19 01:30:09 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2010-09-17 00:21:12 +04:00
|
|
|
/**
|
|
|
|
* AutoFDClose is a RAII wrapper for PRFileDesc.
|
2012-04-12 14:21:24 +04:00
|
|
|
*
|
|
|
|
* Instances |PR_Close| their fds when they go out of scope.
|
2010-09-17 00:21:12 +04:00
|
|
|
**/
|
2012-04-12 14:21:24 +04:00
|
|
|
struct ScopedClosePRFDTraits
|
2010-08-19 01:30:09 +04:00
|
|
|
{
|
2012-04-12 14:21:24 +04:00
|
|
|
typedef PRFileDesc* type;
|
|
|
|
static type empty() { return NULL; }
|
|
|
|
static void release(type fd) {
|
|
|
|
if (fd != NULL) {
|
|
|
|
PR_Close(fd);
|
|
|
|
}
|
2010-08-19 01:30:09 +04:00
|
|
|
}
|
|
|
|
};
|
2012-04-12 14:21:24 +04:00
|
|
|
typedef Scoped<ScopedClosePRFDTraits> AutoFDClose;
|
2010-08-19 01:30:09 +04:00
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
/**
|
2012-04-12 14:21:24 +04:00
|
|
|
* ScopedCloseFD is a RAII wrapper for POSIX file descriptors
|
|
|
|
*
|
|
|
|
* Instances |close()| their fds when they go out of scope.
|
2010-11-09 05:48:59 +03:00
|
|
|
*/
|
2012-04-12 14:21:24 +04:00
|
|
|
struct ScopedCloseFDTraits
|
2010-11-09 05:48:59 +03:00
|
|
|
{
|
2012-04-12 14:21:24 +04:00
|
|
|
typedef int type;
|
|
|
|
static type empty() { return -1; }
|
|
|
|
static void release(type fd) {
|
|
|
|
if (fd != -1) {
|
2013-01-04 06:46:47 +04:00
|
|
|
while ((close(fd) == -1) && (errno == EINTR)) {
|
|
|
|
;
|
|
|
|
}
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-04-12 14:21:24 +04:00
|
|
|
typedef Scoped<ScopedCloseFDTraits> ScopedClose;
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2010-09-17 00:21:12 +04:00
|
|
|
/**
|
|
|
|
* Fallocate efficiently and continuously allocates files via fallocate-type APIs.
|
|
|
|
* This is useful for avoiding fragmentation.
|
|
|
|
* On sucess the file be padded with zeros to grow to aLength.
|
|
|
|
*
|
|
|
|
* @param aFD file descriptor.
|
|
|
|
* @param aLength length of file to grow to.
|
|
|
|
* @return true on success.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_COM_GLUE bool fallocate(PRFileDesc *aFD, int64_t aLength);
|
2010-09-17 00:21:12 +04:00
|
|
|
|
2010-08-19 01:30:09 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
#endif
|