|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=2 et sw=2 tw=80: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "FileStreams.h" |
|
8 |
|
9 #include "QuotaManager.h" |
|
10 #include "prio.h" |
|
11 |
|
12 USING_QUOTA_NAMESPACE |
|
13 |
|
14 template <class FileStreamBase> |
|
15 NS_IMETHODIMP |
|
16 FileQuotaStream<FileStreamBase>::SetEOF() |
|
17 { |
|
18 nsresult rv = FileStreamBase::SetEOF(); |
|
19 NS_ENSURE_SUCCESS(rv, rv); |
|
20 |
|
21 if (mQuotaObject) { |
|
22 int64_t offset; |
|
23 nsresult rv = FileStreamBase::Tell(&offset); |
|
24 NS_ENSURE_SUCCESS(rv, rv); |
|
25 |
|
26 mQuotaObject->UpdateSize(offset); |
|
27 } |
|
28 |
|
29 return NS_OK; |
|
30 } |
|
31 |
|
32 template <class FileStreamBase> |
|
33 NS_IMETHODIMP |
|
34 FileQuotaStream<FileStreamBase>::Close() |
|
35 { |
|
36 nsresult rv = FileStreamBase::Close(); |
|
37 NS_ENSURE_SUCCESS(rv, rv); |
|
38 |
|
39 mQuotaObject = nullptr; |
|
40 |
|
41 return NS_OK; |
|
42 } |
|
43 |
|
44 template <class FileStreamBase> |
|
45 nsresult |
|
46 FileQuotaStream<FileStreamBase>::DoOpen() |
|
47 { |
|
48 QuotaManager* quotaManager = QuotaManager::Get(); |
|
49 NS_ASSERTION(quotaManager, "Shouldn't be null!"); |
|
50 |
|
51 NS_ASSERTION(!mQuotaObject, "Creating quota object more than once?"); |
|
52 mQuotaObject = quotaManager->GetQuotaObject(mPersistenceType, mGroup, mOrigin, |
|
53 FileStreamBase::mOpenParams.localFile); |
|
54 |
|
55 nsresult rv = FileStreamBase::DoOpen(); |
|
56 NS_ENSURE_SUCCESS(rv, rv); |
|
57 |
|
58 if (mQuotaObject && (FileStreamBase::mOpenParams.ioFlags & PR_TRUNCATE)) { |
|
59 mQuotaObject->UpdateSize(0); |
|
60 } |
|
61 |
|
62 return NS_OK; |
|
63 } |
|
64 |
|
65 template <class FileStreamBase> |
|
66 NS_IMETHODIMP |
|
67 FileQuotaStreamWithWrite<FileStreamBase>::Write(const char* aBuf, |
|
68 uint32_t aCount, |
|
69 uint32_t* _retval) |
|
70 { |
|
71 nsresult rv; |
|
72 |
|
73 if (FileQuotaStreamWithWrite::mQuotaObject) { |
|
74 int64_t offset; |
|
75 rv = FileStreamBase::Tell(&offset); |
|
76 NS_ENSURE_SUCCESS(rv, rv); |
|
77 |
|
78 if (!FileQuotaStreamWithWrite:: |
|
79 mQuotaObject->MaybeAllocateMoreSpace(offset, aCount)) { |
|
80 return NS_ERROR_FILE_NO_DEVICE_SPACE; |
|
81 } |
|
82 } |
|
83 |
|
84 rv = FileStreamBase::Write(aBuf, aCount, _retval); |
|
85 NS_ENSURE_SUCCESS(rv, rv); |
|
86 |
|
87 return NS_OK; |
|
88 } |
|
89 |
|
90 NS_IMPL_ISUPPORTS_INHERITED0(FileInputStream, nsFileInputStream) |
|
91 |
|
92 already_AddRefed<FileInputStream> |
|
93 FileInputStream::Create(PersistenceType aPersistenceType, |
|
94 const nsACString& aGroup, const nsACString& aOrigin, |
|
95 nsIFile* aFile, int32_t aIOFlags, int32_t aPerm, |
|
96 int32_t aBehaviorFlags) |
|
97 { |
|
98 nsRefPtr<FileInputStream> stream = |
|
99 new FileInputStream(aPersistenceType, aGroup, aOrigin); |
|
100 nsresult rv = stream->Init(aFile, aIOFlags, aPerm, aBehaviorFlags); |
|
101 NS_ENSURE_SUCCESS(rv, nullptr); |
|
102 return stream.forget(); |
|
103 } |
|
104 |
|
105 NS_IMPL_ISUPPORTS_INHERITED0(FileOutputStream, nsFileOutputStream) |
|
106 |
|
107 already_AddRefed<FileOutputStream> |
|
108 FileOutputStream::Create(PersistenceType aPersistenceType, |
|
109 const nsACString& aGroup, const nsACString& aOrigin, |
|
110 nsIFile* aFile, int32_t aIOFlags, int32_t aPerm, |
|
111 int32_t aBehaviorFlags) |
|
112 { |
|
113 nsRefPtr<FileOutputStream> stream = |
|
114 new FileOutputStream(aPersistenceType, aGroup, aOrigin); |
|
115 nsresult rv = stream->Init(aFile, aIOFlags, aPerm, aBehaviorFlags); |
|
116 NS_ENSURE_SUCCESS(rv, nullptr); |
|
117 return stream.forget(); |
|
118 } |
|
119 |
|
120 NS_IMPL_ISUPPORTS_INHERITED0(FileStream, nsFileStream) |
|
121 |
|
122 already_AddRefed<FileStream> |
|
123 FileStream::Create(PersistenceType aPersistenceType, const nsACString& aGroup, |
|
124 const nsACString& aOrigin, nsIFile* aFile, int32_t aIOFlags, |
|
125 int32_t aPerm, int32_t aBehaviorFlags) |
|
126 { |
|
127 nsRefPtr<FileStream> stream = |
|
128 new FileStream(aPersistenceType, aGroup, aOrigin); |
|
129 nsresult rv = stream->Init(aFile, aIOFlags, aPerm, aBehaviorFlags); |
|
130 NS_ENSURE_SUCCESS(rv, nullptr); |
|
131 return stream.forget(); |
|
132 } |