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