FileDescriptorOutputStream.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "mozilla/devtools/FileDescriptorOutputStream.h"
  6. #include "private/pprio.h"
  7. namespace mozilla {
  8. namespace devtools {
  9. /* static */ already_AddRefed<FileDescriptorOutputStream>
  10. FileDescriptorOutputStream::Create(const ipc::FileDescriptor& fileDescriptor)
  11. {
  12. if (NS_WARN_IF(!fileDescriptor.IsValid()))
  13. return nullptr;
  14. auto rawFD = fileDescriptor.ClonePlatformHandle();
  15. PRFileDesc* prfd = PR_ImportFile(PROsfd(rawFD.release()));
  16. if (NS_WARN_IF(!prfd))
  17. return nullptr;
  18. RefPtr<FileDescriptorOutputStream> stream = new FileDescriptorOutputStream(prfd);
  19. return stream.forget();
  20. }
  21. NS_IMPL_ISUPPORTS(FileDescriptorOutputStream, nsIOutputStream);
  22. NS_IMETHODIMP
  23. FileDescriptorOutputStream::Close()
  24. {
  25. // Repeatedly closing is idempotent.
  26. if (!fd)
  27. return NS_OK;
  28. if (PR_Close(fd) != PR_SUCCESS)
  29. return NS_ERROR_FAILURE;
  30. fd = nullptr;
  31. return NS_OK;
  32. }
  33. NS_IMETHODIMP
  34. FileDescriptorOutputStream::Write(const char* buf, uint32_t count, uint32_t* retval)
  35. {
  36. if (NS_WARN_IF(!fd))
  37. return NS_ERROR_FAILURE;
  38. auto written = PR_Write(fd, buf, count);
  39. if (written < 0)
  40. return NS_ERROR_FAILURE;
  41. *retval = written;
  42. return NS_OK;
  43. }
  44. NS_IMETHODIMP
  45. FileDescriptorOutputStream::Flush()
  46. {
  47. if (NS_WARN_IF(!fd))
  48. return NS_ERROR_FAILURE;
  49. return PR_Sync(fd) == PR_SUCCESS ? NS_OK : NS_ERROR_FAILURE;
  50. }
  51. NS_IMETHODIMP
  52. FileDescriptorOutputStream::WriteFrom(nsIInputStream* fromStream, uint32_t count,
  53. uint32_t* retval)
  54. {
  55. return NS_ERROR_NOT_IMPLEMENTED;
  56. }
  57. NS_IMETHODIMP
  58. FileDescriptorOutputStream::WriteSegments(nsReadSegmentFun reader, void* closure,
  59. uint32_t count, uint32_t* retval)
  60. {
  61. return NS_ERROR_NOT_IMPLEMENTED;
  62. }
  63. NS_IMETHODIMP
  64. FileDescriptorOutputStream::IsNonBlocking(bool* retval)
  65. {
  66. *retval = false;
  67. return NS_OK;
  68. }
  69. } // namespace devtools
  70. } // namespace mozilla