load_file.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 
22 
23 #include <errno.h>
24 #include <string.h>
25 
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
29 
32 
33 grpc_error_handle grpc_load_file(const char* filename, int add_null_terminator,
34  grpc_slice* output) {
35  unsigned char* contents = nullptr;
36  size_t contents_size = 0;
38  FILE* file;
39  size_t bytes_read = 0;
41 
43  file = fopen(filename, "rb");
44  if (file == nullptr) {
45  error = GRPC_OS_ERROR(errno, "fopen");
46  goto end;
47  }
48  fseek(file, 0, SEEK_END);
49  /* Converting to size_t on the assumption that it will not fail */
50  contents_size = static_cast<size_t>(ftell(file));
51  fseek(file, 0, SEEK_SET);
52  contents = static_cast<unsigned char*>(
53  gpr_malloc(contents_size + (add_null_terminator ? 1 : 0)));
54  bytes_read = fread(contents, 1, contents_size, file);
55  if (bytes_read < contents_size) {
57  error = GRPC_OS_ERROR(errno, "fread");
58  GPR_ASSERT(ferror(file));
59  goto end;
60  }
61  if (add_null_terminator) {
62  contents[contents_size++] = 0;
63  }
64  result = grpc_slice_new(contents, contents_size, gpr_free);
65 
66 end:
67  *output = result;
68  if (file != nullptr) fclose(file);
69  if (!GRPC_ERROR_IS_NONE(error)) {
70  grpc_error_handle error_out =
72  "Failed to load file", &error, 1),
74 
75  filename);
77  error = error_out;
78  }
80  return error;
81 }
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
block_annotate.h
filename
const char * filename
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
SEEK_END
#define SEEK_END
Definition: bloaty/third_party/zlib/contrib/minizip/zip.c:84
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
log.h
grpc_load_file
grpc_error_handle grpc_load_file(const char *filename, int add_null_terminator, grpc_slice *output)
Definition: load_file.cc:33
load_file.h
file
const grpc_generator::File * file
Definition: python_private_generator.h:38
grpc_slice_new
GPRAPI grpc_slice grpc_slice_new(void *p, size_t len, void(*destroy)(void *))
Definition: slice/slice.cc:103
string.h
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
error
grpc_error_handle error
Definition: retry_filter.cc:499
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING
#define GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(desc, errs, count)
Definition: error.h:307
grpc_error_set_str
grpc_error_handle grpc_error_set_str(grpc_error_handle src, grpc_error_strs which, absl::string_view str)
Definition: error.cc:650
string_util.h
GRPC_OS_ERROR
#define GRPC_OS_ERROR(err, call_name)
create an error associated with errno!=0 (an 'operating system' error)
Definition: error.h:352
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
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
bytes_read
static size_t bytes_read
Definition: test-ipc-heavy-traffic-deadlock-bug.c:47
grpc_empty_slice
GPRAPI grpc_slice grpc_empty_slice(void)
Definition: slice/slice.cc:42
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
GRPC_SCHEDULING_END_BLOCKING_REGION_NO_EXEC_CTX
#define GRPC_SCHEDULING_END_BLOCKING_REGION_NO_EXEC_CTX
Definition: block_annotate.h:52
GRPC_ERROR_STR_FILENAME
@ GRPC_ERROR_STR_FILENAME
filename that we were trying to read/write when this error occurred
Definition: error.h:129
contents
string_view contents
Definition: elf.cc:597
benchmark.FILE
FILE
Definition: benchmark.py:21
grpc::fclose
fclose(creds_file)
alloc.h
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
grpc_error
Definition: error_internal.h:42
SEEK_SET
#define SEEK_SET
Definition: bloaty/third_party/zlib/contrib/minizip/zip.c:88
errno.h
GRPC_SCHEDULING_START_BLOCKING_REGION
#define GRPC_SCHEDULING_START_BLOCKING_REGION
Definition: block_annotate.h:45
GRPC_ERROR_IS_NONE
#define GRPC_ERROR_IS_NONE(err)
Definition: error.h:241
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:29