blob: 650bd62ccbe0e039deaa0e7873a3ecb5df49a65c [file] [log] [blame]
Julien Boeuf026a4172015-02-02 18:36:37 -08001/*
2 *
Craig Tiller19fa5402016-02-23 08:19:39 -08003 * Copyright 2015-2016, Google Inc.
Julien Boeuf026a4172015-02-02 18:36:37 -08004 * 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 Tiller732a8752016-02-22 15:59:19 -080034#include "src/core/support/load_file.h"
Julien Boeuf026a4172015-02-02 18:36:37 -080035
36#include <errno.h>
37#include <string.h>
38
39#include <grpc/support/alloc.h>
40#include <grpc/support/log.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070041#include <grpc/support/string_util.h>
Julien Boeuf026a4172015-02-02 18:36:37 -080042
vjpai9839d282015-09-24 17:55:18 -070043#include "src/core/support/block_annotate.h"
Julien Boeuf026a4172015-02-02 18:36:37 -080044#include "src/core/support/string.h"
45
Julien Boeuf28d75d92015-04-15 15:52:23 -070046gpr_slice gpr_load_file(const char *filename, int add_null_terminator,
47 int *success) {
Julien Boeuf026a4172015-02-02 18:36:37 -080048 unsigned char *contents = NULL;
49 size_t contents_size = 0;
Julien Boeuf026a4172015-02-02 18:36:37 -080050 char *error_msg = NULL;
51 gpr_slice result = gpr_empty_slice();
vjpai7e0289e2015-09-24 16:59:07 -070052 FILE *file;
Julien Boeuf9d005cc2015-04-16 09:50:13 -070053 size_t bytes_read = 0;
Julien Boeuf026a4172015-02-02 18:36:37 -080054
vjpai9839d282015-09-24 17:55:18 -070055 GRPC_SCHEDULING_START_BLOCKING_REGION;
vjpai7e0289e2015-09-24 16:59:07 -070056 file = fopen(filename, "rb");
Julien Boeuf026a4172015-02-02 18:36:37 -080057 if (file == NULL) {
58 gpr_asprintf(&error_msg, "Could not open file %s (error = %s).", filename,
59 strerror(errno));
60 GPR_ASSERT(error_msg != NULL);
61 goto end;
62 }
Julien Boeuf9d005cc2015-04-16 09:50:13 -070063 fseek(file, 0, SEEK_END);
murgatroid995e71d7a2015-06-19 12:24:44 -070064 /* Converting to size_t on the assumption that it will not fail */
65 contents_size = (size_t)ftell(file);
Julien Boeuf9d005cc2015-04-16 09:50:13 -070066 fseek(file, 0, SEEK_SET);
67 contents = gpr_malloc(contents_size + (add_null_terminator ? 1 : 0));
68 bytes_read = fread(contents, 1, contents_size, file);
69 if (bytes_read < contents_size) {
70 GPR_ASSERT(ferror(file));
71 gpr_asprintf(&error_msg, "Error %s occured while reading file %s.",
72 strerror(errno), filename);
73 GPR_ASSERT(error_msg != NULL);
74 goto end;
Julien Boeuf026a4172015-02-02 18:36:37 -080075 }
76 if (success != NULL) *success = 1;
Julien Boeuf28d75d92015-04-15 15:52:23 -070077 if (add_null_terminator) {
Julien Boeuf28d75d92015-04-15 15:52:23 -070078 contents[contents_size++] = 0;
79 }
Julien Boeuf026a4172015-02-02 18:36:37 -080080 result = gpr_slice_new(contents, contents_size, gpr_free);
81
82end:
83 if (error_msg != NULL) {
84 gpr_log(GPR_ERROR, "%s", error_msg);
85 gpr_free(error_msg);
86 if (success != NULL) *success = 0;
87 }
88 if (file != NULL) fclose(file);
vjpai9839d282015-09-24 17:55:18 -070089 GRPC_SCHEDULING_END_BLOCKING_REGION;
Julien Boeuf026a4172015-02-02 18:36:37 -080090 return result;
Craig Tiller190d3602015-02-18 09:23:38 -080091}