fd.cc
Go to the documentation of this file.
1 /* Copyright (c) 2020, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <openssl/base.h>
16 
17 #include <errno.h>
18 #include <limits.h>
19 #include <stdio.h>
20 
21 #include <algorithm>
22 
23 #include "internal.h"
24 
25 #if defined(OPENSSL_WINDOWS)
26 #include <io.h>
27 #else
28 #include <fcntl.h>
29 #include <unistd.h>
30 #endif
31 
32 
33 ScopedFD OpenFD(const char *path, int flags) {
34 #if defined(OPENSSL_WINDOWS)
35  return ScopedFD(_open(path, flags));
36 #else
37  int fd;
38  do {
39  fd = open(path, flags);
40  } while (fd == -1 && errno == EINTR);
41  return ScopedFD(fd);
42 #endif
43 }
44 
45 void CloseFD(int fd) {
46 #if defined(OPENSSL_WINDOWS)
47  _close(fd);
48 #else
49  close(fd);
50 #endif
51 }
52 
53 bool ReadFromFD(int fd, size_t *out_bytes_read, void *out, size_t num) {
54 #if defined(OPENSSL_WINDOWS)
55  // On Windows, the buffer must be at most |INT_MAX|. See
56  // https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/read?view=vs-2019
57  int ret = _read(fd, out, std::min(size_t{INT_MAX}, num));
58 #else
59  ssize_t ret;
60  do {
61  ret = read(fd, out, num);
62  } while (ret == -1 && errno == EINVAL);
63 #endif
64 
65  if (ret < 0) {
66  *out_bytes_read = 0;
67  return false;
68  }
69  *out_bytes_read = ret;
70  return true;
71 }
72 
73 bool WriteToFD(int fd, size_t *out_bytes_written, const void *in, size_t num) {
74 #if defined(OPENSSL_WINDOWS)
75  // The documentation for |_write| does not say the buffer must be at most
76  // |INT_MAX|, but clamp it to |INT_MAX| instead of |UINT_MAX| in case.
77  int ret = _write(fd, in, std::min(size_t{INT_MAX}, num));
78 #else
79  ssize_t ret;
80  do {
81  ret = write(fd, in, num);
82  } while (ret == -1 && errno == EINVAL);
83 #endif
84 
85  if (ret < 0) {
86  *out_bytes_written = 0;
87  return false;
88  }
89  *out_bytes_written = ret;
90  return true;
91 }
92 
93 ScopedFILE FDToFILE(ScopedFD fd, const char *mode) {
95 #if defined(OPENSSL_WINDOWS)
96  ret.reset(_fdopen(fd.get(), mode));
97 #else
98  ret.reset(fdopen(fd.get(), mode));
99 #endif
100  // |fdopen| takes ownership of |fd| on success.
101  if (ret) {
102  fd.release();
103  }
104  return ret;
105 }
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
internal.h
write
#define write
Definition: test-fs.c:47
CloseFD
void CloseFD(int fd)
Definition: fd.cc:45
WriteToFD
bool WriteToFD(int fd, size_t *out_bytes_written, const void *in, size_t num)
Definition: fd.cc:73
mode
const char int mode
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
check_documentation.path
path
Definition: check_documentation.py:57
ReadFromFD
bool ReadFromFD(int fd, size_t *out_bytes_read, void *out, size_t num)
Definition: fd.cc:53
base.h
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
ssize_t
intptr_t ssize_t
Definition: win.h:27
ScopedFD
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:47
close
#define close
Definition: test-fs.c:48
FDToFILE
ScopedFILE FDToFILE(ScopedFD fd, const char *mode)
Definition: fd.cc:93
ScopedFD::release
int release()
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:74
min
#define min(a, b)
Definition: qsort.h:83
ScopedFD::get
int get() const
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:65
ScopedFILE
std::unique_ptr< FILE, FileCloser > ScopedFILE
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:39
read
int read(izstream &zs, T *x, Items items)
Definition: bloaty/third_party/zlib/contrib/iostream2/zstream.h:115
absl::flags_internal
Definition: abseil-cpp/absl/flags/commandlineflag.h:40
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
xds_manager.num
num
Definition: xds_manager.py:56
OpenFD
ScopedFD OpenFD(const char *path, int flags)
Definition: fd.cc:33
open
#define open
Definition: test-fs.c:46
errno.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:19