diff options
author | Dan Albert <danalbert@google.com> | 2015-02-24 21:26:58 -0800 |
---|---|---|
committer | Dan Albert <danalbert@google.com> | 2015-02-25 15:07:57 -0800 |
commit | cc731cc76786b6bdc58764aad9924c0d0c8d645f (patch) | |
tree | 57906636c803e1ae089c2fcf2905d82488b0ba72 /adb/adb_io.cpp | |
parent | a035d5003ea593322ba565df9a4f4d0dd6647acf (diff) | |
download | system_core-cc731cc76786b6bdc58764aad9924c0d0c8d645f.tar.gz system_core-cc731cc76786b6bdc58764aad9924c0d0c8d645f.tar.bz2 system_core-cc731cc76786b6bdc58764aad9924c0d0c8d645f.zip |
Test readx/writex (now renamed).
Renamed readx/writex to ReadFdExactly/WriteFdExactly respectively.
These read/write a full fixed-size buffer. If the whole buffer cannot
be read/written, these functions return an error.
Rename write_string to WriteStringFully.
Move the TEMP_FAILURE_RETRY definition in sysdeps.h out of the
!Windows section. It seems Windows won't actually interrupt a call,
but it's easier to just define it than to #ifdef each call.
Change-Id: Ia8ddffa2a52764a2f9a281c96c937660e002b9b9
Diffstat (limited to 'adb/adb_io.cpp')
-rw-r--r-- | adb/adb_io.cpp | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/adb/adb_io.cpp b/adb/adb_io.cpp new file mode 100644 index 000000000..ca208ad70 --- /dev/null +++ b/adb/adb_io.cpp @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define TRACE_TAG TRACE_RWX + +#include "sysdeps.h" +#include "adb_io.h" + +#include <unistd.h> + +#include "adb_trace.h" +#include "transport.h" + +bool ReadFdExactly(int fd, void* buf, size_t len) { + char* p = reinterpret_cast<char*>(buf); + +#if ADB_TRACE + size_t len0 = len; +#endif + + D("readx: fd=%d wanted=%zu\n", fd, len); + while (len > 0) { + int r = TEMP_FAILURE_RETRY(adb_read(fd, p, len)); + if (r > 0) { + len -= r; + p += r; + } else if (r == -1) { + D("readx: fd=%d error %d: %s\n", fd, errno, strerror(errno)); + return false; + } else { + D("readx: fd=%d disconnected\n", fd); + errno = 0; + return false; + } + } + +#if ADB_TRACE + D("readx: fd=%d wanted=%zu got=%zu\n", fd, len0, len0 - len); + if (ADB_TRACING) { + dump_hex(reinterpret_cast<const unsigned char*>(buf), len0); + } +#endif + + return true; +} + +bool WriteFdExactly(int fd, const void* buf, size_t len) { + const char* p = reinterpret_cast<const char*>(buf); + int r; + +#if ADB_TRACE + D("writex: fd=%d len=%d: ", fd, (int)len); + if (ADB_TRACING) { + dump_hex(reinterpret_cast<const unsigned char*>(buf), len); + } +#endif + + while (len > 0) { + r = TEMP_FAILURE_RETRY(adb_write(fd, p, len)); + if (r == -1) { + D("writex: fd=%d error %d: %s\n", fd, errno, strerror(errno)); + if (errno == EAGAIN) { + adb_sleep_ms(1); // just yield some cpu time + continue; + } else if (errno == EPIPE) { + D("writex: fd=%d disconnected\n", fd); + errno = 0; + return false; + } + } else { + len -= r; + p += r; + } + } + return true; +} + +bool WriteStringFully(int fd, const char* str) { + return WriteFdExactly(fd, str, strlen(str)); +} |