2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; 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/. */
|
2000-01-13 12:11:01 +03:00
|
|
|
|
2012-08-28 16:41:04 +04:00
|
|
|
#include "ipc/IPCMessageUtils.h"
|
2010-10-21 22:36:13 +04:00
|
|
|
|
2002-12-19 09:27:57 +03:00
|
|
|
#if defined(XP_UNIX) || defined(XP_BEOS)
|
2001-07-25 04:40:42 +04:00
|
|
|
# include <unistd.h>
|
|
|
|
#elif defined(XP_WIN)
|
|
|
|
# include <windows.h>
|
2014-09-27 03:21:57 +04:00
|
|
|
# include "nsILocalFileWin.h"
|
2001-07-25 04:40:42 +04:00
|
|
|
#else
|
|
|
|
// XXX add necessary include file for ftruncate (or equivalent)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "private/pprio.h"
|
2018-06-12 23:22:50 +03:00
|
|
|
#include "prerror.h"
|
2001-07-25 04:40:42 +04:00
|
|
|
|
2018-06-12 23:22:50 +03:00
|
|
|
#include "IOActivityMonitor.h"
|
2000-01-13 12:11:01 +03:00
|
|
|
#include "nsFileStreams.h"
|
2000-04-13 13:20:50 +04:00
|
|
|
#include "nsIFile.h"
|
2001-06-28 07:19:51 +04:00
|
|
|
#include "nsReadLine.h"
|
2010-10-21 22:36:13 +04:00
|
|
|
#include "nsIClassInfoImpl.h"
|
2018-06-12 23:22:50 +03:00
|
|
|
#include "nsLiteralString.h"
|
|
|
|
#include "nsSocketTransport2.h" // for ErrorAccordingToNSPR()
|
2012-08-23 23:33:46 +04:00
|
|
|
#include "mozilla/ipc/InputStreamUtils.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-12-21 19:50:53 +03:00
|
|
|
#include "mozilla/FileUtils.h"
|
2013-09-22 07:04:57 +04:00
|
|
|
#include "nsNetCID.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
2000-04-13 13:20:50 +04:00
|
|
|
|
2012-08-16 08:02:32 +04:00
|
|
|
typedef mozilla::ipc::FileDescriptor::PlatformHandleType FileHandleType;
|
|
|
|
|
|
|
|
using namespace mozilla::ipc;
|
2018-06-12 23:22:50 +03:00
|
|
|
using namespace mozilla::net;
|
|
|
|
|
2014-03-25 22:37:13 +04:00
|
|
|
using mozilla::DebugOnly;
|
2016-09-29 07:20:00 +03:00
|
|
|
using mozilla::Maybe;
|
|
|
|
using mozilla::Nothing;
|
|
|
|
using mozilla::Some;
|
2012-08-16 08:02:32 +04:00
|
|
|
|
2000-01-13 12:11:01 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-05-23 08:23:11 +04:00
|
|
|
// nsFileStreamBase
|
2000-01-13 12:11:01 +03:00
|
|
|
|
2012-05-23 08:23:11 +04:00
|
|
|
nsFileStreamBase::nsFileStreamBase()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mFD(nullptr),
|
2011-03-05 03:36:56 +03:00
|
|
|
mBehaviorFlags(0),
|
2017-04-20 14:39:51 +03:00
|
|
|
mState(eUnitialized),
|
|
|
|
mErrorValue(NS_ERROR_FAILURE) {}
|
2000-01-13 12:11:01 +03:00
|
|
|
|
2012-05-23 08:23:11 +04:00
|
|
|
nsFileStreamBase::~nsFileStreamBase() {
|
2018-01-03 16:02:43 +03:00
|
|
|
// We don't want to try to rewrind the stream when shutting down.
|
|
|
|
mBehaviorFlags &= ~nsIFileInputStream::REOPEN_ON_REWIND;
|
|
|
|
|
2011-03-05 03:36:56 +03:00
|
|
|
Close();
|
2000-01-13 12:11:01 +03:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsFileStreamBase, nsISeekableStream, nsITellableStream,
|
|
|
|
nsIFileMetadata)
|
2000-01-13 12:11:01 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsFileStreamBase::Seek(int32_t whence, int64_t offset) {
|
2011-03-05 03:36:56 +03:00
|
|
|
nsresult rv = DoPendingOpen();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t cnt = PR_Seek64(mFD, offset, (PRSeekWhence)whence);
|
|
|
|
if (cnt == int64_t(-1)) {
|
2000-01-13 12:11:01 +03:00
|
|
|
return NS_ErrorAccordingToNSPR();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsFileStreamBase::Tell(int64_t* result) {
|
2011-03-05 03:36:56 +03:00
|
|
|
nsresult rv = DoPendingOpen();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t cnt = PR_Seek64(mFD, 0, PR_SEEK_CUR);
|
|
|
|
if (cnt == int64_t(-1)) {
|
2000-01-13 12:11:01 +03:00
|
|
|
return NS_ErrorAccordingToNSPR();
|
|
|
|
}
|
|
|
|
*result = cnt;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-07-25 04:40:42 +04:00
|
|
|
NS_IMETHODIMP
|
2012-05-23 08:23:11 +04:00
|
|
|
nsFileStreamBase::SetEOF() {
|
|
|
|
nsresult rv = DoPendingOpen();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_UNIX) || defined(XP_BEOS)
|
2001-07-25 04:40:42 +04:00
|
|
|
// Some system calls require an EOF offset.
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t offset;
|
2012-05-23 08:23:11 +04:00
|
|
|
rv = Tell(&offset);
|
2001-07-25 04:40:42 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
#endif
|
|
|
|
|
2002-12-19 09:27:57 +03:00
|
|
|
#if defined(XP_UNIX) || defined(XP_BEOS)
|
2001-07-25 04:40:42 +04:00
|
|
|
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 23:25:10 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFileStreamBase::GetSize(int64_t* _retval) {
|
|
|
|
nsresult rv = DoPendingOpen();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-10-25 22:18:52 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFileStreamBase::GetFileDescriptor(PRFileDesc** _retval) {
|
|
|
|
nsresult rv = DoPendingOpen();
|
2017-04-20 14:39:51 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2016-10-25 22:18:52 +03:00
|
|
|
|
|
|
|
*_retval = mFD;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-23 08:23:11 +04:00
|
|
|
nsresult nsFileStreamBase::Close() {
|
|
|
|
CleanUpOpen();
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (mFD) {
|
|
|
|
if (PR_Close(mFD) == PR_FAILURE) rv = NS_BASE_STREAM_OSERROR;
|
2012-07-30 18:20:58 +04:00
|
|
|
mFD = nullptr;
|
2017-04-20 14:39:51 +03:00
|
|
|
mState = eClosed;
|
2012-05-23 08:23:11 +04:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult nsFileStreamBase::Available(uint64_t* aResult) {
|
2012-05-23 08:23:11 +04:00
|
|
|
nsresult rv = DoPendingOpen();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// PR_Available with files over 4GB returns an error, so we have to
|
|
|
|
// use the 64-bit version of PR_Available.
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t avail = PR_Available64(mFD);
|
2012-05-23 08:23:11 +04:00
|
|
|
if (avail == -1) {
|
|
|
|
return NS_ErrorAccordingToNSPR();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If available is greater than 4GB, return 4GB
|
2012-08-22 19:56:38 +04:00
|
|
|
*aResult = (uint64_t)avail;
|
2012-05-23 08:23:11 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult nsFileStreamBase::Read(char* aBuf, uint32_t aCount,
|
|
|
|
uint32_t* aResult) {
|
2012-05-23 08:23:11 +04:00
|
|
|
nsresult rv = DoPendingOpen();
|
2017-04-20 14:39:51 +03:00
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
2012-05-23 08:23:11 +04:00
|
|
|
*aResult = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-04-20 14:39:51 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t bytesRead = PR_Read(mFD, aBuf, aCount);
|
2012-05-23 08:23:11 +04:00
|
|
|
if (bytesRead == -1) {
|
|
|
|
return NS_ErrorAccordingToNSPR();
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = bytesRead;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsFileStreamBase::ReadSegments(nsWriteSegmentFun aWriter,
|
2012-08-22 19:56:38 +04:00
|
|
|
void* aClosure, uint32_t aCount,
|
|
|
|
uint32_t* aResult) {
|
2012-05-23 08:23:11 +04: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().
|
|
|
|
|
|
|
|
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-08-22 19:56:38 +04:00
|
|
|
int32_t cnt = PR_Sync(mFD);
|
2012-05-23 08:23:11 +04:00
|
|
|
if (cnt == -1) {
|
|
|
|
return NS_ErrorAccordingToNSPR();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult nsFileStreamBase::Write(const char* buf, uint32_t count,
|
|
|
|
uint32_t* result) {
|
2012-05-23 08:23:11 +04:00
|
|
|
nsresult rv = DoPendingOpen();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t cnt = PR_Write(mFD, buf, count);
|
2012-05-23 08:23:11 +04:00
|
|
|
if (cnt == -1) {
|
|
|
|
return NS_ErrorAccordingToNSPR();
|
|
|
|
}
|
|
|
|
*result = cnt;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-03-05 12:30:25 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult nsFileStreamBase::WriteFrom(nsIInputStream* inStr, uint32_t count,
|
|
|
|
uint32_t* _retval) {
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("WriteFrom (see source comment)");
|
2012-05-23 08:23:11 +04: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
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult nsFileStreamBase::WriteSegments(nsReadSegmentFun reader, void* closure,
|
|
|
|
uint32_t count, uint32_t* _retval) {
|
2012-05-23 08:23:11 +04: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
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult nsFileStreamBase::MaybeOpen(nsIFile* aFile, int32_t aIoFlags,
|
|
|
|
int32_t aPerm, bool aDeferred) {
|
2012-06-07 03:42:10 +04:00
|
|
|
NS_ENSURE_STATE(aFile);
|
|
|
|
|
2011-03-05 03:36:56 +03: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);
|
|
|
|
|
2018-06-27 18:27:33 +03:00
|
|
|
mOpenParams.localFile = file.forget();
|
2011-03-05 03:36:56 +03:00
|
|
|
NS_ENSURE_TRUE(mOpenParams.localFile, NS_ERROR_UNEXPECTED);
|
|
|
|
|
2017-04-20 14:39:51 +03:00
|
|
|
mState = eDeferredOpen;
|
2011-03-05 03:36:56 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mOpenParams.localFile = aFile;
|
|
|
|
|
2016-03-21 05:48:59 +03:00
|
|
|
// Following call open() at main thread.
|
|
|
|
// Main thread might be blocked, while open a remote file.
|
2011-03-05 03:36:56 +03:00
|
|
|
return DoOpen();
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
void nsFileStreamBase::CleanUpOpen() { mOpenParams.localFile = nullptr; }
|
2011-03-05 03:36:56 +03:00
|
|
|
|
2012-05-23 08:23:11 +04:00
|
|
|
nsresult nsFileStreamBase::DoOpen() {
|
2017-04-20 14:39:51 +03:00
|
|
|
MOZ_ASSERT(mState == eDeferredOpen || mState == eUnitialized ||
|
|
|
|
mState == eClosed);
|
2012-08-16 08:02:32 +04:00
|
|
|
NS_ASSERTION(!mFD, "Already have a file descriptor!");
|
|
|
|
NS_ASSERTION(mOpenParams.localFile, "Must have a file to open");
|
2011-03-05 03:36:56 +03:00
|
|
|
|
|
|
|
PRFileDesc* fd;
|
2014-09-27 03:21:57 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2017-06-19 21:50:16 +03:00
|
|
|
if (mOpenParams.ioFlags & PR_CREATE_FILE) {
|
|
|
|
nsCOMPtr<nsIFile> parent;
|
|
|
|
mOpenParams.localFile->GetParent(getter_AddRefs(parent));
|
|
|
|
|
|
|
|
// Result doesn't need to be checked. If the file's parent path does not
|
|
|
|
// exist, make it. If it does exist, do nothing.
|
|
|
|
if (parent) {
|
2017-07-31 18:00:00 +03:00
|
|
|
Unused << parent->Create(nsIFile::DIRECTORY_TYPE, 0755);
|
2017-06-19 21:50:16 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2017-06-19 21:50:16 +03:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
if (mBehaviorFlags & nsIFileInputStream::SHARE_DELETE) {
|
|
|
|
nsCOMPtr<nsILocalFileWin> file = do_QueryInterface(mOpenParams.localFile);
|
|
|
|
MOZ_ASSERT(file);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
rv = file->OpenNSPRFileDescShareDelete(mOpenParams.ioFlags,
|
|
|
|
mOpenParams.perm, &fd);
|
|
|
|
} else
|
|
|
|
#endif // XP_WIN
|
|
|
|
{
|
|
|
|
rv = mOpenParams.localFile->OpenNSPRFileDesc(mOpenParams.ioFlags,
|
|
|
|
mOpenParams.perm, &fd);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-06-12 23:22:50 +03:00
|
|
|
if (rv == NS_OK && IOActivityMonitor::IsActive()) {
|
|
|
|
auto nativePath = mOpenParams.localFile->NativePath();
|
|
|
|
if (!nativePath.IsEmpty()) {
|
|
|
|
// registering the file to the activity monitor
|
|
|
|
#ifdef XP_WIN
|
|
|
|
// 16 bits unicode
|
|
|
|
IOActivityMonitor::MonitorFile(
|
|
|
|
fd, NS_ConvertUTF16toUTF8(nativePath.get()).get());
|
|
|
|
#else
|
|
|
|
// 8 bit unicode
|
|
|
|
IOActivityMonitor::MonitorFile(fd, nativePath.get());
|
|
|
|
#endif
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-06-12 23:22:50 +03:00
|
|
|
|
2011-03-05 03:36:56 +03:00
|
|
|
CleanUpOpen();
|
2018-06-12 23:22:50 +03:00
|
|
|
|
2017-04-20 14:39:51 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mState = eError;
|
|
|
|
mErrorValue = rv;
|
2012-08-16 08:02:32 +04:00
|
|
|
return rv;
|
2017-04-20 14:39:51 +03:00
|
|
|
}
|
|
|
|
|
2011-03-05 03:36:56 +03:00
|
|
|
mFD = fd;
|
2017-04-20 14:39:51 +03:00
|
|
|
mState = eOpened;
|
2011-03-05 03:36:56 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-23 08:23:11 +04:00
|
|
|
nsresult nsFileStreamBase::DoPendingOpen() {
|
2017-04-20 14:39:51 +03:00
|
|
|
switch (mState) {
|
|
|
|
case eUnitialized:
|
|
|
|
MOZ_CRASH("This should not happen.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
case eDeferredOpen:
|
|
|
|
return DoOpen();
|
|
|
|
|
|
|
|
case eOpened:
|
|
|
|
MOZ_ASSERT(mFD);
|
2017-09-21 11:39:36 +03:00
|
|
|
if (NS_WARN_IF(!mFD)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2011-03-05 03:36:56 +03:00
|
|
|
return NS_OK;
|
2017-04-20 14:39:51 +03:00
|
|
|
|
|
|
|
case eClosed:
|
|
|
|
MOZ_ASSERT(!mFD);
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
|
|
|
|
case eError:
|
|
|
|
return mErrorValue;
|
2011-03-05 03:36:56 +03:00
|
|
|
}
|
|
|
|
|
2017-04-20 14:39:51 +03:00
|
|
|
MOZ_CRASH("Invalid mState value.");
|
|
|
|
return NS_ERROR_FAILURE;
|
2011-03-05 03:36:56 +03:00
|
|
|
}
|
|
|
|
|
2000-01-13 12:11:01 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsFileInputStream
|
|
|
|
|
2012-05-23 08:23:11 +04:00
|
|
|
NS_IMPL_ADDREF_INHERITED(nsFileInputStream, nsFileStreamBase)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsFileInputStream, nsFileStreamBase)
|
2010-10-21 22:36:13 +04:00
|
|
|
|
2013-09-19 23:28:26 +04:00
|
|
|
NS_IMPL_CLASSINFO(nsFileInputStream, nullptr, nsIClassInfo::THREADSAFE,
|
2010-10-21 22:36:13 +04: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-16 08:02:32 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIIPCSerializableInputStream)
|
2010-10-21 22:36:13 +04:00
|
|
|
NS_IMPL_QUERY_CLASSINFO(nsFileInputStream)
|
2017-04-24 13:09:41 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsICloneableInputStream, IsCloneable())
|
2012-05-23 08:23:11 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsFileStreamBase)
|
2010-10-21 22:36:13 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsFileInputStream, nsIInputStream,
|
|
|
|
nsIFileInputStream, nsISeekableStream,
|
|
|
|
nsITellableStream, nsILineInputStream)
|
2000-01-13 12:11:01 +03:00
|
|
|
|
|
|
|
nsresult nsFileInputStream::Create(nsISupports* aOuter, REFNSIID aIID,
|
|
|
|
void** aResult) {
|
|
|
|
NS_ENSURE_NO_AGGREGATION(aOuter);
|
|
|
|
|
2018-06-27 18:27:33 +03:00
|
|
|
RefPtr<nsFileInputStream> stream = new nsFileInputStream();
|
|
|
|
return stream->QueryInterface(aIID, aResult);
|
2000-01-13 12:11:01 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult nsFileInputStream::Open(nsIFile* aFile, int32_t aIOFlags,
|
|
|
|
int32_t aPerm) {
|
2002-02-21 05:18:33 +03:00
|
|
|
nsresult rv = NS_OK;
|
2002-04-19 02:02:09 +04:00
|
|
|
|
|
|
|
// 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;
|
2001-06-28 07:19:51 +04:00
|
|
|
|
2017-10-05 08:41:41 +03:00
|
|
|
return MaybeOpen(aFile, aIOFlags, aPerm,
|
|
|
|
mBehaviorFlags & nsIFileInputStream::DEFER_OPEN);
|
2000-01-13 12:11:01 +03:00
|
|
|
}
|
|
|
|
|
2002-04-19 02:02:09 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsFileInputStream::Init(nsIFile* aFile, int32_t aIOFlags, int32_t aPerm,
|
|
|
|
int32_t aBehaviorFlags) {
|
2002-04-19 02:02:09 +04:00
|
|
|
NS_ENSURE_TRUE(!mFD, NS_ERROR_ALREADY_INITIALIZED);
|
2017-04-20 14:39:51 +03:00
|
|
|
NS_ENSURE_TRUE(mState == eUnitialized || mState == eClosed,
|
|
|
|
NS_ERROR_ALREADY_INITIALIZED);
|
2002-04-19 02:02:09 +04:00
|
|
|
|
|
|
|
mBehaviorFlags = aBehaviorFlags;
|
2017-04-20 14:39:51 +03:00
|
|
|
mState = eUnitialized;
|
2002-04-19 02:02:09 +04:00
|
|
|
|
2010-10-21 22:36:13 +04:00
|
|
|
mFile = aFile;
|
|
|
|
mIOFlags = aIOFlags;
|
|
|
|
mPerm = aPerm;
|
2002-04-19 02:02:09 +04:00
|
|
|
|
|
|
|
return Open(aFile, aIOFlags, aPerm);
|
|
|
|
}
|
|
|
|
|
2000-01-13 12:11:01 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFileInputStream::Close() {
|
2012-09-05 01:53:52 +04: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);
|
|
|
|
}
|
|
|
|
|
2004-04-22 19:32:54 +04:00
|
|
|
// null out mLineBuffer in case Close() is called again after failing
|
2012-12-05 03:04:39 +04:00
|
|
|
mLineBuffer = nullptr;
|
2017-10-05 08:41:41 +03:00
|
|
|
return nsFileStreamBase::Close();
|
2000-01-13 12:11:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsFileInputStream::Read(char* aBuf, uint32_t aCount, uint32_t* _retval) {
|
2012-05-23 08:23:11 +04:00
|
|
|
nsresult rv = nsFileStreamBase::Read(aBuf, aCount, _retval);
|
2015-06-30 19:33:37 +03:00
|
|
|
if (rv == NS_ERROR_FILE_NOT_FOUND) {
|
|
|
|
// Don't warn if this is a deffered file not found.
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-03-05 03:36:56 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2002-04-19 02:02:09 +04:00
|
|
|
// Check if we're at the end of file and need to close
|
2012-05-23 08:23:11 +04:00
|
|
|
if (mBehaviorFlags & CLOSE_ON_EOF && *_retval == 0) {
|
|
|
|
Close();
|
2002-04-19 02:02:09 +04:00
|
|
|
}
|
|
|
|
|
2000-01-13 12:11:01 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-06-28 07:19:51 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsFileInputStream::ReadLine(nsACString& aLine, bool* aResult) {
|
2001-07-31 23:05:34 +04:00
|
|
|
if (!mLineBuffer) {
|
2012-12-05 03:04:39 +04:00
|
|
|
mLineBuffer = new nsLineBuffer<char>;
|
2001-07-31 23:05:34 +04:00
|
|
|
}
|
2012-12-05 03:04:39 +04:00
|
|
|
return NS_ReadLine(this, mLineBuffer.get(), aLine, aResult);
|
2001-06-28 07:19:51 +04:00
|
|
|
}
|
|
|
|
|
2002-04-19 02:02:09 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsFileInputStream::Seek(int32_t aWhence, int64_t aOffset) {
|
2016-03-21 05:48:59 +03:00
|
|
|
return SeekInternal(aWhence, aOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsFileInputStream::SeekInternal(int32_t aWhence, int64_t aOffset,
|
|
|
|
bool aClearBuf) {
|
2011-03-05 03:36:56 +03:00
|
|
|
nsresult rv = DoPendingOpen();
|
2017-04-20 14:39:51 +03:00
|
|
|
if (rv != NS_OK && rv != NS_BASE_STREAM_CLOSED) {
|
|
|
|
return rv;
|
|
|
|
}
|
2011-03-05 03:36:56 +03:00
|
|
|
|
2016-03-21 05:48:59 +03:00
|
|
|
if (aClearBuf) {
|
|
|
|
mLineBuffer = nullptr;
|
|
|
|
}
|
2017-04-20 14:39:51 +03:00
|
|
|
|
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
2002-04-19 02:02:09 +04:00
|
|
|
if (mBehaviorFlags & REOPEN_ON_REWIND) {
|
2012-09-05 01:53:52 +04:00
|
|
|
rv = Open(mFile, mIOFlags, mPerm);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-09-05 01:53:52 +04:00
|
|
|
// 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;
|
2002-04-19 02:02:09 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2002-04-19 02:02:09 +04:00
|
|
|
|
2012-05-23 08:23:11 +04:00
|
|
|
return nsFileStreamBase::Seek(aWhence, aOffset);
|
2002-04-19 02:02:09 +04:00
|
|
|
}
|
|
|
|
|
2012-09-05 01:53:52 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFileInputStream::Tell(int64_t* aResult) {
|
|
|
|
return nsFileStreamBase::Tell(aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFileInputStream::Available(uint64_t* aResult) {
|
|
|
|
return nsFileStreamBase::Available(aResult);
|
|
|
|
}
|
|
|
|
|
2014-03-25 22:37:13 +04:00
|
|
|
void nsFileInputStream::Serialize(InputStreamParams& aParams,
|
2019-01-28 12:48:35 +03:00
|
|
|
FileDescriptorArray& aFileDescriptors,
|
2019-02-05 01:50:51 +03:00
|
|
|
bool aDelayedStart, uint32_t aMaxSize,
|
|
|
|
uint32_t* aSizeUsed,
|
2019-02-25 23:04:49 +03:00
|
|
|
mozilla::dom::ContentChild* aManager) {
|
2019-02-05 01:50:51 +03:00
|
|
|
MOZ_ASSERT(aSizeUsed);
|
|
|
|
*aSizeUsed = 0;
|
|
|
|
|
2019-01-28 12:48:35 +03:00
|
|
|
SerializeInternal(aParams, aFileDescriptors);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsFileInputStream::Serialize(InputStreamParams& aParams,
|
|
|
|
FileDescriptorArray& aFileDescriptors,
|
2019-02-05 01:50:51 +03:00
|
|
|
bool aDelayedStart, uint32_t aMaxSize,
|
|
|
|
uint32_t* aSizeUsed,
|
2019-01-28 12:48:35 +03:00
|
|
|
PBackgroundChild* aManager) {
|
2019-02-05 01:50:51 +03:00
|
|
|
MOZ_ASSERT(aSizeUsed);
|
|
|
|
*aSizeUsed = 0;
|
|
|
|
|
2019-01-28 12:48:35 +03:00
|
|
|
SerializeInternal(aParams, aFileDescriptors);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsFileInputStream::Serialize(InputStreamParams& aParams,
|
|
|
|
FileDescriptorArray& aFileDescriptors,
|
2019-02-05 01:50:51 +03:00
|
|
|
bool aDelayedStart, uint32_t aMaxSize,
|
|
|
|
uint32_t* aSizeUsed,
|
2019-02-25 23:04:47 +03:00
|
|
|
mozilla::dom::ContentParent* aManager) {
|
2019-02-05 01:50:51 +03:00
|
|
|
MOZ_ASSERT(aSizeUsed);
|
|
|
|
*aSizeUsed = 0;
|
|
|
|
|
2019-01-28 12:48:35 +03:00
|
|
|
SerializeInternal(aParams, aFileDescriptors);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsFileInputStream::Serialize(InputStreamParams& aParams,
|
|
|
|
FileDescriptorArray& aFileDescriptors,
|
2019-02-05 01:50:51 +03:00
|
|
|
bool aDelayedStart, uint32_t aMaxSize,
|
|
|
|
uint32_t* aSizeUsed,
|
2019-01-28 12:48:35 +03:00
|
|
|
PBackgroundParent* aManager) {
|
2019-02-05 01:50:51 +03:00
|
|
|
MOZ_ASSERT(aSizeUsed);
|
|
|
|
*aSizeUsed = 0;
|
|
|
|
|
2019-01-28 12:48:35 +03:00
|
|
|
SerializeInternal(aParams, aFileDescriptors);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsFileInputStream::SerializeInternal(
|
|
|
|
InputStreamParams& aParams, FileDescriptorArray& aFileDescriptors) {
|
2012-08-16 08:02:32 +04:00
|
|
|
FileInputStreamParams params;
|
|
|
|
|
2017-04-20 14:39:51 +03:00
|
|
|
if (NS_SUCCEEDED(DoPendingOpen())) {
|
|
|
|
MOZ_ASSERT(mFD);
|
2012-08-16 08:02:32 +04:00
|
|
|
FileHandleType fd = FileHandleType(PR_FileDesc2NativeHandle(mFD));
|
|
|
|
NS_ASSERTION(fd, "This should never be null!");
|
|
|
|
|
2014-03-25 22:37:13 +04:00
|
|
|
DebugOnly<FileDescriptor*> dbgFD = aFileDescriptors.AppendElement(fd);
|
|
|
|
NS_ASSERTION(dbgFD->IsValid(), "Sending an invalid file descriptor!");
|
|
|
|
|
|
|
|
params.fileDescriptorIndex() = aFileDescriptors.Length() - 1;
|
2014-09-27 03:21:57 +04:00
|
|
|
|
|
|
|
Close();
|
2012-08-16 08:02:32 +04: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-27 03:21:57 +04:00
|
|
|
|
|
|
|
params.fileDescriptorIndex() = UINT32_MAX;
|
2012-08-16 08:02:32 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t behaviorFlags = mBehaviorFlags;
|
2012-08-16 08:02:32 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
// The receiving process (or thread) is going to have an open file
|
|
|
|
// descriptor automatically so transferring this flag is meaningless.
|
2012-08-16 08:02:32 +04:00
|
|
|
behaviorFlags &= ~nsIFileInputStream::DEFER_OPEN;
|
|
|
|
|
|
|
|
params.behaviorFlags() = behaviorFlags;
|
|
|
|
params.ioFlags() = mIOFlags;
|
|
|
|
|
|
|
|
aParams = params;
|
|
|
|
}
|
|
|
|
|
2014-03-25 22:37:13 +04:00
|
|
|
bool nsFileInputStream::Deserialize(
|
|
|
|
const InputStreamParams& aParams,
|
|
|
|
const FileDescriptorArray& aFileDescriptors) {
|
2012-08-16 08:02:32 +04:00
|
|
|
NS_ASSERTION(!mFD, "Already have a file descriptor?!");
|
2017-04-20 14:39:51 +03:00
|
|
|
NS_ASSERTION(mState == nsFileStreamBase::eUnitialized, "Deferring open?!");
|
2012-08-16 08:02:32 +04:00
|
|
|
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 22:37:13 +04:00
|
|
|
uint32_t fileDescriptorIndex = params.fileDescriptorIndex();
|
|
|
|
|
|
|
|
FileDescriptor fd;
|
|
|
|
if (fileDescriptorIndex < aFileDescriptors.Length()) {
|
|
|
|
fd = aFileDescriptors[fileDescriptorIndex];
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(fd.IsValid(), "Received an invalid file descriptor!");
|
2014-03-25 22:37:13 +04:00
|
|
|
} else {
|
|
|
|
NS_WARNING("Received a bad file descriptor index!");
|
|
|
|
}
|
2012-08-16 08:02:32 +04:00
|
|
|
|
|
|
|
if (fd.IsValid()) {
|
2016-05-27 11:12:51 +03:00
|
|
|
auto rawFD = fd.ClonePlatformHandle();
|
|
|
|
PRFileDesc* fileDesc = PR_ImportFile(PROsfd(rawFD.release()));
|
2012-08-16 08:02:32 +04:00
|
|
|
if (!fileDesc) {
|
|
|
|
NS_WARNING("Failed to import file handle!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mFD = fileDesc;
|
2017-04-20 14:39:51 +03:00
|
|
|
mState = eOpened;
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2017-04-20 14:39:51 +03:00
|
|
|
mState = eError;
|
|
|
|
mErrorValue = NS_ERROR_FILE_NOT_FOUND;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2012-08-16 08:02:32 +04:00
|
|
|
|
|
|
|
mBehaviorFlags = params.behaviorFlags();
|
2014-09-27 03:21:57 +04:00
|
|
|
|
2015-07-04 04:29:00 +03:00
|
|
|
if (!XRE_IsParentProcess()) {
|
2014-09-27 03:21:57 +04:00
|
|
|
// 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-16 08:02:32 +04:00
|
|
|
mIOFlags = params.ioFlags();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-24 13:09:41 +03:00
|
|
|
bool nsFileInputStream::IsCloneable() const {
|
2017-04-28 13:16:13 +03:00
|
|
|
// This inputStream is cloneable only if has been created using Init() and
|
|
|
|
// it owns a nsIFile. This is not true when it is deserialized from IPC.
|
|
|
|
return XRE_IsParentProcess() && mFile;
|
2017-04-24 13:09:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFileInputStream::GetCloneable(bool* aCloneable) {
|
|
|
|
*aCloneable = IsCloneable();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFileInputStream::Clone(nsIInputStream** aResult) {
|
|
|
|
MOZ_ASSERT(IsCloneable());
|
|
|
|
return NS_NewLocalFileInputStream(aResult, mFile, mIOFlags, mPerm,
|
|
|
|
mBehaviorFlags);
|
|
|
|
}
|
|
|
|
|
2000-01-13 12:11:01 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsFileOutputStream
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(nsFileOutputStream, nsFileStreamBase,
|
|
|
|
nsIOutputStream, nsIFileOutputStream)
|
2015-03-05 12:30:25 +03:00
|
|
|
|
2000-01-13 12:11:01 +03:00
|
|
|
nsresult nsFileOutputStream::Create(nsISupports* aOuter, REFNSIID aIID,
|
|
|
|
void** aResult) {
|
|
|
|
NS_ENSURE_NO_AGGREGATION(aOuter);
|
|
|
|
|
2018-06-27 18:27:33 +03:00
|
|
|
RefPtr<nsFileOutputStream> stream = new nsFileOutputStream();
|
|
|
|
return stream->QueryInterface(aIID, aResult);
|
2000-01-13 12:11:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsFileOutputStream::Init(nsIFile* file, int32_t ioFlags, int32_t perm,
|
|
|
|
int32_t behaviorFlags) {
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(mFD == nullptr, NS_ERROR_ALREADY_INITIALIZED);
|
2017-04-20 14:39:51 +03:00
|
|
|
NS_ENSURE_TRUE(mState == eUnitialized || mState == eClosed,
|
|
|
|
NS_ERROR_ALREADY_INITIALIZED);
|
2011-03-05 03:36:56 +03:00
|
|
|
|
|
|
|
mBehaviorFlags = behaviorFlags;
|
2017-04-20 14:39:51 +03:00
|
|
|
mState = eUnitialized;
|
2002-03-27 02:33:19 +03:00
|
|
|
|
Bit checkin for bug 68045, r/sr=waterson&shaver, second attempt. It all works
for me on optimized and debug gcc2.96, rh7.1.
- Better failure codes from nsXULPrototypeScript::Deserialize.
- Call nsXULDocument::AbortFastLoads after nsXULPrototypeScript::Serialize
failure, instead of just nulling the FastLoad service's output stream.
- Expose nsXULDocument::AbortFastLoads via nsIXULPrototypeCache, for use from
nsChromeProtocolHandler.cpp. AbortFastLoads flushes the XUL cache now, for
good measure.
- The needless "Current" adjective in nsIFastLoadService attribute and method
names is no more.
- Add a do_GetFastLoadService() helper, to use CID instead of contractid, and
to let the compiler consolidate the static inline CID.
- Add "nglayout.debug.checksum_xul_fastload_file" pref so people can do without
the checksum verification step when reading a FastLoad file.
- Verify the FastLoad file checksum, by default. Also, cache it in the FastLoad
service so we don't recompute it when re-opening the FastLoad file as mailnews
and other top-levels start up. Fill the checksum cache in EndFastLoad, when
the last pseudo-concurrent top-level finishes loading.
My hope to compute the checksum while writing the FastLoad file ran afoul of
misordered writes. The old code to checksum the in-memory nsFastLoadHeader
also was broken on little endian platforms. Now all checksumming is done via
a separate read pass over the complete file, save for the header's checksum
field, which is summed as if it contained zero.
- Track and check FastLoad file dependencies. This required groveling with a
bunch of Necko interfaces in nsChromeProtocolHandler::NewChannel -- read it
and weep. Dependency checking, as well as checksum access and computation,
use better-factored nsIFastLoad{File,Read,Write}Control interfaces.
- nsBufferedStream::Seek wasn't flushing the buffer when seeking backward
within the buffer, but it must, because mCursor bounds the amount to write
if the buffer contains the end of file.
- Add an unbufferedStream readonly attribute to nsIStreamBufferAccess, so we
don't have to screw around with the bufferying layer when checksumming. Also
implement nsIStreamBufferAccess in nsBufferedOutputStream.
- nsISeekableOutputStream was bogus, based on a bad state I had put the
nsBufferedOutputStream code in on its way from being completely broken when
you seek backwards outside of the buffer. Removing this interface required
using nsIFastLoadFileIO in nsFastLoadFileWriter, and it also required careful
ordering of Close calls (the Reader must close after the Writer or Updater,
so that the Reader's underlying, unbuffered input stream can be read by
nsFastLoadFileWriter::Close to compute the checksum.
- Miscellaneous tab/indentation, comment typo, bracing, if( => if ( style,
nsnull vs. 0, useless variable elimination, tortured control flow,
AutoString instead of String, and gratuitous ; after nsISupportsUtils.h
macro call cleanups.
2001-08-22 00:51:34 +04:00
|
|
|
if (ioFlags == -1) ioFlags = PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE;
|
2000-04-14 13:32:24 +04:00
|
|
|
if (perm <= 0) perm = 0664;
|
2002-03-27 02:33:19 +03:00
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
return MaybeOpen(file, ioFlags, perm,
|
2011-03-05 03:36:56 +03:00
|
|
|
mBehaviorFlags & nsIFileOutputStream::DEFER_OPEN);
|
2000-01-13 12:11:01 +03:00
|
|
|
}
|
|
|
|
|
2015-12-21 19:50:53 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFileOutputStream::Preallocate(int64_t aLength) {
|
|
|
|
if (!mFD) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mozilla::fallocate(mFD, aLength)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-01-13 12:11:01 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2013-12-02 21:51:25 +04:00
|
|
|
// nsAtomicFileOutputStream
|
2004-07-07 00:35:40 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(nsAtomicFileOutputStream, nsFileOutputStream,
|
|
|
|
nsISafeOutputStream, nsIOutputStream,
|
|
|
|
nsIFileOutputStream)
|
2004-07-07 00:35:40 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-12-02 21:51:25 +04:00
|
|
|
nsAtomicFileOutputStream::Init(nsIFile* file, int32_t ioFlags, int32_t perm,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t behaviorFlags) {
|
2015-01-15 18:01:46 +03: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-05 03:36:56 +03:00
|
|
|
return nsFileOutputStream::Init(file, ioFlags, perm, behaviorFlags);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-12-02 21:51:25 +04:00
|
|
|
nsresult nsAtomicFileOutputStream::DoOpen() {
|
2011-03-05 03:36:56 +03:00
|
|
|
// Make sure mOpenParams.localFile will be empty if we bail somewhere in
|
|
|
|
// this function
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> file;
|
2011-03-05 03:36:56 +03:00
|
|
|
file.swap(mOpenParams.localFile);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-06-18 22:17:56 +03:00
|
|
|
if (!file) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2004-07-07 00:35:40 +04:00
|
|
|
nsresult rv = file->Exists(&mTargetFileExists);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ERROR("Can't tell if target file exists");
|
2011-10-17 18:59:28 +04:00
|
|
|
mTargetFileExists =
|
|
|
|
true; // Safer to assume it exists - we just do more work.
|
2004-07-07 00:35:40 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-07-30 09:00:32 +04:00
|
|
|
// follow symlinks, for two reasons:
|
2018-11-29 13:30:46 +03:00
|
|
|
// 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).
|
2004-07-23 11:50:57 +04:00
|
|
|
nsCOMPtr<nsIFile> tempResult;
|
|
|
|
rv = file->Clone(getter_AddRefs(tempResult));
|
2004-07-30 09:00:32 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-06-06 06:08:30 +04:00
|
|
|
tempResult->SetFollowLinks(true);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-07-30 09:00:32 +04:00
|
|
|
// XP_UNIX ignores SetFollowLinks(), so we have to normalize.
|
2016-02-03 21:47:20 +03:00
|
|
|
if (mTargetFileExists) {
|
|
|
|
tempResult->Normalize();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-02-03 21:47:20 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-07-07 00:35:40 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && mTargetFileExists) {
|
2018-01-31 02:01:26 +03:00
|
|
|
// Abort if |file| is not writable; it won't work as an output stream.
|
|
|
|
bool isWritable;
|
|
|
|
if (NS_SUCCEEDED(file->IsWritable(&isWritable)) && !isWritable) {
|
|
|
|
return NS_ERROR_FILE_ACCESS_DENIED;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t origPerm;
|
2004-07-07 00:35:40 +04:00
|
|
|
if (NS_FAILED(file->GetPermissions(&origPerm))) {
|
|
|
|
NS_ERROR("Can't get permissions of target file");
|
2011-03-05 03:36:56 +03:00
|
|
|
origPerm = mOpenParams.perm;
|
2004-07-07 00:35:40 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-07-07 00:35:40 +04:00
|
|
|
// XXX What if |perm| is more restrictive then |origPerm|?
|
2004-07-20 02:00:03 +04:00
|
|
|
// This leaves the user supplied permissions as they were.
|
2004-07-07 00:35:40 +04:00
|
|
|
rv = tempResult->CreateUnique(nsIFile::NORMAL_FILE_TYPE, origPerm);
|
|
|
|
}
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2011-03-05 03:36:56 +03:00
|
|
|
// nsFileOutputStream::DoOpen will work on the temporary file, so we
|
|
|
|
// prepare it and place it in mOpenParams.localFile.
|
2012-06-06 06:08:30 +04:00
|
|
|
mOpenParams.localFile = tempResult;
|
2004-07-07 00:35:40 +04:00
|
|
|
mTempFile = tempResult;
|
|
|
|
mTargetFile = file;
|
2011-03-05 03:36:56 +03:00
|
|
|
rv = nsFileOutputStream::DoOpen();
|
2004-07-07 00:35:40 +04:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-12-02 21:51:25 +04:00
|
|
|
nsAtomicFileOutputStream::Close() {
|
2004-07-15 09:52:37 +04: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 18:59:28 +04:00
|
|
|
mTempFile->Remove(false);
|
2012-07-30 18:20:58 +04:00
|
|
|
mTempFile = nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2004-07-15 09:52:37 +04:00
|
|
|
return rv;
|
2004-07-07 00:35:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-12-02 21:51:25 +04:00
|
|
|
nsAtomicFileOutputStream::Finish() {
|
2004-07-07 00:35:40 +04:00
|
|
|
nsresult rv = nsFileOutputStream::Close();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-07-07 00:35:40 +04:00
|
|
|
// 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;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-07-07 00:35:40 +04:00
|
|
|
// Only overwrite if everything was ok, and the temp file could be closed.
|
2004-07-15 09:52:37 +04:00
|
|
|
if (NS_SUCCEEDED(mWriteResult) && NS_SUCCEEDED(rv)) {
|
2004-07-07 00:35:40 +04:00
|
|
|
NS_ENSURE_STATE(mTargetFile);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-07-07 00:35:40 +04:00
|
|
|
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 21:51:25 +04:00
|
|
|
#ifdef DEBUG
|
2011-09-29 10:19:26 +04:00
|
|
|
bool equal;
|
2004-07-07 00:35:40 +04:00
|
|
|
if (NS_FAILED(mTargetFile->Equals(mTempFile, &equal)) || !equal)
|
2015-03-05 12:31:41 +03:00
|
|
|
NS_WARNING("mTempFile not equal to mTargetFile");
|
2004-07-07 00:35:40 +04:00
|
|
|
#endif
|
2007-05-20 22:12:26 +04:00
|
|
|
} else {
|
2011-02-03 20:36:46 +03:00
|
|
|
nsAutoString targetFilename;
|
|
|
|
rv = mTargetFile->GetLeafName(targetFilename);
|
2007-05-20 22:12:26 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// This will replace target.
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = mTempFile->MoveTo(nullptr, targetFilename);
|
2011-10-17 18:59:28 +04:00
|
|
|
if (NS_FAILED(rv)) mTempFile->Remove(false);
|
2004-07-20 05:35:13 +04:00
|
|
|
}
|
2004-07-07 00:35:40 +04:00
|
|
|
}
|
|
|
|
} else {
|
2011-10-17 18:59:28 +04:00
|
|
|
mTempFile->Remove(false);
|
2004-07-15 09:52:37 +04:00
|
|
|
|
|
|
|
// if writing failed, propagate the failure code to the caller.
|
|
|
|
if (NS_FAILED(mWriteResult)) rv = mWriteResult;
|
2004-07-07 00:35:40 +04:00
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
mTempFile = nullptr;
|
2004-07-07 00:35:40 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-12-02 21:51:25 +04:00
|
|
|
nsAtomicFileOutputStream::Write(const char* buf, uint32_t count,
|
|
|
|
uint32_t* result) {
|
2004-07-07 00:35:40 +04:00
|
|
|
nsresult rv = nsFileOutputStream::Write(buf, count, result);
|
2004-07-15 09:52:37 +04:00
|
|
|
if (NS_SUCCEEDED(mWriteResult)) {
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
mWriteResult = rv;
|
|
|
|
else if (count != *result)
|
|
|
|
mWriteResult = NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-07-23 11:47:10 +04:00
|
|
|
if (NS_FAILED(mWriteResult) && count > 0)
|
2004-07-15 09:52:37 +04:00
|
|
|
NS_WARNING("writing to output stream failed! data may be lost");
|
2013-12-02 21:51:25 +04:00
|
|
|
}
|
2004-07-07 00:35:40 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-12-02 21:51:25 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsSafeFileOutputStream
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSafeFileOutputStream::Finish() {
|
|
|
|
(void)Flush();
|
|
|
|
return nsAtomicFileOutputStream::Finish();
|
|
|
|
}
|
|
|
|
|
2004-07-07 00:35:40 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-05-23 08:23:11 +04:00
|
|
|
// nsFileStream
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(nsFileStream, nsFileStreamBase, nsIInputStream,
|
|
|
|
nsIOutputStream, nsIFileStream)
|
2012-12-17 23:25:10 +04:00
|
|
|
|
2012-05-23 08:23:11 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsFileStream::Init(nsIFile* file, int32_t ioFlags, int32_t perm,
|
|
|
|
int32_t behaviorFlags) {
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(mFD == nullptr, NS_ERROR_ALREADY_INITIALIZED);
|
2017-04-20 14:39:51 +03:00
|
|
|
NS_ENSURE_TRUE(mState == eUnitialized || mState == eClosed,
|
|
|
|
NS_ERROR_ALREADY_INITIALIZED);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-05-23 08:23:11 +04:00
|
|
|
mBehaviorFlags = behaviorFlags;
|
2017-04-20 14:39:51 +03:00
|
|
|
mState = eUnitialized;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-05-23 08:23:11 +04:00
|
|
|
if (ioFlags == -1) ioFlags = PR_RDWR;
|
|
|
|
if (perm <= 0) perm = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
return MaybeOpen(file, ioFlags, perm,
|
2012-05-23 08:23:11 +04:00
|
|
|
mBehaviorFlags & nsIFileStream::DEFER_OPEN);
|
|
|
|
}
|
|
|
|
|
2012-08-25 22:19:00 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|