nsIFileStream.cpp revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Pierre Phaneuf <pp@ludusdesign.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsIFileStream.h"
#include "nsFileSpec.h"
#include "nsCOMPtr.h"
#include "prerror.h"
#include "nsSegmentedBuffer.h"
#include "nsInt64.h"
#ifdef XP_MAC
#include "pprio.h" // To get PR_ImportFile
#else
#include "prio.h"
#endif
#ifdef XP_MAC
#include <Errors.h>
#include <iostream>
#endif
//========================================================================================
class FileImpl
: public nsIRandomAccessStore
, public nsIFileSpecOutputStream
, public nsIFileSpecInputStream
, public nsIOpenFile
//========================================================================================
{
public:
// nsISupports interface
// nsIOpenFile interface
NS_IMETHOD Close();
// nsIInputStream interface
NS_IMETHOD ReadSegments(nsWriteSegmentFun writer, void * closure, PRUint32 count, PRUint32 *_retval);
// nsIOutputStream interface
NS_IMETHOD Flush();
NS_IMETHOD WriteSegments(nsReadSegmentFun reader, void * closure, PRUint32 count, PRUint32 *_retval);
// nsIRandomAccessStore interface
private:
~FileImpl();
protected:
enum {
kOuputBufferSegmentSize = 4096,
kOuputBufferMaxSize = 4096
};
int mNSPRMode;
char* mWriteCursor;
char* mWriteLimit;
}; // class FileImpl
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
, mNSPRMode(0)
, mLength(-1)
{
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
, mNSPRMode(-1)
, mLength(-1)
{
{
#if DEBUG
#endif
}
else
{
}
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
{
}
//----------------------------------------------------------------------------------------
const nsFileSpec& inFile,
int nsprMode,
//----------------------------------------------------------------------------------------
{
if (mFileDesc)
return NS_OK;
else
return NS_FILE_RESULT(PR_ILLEGAL_ACCESS_ERROR);
const int nspr_modes[]={
// "wb",
// "ab",
// "wb",
// "rb",
// "r+b",
// "w+b",
0 };
const int* currentLegalMode = nspr_modes;
if (!*currentLegalMode)
return NS_FILE_RESULT(PR_ILLEGAL_ACCESS_ERROR);
#ifdef XP_MAC
// Use the file spec to open the file, because one path can be common to
// several files on the Macintosh (you can have several volumes with the
// same name, see).
mFileDesc = 0;
#if DEBUG
#else
#endif
// Resolve the alias to the original file.
if (nsprMode & PR_CREATE_FILE) {
// Don't propagate any errors in doing this. If any error, just use FSpCreate.
if (NS_SUCCEEDED(res)) {
if (NS_SUCCEEDED(res)) {
if (res == NS_ERROR_FILE_ALREADY_EXISTS)
}
}
}
return NS_FILE_RESULT(err);
perm = fsRdWrPerm;
else
short refnum;
return NS_FILE_RESULT(err);
return NS_FILE_RESULT(PR_GetError());
#else
// Platforms other than Macintosh...
// Another bug in NSPR: Mac PR_Open assumes a unix style path, but Win PR_Open assumes
// a windows path.
return NS_FILE_RESULT(PR_GetError());
#endif
return NS_OK;
} // FileImpl::Open
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
{
if (!aLength)
return NS_ERROR_NULL_POINTER;
if (mLength < 0)
return NS_ERROR_UNEXPECTED;
return NS_OK;
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
{
return NS_OK;
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
{
return NS_FILE_RESULT(PR_BAD_DESCRIPTOR_ERROR);
// To avoid corruption, we flush during a seek. see bug number 18949
switch (whence)
{
case NS_SEEK_SET: ; break;
}
if (newPosition < zero)
{
newPosition = 0;
}
{
}
return NS_OK;
} // FileImpl::Seek
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
{
if (!aBuf)
return NS_ERROR_NULL_POINTER;
if (!aReadCount)
return NS_ERROR_NULL_POINTER;
if (!mFileDesc)
return NS_FILE_RESULT(PR_BAD_DESCRIPTOR_ERROR);
if (mFailed)
return NS_ERROR_FAILURE;
if (bytesRead < 0)
{
*aReadCount = 0;
return NS_FILE_RESULT(PR_GetError());
}
else if (bytesRead == 0)
{
}
*aReadCount = bytesRead;
return NS_OK;
}
{
NS_NOTREACHED("ReadSegments");
return NS_ERROR_NOT_IMPLEMENTED;
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
{
*aWriteCount = 0;
#ifdef XP_MAC
// Calling PR_Write on stdout is sure suicide.
{
*aWriteCount = aCount;
return NS_OK;
}
#endif
if (!mFileDesc)
return NS_FILE_RESULT(PR_BAD_DESCRIPTOR_ERROR);
if (mFailed)
return NS_ERROR_FAILURE;
if (!mGotBuffers)
{
return rv; // try to write non-buffered?
}
PRUint32 currentWrite = 0;
while (aCount > 0)
{
{
{
// buffer is full, try again
return NS_ERROR_OUT_OF_MEMORY;
}
mWriteCursor = seg;
}
// move
if (aCount < currentWrite)
aCount -= currentWrite;
*aWriteCount += currentWrite;
}
return NS_OK;
}
static NS_METHOD
void* closure,
const char* fromRawSegment,
{
NS_NOTREACHED("nsWriteSegmentToFile");
return NS_ERROR_NOT_IMPLEMENTED;
}
{
}
{
NS_NOTREACHED("WriteSegments");
return NS_ERROR_NOT_IMPLEMENTED;
}
{
*aNonBlocking = PR_FALSE;
return NS_OK;
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
{
return NS_FILE_RESULT(PR_BAD_DESCRIPTOR_ERROR);
return NS_OK;
} // FileImpl::Tell
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
{
return NS_OK;
mFileDesc = 0;
else
return NS_FILE_RESULT(PR_GetError());
return NS_OK;
} // FileImpl::close
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
{
// for external callers, this will do a Sync as well as flush buffers.
return InternalFlush(PR_TRUE);
} // FileImpl::flush
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
{
return NS_OK;
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
{
return NS_OK;
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
{
NS_NOTYETIMPLEMENTED("FileImpl::SetEOF");
return NS_ERROR_NOT_IMPLEMENTED;
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
{
if (NS_SUCCEEDED(rv))
return rv;
}
// external callers of Flush will have sync get called,
// but internal callers just want to flush the buffers to disk.
{
#ifdef XP_MAC
{
return NS_OK;
}
#endif
if (!mFileDesc)
return NS_FILE_RESULT(PR_BAD_DESCRIPTOR_ERROR);
{
// if it is the last buffer, it may not be completely full.
if(i == (segCount-1))
{
return NS_FILE_RESULT(PR_GetError());
}
}
if (mGotBuffers)
mOutBuffer.Empty();
// On unix, it seems to fail always.
return NS_OK;
}
//----------------------------------------------------------------------------------------
const nsFileSpec& inFile
/*Default nsprMode == PR_RDONLY*/
/*Default accessmode = 0666 (octal)*/)
// Factory method to get an nsInputStream from a file, using most common options
//----------------------------------------------------------------------------------------
{
// This QueryInterface was needed because NS_NewIOFileStream
// does a cast from (void *) to (nsISupports *) thus causing a
// vtable problem on Windows, where we really didn't have the proper pointer
// to an nsIInputStream, this ensures that we do
if (NS_SUCCEEDED(rv)) {
}
}
return rv;
}
//----------------------------------------------------------------------------------------
const nsFileSpec& inFile
/*default nsprMode= (PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE)*/
/*Default accessMode= 0666 (octal)*/)
// Factory method to get an nsOutputStream to a file - most common case.
//----------------------------------------------------------------------------------------
{
// This QueryInterface was needed because NS_NewIOFileStream
// does a cast from (void *) to (nsISupports *) thus causing a
// vtable problem on Windows, where we really didn't have the proper pointer
// to an nsIOutputStream, this ensures that we do
#if 1
/* nsISupports * supports;
nsIOutputStream * outStr;
nsresult rv = NS_NewIOFileStream(
&supports,
inFile,
(PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE),
0666);
*aResult = nsnull;
if (NS_SUCCEEDED(rv)) {
if (NS_SUCCEEDED(supports->QueryInterface(NS_GET_IID(nsIOutputStream), (void**)&outStr))) {
*aResult = outStr;
}
NS_RELEASE(supports);
}
return rv;
*/
0666);
if (NS_SUCCEEDED(rv)) {
}
}
return rv;
#else
return NS_NewIOFileStream(
0666);
#endif
}
//----------------------------------------------------------------------------------------
const nsFileSpec& inFile,
// Factory method to get an object that implements both nsIInputStream
// and nsIOutputStream, associated with a file.
//----------------------------------------------------------------------------------------
{
if (!aResult)
return NS_ERROR_NULL_POINTER;
if (! stream)
return NS_ERROR_OUT_OF_MEMORY;
if (!isOpened)
{
return NS_ERROR_FAILURE;
}
return NS_OK;
}