2014-06-24 20:36:44 +04:00
|
|
|
// /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
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"
|
|
|
|
#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
|
|
|
|
2004-01-28 03:22:55 +03:00
|
|
|
|
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:
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2000-02-04 10:31:43 +03:00
|
|
|
NS_DECL_NSISEEKABLESTREAM
|
2012-12-17 23:25:10 +04:00
|
|
|
NS_DECL_NSIFILEMETADATA
|
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
|
|
|
protected:
|
2014-06-24 20:36:44 +04:00
|
|
|
virtual ~nsFileStreamBase();
|
|
|
|
|
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);
|
2000-08-22 11:03:33 +04:00
|
|
|
|
2011-03-05 03:36:56 +03:00
|
|
|
PRFileDesc* mFD;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flags describing our behavior. See the IDL file for possible values.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mBehaviorFlags;
|
2011-03-05 03:36:56 +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;
|
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
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data we need to do an open.
|
|
|
|
*/
|
|
|
|
OpenParams mOpenParams;
|
|
|
|
|
2017-04-20 14:39:51 +03:00
|
|
|
nsresult mErrorValue;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleans up data prepared in MaybeOpen.
|
|
|
|
*/
|
|
|
|
void CleanUpOpen();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
|
|
|
|
/**
|
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-20 19:36:42 +03:00
|
|
|
class nsFileInputStream : public nsFileStreamBase,
|
|
|
|
public nsIFileInputStream,
|
|
|
|
public nsILineInputStream,
|
|
|
|
public nsIIPCSerializableInputStream
|
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
|
2012-05-23 08:23:11 +04: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
|
|
|
}
|
|
|
|
|
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;
|
2002-04-19 02:02:09 +04: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)
|
|
|
|
{}
|
2012-08-16 08:02:32 +04:00
|
|
|
|
2014-07-09 01:23:18 +04:00
|
|
|
static nsresult
|
|
|
|
Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
|
|
|
|
|
|
|
|
protected:
|
2012-09-05 01:53:52 +04:00
|
|
|
virtual ~nsFileInputStream()
|
2002-01-26 03:38:37 +03:00
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
2000-01-13 12:11:01 +03:00
|
|
|
|
2016-03-21 05:48:59 +03:00
|
|
|
nsresult SeekInternal(int32_t aWhence, int64_t aOffset, bool aClearBuf=true);
|
|
|
|
|
2012-12-05 03:04:39 +04:00
|
|
|
nsAutoPtr<nsLineBuffer<char> > mLineBuffer;
|
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;
|
2002-04-19 02:02:09 +04:00
|
|
|
|
2012-09-05 01:53:52 +04:00
|
|
|
/**
|
|
|
|
* Cached position for Tell for automatically reopening streams.
|
|
|
|
*/
|
|
|
|
int64_t mCachedPosition;
|
|
|
|
|
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);
|
2000-01-13 12:11:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-05-23 08:23:11 +04:00
|
|
|
class nsFileOutputStream : public nsFileStreamBase,
|
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
|
|
|
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:
|
2012-05-23 08:23:11 +04:00
|
|
|
virtual ~nsFileOutputStream()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
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
|
|
|
|
2013-12-02 21:51:25 +04:00
|
|
|
nsAtomicFileOutputStream() :
|
2011-10-17 18:59:28 +04:00
|
|
|
mTargetFileExists(true),
|
2004-07-15 09:52:37 +04:00
|
|
|
mWriteResult(NS_OK) {}
|
2004-07-07 00:35:40 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual nsresult DoOpen() override;
|
2011-03-05 03:36:56 +03:00
|
|
|
|
2016-08-08 03:54:47 +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;
|
2004-07-07 00:35:40 +04:00
|
|
|
|
|
|
|
protected:
|
2014-07-09 01:23:18 +04:00
|
|
|
virtual ~nsAtomicFileOutputStream()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
2004-07-07 00:35:40 +04:00
|
|
|
nsCOMPtr<nsIFile> mTargetFile;
|
|
|
|
nsCOMPtr<nsIFile> mTempFile;
|
|
|
|
|
2011-09-29 10:19:26 +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::)
|
|
|
|
|
|
|
|
// Can't use NS_FORWARD_NSIOUTPUTSTREAM due to overlapping methods
|
|
|
|
// Close() and IsNonBlocking()
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Flush() override
|
2012-05-23 08:23:11 +04:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-07-09 01:23:18 +04:00
|
|
|
protected:
|
2012-05-23 08:23:11 +04:00
|
|
|
virtual ~nsFileStream()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2000-01-13 12:11:01 +03:00
|
|
|
#endif // nsFileStreams_h__
|