Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Craig Tiller | 8517886 | 2016-05-18 16:09:16 -0700 | [diff] [blame] | 34 | #include "src/core/lib/iomgr/load_file.h" |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 35 | |
| 36 | #include <errno.h> |
| 37 | #include <string.h> |
| 38 | |
| 39 | #include <grpc/support/alloc.h> |
| 40 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 41 | #include <grpc/support/string_util.h> |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 42 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 43 | #include "src/core/lib/support/block_annotate.h" |
| 44 | #include "src/core/lib/support/string.h" |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 45 | |
Craig Tiller | 8517886 | 2016-05-18 16:09:16 -0700 | [diff] [blame] | 46 | grpc_error *grpc_load_file(const char *filename, int add_null_terminator, |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 47 | grpc_slice *output) { |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 48 | unsigned char *contents = NULL; |
| 49 | size_t contents_size = 0; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 50 | grpc_slice result = grpc_empty_slice(); |
vjpai | 7e0289e | 2015-09-24 16:59:07 -0700 | [diff] [blame] | 51 | FILE *file; |
Julien Boeuf | 9d005cc | 2015-04-16 09:50:13 -0700 | [diff] [blame] | 52 | size_t bytes_read = 0; |
Craig Tiller | 4727b9b | 2016-05-17 17:19:19 -0700 | [diff] [blame] | 53 | grpc_error *error = GRPC_ERROR_NONE; |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 54 | |
vjpai | 9839d28 | 2015-09-24 17:55:18 -0700 | [diff] [blame] | 55 | GRPC_SCHEDULING_START_BLOCKING_REGION; |
vjpai | 7e0289e | 2015-09-24 16:59:07 -0700 | [diff] [blame] | 56 | file = fopen(filename, "rb"); |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 57 | if (file == NULL) { |
Craig Tiller | 4727b9b | 2016-05-17 17:19:19 -0700 | [diff] [blame] | 58 | error = GRPC_OS_ERROR(errno, "fopen"); |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 59 | goto end; |
| 60 | } |
Julien Boeuf | 9d005cc | 2015-04-16 09:50:13 -0700 | [diff] [blame] | 61 | fseek(file, 0, SEEK_END); |
murgatroid99 | 5e71d7a | 2015-06-19 12:24:44 -0700 | [diff] [blame] | 62 | /* Converting to size_t on the assumption that it will not fail */ |
| 63 | contents_size = (size_t)ftell(file); |
Julien Boeuf | 9d005cc | 2015-04-16 09:50:13 -0700 | [diff] [blame] | 64 | fseek(file, 0, SEEK_SET); |
| 65 | contents = gpr_malloc(contents_size + (add_null_terminator ? 1 : 0)); |
| 66 | bytes_read = fread(contents, 1, contents_size, file); |
| 67 | if (bytes_read < contents_size) { |
Craig Tiller | 4727b9b | 2016-05-17 17:19:19 -0700 | [diff] [blame] | 68 | error = GRPC_OS_ERROR(errno, "fread"); |
Julien Boeuf | 9d005cc | 2015-04-16 09:50:13 -0700 | [diff] [blame] | 69 | GPR_ASSERT(ferror(file)); |
Julien Boeuf | 9d005cc | 2015-04-16 09:50:13 -0700 | [diff] [blame] | 70 | goto end; |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 71 | } |
Julien Boeuf | 28d75d9 | 2015-04-15 15:52:23 -0700 | [diff] [blame] | 72 | if (add_null_terminator) { |
Julien Boeuf | 28d75d9 | 2015-04-15 15:52:23 -0700 | [diff] [blame] | 73 | contents[contents_size++] = 0; |
| 74 | } |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 75 | result = grpc_slice_new(contents, contents_size, gpr_free); |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 76 | |
| 77 | end: |
Craig Tiller | 4727b9b | 2016-05-17 17:19:19 -0700 | [diff] [blame] | 78 | *output = result; |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 79 | if (file != NULL) fclose(file); |
Craig Tiller | 4727b9b | 2016-05-17 17:19:19 -0700 | [diff] [blame] | 80 | if (error != GRPC_ERROR_NONE) { |
| 81 | grpc_error *error_out = grpc_error_set_str( |
| 82 | GRPC_ERROR_CREATE_REFERENCING("Failed to load file", &error, 1), |
| 83 | GRPC_ERROR_STR_FILENAME, filename); |
| 84 | GRPC_ERROR_UNREF(error); |
| 85 | error = error_out; |
| 86 | } |
vjpai | 9839d28 | 2015-09-24 17:55:18 -0700 | [diff] [blame] | 87 | GRPC_SCHEDULING_END_BLOCKING_REGION; |
Craig Tiller | 4727b9b | 2016-05-17 17:19:19 -0700 | [diff] [blame] | 88 | return error; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 89 | } |