michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "FileStreams.h" michael@0: michael@0: #include "QuotaManager.h" michael@0: #include "prio.h" michael@0: michael@0: USING_QUOTA_NAMESPACE michael@0: michael@0: template michael@0: NS_IMETHODIMP michael@0: FileQuotaStream::SetEOF() michael@0: { michael@0: nsresult rv = FileStreamBase::SetEOF(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if (mQuotaObject) { michael@0: int64_t offset; michael@0: nsresult rv = FileStreamBase::Tell(&offset); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mQuotaObject->UpdateSize(offset); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: template michael@0: NS_IMETHODIMP michael@0: FileQuotaStream::Close() michael@0: { michael@0: nsresult rv = FileStreamBase::Close(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mQuotaObject = nullptr; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: template michael@0: nsresult michael@0: FileQuotaStream::DoOpen() michael@0: { michael@0: QuotaManager* quotaManager = QuotaManager::Get(); michael@0: NS_ASSERTION(quotaManager, "Shouldn't be null!"); michael@0: michael@0: NS_ASSERTION(!mQuotaObject, "Creating quota object more than once?"); michael@0: mQuotaObject = quotaManager->GetQuotaObject(mPersistenceType, mGroup, mOrigin, michael@0: FileStreamBase::mOpenParams.localFile); michael@0: michael@0: nsresult rv = FileStreamBase::DoOpen(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if (mQuotaObject && (FileStreamBase::mOpenParams.ioFlags & PR_TRUNCATE)) { michael@0: mQuotaObject->UpdateSize(0); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: template michael@0: NS_IMETHODIMP michael@0: FileQuotaStreamWithWrite::Write(const char* aBuf, michael@0: uint32_t aCount, michael@0: uint32_t* _retval) michael@0: { michael@0: nsresult rv; michael@0: michael@0: if (FileQuotaStreamWithWrite::mQuotaObject) { michael@0: int64_t offset; michael@0: rv = FileStreamBase::Tell(&offset); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if (!FileQuotaStreamWithWrite:: michael@0: mQuotaObject->MaybeAllocateMoreSpace(offset, aCount)) { michael@0: return NS_ERROR_FILE_NO_DEVICE_SPACE; michael@0: } michael@0: } michael@0: michael@0: rv = FileStreamBase::Write(aBuf, aCount, _retval); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS_INHERITED0(FileInputStream, nsFileInputStream) michael@0: michael@0: already_AddRefed michael@0: FileInputStream::Create(PersistenceType aPersistenceType, michael@0: const nsACString& aGroup, const nsACString& aOrigin, michael@0: nsIFile* aFile, int32_t aIOFlags, int32_t aPerm, michael@0: int32_t aBehaviorFlags) michael@0: { michael@0: nsRefPtr stream = michael@0: new FileInputStream(aPersistenceType, aGroup, aOrigin); michael@0: nsresult rv = stream->Init(aFile, aIOFlags, aPerm, aBehaviorFlags); michael@0: NS_ENSURE_SUCCESS(rv, nullptr); michael@0: return stream.forget(); michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS_INHERITED0(FileOutputStream, nsFileOutputStream) michael@0: michael@0: already_AddRefed michael@0: FileOutputStream::Create(PersistenceType aPersistenceType, michael@0: const nsACString& aGroup, const nsACString& aOrigin, michael@0: nsIFile* aFile, int32_t aIOFlags, int32_t aPerm, michael@0: int32_t aBehaviorFlags) michael@0: { michael@0: nsRefPtr stream = michael@0: new FileOutputStream(aPersistenceType, aGroup, aOrigin); michael@0: nsresult rv = stream->Init(aFile, aIOFlags, aPerm, aBehaviorFlags); michael@0: NS_ENSURE_SUCCESS(rv, nullptr); michael@0: return stream.forget(); michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS_INHERITED0(FileStream, nsFileStream) michael@0: michael@0: already_AddRefed michael@0: FileStream::Create(PersistenceType aPersistenceType, const nsACString& aGroup, michael@0: const nsACString& aOrigin, nsIFile* aFile, int32_t aIOFlags, michael@0: int32_t aPerm, int32_t aBehaviorFlags) michael@0: { michael@0: nsRefPtr stream = michael@0: new FileStream(aPersistenceType, aGroup, aOrigin); michael@0: nsresult rv = stream->Init(aFile, aIOFlags, aPerm, aBehaviorFlags); michael@0: NS_ENSURE_SUCCESS(rv, nullptr); michael@0: return stream.forget(); michael@0: }