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
|
|
|
|
|
|
|
#ifndef nsFileStreams_h__
|
|
|
|
#define nsFileStreams_h__
|
|
|
|
|
2012-12-05 03:04:39 +04:00
|
|
|
#include "nsAutoPtr.h"
|
2000-01-13 12:11:01 +03:00
|
|
|
#include "nsIFileStreams.h"
|
|
|
|
#include "nsIFile.h"
|
2017-04-24 13:09:41 +03:00
|
|
|
#include "nsICloneableInputStream.h"
|
2000-01-13 12:11:01 +03:00
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIOutputStream.h"
|
2004-07-20 02:00:03 +04:00
|
|
|
#include "nsISafeOutputStream.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsISeekableStream.h"
|
2001-06-28 07:19:51 +04:00
|
|
|
#include "nsILineInputStream.h"
|
2000-01-13 12:11:01 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2012-08-16 08:02:32 +04:00
|
|
|
#include "nsIIPCSerializableInputStream.h"
|
2012-12-05 03:04:39 +04:00
|
|
|
#include "nsReadLine.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2000-04-13 13:20:50 +04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-12-17 23:25:10 +04:00
|
|
|
class nsFileStreamBase : public nsISeekableStream, public nsIFileMetadata {
|
2000-01-13 12:11:01 +03:00
|
|
|
public:
|
2018-08-14 03:42:19 +03:00
|
|
|
// Record refcount changes to ensure that streams are destroyed on
|
|
|
|
// consistent threads when recording/replaying.
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS_WITH_RECORDING(
|
|
|
|
mozilla::recordreplay::Behavior::Preserve)
|
2000-02-04 10:31:43 +03:00
|
|
|
NS_DECL_NSISEEKABLESTREAM
|
Bug 1496581 - Split nsISeekableStream in 2 classes: nsISeekableStream and nsITellableStream, f=mayhemer, r=froydnj
In the current code there are 3 main issues:
1. nsFileStream is not really thread-safe. There is nothing to protect the
internal members and we see crashes.
2. nsPipeInputStream doesn't implement ::Seek() method and that caused issues
in devtools when a nsHttpChannel sends POST data using a pipe. In order to fix
this, bug 1494176 added a check in nsHttpChannel: if the stream doesn't
implement ::Seek(), let's clone it. This was an hack around nsPipeInputStream,
and it's bad.
3. When nsHttpChannel sends POST data using a file stream, nsFileStream does
I/O on main-thread because of the issue 2. Plus, ::Seek() is called on the
main-thread causing issue 1.
Note that nsPipeInputStream implements only ::Tell(), of the nsISeekableStream
methods. It doesn't implement ::Seek() and it doesn't implement ::SetEOF().
With this patch I want to fix point 2 and point 3 (and consequentially issue 1
- but we need a separate fix for it - follow up). The patch does:
1. it splits nsISeekableStream in 2 interfaces: nsITellableStream and
nsISeekableStream.
2. nsPipeInputStream implements only nsITellableStream. Doing this, we don't
need the ::Seek() check for point 2 in nsHttpChannel: a simple QI check is
enough.
3. Because we don't call ::Seek() in nsHttpChannel, nsFileStream doesn't do I/O
on the main-thread, and we don't crash doing so.
2018-10-18 14:35:35 +03:00
|
|
|
NS_DECL_NSITELLABLESTREAM
|
2012-12-17 23:25:10 +04:00
|
|
|
NS_DECL_NSIFILEMETADATA
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-05-23 08:23:11 +04:00
|
|
|
nsFileStreamBase();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-05-23 08:23:11 +04:00
|
|
|
protected:
|
2014-06-24 20:36:44 +04:00
|
|
|
virtual ~nsFileStreamBase();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2000-08-22 11:03:33 +04:00
|
|
|
nsresult Close();
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult Available(uint64_t* _retval);
|
|
|
|
nsresult Read(char* aBuf, uint32_t aCount, uint32_t* _retval);
|
2012-05-23 08:23:11 +04:00
|
|
|
nsresult ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aCount, uint32_t* _retval);
|
2012-05-23 08:23:11 +04:00
|
|
|
nsresult IsNonBlocking(bool* _retval);
|
|
|
|
nsresult Flush();
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult Write(const char* aBuf, uint32_t aCount, uint32_t* _retval);
|
|
|
|
nsresult WriteFrom(nsIInputStream* aFromStream, uint32_t aCount,
|
|
|
|
uint32_t* _retval);
|
2012-05-23 08:23:11 +04:00
|
|
|
nsresult WriteSegments(nsReadSegmentFun aReader, void* aClosure,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aCount, uint32_t* _retval);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-03-05 03:36:56 +03:00
|
|
|
PRFileDesc* mFD;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-03-05 03:36:56 +03:00
|
|
|
/**
|
|
|
|
* Flags describing our behavior. See the IDL file for possible values.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mBehaviorFlags;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-20 14:39:51 +03:00
|
|
|
enum {
|
|
|
|
// This is the default value. It will be changed by Deserialize or Init.
|
|
|
|
eUnitialized,
|
|
|
|
// The opening has been deferred. See DEFER_OPEN.
|
|
|
|
eDeferredOpen,
|
|
|
|
// The file has been opened. mFD is not null.
|
|
|
|
eOpened,
|
|
|
|
// The file has been closed. mFD is null.
|
|
|
|
eClosed,
|
|
|
|
// Something bad happen in the Open() or in Deserialize(). The actual
|
|
|
|
// error value is stored in mErrorValue.
|
|
|
|
eError
|
|
|
|
} mState;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-03-05 03:36:56 +03:00
|
|
|
struct OpenParams {
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> localFile;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t ioFlags;
|
|
|
|
int32_t perm;
|
2011-03-05 03:36:56 +03:00
|
|
|
};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-03-05 03:36:56 +03:00
|
|
|
/**
|
|
|
|
* Data we need to do an open.
|
|
|
|
*/
|
|
|
|
OpenParams mOpenParams;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-20 14:39:51 +03:00
|
|
|
nsresult mErrorValue;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-03-05 03:36:56 +03:00
|
|
|
/**
|
|
|
|
* Prepares the data we need to open the file, and either does the open now
|
|
|
|
* by calling DoOpen(), or leaves it to be opened later by a call to
|
|
|
|
* DoPendingOpen().
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult MaybeOpen(nsIFile* aFile, int32_t aIoFlags, int32_t aPerm,
|
2011-03-05 03:36:56 +03:00
|
|
|
bool aDeferred);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-03-05 03:36:56 +03:00
|
|
|
/**
|
|
|
|
* Cleans up data prepared in MaybeOpen.
|
|
|
|
*/
|
|
|
|
void CleanUpOpen();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-03-05 03:36:56 +03:00
|
|
|
/**
|
|
|
|
* Open the file. This is called either from MaybeOpen (during Init)
|
|
|
|
* or from DoPendingOpen (if DEFER_OPEN is used when initializing this
|
|
|
|
* stream). The default behavior of DoOpen is to open the file and save the
|
|
|
|
* file descriptor.
|
|
|
|
*/
|
|
|
|
virtual nsresult DoOpen();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-03-05 03:36:56 +03:00
|
|
|
/**
|
2017-04-20 14:39:51 +03:00
|
|
|
* Based on mState, this method does the opening, return an error, or do
|
|
|
|
* nothing. If the return value is not NS_OK, please, return it back to the
|
|
|
|
* callee.
|
2011-03-05 03:36:56 +03:00
|
|
|
*/
|
|
|
|
inline nsresult DoPendingOpen();
|
2000-01-13 12:11:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-04-24 13:09:41 +03:00
|
|
|
// nsFileInputStream is cloneable only on the parent process because only there
|
|
|
|
// it can open the same file multiple times.
|
|
|
|
|
|
|
|
class nsFileInputStream : public nsFileStreamBase,
|
|
|
|
public nsIFileInputStream,
|
|
|
|
public nsILineInputStream,
|
|
|
|
public nsIIPCSerializableInputStream,
|
|
|
|
public nsICloneableInputStream {
|
2000-01-13 12:11:01 +03:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_NSIFILEINPUTSTREAM
|
2001-06-28 07:19:51 +04:00
|
|
|
NS_DECL_NSILINEINPUTSTREAM
|
2012-08-16 08:02:32 +04:00
|
|
|
NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
|
2017-04-24 13:09:41 +03:00
|
|
|
NS_DECL_NSICLONEABLEINPUTSTREAM
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Close() override;
|
|
|
|
NS_IMETHOD Tell(int64_t* aResult) override;
|
|
|
|
NS_IMETHOD Available(uint64_t* _retval) override;
|
|
|
|
NS_IMETHOD Read(char* aBuf, uint32_t aCount, uint32_t* _retval) override;
|
2012-05-23 08:23:11 +04:00
|
|
|
NS_IMETHOD ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
|
2015-03-21 19:28:04 +03:00
|
|
|
uint32_t aCount, uint32_t* _retval) override {
|
2012-05-23 08:23:11 +04:00
|
|
|
return nsFileStreamBase::ReadSegments(aWriter, aClosure, aCount, _retval);
|
|
|
|
}
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD IsNonBlocking(bool* _retval) override {
|
2012-05-23 08:23:11 +04:00
|
|
|
return nsFileStreamBase::IsNonBlocking(_retval);
|
2012-12-17 23:25:10 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-05-23 08:23:11 +04:00
|
|
|
// Overrided from nsFileStreamBase
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Seek(int32_t aWhence, int64_t aOffset) override;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-05-23 08:23:11 +04:00
|
|
|
nsFileInputStream()
|
2012-09-05 01:53:52 +04:00
|
|
|
: mLineBuffer(nullptr), mIOFlags(0), mPerm(0), mCachedPosition(0) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-07-09 01:23:18 +04:00
|
|
|
static nsresult Create(nsISupports* aOuter, REFNSIID aIID, void** aResult);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-07-09 01:23:18 +04:00
|
|
|
protected:
|
2018-01-03 16:02:43 +03:00
|
|
|
virtual ~nsFileInputStream() = default;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-01-28 12:48:35 +03:00
|
|
|
void SerializeInternal(mozilla::ipc::InputStreamParams& aParams,
|
|
|
|
FileDescriptorArray& aFileDescriptors);
|
|
|
|
|
2016-03-21 05:48:59 +03:00
|
|
|
nsresult SeekInternal(int32_t aWhence, int64_t aOffset,
|
|
|
|
bool aClearBuf = true);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-12-05 03:04:39 +04:00
|
|
|
nsAutoPtr<nsLineBuffer<char> > mLineBuffer;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2002-04-19 02:02:09 +04:00
|
|
|
/**
|
2010-10-21 22:36:13 +04:00
|
|
|
* The file being opened.
|
2002-04-19 02:02:09 +04:00
|
|
|
*/
|
|
|
|
nsCOMPtr<nsIFile> mFile;
|
|
|
|
/**
|
|
|
|
* The IO flags passed to Init() for the file open.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mIOFlags;
|
2002-04-19 02:02:09 +04:00
|
|
|
/**
|
|
|
|
* The permissions passed to Init() for the file open.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mPerm;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-09-05 01:53:52 +04:00
|
|
|
/**
|
|
|
|
* Cached position for Tell for automatically reopening streams.
|
|
|
|
*/
|
|
|
|
int64_t mCachedPosition;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2002-04-19 02:02:09 +04:00
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Internal, called to open a file. Parameters are the same as their
|
|
|
|
* Init() analogues.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult Open(nsIFile* file, int32_t ioFlags, int32_t perm);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-24 13:09:41 +03:00
|
|
|
bool IsCloneable() const;
|
2000-01-13 12:11:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-05-23 08:23:11 +04:00
|
|
|
class nsFileOutputStream : public nsFileStreamBase, public nsIFileOutputStream {
|
2000-01-13 12:11:01 +03:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_NSIFILEOUTPUTSTREAM
|
2012-05-23 08:23:11 +04:00
|
|
|
NS_FORWARD_NSIOUTPUTSTREAM(nsFileStreamBase::)
|
|
|
|
|
2014-07-09 01:23:18 +04:00
|
|
|
static nsresult Create(nsISupports* aOuter, REFNSIID aIID, void** aResult);
|
|
|
|
|
|
|
|
protected:
|
2018-01-03 16:02:43 +03:00
|
|
|
virtual ~nsFileOutputStream() = default;
|
2000-01-13 12:11:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-12-02 21:51:25 +04:00
|
|
|
/**
|
|
|
|
* A safe file output stream that overwrites the destination file only
|
|
|
|
* once writing is complete. This protects against incomplete writes
|
|
|
|
* due to the process or the thread being interrupted or crashed.
|
|
|
|
*/
|
|
|
|
class nsAtomicFileOutputStream : public nsFileOutputStream,
|
|
|
|
public nsISafeOutputStream {
|
2004-07-07 00:35:40 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2004-07-20 02:00:03 +04:00
|
|
|
NS_DECL_NSISAFEOUTPUTSTREAM
|
2004-07-07 00:35:40 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
nsAtomicFileOutputStream() : mTargetFileExists(true), mWriteResult(NS_OK) {}
|
2011-03-05 03:36:56 +03:00
|
|
|
|
2016-08-08 03:54:47 +03:00
|
|
|
virtual nsresult DoOpen() override;
|
2004-07-07 00:35:40 +04:00
|
|
|
|
2018-01-03 16:02:43 +03:00
|
|
|
NS_IMETHOD Close() override;
|
|
|
|
NS_IMETHOD Write(const char* buf, uint32_t count, uint32_t* result) override;
|
|
|
|
NS_IMETHOD Init(nsIFile* file, int32_t ioFlags, int32_t perm,
|
|
|
|
int32_t behaviorFlags) override;
|
2014-07-09 01:23:18 +04:00
|
|
|
|
2004-07-07 00:35:40 +04:00
|
|
|
protected:
|
|
|
|
virtual ~nsAtomicFileOutputStream() = default;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
nsCOMPtr<nsIFile> mTargetFile;
|
2004-07-15 09:52:37 +04:00
|
|
|
nsCOMPtr<nsIFile> mTempFile;
|
2013-12-02 21:51:25 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
bool mTargetFileExists;
|
2004-07-15 09:52:37 +04:00
|
|
|
nsresult mWriteResult; // Internally set in Write()
|
2013-12-02 21:51:25 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A safe file output stream that overwrites the destination file only
|
|
|
|
* once writing + flushing is complete. This protects against more
|
|
|
|
* classes of software/hardware errors than nsAtomicFileOutputStream,
|
|
|
|
* at the expense of being more costly to the disk, OS and battery.
|
|
|
|
*/
|
|
|
|
class nsSafeFileOutputStream : public nsAtomicFileOutputStream {
|
|
|
|
public:
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Finish() override;
|
2004-07-07 00:35:40 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-05-23 08:23:11 +04:00
|
|
|
class nsFileStream : public nsFileStreamBase,
|
|
|
|
public nsIInputStream,
|
|
|
|
public nsIOutputStream,
|
2012-12-17 23:25:10 +04:00
|
|
|
public nsIFileStream {
|
2012-05-23 08:23:11 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_NSIFILESTREAM
|
|
|
|
NS_FORWARD_NSIINPUTSTREAM(nsFileStreamBase::)
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-05-23 08:23:11 +04:00
|
|
|
// Can't use NS_FORWARD_NSIOUTPUTSTREAM due to overlapping methods
|
2017-07-06 15:00:35 +03:00
|
|
|
// Close() and IsNonBlocking()
|
2012-05-23 08:23:11 +04:00
|
|
|
NS_IMETHOD Flush() override { return nsFileStreamBase::Flush(); }
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Write(const char* aBuf, uint32_t aCount,
|
|
|
|
uint32_t* _retval) override {
|
2012-05-23 08:23:11 +04:00
|
|
|
return nsFileStreamBase::Write(aBuf, aCount, _retval);
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHOD WriteFrom(nsIInputStream* aFromStream, uint32_t aCount,
|
2015-03-21 19:28:04 +03:00
|
|
|
uint32_t* _retval) override {
|
2012-05-23 08:23:11 +04:00
|
|
|
return nsFileStreamBase::WriteFrom(aFromStream, aCount, _retval);
|
|
|
|
}
|
|
|
|
NS_IMETHOD WriteSegments(nsReadSegmentFun aReader, void* aClosure,
|
2015-03-21 19:28:04 +03:00
|
|
|
uint32_t aCount, uint32_t* _retval) override {
|
2012-05-23 08:23:11 +04:00
|
|
|
return nsFileStreamBase::WriteSegments(aReader, aClosure, aCount, _retval);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-07-09 01:23:18 +04:00
|
|
|
protected:
|
2018-01-03 16:02:43 +03:00
|
|
|
virtual ~nsFileStream() = default;
|
2012-05-23 08:23:11 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2000-01-13 12:11:01 +03:00
|
|
|
#endif // nsFileStreams_h__
|