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/. */
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2004-10-22 00:57:17 +04:00
|
|
|
#include "TestCommon.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
#include "nsIComponentRegistrar.h"
|
|
|
|
#include "nsIStreamTransportService.h"
|
|
|
|
#include "nsIAsyncInputStream.h"
|
|
|
|
#include "nsIProgressEventSink.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsIRequest.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsMemory.h"
|
2005-11-08 22:23:00 +03:00
|
|
|
#include "nsStringAPI.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
#include "nsIFileStreams.h"
|
|
|
|
#include "nsIStreamListener.h"
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsAutoLock.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
#include "prenv.h"
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
//
|
|
|
|
// set NSPR_LOG_MODULES=Test:5
|
|
|
|
//
|
2012-07-30 18:20:58 +04:00
|
|
|
static PRLogModuleInfo *gTestLog = nullptr;
|
2003-01-18 04:27:53 +03:00
|
|
|
#endif
|
|
|
|
#define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static NS_DEFINE_CID(kStreamTransportServiceCID, NS_STREAMTRANSPORTSERVICE_CID);
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#define CHUNK_SIZE 500
|
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
class MyCopier : public nsIInputStreamCallback
|
|
|
|
, public nsIOutputStreamCallback
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
|
|
|
public:
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
MyCopier()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mLock(nullptr)
|
2003-01-18 04:27:53 +03:00
|
|
|
, mInputCondition(NS_OK)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~MyCopier()
|
|
|
|
{
|
|
|
|
if (mLock)
|
2011-03-29 19:43:26 +04:00
|
|
|
nsAutoLock::DestroyLock(mLock);
|
2003-01-18 04:27:53 +03:00
|
|
|
if (mInput)
|
|
|
|
mInput->Close();
|
|
|
|
if (mOutput)
|
|
|
|
mOutput->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// called on any thread
|
|
|
|
NS_IMETHOD OnInputStreamReady(nsIAsyncInputStream *inStr)
|
|
|
|
{
|
|
|
|
LOG(("OnInputStreamReady\n"));
|
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
NS_ASSERTION(inStr == mInput, "unexpected stream");
|
|
|
|
Process_Locked();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// called on any thread
|
|
|
|
NS_IMETHOD OnOutputStreamReady(nsIAsyncOutputStream *outStr)
|
|
|
|
{
|
|
|
|
LOG(("OnOutputStreamReady\n"));
|
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
NS_ASSERTION(outStr == mOutput, "unexpected stream");
|
|
|
|
Process_Locked();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Close_Locked()
|
|
|
|
{
|
|
|
|
LOG(("Close_Locked\n"));
|
|
|
|
|
|
|
|
mOutput->Close();
|
|
|
|
mOutput = 0;
|
|
|
|
mInput->Close();
|
|
|
|
mInput = 0;
|
|
|
|
|
|
|
|
// post done copying event
|
2006-05-10 21:30:15 +04:00
|
|
|
QuitPumpingEvents();
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Process_Locked()
|
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
mInputCondition = NS_OK; // reset
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t n;
|
2003-01-18 04:27:53 +03:00
|
|
|
nsresult rv = mOutput->WriteSegments(FillOutputBuffer, this, CHUNK_SIZE, &n);
|
|
|
|
if (NS_FAILED(rv) || (n == 0)) {
|
|
|
|
if (rv == NS_BASE_STREAM_WOULD_BLOCK)
|
2012-07-30 18:20:58 +04:00
|
|
|
mOutput->AsyncWait(this, 0, 0, nullptr);
|
2003-01-18 04:27:53 +03:00
|
|
|
else if (mInputCondition == NS_BASE_STREAM_WOULD_BLOCK)
|
2012-07-30 18:20:58 +04:00
|
|
|
mInput->AsyncWait(this, 0, 0, nullptr);
|
2003-01-18 04:27:53 +03:00
|
|
|
else
|
|
|
|
Close_Locked();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult AsyncCopy(nsITransport *srcTrans, nsITransport *destTrans)
|
|
|
|
{
|
2011-03-29 19:43:26 +04:00
|
|
|
mLock = nsAutoLock::NewLock("MyCopier::mLock");
|
2003-01-18 04:27:53 +03:00
|
|
|
if (!mLock)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> inStr;
|
|
|
|
rv = srcTrans->OpenInputStream(0, 0, 0, getter_AddRefs(inStr));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOutputStream> outStr;
|
|
|
|
rv = destTrans->OpenOutputStream(0, 0, 0, getter_AddRefs(outStr));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
mInput = do_QueryInterface(inStr);
|
|
|
|
mOutput = do_QueryInterface(outStr);
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return mInput->AsyncWait(this, 0, 0, nullptr);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static NS_METHOD FillOutputBuffer(nsIOutputStream *outStr,
|
|
|
|
void *closure,
|
|
|
|
char *buffer,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t offset,
|
|
|
|
uint32_t count,
|
|
|
|
uint32_t *countRead)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
|
|
|
MyCopier *self = (MyCopier *) closure;
|
|
|
|
|
|
|
|
nsresult rv = self->mInput->Read(buffer, count, countRead);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
self->mInputCondition = rv;
|
|
|
|
else if (*countRead == 0)
|
|
|
|
self->mInputCondition = NS_BASE_STREAM_CLOSED;
|
|
|
|
|
|
|
|
return self->mInputCondition;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
PRLock *mLock;
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> mInput;
|
|
|
|
nsCOMPtr<nsIAsyncOutputStream> mOutput;
|
|
|
|
nsresult mInputCondition;
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(MyCopier,
|
|
|
|
nsIInputStreamCallback,
|
|
|
|
nsIOutputStreamCallback)
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* asynchronously copy file.
|
|
|
|
*/
|
|
|
|
static nsresult
|
|
|
|
RunTest(nsIFile *srcFile, nsIFile *destFile)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
LOG(("RunTest\n"));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamTransportService> sts =
|
|
|
|
do_GetService(kStreamTransportServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> srcStr;
|
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(srcStr), srcFile);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOutputStream> destStr;
|
|
|
|
rv = NS_NewLocalFileOutputStream(getter_AddRefs(destStr), destFile);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsITransport> srcTransport;
|
2012-08-22 19:56:38 +04:00
|
|
|
rv = sts->CreateInputTransport(srcStr, int64_t(-1), int64_t(-1), true,
|
2005-01-09 23:43:35 +03:00
|
|
|
getter_AddRefs(srcTransport));
|
2003-01-18 04:27:53 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsITransport> destTransport;
|
2012-08-22 19:56:38 +04:00
|
|
|
rv = sts->CreateOutputTransport(destStr, int64_t(-1), int64_t(-1), true,
|
2005-01-09 23:43:35 +03:00
|
|
|
getter_AddRefs(destTransport));
|
2003-01-18 04:27:53 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
MyCopier *copier = new MyCopier();
|
2012-07-30 18:20:58 +04:00
|
|
|
if (copier == nullptr)
|
2003-01-18 04:27:53 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(copier);
|
|
|
|
|
|
|
|
rv = copier->AsyncCopy(srcTransport, destTransport);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
PumpEvents();
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
NS_RELEASE(copier);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
RunBlockingTest(nsIFile *srcFile, nsIFile *destFile)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
LOG(("RunBlockingTest\n"));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamTransportService> sts =
|
|
|
|
do_GetService(kStreamTransportServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> srcIn;
|
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(srcIn), srcFile);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOutputStream> fileOut;
|
|
|
|
rv = NS_NewLocalFileOutputStream(getter_AddRefs(fileOut), destFile);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsITransport> destTransport;
|
2012-08-22 19:56:38 +04:00
|
|
|
rv = sts->CreateOutputTransport(fileOut, int64_t(-1), int64_t(-1),
|
2011-10-17 18:59:28 +04:00
|
|
|
true, getter_AddRefs(destTransport));
|
2003-01-18 04:27:53 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOutputStream> destOut;
|
|
|
|
rv = destTransport->OpenOutputStream(nsITransport::OPEN_BLOCKING, 100, 10, getter_AddRefs(destOut));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
char buf[120];
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t n;
|
2003-01-18 04:27:53 +03:00
|
|
|
for (;;) {
|
|
|
|
rv = srcIn->Read(buf, sizeof(buf), &n);
|
|
|
|
if (NS_FAILED(rv) || (n == 0)) return rv;
|
|
|
|
|
|
|
|
rv = destOut->Write(buf, n, &n);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
2004-10-22 00:57:17 +04:00
|
|
|
if (test_common_init(&argc, &argv) != 0)
|
|
|
|
return -1;
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
printf("usage: %s <file-to-read>\n", argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
char* fileName = argv[1];
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIServiceManager> servMan;
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
|
2003-01-18 04:27:53 +03:00
|
|
|
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
|
|
|
|
NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
|
|
|
|
if (registrar)
|
2012-07-30 18:20:58 +04:00
|
|
|
registrar->AutoRegister(nullptr);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
gTestLog = PR_NewLogModule("Test");
|
|
|
|
#endif
|
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> srcFile;
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = NS_NewNativeLocalFile(nsDependentCString(fileName), false, getter_AddRefs(srcFile));
|
2003-01-18 04:27:53 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> destFile;
|
|
|
|
rv = srcFile->Clone(getter_AddRefs(destFile));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString leafName;
|
2003-01-18 04:27:53 +03:00
|
|
|
rv = destFile->GetNativeLeafName(leafName);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString newName(leafName);
|
2014-05-22 07:48:50 +04:00
|
|
|
newName.AppendLiteral(".1");
|
2003-01-18 04:27:53 +03:00
|
|
|
rv = destFile->SetNativeLeafName(newName);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = RunTest(srcFile, destFile);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
|
|
|
|
|
2005-11-08 22:23:00 +03:00
|
|
|
newName = leafName;
|
2014-05-22 07:48:50 +04:00
|
|
|
newName.AppendLiteral(".2");
|
2003-01-18 04:27:53 +03:00
|
|
|
rv = destFile->SetNativeLeafName(newName);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = RunBlockingTest(srcFile, destFile);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "RunBlockingTest failed");
|
|
|
|
|
|
|
|
// give background threads a chance to finish whatever work they may
|
|
|
|
// be doing.
|
|
|
|
PR_Sleep(PR_SecondsToInterval(1));
|
|
|
|
} // this scopes the nsCOMPtrs
|
|
|
|
// no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = NS_ShutdownXPCOM(nullptr);
|
2003-01-18 04:27:53 +03:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
|
|
|
|
return NS_OK;
|
|
|
|
}
|