Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
file_support.cc
Go to the documentation of this file.
1// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "bin/file.h"
6
7#include "bin/builtin.h"
8#include "bin/dartutils.h"
9#include "bin/io_buffer.h"
10#include "bin/utils.h"
11
13#include "include/dart_api.h"
15
16namespace dart {
17namespace bin {
18
19// Are we capturing output from stdout for the VM service?
20static bool capture_stdout = false;
21
22// Are we capturing output from stderr for the VM service?
23static bool capture_stderr = false;
24
25void SetCaptureStdout(bool value) {
27}
28
29void SetCaptureStderr(bool value) {
31}
32
34 return capture_stdout;
35}
36
38 return capture_stderr;
39}
40
41bool File::ReadFully(void* buffer, int64_t num_bytes) {
42 int64_t remaining = num_bytes;
43 char* current_buffer = reinterpret_cast<char*>(buffer);
44 while (remaining > 0) {
45 int64_t bytes_read = Read(current_buffer, remaining);
46 if (bytes_read <= 0) {
47 return false;
48 }
49 remaining -= bytes_read; // Reduce the number of remaining bytes.
50 current_buffer += bytes_read; // Move the buffer forward.
51 }
52 return true;
53}
54
55bool File::WriteFully(const void* buffer, int64_t num_bytes) {
56 int64_t remaining = num_bytes;
57 const char* current_buffer = reinterpret_cast<const char*>(buffer);
58 while (remaining > 0) {
59 // On Windows, narrowing conversion from int64_t to DWORD will cause
60 // unexpected error.
61 // On MacOS, a single write() with more than kMaxInt32 will have
62 // "invalid argument" error.
63 // Therefore, limit the size for single write.
64 int64_t byte_to_write = remaining > kMaxInt32 ? kMaxInt32 : remaining;
65 int64_t bytes_written = Write(current_buffer, byte_to_write);
66 if (bytes_written < 0) {
67 return false;
68 }
69 remaining -= bytes_written; // Reduce the number of remaining bytes.
70 current_buffer += bytes_written; // Move the buffer forward.
71 }
73 intptr_t fd = GetFD();
74 const char* result = nullptr;
75 if ((fd == STDOUT_FILENO) && capture_stdout) {
77 "Stdout", "WriteEvent", reinterpret_cast<const uint8_t*>(buffer),
78 num_bytes);
79 } else if ((fd == STDERR_FILENO) && capture_stderr) {
81 "Stderr", "WriteEvent", reinterpret_cast<const uint8_t*>(buffer),
82 num_bytes);
83 }
84 ASSERT(result == nullptr);
85 }
86 return true;
87}
88
90 ASSERT((mode == File::kDartRead) || (mode == File::kDartWrite) ||
91 (mode == File::kDartAppend) || (mode == File::kDartWriteOnly) ||
93 if (mode == File::kDartWrite) {
95 }
96 if (mode == File::kDartAppend) {
97 return File::kWrite;
98 }
99 if (mode == File::kDartWriteOnly) {
101 }
102 if (mode == File::kDartWriteOnlyAppend) {
103 return File::kWriteOnly;
104 }
105 return File::kRead;
106}
107
108} // namespace bin
109} // namespace dart
int64_t Read(void *buffer, int64_t num_bytes)
bool WriteFully(const void *buffer, int64_t num_bytes)
@ kDartWriteOnlyAppend
Definition file.h:72
@ kDartWriteOnly
Definition file.h:71
bool ReadFully(void *buffer, int64_t num_bytes)
intptr_t GetFD()
bool result
Definition file.h:170
int64_t Write(const void *buffer, int64_t num_bytes)
static FileOpenMode DartModeToFileMode(DartFileOpenMode mode)
@ kWriteTruncate
Definition file.h:60
@ kWriteOnlyTruncate
Definition file.h:61
#define ASSERT(E)
static const uint8_t buffer[]
uint8_t value
void SetCaptureStdout(bool value)
bool ShouldCaptureStdout()
static bool capture_stderr
bool ShouldCaptureStderr()
static bool capture_stdout
void SetCaptureStderr(bool value)
DART_EXPORT char * Dart_ServiceSendDataEvent(const char *stream_id, const char *event_kind, const uint8_t *bytes, intptr_t bytes_length)
constexpr int32_t kMaxInt32
Definition globals.h:483