tmpfile_posix.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
21 #ifdef GPR_POSIX_TMPFILE
22 
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
31 
34 
35 FILE* gpr_tmpfile(const char* prefix, char** tmp_filename) {
36  FILE* result = nullptr;
37  char* filename_template;
38  int fd;
39 
40  if (tmp_filename != nullptr) *tmp_filename = nullptr;
41 
42  gpr_asprintf(&filename_template, "/tmp/%s_XXXXXX", prefix);
43  GPR_ASSERT(filename_template != nullptr);
44 
45  fd = mkstemp(filename_template);
46  if (fd == -1) {
47  gpr_log(GPR_ERROR, "mkstemp failed for filename_template %s with error %s.",
48  filename_template, strerror(errno));
49  goto end;
50  }
51  result = fdopen(fd, "w+");
52  if (result == nullptr) {
53  gpr_log(GPR_ERROR, "Could not open file %s from fd %d (error = %s).",
54  filename_template, fd, strerror(errno));
55  unlink(filename_template);
56  close(fd);
57  goto end;
58  }
59 
60 end:
61  if (result != nullptr && tmp_filename != nullptr) {
62  *tmp_filename = filename_template;
63  } else {
64  gpr_free(filename_template);
65  }
66  return result;
67 }
68 
69 #endif /* GPR_POSIX_TMPFILE */
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
log.h
string.h
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
gpr_tmpfile
FILE * gpr_tmpfile(const char *prefix, char **tmp_filename)
string_util.h
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
end
char * end
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1008
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
tmpfile.h
close
#define close
Definition: test-fs.c:48
gpr_asprintf
GPRAPI int gpr_asprintf(char **strp, const char *format,...) GPR_PRINT_FORMAT_CHECK(2
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
benchmark.FILE
FILE
Definition: benchmark.py:21
alloc.h
prefix
static const char prefix[]
Definition: head_of_line_blocking.cc:28
unlink
#define unlink
Definition: test-fs-copyfile.c:33
errno.h
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:40