nsTemporaryFileInputStream.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsTemporaryFileInputStream.h"
  6. #include "mozilla/ipc/InputStreamUtils.h"
  7. #include "mozilla/Mutex.h"
  8. #include "nsStreamUtils.h"
  9. #include "private/pprio.h"
  10. #include <algorithm>
  11. using namespace mozilla;
  12. using namespace mozilla::ipc;
  13. typedef mozilla::ipc::FileDescriptor::PlatformHandleType FileHandleType;
  14. NS_IMPL_ISUPPORTS(nsTemporaryFileInputStream,
  15. nsIInputStream,
  16. nsISeekableStream,
  17. nsIIPCSerializableInputStream)
  18. nsTemporaryFileInputStream::nsTemporaryFileInputStream(FileDescOwner* aFileDescOwner, uint64_t aStartPos, uint64_t aEndPos)
  19. : mFileDescOwner(aFileDescOwner),
  20. mStartPos(aStartPos),
  21. mCurPos(aStartPos),
  22. mEndPos(aEndPos),
  23. mClosed(false)
  24. {
  25. NS_ASSERTION(aStartPos <= aEndPos, "StartPos should less equal than EndPos!");
  26. }
  27. nsTemporaryFileInputStream::nsTemporaryFileInputStream()
  28. : mStartPos(0),
  29. mCurPos(0),
  30. mEndPos(0),
  31. mClosed(false)
  32. {
  33. }
  34. NS_IMETHODIMP
  35. nsTemporaryFileInputStream::Close()
  36. {
  37. mClosed = true;
  38. return NS_OK;
  39. }
  40. NS_IMETHODIMP
  41. nsTemporaryFileInputStream::Available(uint64_t * bytesAvailable)
  42. {
  43. if (mClosed)
  44. return NS_BASE_STREAM_CLOSED;
  45. NS_ASSERTION(mCurPos <= mEndPos, "CurPos should less equal than EndPos!");
  46. *bytesAvailable = mEndPos - mCurPos;
  47. return NS_OK;
  48. }
  49. NS_IMETHODIMP
  50. nsTemporaryFileInputStream::Read(char* buffer, uint32_t count, uint32_t* bytesRead)
  51. {
  52. return ReadSegments(NS_CopySegmentToBuffer, buffer, count, bytesRead);
  53. }
  54. NS_IMETHODIMP
  55. nsTemporaryFileInputStream::ReadSegments(nsWriteSegmentFun writer,
  56. void * closure,
  57. uint32_t count,
  58. uint32_t * result)
  59. {
  60. NS_ASSERTION(result, "null ptr");
  61. NS_ASSERTION(mCurPos <= mEndPos, "bad stream state");
  62. *result = 0;
  63. if (mClosed) {
  64. return NS_BASE_STREAM_CLOSED;
  65. }
  66. mozilla::MutexAutoLock lock(mFileDescOwner->FileMutex());
  67. int64_t offset = PR_Seek64(mFileDescOwner->mFD, mCurPos, PR_SEEK_SET);
  68. if (offset == -1) {
  69. return NS_ErrorAccordingToNSPR();
  70. }
  71. // Limit requested count to the amount remaining in our section of the file.
  72. count = std::min(count, uint32_t(mEndPos - mCurPos));
  73. char buf[4096];
  74. while (*result < count) {
  75. uint32_t bufCount = std::min(count - *result, (uint32_t) sizeof(buf));
  76. int32_t bytesRead = PR_Read(mFileDescOwner->mFD, buf, bufCount);
  77. if (bytesRead == 0) {
  78. mClosed = true;
  79. return NS_OK;
  80. }
  81. if (bytesRead < 0) {
  82. return NS_ErrorAccordingToNSPR();
  83. }
  84. int32_t bytesWritten = 0;
  85. while (bytesWritten < bytesRead) {
  86. uint32_t writerCount = 0;
  87. nsresult rv = writer(this, closure, buf + bytesWritten, *result,
  88. bytesRead - bytesWritten, &writerCount);
  89. if (NS_FAILED(rv) || writerCount == 0) {
  90. // nsIInputStream::ReadSegments' contract specifies that errors
  91. // from writer are not propagated to ReadSegments' caller.
  92. //
  93. // If writer fails, leaving bytes still in buf, that's okay: we
  94. // only update mCurPos to reflect successful writes, so the call
  95. // to PR_Seek64 at the top will restart us at the right spot.
  96. return NS_OK;
  97. }
  98. NS_ASSERTION(writerCount <= (uint32_t) (bytesRead - bytesWritten),
  99. "writer should not write more than we asked it to write");
  100. bytesWritten += writerCount;
  101. *result += writerCount;
  102. mCurPos += writerCount;
  103. }
  104. }
  105. return NS_OK;
  106. }
  107. NS_IMETHODIMP
  108. nsTemporaryFileInputStream::IsNonBlocking(bool * nonBlocking)
  109. {
  110. *nonBlocking = false;
  111. return NS_OK;
  112. }
  113. NS_IMETHODIMP
  114. nsTemporaryFileInputStream::Seek(int32_t aWhence, int64_t aOffset)
  115. {
  116. if (mClosed) {
  117. return NS_BASE_STREAM_CLOSED;
  118. }
  119. switch (aWhence) {
  120. case nsISeekableStream::NS_SEEK_SET:
  121. aOffset += mStartPos;
  122. break;
  123. case nsISeekableStream::NS_SEEK_CUR:
  124. aOffset += mCurPos;
  125. break;
  126. case nsISeekableStream::NS_SEEK_END:
  127. aOffset += mEndPos;
  128. break;
  129. default:
  130. return NS_ERROR_FAILURE;
  131. }
  132. if (aOffset < (int64_t)mStartPos || aOffset > (int64_t)mEndPos) {
  133. return NS_ERROR_INVALID_ARG;
  134. }
  135. mCurPos = aOffset;
  136. return NS_OK;
  137. }
  138. NS_IMETHODIMP
  139. nsTemporaryFileInputStream::Tell(int64_t* aPos)
  140. {
  141. if (!aPos) {
  142. return NS_ERROR_FAILURE;
  143. }
  144. if (mClosed) {
  145. return NS_BASE_STREAM_CLOSED;
  146. }
  147. MOZ_ASSERT(mStartPos <= mCurPos, "StartPos should less equal than CurPos!");
  148. *aPos = mCurPos - mStartPos;
  149. return NS_OK;
  150. }
  151. NS_IMETHODIMP
  152. nsTemporaryFileInputStream::SetEOF()
  153. {
  154. if (mClosed) {
  155. return NS_BASE_STREAM_CLOSED;
  156. }
  157. return Close();
  158. }
  159. void
  160. nsTemporaryFileInputStream::Serialize(InputStreamParams& aParams,
  161. FileDescriptorArray& aFileDescriptors)
  162. {
  163. TemporaryFileInputStreamParams params;
  164. MutexAutoLock lock(mFileDescOwner->FileMutex());
  165. MOZ_ASSERT(mFileDescOwner->mFD);
  166. if (!mClosed) {
  167. FileHandleType fd = FileHandleType(PR_FileDesc2NativeHandle(mFileDescOwner->mFD));
  168. NS_ASSERTION(fd, "This should never be null!");
  169. DebugOnly<FileDescriptor*> dbgFD = aFileDescriptors.AppendElement(fd);
  170. NS_ASSERTION(dbgFD->IsValid(), "Sending an invalid file descriptor!");
  171. params.fileDescriptorIndex() = aFileDescriptors.Length() - 1;
  172. Close();
  173. } else {
  174. NS_WARNING("The stream is already closed. "
  175. "Sending an invalid file descriptor to the other process!");
  176. params.fileDescriptorIndex() = UINT32_MAX;
  177. }
  178. params.startPos() = mCurPos;
  179. params.endPos() = mEndPos;
  180. aParams = params;
  181. }
  182. bool
  183. nsTemporaryFileInputStream::Deserialize(const InputStreamParams& aParams,
  184. const FileDescriptorArray& aFileDescriptors)
  185. {
  186. const TemporaryFileInputStreamParams& params = aParams.get_TemporaryFileInputStreamParams();
  187. uint32_t fileDescriptorIndex = params.fileDescriptorIndex();
  188. FileDescriptor fd;
  189. if (fileDescriptorIndex < aFileDescriptors.Length()) {
  190. fd = aFileDescriptors[fileDescriptorIndex];
  191. NS_WARNING_ASSERTION(fd.IsValid(),
  192. "Received an invalid file descriptor!");
  193. } else {
  194. NS_WARNING("Received a bad file descriptor index!");
  195. }
  196. if (fd.IsValid()) {
  197. auto rawFD = fd.ClonePlatformHandle();
  198. PRFileDesc* fileDesc = PR_ImportFile(PROsfd(rawFD.release()));
  199. if (!fileDesc) {
  200. NS_WARNING("Failed to import file handle!");
  201. return false;
  202. }
  203. mFileDescOwner = new FileDescOwner(fileDesc);
  204. } else {
  205. mClosed = true;
  206. }
  207. mStartPos = mCurPos = params.startPos();
  208. mEndPos = params.endPos();
  209. return true;
  210. }
  211. Maybe<uint64_t>
  212. nsTemporaryFileInputStream::ExpectedSerializedLength()
  213. {
  214. return Nothing();
  215. }