Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause 2 : : * Copyright (C) 2015 Intel Corporation. 3 : : * All rights reserved. 4 : : */ 5 : : 6 : : #include "spdk/file.h" 7 : : 8 : : void * 9 : 1158 : spdk_posix_file_load(FILE *file, size_t *size) 10 : : { 11 : 1158 : uint8_t *newbuf, *buf = NULL; 12 : 1158 : size_t rc, buf_size, cur_size = 0; 13 : : 14 : 1158 : *size = 0; 15 : 1158 : buf_size = 128 * 1024; 16 : : 17 [ + - ]: 1158 : while (buf_size <= 1024 * 1024 * 1024) { 18 : 1158 : newbuf = realloc(buf, buf_size); 19 [ - + ]: 1158 : if (newbuf == NULL) { 20 : 0 : free(buf); 21 : 0 : return NULL; 22 : : } 23 : 1158 : buf = newbuf; 24 : : 25 : 1158 : rc = fread(buf + cur_size, 1, buf_size - cur_size, file); 26 : 1158 : cur_size += rc; 27 : : 28 [ + - ]: 1158 : if (feof(file)) { 29 : 1158 : *size = cur_size; 30 : 1158 : return buf; 31 : : } 32 : : 33 [ # # ]: 0 : if (ferror(file)) { 34 : 0 : free(buf); 35 : 0 : return NULL; 36 : : } 37 : : 38 : 0 : buf_size *= 2; 39 : : } 40 : : 41 : 0 : free(buf); 42 : 0 : return NULL; 43 : : } 44 : : 45 : : void * 46 : 1159 : spdk_posix_file_load_from_name(const char *file_name, size_t *size) 47 : : { 48 : 1159 : FILE *file = fopen(file_name, "r"); 49 : : void *data; 50 : : 51 [ + + ]: 1159 : if (file == NULL) { 52 : 1 : return NULL; 53 : : } 54 : : 55 : 1158 : data = spdk_posix_file_load(file, size); 56 : 1158 : fclose(file); 57 : : 58 : 1158 : return data; 59 : : }