2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-28 05:41:04 -07:00
|
|
|
#include "ipc/IPCMessageUtils.h"
|
2010-10-21 11:36:13 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#if defined(XP_UNIX) || defined(XP_BEOS)
|
|
|
|
#include <unistd.h>
|
|
|
|
#elif defined(XP_WIN)
|
|
|
|
#include <windows.h>
|
2014-09-26 16:21:57 -07:00
|
|
|
#include "nsILocalFileWin.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#else
|
|
|
|
// XXX add necessary include file for ftruncate (or equivalent)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "private/pprio.h"
|
|
|
|
|
|
|
|
#include "nsFileStreams.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsReadLine.h"
|
2010-10-21 11:36:13 -07:00
|
|
|
#include "nsIClassInfoImpl.h"
|
2012-08-23 12:33:46 -07:00
|
|
|
#include "mozilla/ipc/InputStreamUtils.h"
|
2013-09-21 20:04:57 -07:00
|
|
|
#include "nsNetCID.h"
|
2014-09-26 16:21:57 -07:00
|
|
|
#include "nsXULAppAPI.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#define NS_NO_INPUT_BUFFERING 1 // see http://bugzilla.mozilla.org/show_bug.cgi?id=41067
|
|
|
|
|
2012-08-15 21:02:32 -07:00
|
|
|
typedef mozilla::ipc::FileDescriptor::PlatformHandleType FileHandleType;
|
|
|
|
|
|
|
|
using namespace mozilla::ipc;
|
2014-03-25 11:37:13 -07:00
|
|
|
using mozilla::DebugOnly;
|
2012-08-15 21:02:32 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-05-22 21:23:11 -07:00
|
|
|
// nsFileStreamBase
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-05-22 21:23:11 -07:00
|
|
|
nsFileStreamBase::nsFileStreamBase()
|
2012-07-30 07:20:58 -07:00
|
|
|
: mFD(nullptr)
|
2011-03-04 16:36:56 -08:00
|
|
|
, mBehaviorFlags(0)
|
|
|
|
, mDeferredOpen(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-05-22 21:23:11 -07:00
|
|
|
nsFileStreamBase::~nsFileStreamBase()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-03-04 16:36:56 -08:00
|
|
|
Close();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsFileStreamBase,
|
|
|
|
nsISeekableStream,
|
|
|
|
nsIFileMetadata)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsFileStreamBase::Seek(int32_t whence, int64_t offset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-03-04 16:36:56 -08:00
|
|
|
nsresult rv = DoPendingOpen();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
if (mFD == nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t cnt = PR_Seek64(mFD, offset, (PRSeekWhence)whence);
|
|
|
|
if (cnt == int64_t(-1)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ErrorAccordingToNSPR();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsFileStreamBase::Tell(int64_t *result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-03-04 16:36:56 -08:00
|
|
|
nsresult rv = DoPendingOpen();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
if (mFD == nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t cnt = PR_Seek64(mFD, 0, PR_SEEK_CUR);
|
|
|
|
if (cnt == int64_t(-1)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ErrorAccordingToNSPR();
|
|
|
|
}
|
|
|
|
*result = cnt;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-05-22 21:23:11 -07:00
|
|
|
nsFileStreamBase::SetEOF()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-05-22 21:23:11 -07:00
|
|
|
nsresult rv = DoPendingOpen();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
if (mFD == nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
|
2014-02-10 14:57:01 -08:00
|
|
|
#if defined(XP_UNIX) || defined(XP_BEOS)
|
2007-03-22 10:30:00 -07:00
|
|
|
// Some system calls require an EOF offset.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t offset;
|
2012-05-22 21:23:11 -07:00
|
|
|
rv = Tell(&offset);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(XP_UNIX) || defined(XP_BEOS)
|
|
|
|
if (ftruncate(PR_FileDesc2NativeHandle(mFD), offset) != 0) {
|
|
|
|
NS_ERROR("ftruncate failed");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
#elif defined(XP_WIN)
|
|
|
|
if (!SetEndOfFile((HANDLE) PR_FileDesc2NativeHandle(mFD))) {
|
|
|
|
NS_ERROR("SetEndOfFile failed");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// XXX not implemented
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFileStreamBase::GetSize(int64_t* _retval)
|
|
|
|
{
|
|
|
|
nsresult rv = DoPendingOpen();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!mFD) {
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRFileInfo64 info;
|
|
|
|
if (PR_GetOpenFileInfo64(mFD, &info) == PR_FAILURE) {
|
|
|
|
return NS_BASE_STREAM_OSERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
*_retval = int64_t(info.size);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFileStreamBase::GetLastModified(int64_t* _retval)
|
|
|
|
{
|
|
|
|
nsresult rv = DoPendingOpen();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!mFD) {
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRFileInfo64 info;
|
|
|
|
if (PR_GetOpenFileInfo64(mFD, &info) == PR_FAILURE) {
|
|
|
|
return NS_BASE_STREAM_OSERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t modTime = int64_t(info.modifyTime);
|
|
|
|
if (modTime == 0) {
|
|
|
|
*_retval = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*_retval = modTime / int64_t(PR_USEC_PER_MSEC);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-03-04 16:36:56 -08:00
|
|
|
nsresult
|
2012-05-22 21:23:11 -07:00
|
|
|
nsFileStreamBase::Close()
|
|
|
|
{
|
|
|
|
CleanUpOpen();
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (mFD) {
|
|
|
|
if (PR_Close(mFD) == PR_FAILURE)
|
|
|
|
rv = NS_BASE_STREAM_OSERROR;
|
2012-07-30 07:20:58 -07:00
|
|
|
mFD = nullptr;
|
2012-05-22 21:23:11 -07:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsFileStreamBase::Available(uint64_t* aResult)
|
2012-05-22 21:23:11 -07:00
|
|
|
{
|
|
|
|
nsresult rv = DoPendingOpen();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!mFD) {
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PR_Available with files over 4GB returns an error, so we have to
|
|
|
|
// use the 64-bit version of PR_Available.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t avail = PR_Available64(mFD);
|
2012-05-22 21:23:11 -07:00
|
|
|
if (avail == -1) {
|
|
|
|
return NS_ErrorAccordingToNSPR();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If available is greater than 4GB, return 4GB
|
2012-08-22 08:56:38 -07:00
|
|
|
*aResult = (uint64_t)avail;
|
2012-05-22 21:23:11 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsFileStreamBase::Read(char* aBuf, uint32_t aCount, uint32_t* aResult)
|
2012-05-22 21:23:11 -07:00
|
|
|
{
|
|
|
|
nsresult rv = DoPendingOpen();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!mFD) {
|
|
|
|
*aResult = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t bytesRead = PR_Read(mFD, aBuf, aCount);
|
2012-05-22 21:23:11 -07:00
|
|
|
if (bytesRead == -1) {
|
|
|
|
return NS_ErrorAccordingToNSPR();
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = bytesRead;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsFileStreamBase::ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aCount, uint32_t* aResult)
|
2012-05-22 21:23:11 -07:00
|
|
|
{
|
|
|
|
// ReadSegments is not implemented because it would be inefficient when
|
|
|
|
// the writer does not consume all data. If you want to call ReadSegments,
|
|
|
|
// wrap a BufferedInputStream around the file stream. That will call
|
|
|
|
// Read().
|
|
|
|
|
|
|
|
// If this is ever implemented you might need to modify
|
|
|
|
// nsPartialFileInputStream::ReadSegments
|
|
|
|
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsFileStreamBase::IsNonBlocking(bool *aNonBlocking)
|
|
|
|
{
|
|
|
|
*aNonBlocking = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsFileStreamBase::Flush(void)
|
|
|
|
{
|
|
|
|
nsresult rv = DoPendingOpen();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
if (mFD == nullptr)
|
2012-05-22 21:23:11 -07:00
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t cnt = PR_Sync(mFD);
|
2012-05-22 21:23:11 -07:00
|
|
|
if (cnt == -1) {
|
|
|
|
return NS_ErrorAccordingToNSPR();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsFileStreamBase::Write(const char *buf, uint32_t count, uint32_t *result)
|
2012-05-22 21:23:11 -07:00
|
|
|
{
|
|
|
|
nsresult rv = DoPendingOpen();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
if (mFD == nullptr)
|
2012-05-22 21:23:11 -07:00
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t cnt = PR_Write(mFD, buf, count);
|
2012-05-22 21:23:11 -07:00
|
|
|
if (cnt == -1) {
|
|
|
|
return NS_ErrorAccordingToNSPR();
|
|
|
|
}
|
|
|
|
*result = cnt;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-03-05 01:30:25 -08:00
|
|
|
|
2012-05-22 21:23:11 -07:00
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsFileStreamBase::WriteFrom(nsIInputStream *inStr, uint32_t count, uint32_t *_retval)
|
2012-05-22 21:23:11 -07:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("WriteFrom (see source comment)");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
// File streams intentionally do not support this method.
|
|
|
|
// If you need something like this, then you should wrap
|
|
|
|
// the file stream using nsIBufferedOutputStream
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsFileStreamBase::WriteSegments(nsReadSegmentFun reader, void * closure, uint32_t count, uint32_t *_retval)
|
2012-05-22 21:23:11 -07:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
// File streams intentionally do not support this method.
|
|
|
|
// If you need something like this, then you should wrap
|
|
|
|
// the file stream using nsIBufferedOutputStream
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsFileStreamBase::MaybeOpen(nsIFile* aFile, int32_t aIoFlags,
|
|
|
|
int32_t aPerm, bool aDeferred)
|
2011-03-04 16:36:56 -08:00
|
|
|
{
|
2012-06-06 16:42:10 -07:00
|
|
|
NS_ENSURE_STATE(aFile);
|
|
|
|
|
2011-03-04 16:36:56 -08:00
|
|
|
mOpenParams.ioFlags = aIoFlags;
|
|
|
|
mOpenParams.perm = aPerm;
|
|
|
|
|
|
|
|
if (aDeferred) {
|
|
|
|
// Clone the file, as it may change between now and the deferred open
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
nsresult rv = aFile->Clone(getter_AddRefs(file));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mOpenParams.localFile = do_QueryInterface(file);
|
|
|
|
NS_ENSURE_TRUE(mOpenParams.localFile, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
mDeferredOpen = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mOpenParams.localFile = aFile;
|
|
|
|
|
|
|
|
return DoOpen();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-22 21:23:11 -07:00
|
|
|
nsFileStreamBase::CleanUpOpen()
|
2011-03-04 16:36:56 -08:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
mOpenParams.localFile = nullptr;
|
2011-03-04 16:36:56 -08:00
|
|
|
mDeferredOpen = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-05-22 21:23:11 -07:00
|
|
|
nsFileStreamBase::DoOpen()
|
2011-03-04 16:36:56 -08:00
|
|
|
{
|
2012-08-15 21:02:32 -07:00
|
|
|
NS_ASSERTION(!mFD, "Already have a file descriptor!");
|
|
|
|
NS_ASSERTION(mOpenParams.localFile, "Must have a file to open");
|
2011-03-04 16:36:56 -08:00
|
|
|
|
|
|
|
PRFileDesc* fd;
|
2014-09-26 16:21:57 -07:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
if (mBehaviorFlags & nsIFileInputStream::SHARE_DELETE) {
|
|
|
|
nsCOMPtr<nsILocalFileWin> file = do_QueryInterface(mOpenParams.localFile);
|
|
|
|
MOZ_ASSERT(file);
|
|
|
|
|
|
|
|
rv = file->OpenNSPRFileDescShareDelete(mOpenParams.ioFlags,
|
|
|
|
mOpenParams.perm,
|
|
|
|
&fd);
|
|
|
|
} else
|
|
|
|
#endif // XP_WIN
|
|
|
|
{
|
|
|
|
rv = mOpenParams.localFile->OpenNSPRFileDesc(mOpenParams.ioFlags,
|
|
|
|
mOpenParams.perm,
|
|
|
|
&fd);
|
|
|
|
}
|
|
|
|
|
2011-03-04 16:36:56 -08:00
|
|
|
CleanUpOpen();
|
2012-08-15 21:02:32 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2011-03-04 16:36:56 -08:00
|
|
|
mFD = fd;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-05-22 21:23:11 -07:00
|
|
|
nsFileStreamBase::DoPendingOpen()
|
2011-03-04 16:36:56 -08:00
|
|
|
{
|
|
|
|
if (!mDeferredOpen) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DoOpen();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsFileInputStream
|
|
|
|
|
2012-05-22 21:23:11 -07:00
|
|
|
NS_IMPL_ADDREF_INHERITED(nsFileInputStream, nsFileStreamBase)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsFileInputStream, nsFileStreamBase)
|
2010-10-21 11:36:13 -07:00
|
|
|
|
2013-09-19 12:28:26 -07:00
|
|
|
NS_IMPL_CLASSINFO(nsFileInputStream, nullptr, nsIClassInfo::THREADSAFE,
|
2010-10-21 11:36:13 -07:00
|
|
|
NS_LOCALFILEINPUTSTREAM_CID)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsFileInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIFileInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsILineInputStream)
|
2012-08-15 21:02:32 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIIPCSerializableInputStream)
|
2010-10-21 11:36:13 -07:00
|
|
|
NS_IMPL_QUERY_CLASSINFO(nsFileInputStream)
|
2012-05-22 21:23:11 -07:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsFileStreamBase)
|
2010-10-21 11:36:13 -07:00
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsFileInputStream,
|
|
|
|
nsIInputStream,
|
|
|
|
nsIFileInputStream,
|
|
|
|
nsISeekableStream,
|
|
|
|
nsILineInputStream)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-10 11:11:40 -07:00
|
|
|
nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
nsFileInputStream::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
|
|
|
|
{
|
|
|
|
NS_ENSURE_NO_AGGREGATION(aOuter);
|
|
|
|
|
|
|
|
nsFileInputStream* stream = new nsFileInputStream();
|
2012-07-30 07:20:58 -07:00
|
|
|
if (stream == nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(stream);
|
|
|
|
nsresult rv = stream->QueryInterface(aIID, aResult);
|
|
|
|
NS_RELEASE(stream);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsFileInputStream::Open(nsIFile* aFile, int32_t aIOFlags, int32_t aPerm)
|
2015-03-05 01:30:25 -08:00
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// If the previous file is open, close it
|
|
|
|
if (mFD) {
|
|
|
|
rv = Close();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the file
|
|
|
|
if (aIOFlags == -1)
|
|
|
|
aIOFlags = PR_RDONLY;
|
|
|
|
if (aPerm == -1)
|
|
|
|
aPerm = 0;
|
|
|
|
|
2012-06-05 19:08:30 -07:00
|
|
|
rv = MaybeOpen(aFile, aIOFlags, aPerm,
|
2011-03-04 16:36:56 -08:00
|
|
|
mBehaviorFlags & nsIFileInputStream::DEFER_OPEN);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (mBehaviorFlags & DELETE_ON_CLOSE) {
|
|
|
|
// POSIX compatible filesystems allow a file to be unlinked while a
|
|
|
|
// file descriptor is still referencing the file. since we've already
|
|
|
|
// opened the file descriptor, we'll try to remove the file. if that
|
|
|
|
// fails, then we'll just remember the nsIFile and remove it after we
|
|
|
|
// close the file descriptor.
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = aFile->Remove(false);
|
2010-10-21 11:36:13 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// No need to remove it later. Clear the flag.
|
|
|
|
mBehaviorFlags &= ~DELETE_ON_CLOSE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsFileInputStream::Init(nsIFile* aFile, int32_t aIOFlags, int32_t aPerm,
|
|
|
|
int32_t aBehaviorFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(!mFD, NS_ERROR_ALREADY_INITIALIZED);
|
2011-03-04 16:36:56 -08:00
|
|
|
NS_ENSURE_TRUE(!mDeferredOpen, NS_ERROR_ALREADY_INITIALIZED);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mBehaviorFlags = aBehaviorFlags;
|
|
|
|
|
2010-10-21 11:36:13 -07:00
|
|
|
mFile = aFile;
|
|
|
|
mIOFlags = aIOFlags;
|
|
|
|
mPerm = aPerm;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return Open(aFile, aIOFlags, aPerm);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFileInputStream::Close()
|
|
|
|
{
|
2012-09-04 14:53:52 -07:00
|
|
|
// Get the cache position at the time the file was close. This allows
|
|
|
|
// NS_SEEK_CUR on a closed file that has been opened with
|
|
|
|
// REOPEN_ON_REWIND.
|
|
|
|
if (mBehaviorFlags & REOPEN_ON_REWIND) {
|
|
|
|
// Get actual position. Not one modified by subclasses
|
|
|
|
nsFileStreamBase::Tell(&mCachedPosition);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// null out mLineBuffer in case Close() is called again after failing
|
2012-12-04 15:04:39 -08:00
|
|
|
mLineBuffer = nullptr;
|
2012-05-22 21:23:11 -07:00
|
|
|
nsresult rv = nsFileStreamBase::Close();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2010-10-19 19:46:54 -07:00
|
|
|
if (mFile && (mBehaviorFlags & DELETE_ON_CLOSE)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = mFile->Remove(false);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to delete file");
|
2010-10-19 19:46:54 -07:00
|
|
|
// If we don't need to save the file for reopening, free it up
|
|
|
|
if (!(mBehaviorFlags & REOPEN_ON_REWIND)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mFile = nullptr;
|
2010-10-19 19:46:54 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsFileInputStream::Read(char* aBuf, uint32_t aCount, uint32_t* _retval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-05-22 21:23:11 -07:00
|
|
|
nsresult rv = nsFileStreamBase::Read(aBuf, aCount, _retval);
|
2011-03-04 16:36:56 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Check if we're at the end of file and need to close
|
2012-05-22 21:23:11 -07:00
|
|
|
if (mBehaviorFlags & CLOSE_ON_EOF && *_retval == 0) {
|
|
|
|
Close();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsFileInputStream::ReadLine(nsACString& aLine, bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-03-04 16:36:56 -08:00
|
|
|
nsresult rv = DoPendingOpen();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mLineBuffer) {
|
2012-12-04 15:04:39 -08:00
|
|
|
mLineBuffer = new nsLineBuffer<char>;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-12-04 15:04:39 -08:00
|
|
|
return NS_ReadLine(this, mLineBuffer.get(), aLine, aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsFileInputStream::Seek(int32_t aWhence, int64_t aOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-03-04 16:36:56 -08:00
|
|
|
nsresult rv = DoPendingOpen();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-12-04 15:04:39 -08:00
|
|
|
mLineBuffer = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mFD) {
|
|
|
|
if (mBehaviorFlags & REOPEN_ON_REWIND) {
|
2012-09-04 14:53:52 -07:00
|
|
|
rv = Open(mFile, mIOFlags, mPerm);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// If the file was closed, and we do a relative seek, use the
|
|
|
|
// position we cached when we closed the file to seek to the right
|
|
|
|
// location.
|
|
|
|
if (aWhence == NS_SEEK_CUR) {
|
|
|
|
aWhence = NS_SEEK_SET;
|
|
|
|
aOffset += mCachedPosition;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-22 21:23:11 -07:00
|
|
|
return nsFileStreamBase::Seek(aWhence, aOffset);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-09-04 14:53:52 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFileInputStream::Tell(int64_t *aResult)
|
|
|
|
{
|
|
|
|
return nsFileStreamBase::Tell(aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFileInputStream::Available(uint64_t *aResult)
|
|
|
|
{
|
|
|
|
return nsFileStreamBase::Available(aResult);
|
|
|
|
}
|
|
|
|
|
2012-08-15 21:02:32 -07:00
|
|
|
void
|
2014-03-25 11:37:13 -07:00
|
|
|
nsFileInputStream::Serialize(InputStreamParams& aParams,
|
|
|
|
FileDescriptorArray& aFileDescriptors)
|
2012-08-15 21:02:32 -07:00
|
|
|
{
|
|
|
|
FileInputStreamParams params;
|
|
|
|
|
|
|
|
if (mFD) {
|
|
|
|
FileHandleType fd = FileHandleType(PR_FileDesc2NativeHandle(mFD));
|
|
|
|
NS_ASSERTION(fd, "This should never be null!");
|
|
|
|
|
2014-03-25 11:37:13 -07:00
|
|
|
DebugOnly<FileDescriptor*> dbgFD = aFileDescriptors.AppendElement(fd);
|
|
|
|
NS_ASSERTION(dbgFD->IsValid(), "Sending an invalid file descriptor!");
|
|
|
|
|
|
|
|
params.fileDescriptorIndex() = aFileDescriptors.Length() - 1;
|
2014-09-26 16:21:57 -07:00
|
|
|
|
|
|
|
Close();
|
2012-08-15 21:02:32 -07:00
|
|
|
} else {
|
|
|
|
NS_WARNING("This file has not been opened (or could not be opened). "
|
|
|
|
"Sending an invalid file descriptor to the other process!");
|
2014-09-26 16:21:57 -07:00
|
|
|
|
|
|
|
params.fileDescriptorIndex() = UINT32_MAX;
|
2012-08-15 21:02:32 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t behaviorFlags = mBehaviorFlags;
|
2012-08-15 21:02:32 -07:00
|
|
|
|
2014-09-26 16:21:57 -07:00
|
|
|
// The receiving process (or thread) is going to have an open file
|
|
|
|
// descriptor automatically so transferring this flag is meaningless.
|
2012-08-15 21:02:32 -07:00
|
|
|
behaviorFlags &= ~nsIFileInputStream::DEFER_OPEN;
|
|
|
|
|
|
|
|
params.behaviorFlags() = behaviorFlags;
|
|
|
|
params.ioFlags() = mIOFlags;
|
|
|
|
|
|
|
|
aParams = params;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-03-25 11:37:13 -07:00
|
|
|
nsFileInputStream::Deserialize(const InputStreamParams& aParams,
|
|
|
|
const FileDescriptorArray& aFileDescriptors)
|
2012-08-15 21:02:32 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!mFD, "Already have a file descriptor?!");
|
|
|
|
NS_ASSERTION(!mDeferredOpen, "Deferring open?!");
|
|
|
|
NS_ASSERTION(!mFile, "Should never have a file here!");
|
|
|
|
NS_ASSERTION(!mPerm, "This should always be 0!");
|
|
|
|
|
|
|
|
if (aParams.type() != InputStreamParams::TFileInputStreamParams) {
|
|
|
|
NS_WARNING("Received unknown parameters from the other process!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FileInputStreamParams& params = aParams.get_FileInputStreamParams();
|
|
|
|
|
2014-03-25 11:37:13 -07:00
|
|
|
uint32_t fileDescriptorIndex = params.fileDescriptorIndex();
|
|
|
|
|
|
|
|
FileDescriptor fd;
|
|
|
|
if (fileDescriptorIndex < aFileDescriptors.Length()) {
|
|
|
|
fd = aFileDescriptors[fileDescriptorIndex];
|
|
|
|
NS_WARN_IF_FALSE(fd.IsValid(), "Received an invalid file descriptor!");
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Received a bad file descriptor index!");
|
|
|
|
}
|
2012-08-15 21:02:32 -07:00
|
|
|
|
|
|
|
if (fd.IsValid()) {
|
|
|
|
PRFileDesc* fileDesc = PR_ImportFile(PROsfd(fd.PlatformHandle()));
|
|
|
|
if (!fileDesc) {
|
|
|
|
NS_WARNING("Failed to import file handle!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mFD = fileDesc;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBehaviorFlags = params.behaviorFlags();
|
2014-09-26 16:21:57 -07:00
|
|
|
|
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
// A child process shouldn't close when it reads the end because it will
|
|
|
|
// not be able to reopen the file later.
|
|
|
|
mBehaviorFlags &= ~nsIFileInputStream::CLOSE_ON_EOF;
|
|
|
|
|
|
|
|
// A child process will not be able to reopen the file so this flag is
|
|
|
|
// meaningless.
|
|
|
|
mBehaviorFlags &= ~nsIFileInputStream::REOPEN_ON_REWIND;
|
|
|
|
}
|
|
|
|
|
2012-08-15 21:02:32 -07:00
|
|
|
mIOFlags = params.ioFlags();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-13 16:25:33 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsPartialFileInputStream
|
|
|
|
|
2012-08-03 13:37:58 -07:00
|
|
|
NS_IMPL_ADDREF_INHERITED(nsPartialFileInputStream, nsFileStreamBase)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsPartialFileInputStream, nsFileStreamBase)
|
|
|
|
|
2013-09-19 12:28:26 -07:00
|
|
|
NS_IMPL_CLASSINFO(nsPartialFileInputStream, nullptr, nsIClassInfo::THREADSAFE,
|
2012-08-03 13:37:58 -07:00
|
|
|
NS_PARTIALLOCALFILEINPUTSTREAM_CID)
|
|
|
|
|
2010-10-13 16:25:33 -07:00
|
|
|
// Don't forward to nsFileInputStream as we don't want to QI to
|
|
|
|
// nsIFileInputStream
|
2012-08-03 13:37:58 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsPartialFileInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIPartialFileInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsILineInputStream)
|
2012-08-15 21:02:32 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIIPCSerializableInputStream)
|
2012-08-03 13:37:58 -07:00
|
|
|
NS_IMPL_QUERY_CLASSINFO(nsPartialFileInputStream)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsFileStreamBase)
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsPartialFileInputStream,
|
|
|
|
nsIInputStream,
|
|
|
|
nsIPartialFileInputStream,
|
|
|
|
nsISeekableStream,
|
|
|
|
nsILineInputStream)
|
2010-10-13 16:25:33 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPartialFileInputStream::Create(nsISupports *aOuter, REFNSIID aIID,
|
|
|
|
void **aResult)
|
|
|
|
{
|
|
|
|
NS_ENSURE_NO_AGGREGATION(aOuter);
|
|
|
|
|
|
|
|
nsPartialFileInputStream* stream = new nsPartialFileInputStream();
|
|
|
|
|
|
|
|
NS_ADDREF(stream);
|
|
|
|
nsresult rv = stream->QueryInterface(aIID, aResult);
|
|
|
|
NS_RELEASE(stream);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsPartialFileInputStream::Init(nsIFile* aFile, uint64_t aStart,
|
|
|
|
uint64_t aLength, int32_t aIOFlags,
|
|
|
|
int32_t aPerm, int32_t aBehaviorFlags)
|
2010-10-13 16:25:33 -07:00
|
|
|
{
|
|
|
|
mStart = aStart;
|
|
|
|
mLength = aLength;
|
|
|
|
mPosition = 0;
|
|
|
|
|
|
|
|
nsresult rv = nsFileInputStream::Init(aFile, aIOFlags, aPerm,
|
|
|
|
aBehaviorFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-03-05 01:30:25 -08:00
|
|
|
|
2010-10-13 16:25:33 -07:00
|
|
|
return nsFileInputStream::Seek(NS_SEEK_SET, mStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsPartialFileInputStream::Tell(int64_t *aResult)
|
2010-10-13 16:25:33 -07:00
|
|
|
{
|
2012-08-25 11:19:00 -07:00
|
|
|
int64_t tell = 0;
|
2010-10-13 16:25:33 -07:00
|
|
|
nsresult rv = nsFileInputStream::Tell(&tell);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*aResult = tell - mStart;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsPartialFileInputStream::Available(uint64_t* aResult)
|
2010-10-13 16:25:33 -07:00
|
|
|
{
|
2012-08-25 11:19:00 -07:00
|
|
|
uint64_t available = 0;
|
2010-10-13 16:25:33 -07:00
|
|
|
nsresult rv = nsFileInputStream::Available(&available);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*aResult = TruncateSize(available);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsPartialFileInputStream::Read(char* aBuf, uint32_t aCount, uint32_t* aResult)
|
2010-10-13 16:25:33 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t readsize = (uint32_t) TruncateSize(aCount);
|
2010-10-13 16:25:33 -07:00
|
|
|
if (readsize == 0 && mBehaviorFlags & CLOSE_ON_EOF) {
|
|
|
|
Close();
|
|
|
|
*aResult = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = nsFileInputStream::Read(aBuf, readsize, aResult);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mPosition += readsize;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsPartialFileInputStream::Seek(int32_t aWhence, int64_t aOffset)
|
2010-10-13 16:25:33 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t offset;
|
2010-10-13 16:25:33 -07:00
|
|
|
switch (aWhence) {
|
|
|
|
case NS_SEEK_SET:
|
|
|
|
offset = mStart + aOffset;
|
|
|
|
break;
|
|
|
|
case NS_SEEK_CUR:
|
|
|
|
offset = mStart + mPosition + aOffset;
|
|
|
|
break;
|
|
|
|
case NS_SEEK_END:
|
|
|
|
offset = mStart + mLength + aOffset;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
if (offset < (int64_t)mStart || offset > (int64_t)(mStart + mLength)) {
|
2010-10-13 16:25:33 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = nsFileInputStream::Seek(NS_SEEK_SET, offset);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mPosition = offset - mStart;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-15 21:02:32 -07:00
|
|
|
void
|
2014-03-25 11:37:13 -07:00
|
|
|
nsPartialFileInputStream::Serialize(InputStreamParams& aParams,
|
|
|
|
FileDescriptorArray& aFileDescriptors)
|
2012-08-15 21:02:32 -07:00
|
|
|
{
|
|
|
|
// Serialize the base class first.
|
|
|
|
InputStreamParams fileParams;
|
2014-03-25 11:37:13 -07:00
|
|
|
nsFileInputStream::Serialize(fileParams, aFileDescriptors);
|
2012-08-15 21:02:32 -07:00
|
|
|
|
|
|
|
PartialFileInputStreamParams params;
|
|
|
|
|
|
|
|
params.fileStreamParams() = fileParams.get_FileInputStreamParams();
|
|
|
|
params.begin() = mStart;
|
|
|
|
params.length() = mLength;
|
|
|
|
|
|
|
|
aParams = params;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-03-25 11:37:13 -07:00
|
|
|
nsPartialFileInputStream::Deserialize(
|
|
|
|
const InputStreamParams& aParams,
|
|
|
|
const FileDescriptorArray& aFileDescriptors)
|
2012-08-15 21:02:32 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!mFD, "Already have a file descriptor?!");
|
|
|
|
NS_ASSERTION(!mStart, "Already have a start?!");
|
|
|
|
NS_ASSERTION(!mLength, "Already have a length?!");
|
|
|
|
NS_ASSERTION(!mPosition, "Already have a position?!");
|
|
|
|
|
|
|
|
if (aParams.type() != InputStreamParams::TPartialFileInputStreamParams) {
|
2014-03-25 11:37:13 -07:00
|
|
|
NS_WARNING("Received unknown parameters from the other process!");
|
2012-08-15 21:02:32 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PartialFileInputStreamParams& params =
|
|
|
|
aParams.get_PartialFileInputStreamParams();
|
|
|
|
|
|
|
|
// Deserialize the base class first.
|
|
|
|
InputStreamParams fileParams(params.fileStreamParams());
|
2014-03-25 11:37:13 -07:00
|
|
|
if (!nsFileInputStream::Deserialize(fileParams, aFileDescriptors)) {
|
|
|
|
NS_WARNING("Base class deserialize failed!");
|
2012-08-15 21:02:32 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mFD, "Must have a file descriptor now!");
|
|
|
|
|
|
|
|
mStart = params.begin();
|
|
|
|
mLength = params.length();
|
|
|
|
mPosition = 0;
|
|
|
|
|
|
|
|
if (!mStart) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX This is so broken. Main thread IO alert.
|
|
|
|
return NS_SUCCEEDED(nsFileInputStream::Seek(NS_SEEK_SET, mStart));
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsFileOutputStream
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(nsFileOutputStream,
|
|
|
|
nsFileStreamBase,
|
|
|
|
nsIOutputStream,
|
|
|
|
nsIFileOutputStream)
|
2015-03-05 01:30:25 -08:00
|
|
|
|
2010-06-10 11:11:40 -07:00
|
|
|
nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
nsFileOutputStream::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
|
|
|
|
{
|
|
|
|
NS_ENSURE_NO_AGGREGATION(aOuter);
|
|
|
|
|
|
|
|
nsFileOutputStream* stream = new nsFileOutputStream();
|
2012-07-30 07:20:58 -07:00
|
|
|
if (stream == nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(stream);
|
|
|
|
nsresult rv = stream->QueryInterface(aIID, aResult);
|
|
|
|
NS_RELEASE(stream);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsFileOutputStream::Init(nsIFile* file, int32_t ioFlags, int32_t perm,
|
|
|
|
int32_t behaviorFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(mFD == nullptr, NS_ERROR_ALREADY_INITIALIZED);
|
2011-03-04 16:36:56 -08:00
|
|
|
NS_ENSURE_TRUE(!mDeferredOpen, NS_ERROR_ALREADY_INITIALIZED);
|
|
|
|
|
|
|
|
mBehaviorFlags = behaviorFlags;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (ioFlags == -1)
|
|
|
|
ioFlags = PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE;
|
|
|
|
if (perm <= 0)
|
|
|
|
perm = 0664;
|
|
|
|
|
2012-06-05 19:08:30 -07:00
|
|
|
return MaybeOpen(file, ioFlags, perm,
|
2011-03-04 16:36:56 -08:00
|
|
|
mBehaviorFlags & nsIFileOutputStream::DEFER_OPEN);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2013-12-02 09:51:25 -08:00
|
|
|
// nsAtomicFileOutputStream
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(nsAtomicFileOutputStream,
|
|
|
|
nsFileOutputStream,
|
|
|
|
nsISafeOutputStream,
|
|
|
|
nsIOutputStream,
|
|
|
|
nsIFileOutputStream)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-12-02 09:51:25 -08:00
|
|
|
nsAtomicFileOutputStream::Init(nsIFile* file, int32_t ioFlags, int32_t perm,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t behaviorFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2015-01-15 07:01:46 -08:00
|
|
|
// While `PR_APPEND` is not supported, `-1` is used as `ioFlags` parameter
|
|
|
|
// in some places, and `PR_APPEND | PR_TRUNCATE` does not require appending
|
|
|
|
// to existing file. So, throw an exception only if `PR_APPEND` is
|
|
|
|
// explicitly specified without `PR_TRUNCATE`.
|
|
|
|
if ((ioFlags & PR_APPEND) && !(ioFlags & PR_TRUNCATE)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2011-03-04 16:36:56 -08:00
|
|
|
return nsFileOutputStream::Init(file, ioFlags, perm, behaviorFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-12-02 09:51:25 -08:00
|
|
|
nsAtomicFileOutputStream::DoOpen()
|
2011-03-04 16:36:56 -08:00
|
|
|
{
|
|
|
|
// Make sure mOpenParams.localFile will be empty if we bail somewhere in
|
|
|
|
// this function
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> file;
|
2011-03-04 16:36:56 -08:00
|
|
|
file.swap(mOpenParams.localFile);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult rv = file->Exists(&mTargetFileExists);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ERROR("Can't tell if target file exists");
|
2011-10-17 07:59:28 -07:00
|
|
|
mTargetFileExists = true; // Safer to assume it exists - we just do more work.
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// follow symlinks, for two reasons:
|
|
|
|
// 1) if a user has deliberately set up a profile file as a symlink, we honor it
|
|
|
|
// 2) to make the MoveToNative() in Finish() an atomic operation (which may not
|
|
|
|
// be the case if moving across directories on different filesystems).
|
|
|
|
nsCOMPtr<nsIFile> tempResult;
|
|
|
|
rv = file->Clone(getter_AddRefs(tempResult));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-06-05 19:08:30 -07:00
|
|
|
tempResult->SetFollowLinks(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// XP_UNIX ignores SetFollowLinks(), so we have to normalize.
|
|
|
|
tempResult->Normalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && mTargetFileExists) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t origPerm;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(file->GetPermissions(&origPerm))) {
|
|
|
|
NS_ERROR("Can't get permissions of target file");
|
2011-03-04 16:36:56 -08:00
|
|
|
origPerm = mOpenParams.perm;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
// XXX What if |perm| is more restrictive then |origPerm|?
|
|
|
|
// This leaves the user supplied permissions as they were.
|
|
|
|
rv = tempResult->CreateUnique(nsIFile::NORMAL_FILE_TYPE, origPerm);
|
|
|
|
}
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2011-03-04 16:36:56 -08:00
|
|
|
// nsFileOutputStream::DoOpen will work on the temporary file, so we
|
|
|
|
// prepare it and place it in mOpenParams.localFile.
|
2012-06-05 19:08:30 -07:00
|
|
|
mOpenParams.localFile = tempResult;
|
2007-03-22 10:30:00 -07:00
|
|
|
mTempFile = tempResult;
|
|
|
|
mTargetFile = file;
|
2011-03-04 16:36:56 -08:00
|
|
|
rv = nsFileOutputStream::DoOpen();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-12-02 09:51:25 -08:00
|
|
|
nsAtomicFileOutputStream::Close()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv = nsFileOutputStream::Close();
|
|
|
|
|
|
|
|
// the consumer doesn't want the original file overwritten -
|
|
|
|
// so clean up by removing the temp file.
|
|
|
|
if (mTempFile) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mTempFile->Remove(false);
|
2012-07-30 07:20:58 -07:00
|
|
|
mTempFile = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-12-02 09:51:25 -08:00
|
|
|
nsAtomicFileOutputStream::Finish()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv = nsFileOutputStream::Close();
|
|
|
|
|
|
|
|
// if there is no temp file, don't try to move it over the original target.
|
|
|
|
// It would destroy the targetfile if close() is called twice.
|
|
|
|
if (!mTempFile)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
// Only overwrite if everything was ok, and the temp file could be closed.
|
|
|
|
if (NS_SUCCEEDED(mWriteResult) && NS_SUCCEEDED(rv)) {
|
|
|
|
NS_ENSURE_STATE(mTargetFile);
|
|
|
|
|
|
|
|
if (!mTargetFileExists) {
|
|
|
|
// If the target file did not exist when we were initialized, then the
|
|
|
|
// temp file we gave out was actually a reference to the target file.
|
|
|
|
// since we succeeded in writing to the temp file (and hence succeeded
|
|
|
|
// in writing to the target file), there is nothing more to do.
|
2013-12-02 09:51:25 -08:00
|
|
|
#ifdef DEBUG
|
2011-09-28 23:19:26 -07:00
|
|
|
bool equal;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(mTargetFile->Equals(mTempFile, &equal)) || !equal)
|
2015-03-05 01:31:41 -08:00
|
|
|
NS_WARNING("mTempFile not equal to mTargetFile");
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2007-05-20 11:12:26 -07:00
|
|
|
}
|
|
|
|
else {
|
2011-02-03 09:36:46 -08:00
|
|
|
nsAutoString targetFilename;
|
|
|
|
rv = mTargetFile->GetLeafName(targetFilename);
|
2007-05-20 11:12:26 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// This will replace target.
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = mTempFile->MoveTo(nullptr, targetFilename);
|
2007-05-20 11:12:26 -07:00
|
|
|
if (NS_FAILED(rv))
|
2011-10-17 07:59:28 -07:00
|
|
|
mTempFile->Remove(false);
|
2007-05-20 11:12:26 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2011-10-17 07:59:28 -07:00
|
|
|
mTempFile->Remove(false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// if writing failed, propagate the failure code to the caller.
|
|
|
|
if (NS_FAILED(mWriteResult))
|
|
|
|
rv = mWriteResult;
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
mTempFile = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-12-02 09:51:25 -08:00
|
|
|
nsAtomicFileOutputStream::Write(const char *buf, uint32_t count, uint32_t *result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv = nsFileOutputStream::Write(buf, count, result);
|
|
|
|
if (NS_SUCCEEDED(mWriteResult)) {
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
mWriteResult = rv;
|
|
|
|
else if (count != *result)
|
|
|
|
mWriteResult = NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
|
|
|
|
|
|
|
|
if (NS_FAILED(mWriteResult) && count > 0)
|
|
|
|
NS_WARNING("writing to output stream failed! data may be lost");
|
2013-12-02 09:51:25 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-12-02 09:51:25 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsSafeFileOutputStream
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSafeFileOutputStream::Finish()
|
|
|
|
{
|
|
|
|
(void) Flush();
|
|
|
|
return nsAtomicFileOutputStream::Finish();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-05-22 21:23:11 -07:00
|
|
|
// nsFileStream
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(nsFileStream,
|
|
|
|
nsFileStreamBase,
|
|
|
|
nsIInputStream,
|
|
|
|
nsIOutputStream,
|
|
|
|
nsIFileStream)
|
2012-12-17 11:25:10 -08:00
|
|
|
|
2012-05-22 21:23:11 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsFileStream::Init(nsIFile* file, int32_t ioFlags, int32_t perm,
|
|
|
|
int32_t behaviorFlags)
|
2012-05-22 21:23:11 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(mFD == nullptr, NS_ERROR_ALREADY_INITIALIZED);
|
2012-05-22 21:23:11 -07:00
|
|
|
NS_ENSURE_TRUE(!mDeferredOpen, NS_ERROR_ALREADY_INITIALIZED);
|
|
|
|
|
|
|
|
mBehaviorFlags = behaviorFlags;
|
|
|
|
|
|
|
|
if (ioFlags == -1)
|
|
|
|
ioFlags = PR_RDWR;
|
|
|
|
if (perm <= 0)
|
|
|
|
perm = 0;
|
|
|
|
|
2012-06-05 19:08:30 -07:00
|
|
|
return MaybeOpen(file, ioFlags, perm,
|
2012-05-22 21:23:11 -07:00
|
|
|
mBehaviorFlags & nsIFileStream::DEFER_OPEN);
|
|
|
|
}
|
|
|
|
|
2012-08-25 11:19:00 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|