• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "mojo/edk/test/test_utils.h"
6 
7 #include <fcntl.h>
8 #include <stddef.h>
9 #include <unistd.h>
10 
11 #include "base/posix/eintr_wrapper.h"
12 
13 namespace mojo {
14 namespace edk {
15 namespace test {
16 
BlockingWrite(const PlatformHandle & handle,const void * buffer,size_t bytes_to_write,size_t * bytes_written)17 bool BlockingWrite(const PlatformHandle& handle,
18                    const void* buffer,
19                    size_t bytes_to_write,
20                    size_t* bytes_written) {
21   int original_flags = fcntl(handle.handle, F_GETFL);
22   if (original_flags == -1 ||
23       fcntl(handle.handle, F_SETFL, original_flags & (~O_NONBLOCK)) != 0) {
24     return false;
25   }
26 
27   ssize_t result = HANDLE_EINTR(write(handle.handle, buffer, bytes_to_write));
28 
29   fcntl(handle.handle, F_SETFL, original_flags);
30 
31   if (result < 0)
32     return false;
33 
34   *bytes_written = result;
35   return true;
36 }
37 
BlockingRead(const PlatformHandle & handle,void * buffer,size_t buffer_size,size_t * bytes_read)38 bool BlockingRead(const PlatformHandle& handle,
39                   void* buffer,
40                   size_t buffer_size,
41                   size_t* bytes_read) {
42   int original_flags = fcntl(handle.handle, F_GETFL);
43   if (original_flags == -1 ||
44       fcntl(handle.handle, F_SETFL, original_flags & (~O_NONBLOCK)) != 0) {
45     return false;
46   }
47 
48   ssize_t result = HANDLE_EINTR(read(handle.handle, buffer, buffer_size));
49 
50   fcntl(handle.handle, F_SETFL, original_flags);
51 
52   if (result < 0)
53     return false;
54 
55   *bytes_read = result;
56   return true;
57 }
58 
NonBlockingRead(const PlatformHandle & handle,void * buffer,size_t buffer_size,size_t * bytes_read)59 bool NonBlockingRead(const PlatformHandle& handle,
60                      void* buffer,
61                      size_t buffer_size,
62                      size_t* bytes_read) {
63   ssize_t result = HANDLE_EINTR(read(handle.handle, buffer, buffer_size));
64 
65   if (result < 0) {
66     if (errno != EAGAIN && errno != EWOULDBLOCK)
67       return false;
68 
69     *bytes_read = 0;
70   } else {
71     *bytes_read = result;
72   }
73 
74   return true;
75 }
76 
PlatformHandleFromFILE(base::ScopedFILE fp)77 ScopedPlatformHandle PlatformHandleFromFILE(base::ScopedFILE fp) {
78   CHECK(fp);
79   int rv = dup(fileno(fp.get()));
80   PCHECK(rv != -1) << "dup";
81   return ScopedPlatformHandle(PlatformHandle(rv));
82 }
83 
FILEFromPlatformHandle(ScopedPlatformHandle h,const char * mode)84 base::ScopedFILE FILEFromPlatformHandle(ScopedPlatformHandle h,
85                                         const char* mode) {
86   CHECK(h.is_valid());
87   base::ScopedFILE rv(fdopen(h.release().handle, mode));
88   PCHECK(rv) << "fdopen";
89   return rv;
90 }
91 
92 }  // namespace test
93 }  // namespace edk
94 }  // namespace mojo
95