LCOV - code coverage report
Current view: top level - src/core/support - file.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 28 33 84.8 %
Date: 2015-10-10 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, Google Inc.
       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             : 
      34             : #include "src/core/support/file.h"
      35             : 
      36             : #include <errno.h>
      37             : #include <string.h>
      38             : 
      39             : #include <grpc/support/alloc.h>
      40             : #include <grpc/support/log.h>
      41             : #include <grpc/support/string_util.h>
      42             : 
      43             : #include "src/core/support/block_annotate.h"
      44             : #include "src/core/support/string.h"
      45             : 
      46          97 : gpr_slice gpr_load_file(const char *filename, int add_null_terminator,
      47             :                         int *success) {
      48          97 :   unsigned char *contents = NULL;
      49          97 :   size_t contents_size = 0;
      50          97 :   char *error_msg = NULL;
      51          97 :   gpr_slice result = gpr_empty_slice();
      52             :   FILE *file;
      53          97 :   size_t bytes_read = 0;
      54             : 
      55             :   GRPC_SCHEDULING_START_BLOCKING_REGION;
      56          97 :   file = fopen(filename, "rb");
      57          97 :   if (file == NULL) {
      58           1 :     gpr_asprintf(&error_msg, "Could not open file %s (error = %s).", filename,
      59           1 :                  strerror(errno));
      60           1 :     GPR_ASSERT(error_msg != NULL);
      61           1 :     goto end;
      62             :   }
      63          96 :   fseek(file, 0, SEEK_END);
      64             :   /* Converting to size_t on the assumption that it will not fail */
      65          96 :   contents_size = (size_t)ftell(file);
      66          96 :   fseek(file, 0, SEEK_SET);
      67          96 :   contents = gpr_malloc(contents_size + (add_null_terminator ? 1 : 0));
      68          96 :   bytes_read = fread(contents, 1, contents_size, file);
      69          96 :   if (bytes_read < contents_size) {
      70           0 :     GPR_ASSERT(ferror(file));
      71           0 :     gpr_asprintf(&error_msg, "Error %s occured while reading file %s.",
      72           0 :                  strerror(errno), filename);
      73           0 :     GPR_ASSERT(error_msg != NULL);
      74           0 :     goto end;
      75             :   }
      76          96 :   if (success != NULL) *success = 1;
      77          96 :   if (add_null_terminator) {
      78           2 :     contents[contents_size++] = 0;
      79             :   }
      80          96 :   result = gpr_slice_new(contents, contents_size, gpr_free);
      81             : 
      82             : end:
      83          97 :   if (error_msg != NULL) {
      84           1 :     gpr_log(GPR_ERROR, "%s", error_msg);
      85           1 :     gpr_free(error_msg);
      86           1 :     if (success != NULL) *success = 0;
      87             :   }
      88          97 :   if (file != NULL) fclose(file);
      89             :   GRPC_SCHEDULING_END_BLOCKING_REGION;
      90          97 :   return result;
      91             : }

Generated by: LCOV version 1.10