nsFileStreams.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  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. #ifndef nsFileStreams_h__
  6. #define nsFileStreams_h__
  7. #include "nsAutoPtr.h"
  8. #include "nsIFileStreams.h"
  9. #include "nsIFile.h"
  10. #include "nsIInputStream.h"
  11. #include "nsIOutputStream.h"
  12. #include "nsISafeOutputStream.h"
  13. #include "nsISeekableStream.h"
  14. #include "nsILineInputStream.h"
  15. #include "nsCOMPtr.h"
  16. #include "nsIIPCSerializableInputStream.h"
  17. #include "nsReadLine.h"
  18. #include <algorithm>
  19. ////////////////////////////////////////////////////////////////////////////////
  20. class nsFileStreamBase : public nsISeekableStream,
  21. public nsIFileMetadata
  22. {
  23. public:
  24. NS_DECL_THREADSAFE_ISUPPORTS
  25. NS_DECL_NSISEEKABLESTREAM
  26. NS_DECL_NSIFILEMETADATA
  27. nsFileStreamBase();
  28. protected:
  29. virtual ~nsFileStreamBase();
  30. nsresult Close();
  31. nsresult Available(uint64_t* _retval);
  32. nsresult Read(char* aBuf, uint32_t aCount, uint32_t* _retval);
  33. nsresult ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
  34. uint32_t aCount, uint32_t* _retval);
  35. nsresult IsNonBlocking(bool* _retval);
  36. nsresult Flush();
  37. nsresult Write(const char* aBuf, uint32_t aCount, uint32_t* _retval);
  38. nsresult WriteFrom(nsIInputStream* aFromStream, uint32_t aCount,
  39. uint32_t* _retval);
  40. nsresult WriteSegments(nsReadSegmentFun aReader, void* aClosure,
  41. uint32_t aCount, uint32_t* _retval);
  42. PRFileDesc* mFD;
  43. /**
  44. * Flags describing our behavior. See the IDL file for possible values.
  45. */
  46. int32_t mBehaviorFlags;
  47. /**
  48. * Whether we have a pending open (see DEFER_OPEN in the IDL file).
  49. */
  50. bool mDeferredOpen;
  51. struct OpenParams {
  52. nsCOMPtr<nsIFile> localFile;
  53. int32_t ioFlags;
  54. int32_t perm;
  55. };
  56. /**
  57. * Data we need to do an open.
  58. */
  59. OpenParams mOpenParams;
  60. /**
  61. * Prepares the data we need to open the file, and either does the open now
  62. * by calling DoOpen(), or leaves it to be opened later by a call to
  63. * DoPendingOpen().
  64. */
  65. nsresult MaybeOpen(nsIFile* aFile, int32_t aIoFlags, int32_t aPerm,
  66. bool aDeferred);
  67. /**
  68. * Cleans up data prepared in MaybeOpen.
  69. */
  70. void CleanUpOpen();
  71. /**
  72. * Open the file. This is called either from MaybeOpen (during Init)
  73. * or from DoPendingOpen (if DEFER_OPEN is used when initializing this
  74. * stream). The default behavior of DoOpen is to open the file and save the
  75. * file descriptor.
  76. */
  77. virtual nsresult DoOpen();
  78. /**
  79. * If there is a pending open, do it now. It's important for this to be
  80. * inline since we do it in almost every stream API call.
  81. */
  82. inline nsresult DoPendingOpen();
  83. };
  84. ////////////////////////////////////////////////////////////////////////////////
  85. class nsFileInputStream : public nsFileStreamBase,
  86. public nsIFileInputStream,
  87. public nsILineInputStream,
  88. public nsIIPCSerializableInputStream
  89. {
  90. public:
  91. NS_DECL_ISUPPORTS_INHERITED
  92. NS_DECL_NSIFILEINPUTSTREAM
  93. NS_DECL_NSILINEINPUTSTREAM
  94. NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
  95. NS_IMETHOD Close() override;
  96. NS_IMETHOD Tell(int64_t *aResult) override;
  97. NS_IMETHOD Available(uint64_t* _retval) override;
  98. NS_IMETHOD Read(char* aBuf, uint32_t aCount, uint32_t* _retval) override;
  99. NS_IMETHOD ReadSegments(nsWriteSegmentFun aWriter, void *aClosure,
  100. uint32_t aCount, uint32_t* _retval) override
  101. {
  102. return nsFileStreamBase::ReadSegments(aWriter, aClosure, aCount,
  103. _retval);
  104. }
  105. NS_IMETHOD IsNonBlocking(bool* _retval) override
  106. {
  107. return nsFileStreamBase::IsNonBlocking(_retval);
  108. }
  109. // Overrided from nsFileStreamBase
  110. NS_IMETHOD Seek(int32_t aWhence, int64_t aOffset) override;
  111. nsFileInputStream()
  112. : mLineBuffer(nullptr), mIOFlags(0), mPerm(0), mCachedPosition(0)
  113. {}
  114. static nsresult
  115. Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
  116. protected:
  117. virtual ~nsFileInputStream()
  118. {
  119. Close();
  120. }
  121. nsresult SeekInternal(int32_t aWhence, int64_t aOffset, bool aClearBuf=true);
  122. nsAutoPtr<nsLineBuffer<char> > mLineBuffer;
  123. /**
  124. * The file being opened.
  125. */
  126. nsCOMPtr<nsIFile> mFile;
  127. /**
  128. * The IO flags passed to Init() for the file open.
  129. */
  130. int32_t mIOFlags;
  131. /**
  132. * The permissions passed to Init() for the file open.
  133. */
  134. int32_t mPerm;
  135. /**
  136. * Cached position for Tell for automatically reopening streams.
  137. */
  138. int64_t mCachedPosition;
  139. protected:
  140. /**
  141. * Internal, called to open a file. Parameters are the same as their
  142. * Init() analogues.
  143. */
  144. nsresult Open(nsIFile* file, int32_t ioFlags, int32_t perm);
  145. };
  146. ////////////////////////////////////////////////////////////////////////////////
  147. class nsPartialFileInputStream : public nsFileInputStream,
  148. public nsIPartialFileInputStream
  149. {
  150. public:
  151. using nsFileInputStream::Init;
  152. using nsFileInputStream::Read;
  153. NS_DECL_ISUPPORTS_INHERITED
  154. NS_DECL_NSIPARTIALFILEINPUTSTREAM
  155. NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
  156. nsPartialFileInputStream()
  157. : mStart(0), mLength(0), mPosition(0), mDeferredSeek(false)
  158. { }
  159. NS_IMETHOD Tell(int64_t *aResult) override;
  160. NS_IMETHOD Available(uint64_t *aResult) override;
  161. NS_IMETHOD Read(char* aBuf, uint32_t aCount, uint32_t* aResult) override;
  162. NS_IMETHOD Seek(int32_t aWhence, int64_t aOffset) override;
  163. static nsresult
  164. Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
  165. protected:
  166. ~nsPartialFileInputStream()
  167. { }
  168. inline nsresult DoPendingSeek();
  169. private:
  170. uint64_t TruncateSize(uint64_t aSize) {
  171. return std::min<uint64_t>(mLength - mPosition, aSize);
  172. }
  173. uint64_t mStart;
  174. uint64_t mLength;
  175. uint64_t mPosition;
  176. bool mDeferredSeek;
  177. };
  178. ////////////////////////////////////////////////////////////////////////////////
  179. class nsFileOutputStream : public nsFileStreamBase,
  180. public nsIFileOutputStream
  181. {
  182. public:
  183. NS_DECL_ISUPPORTS_INHERITED
  184. NS_DECL_NSIFILEOUTPUTSTREAM
  185. NS_FORWARD_NSIOUTPUTSTREAM(nsFileStreamBase::)
  186. static nsresult
  187. Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
  188. protected:
  189. virtual ~nsFileOutputStream()
  190. {
  191. Close();
  192. }
  193. };
  194. ////////////////////////////////////////////////////////////////////////////////
  195. /**
  196. * A safe file output stream that overwrites the destination file only
  197. * once writing is complete. This protects against incomplete writes
  198. * due to the process or the thread being interrupted or crashed.
  199. */
  200. class nsAtomicFileOutputStream : public nsFileOutputStream,
  201. public nsISafeOutputStream
  202. {
  203. public:
  204. NS_DECL_ISUPPORTS_INHERITED
  205. NS_DECL_NSISAFEOUTPUTSTREAM
  206. nsAtomicFileOutputStream() :
  207. mTargetFileExists(true),
  208. mWriteResult(NS_OK) {}
  209. virtual nsresult DoOpen() override;
  210. NS_IMETHOD Close() override;
  211. NS_IMETHOD Write(const char *buf, uint32_t count, uint32_t *result) override;
  212. NS_IMETHOD Init(nsIFile* file, int32_t ioFlags, int32_t perm, int32_t behaviorFlags) override;
  213. protected:
  214. virtual ~nsAtomicFileOutputStream()
  215. {
  216. Close();
  217. }
  218. nsCOMPtr<nsIFile> mTargetFile;
  219. nsCOMPtr<nsIFile> mTempFile;
  220. bool mTargetFileExists;
  221. nsresult mWriteResult; // Internally set in Write()
  222. };
  223. ////////////////////////////////////////////////////////////////////////////////
  224. /**
  225. * A safe file output stream that overwrites the destination file only
  226. * once writing + flushing is complete. This protects against more
  227. * classes of software/hardware errors than nsAtomicFileOutputStream,
  228. * at the expense of being more costly to the disk, OS and battery.
  229. */
  230. class nsSafeFileOutputStream : public nsAtomicFileOutputStream
  231. {
  232. public:
  233. NS_IMETHOD Finish() override;
  234. };
  235. ////////////////////////////////////////////////////////////////////////////////
  236. class nsFileStream : public nsFileStreamBase,
  237. public nsIInputStream,
  238. public nsIOutputStream,
  239. public nsIFileStream
  240. {
  241. public:
  242. NS_DECL_ISUPPORTS_INHERITED
  243. NS_DECL_NSIFILESTREAM
  244. NS_FORWARD_NSIINPUTSTREAM(nsFileStreamBase::)
  245. // Can't use NS_FORWARD_NSIOUTPUTSTREAM due to overlapping methods
  246. // Close() and IsNonBlocking()
  247. NS_IMETHOD Flush() override
  248. {
  249. return nsFileStreamBase::Flush();
  250. }
  251. NS_IMETHOD Write(const char* aBuf, uint32_t aCount, uint32_t* _retval) override
  252. {
  253. return nsFileStreamBase::Write(aBuf, aCount, _retval);
  254. }
  255. NS_IMETHOD WriteFrom(nsIInputStream* aFromStream, uint32_t aCount,
  256. uint32_t* _retval) override
  257. {
  258. return nsFileStreamBase::WriteFrom(aFromStream, aCount, _retval);
  259. }
  260. NS_IMETHOD WriteSegments(nsReadSegmentFun aReader, void* aClosure,
  261. uint32_t aCount, uint32_t* _retval) override
  262. {
  263. return nsFileStreamBase::WriteSegments(aReader, aClosure, aCount,
  264. _retval);
  265. }
  266. protected:
  267. virtual ~nsFileStream()
  268. {
  269. Close();
  270. }
  271. };
  272. ////////////////////////////////////////////////////////////////////////////////
  273. #endif // nsFileStreams_h__