2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "TestCommon.h"
|
|
|
|
#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"
|
|
|
|
#include "nsStringAPI.h"
|
|
|
|
#include "nsIFileStreams.h"
|
|
|
|
#include "nsIStreamListener.h"
|
2012-06-05 19:08:30 -07:00
|
|
|
#include "nsIFile.h"
|
2007-03-22 10:30:00 -07: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 07:20:58 -07:00
|
|
|
static PRLogModuleInfo *gTestLog = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
#define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static NS_DEFINE_CID(kStreamTransportServiceCID, NS_STREAMTRANSPORTSERVICE_CID);
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#define CHUNK_SIZE 500
|
|
|
|
|
|
|
|
class MyCopier : public nsIInputStreamCallback
|
|
|
|
, public nsIOutputStreamCallback
|
|
|
|
{
|
|
|
|
public:
|
2013-07-18 19:24:13 -07:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
MyCopier()
|
2012-07-30 07:20:58 -07:00
|
|
|
: mLock(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
, mInputCondition(NS_OK)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~MyCopier()
|
|
|
|
{
|
|
|
|
if (mLock)
|
2011-03-29 08:43:26 -07:00
|
|
|
nsAutoLock::DestroyLock(mLock);
|
2007-03-22 10:30:00 -07: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
|
|
|
|
QuitPumpingEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Process_Locked()
|
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
mInputCondition = NS_OK; // reset
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t n;
|
2007-03-22 10:30:00 -07: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 07:20:58 -07:00
|
|
|
mOutput->AsyncWait(this, 0, 0, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
else if (mInputCondition == NS_BASE_STREAM_WOULD_BLOCK)
|
2012-07-30 07:20:58 -07:00
|
|
|
mInput->AsyncWait(this, 0, 0, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
else
|
|
|
|
Close_Locked();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult AsyncCopy(nsITransport *srcTrans, nsITransport *destTrans)
|
|
|
|
{
|
2011-03-29 08:43:26 -07:00
|
|
|
mLock = nsAutoLock::NewLock("MyCopier::mLock");
|
2007-03-22 10:30:00 -07: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 07:20:58 -07:00
|
|
|
return mInput->AsyncWait(this, 0, 0, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static NS_METHOD FillOutputBuffer(nsIOutputStream *outStr,
|
|
|
|
void *closure,
|
|
|
|
char *buffer,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t offset,
|
|
|
|
uint32_t count,
|
|
|
|
uint32_t *countRead)
|
2007-03-22 10:30:00 -07: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 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(MyCopier,
|
|
|
|
nsIInputStreamCallback,
|
|
|
|
nsIOutputStreamCallback)
|
2007-03-22 10:30:00 -07: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 08:56:38 -07:00
|
|
|
rv = sts->CreateInputTransport(srcStr, int64_t(-1), int64_t(-1), true,
|
2007-03-22 10:30:00 -07:00
|
|
|
getter_AddRefs(srcTransport));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsITransport> destTransport;
|
2012-08-22 08:56:38 -07:00
|
|
|
rv = sts->CreateOutputTransport(destStr, int64_t(-1), int64_t(-1), true,
|
2007-03-22 10:30:00 -07:00
|
|
|
getter_AddRefs(destTransport));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
MyCopier *copier = new MyCopier();
|
2012-07-30 07:20:58 -07:00
|
|
|
if (copier == nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(copier);
|
|
|
|
|
|
|
|
rv = copier->AsyncCopy(srcTransport, destTransport);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
PumpEvents();
|
|
|
|
|
|
|
|
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 08:56:38 -07:00
|
|
|
rv = sts->CreateOutputTransport(fileOut, int64_t(-1), int64_t(-1),
|
2011-10-17 07:59:28 -07:00
|
|
|
true, getter_AddRefs(destTransport));
|
2007-03-22 10:30:00 -07: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 08:56:38 -07:00
|
|
|
uint32_t n;
|
2007-03-22 10:30:00 -07: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[])
|
|
|
|
{
|
|
|
|
if (test_common_init(&argc, &argv) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
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 07:20:58 -07:00
|
|
|
NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
|
|
|
|
NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
|
|
|
|
if (registrar)
|
2012-07-30 07:20:58 -07:00
|
|
|
registrar->AutoRegister(nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
gTestLog = PR_NewLogModule("Test");
|
|
|
|
#endif
|
|
|
|
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> srcFile;
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = NS_NewNativeLocalFile(nsDependentCString(fileName), false, getter_AddRefs(srcFile));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> destFile;
|
|
|
|
rv = srcFile->Clone(getter_AddRefs(destFile));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString leafName;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = destFile->GetNativeLeafName(leafName);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString newName(leafName);
|
2014-05-21 20:48:50 -07:00
|
|
|
newName.AppendLiteral(".1");
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = destFile->SetNativeLeafName(newName);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = RunTest(srcFile, destFile);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
|
|
|
|
|
|
|
|
newName = leafName;
|
2014-05-21 20:48:50 -07:00
|
|
|
newName.AppendLiteral(".2");
|
2007-03-22 10:30:00 -07: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 07:20:58 -07:00
|
|
|
rv = NS_ShutdownXPCOM(nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
|
|
|
|
return NS_OK;
|
|
|
|
}
|