2 // Copyright (c) 2010-2017 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
19 #include <sys/types.h>
23 #include "prox_args.h"
24 #include "file_utils.h"
26 static char file_error_string[128] = {0};
28 const char *file_get_error(void)
30 return file_error_string;
33 __attribute__((format(printf, 1 ,2))) static void file_set_error(const char *fmt, ...)
38 vsnprintf(file_error_string, sizeof(file_error_string), fmt, ap);
42 static void resolve_path_cfg_dir(char *file_name, size_t len, const char *path)
45 snprintf(file_name, len, "%s/%s", get_cfg_dir(), path);
47 strncpy(file_name, path, len);
50 long file_get_size(const char *path)
52 char file_name[PATH_MAX];
55 resolve_path_cfg_dir(file_name, sizeof(file_name), path);
57 if (stat(file_name, &s)) {
58 file_set_error("Stat failed on '%s': %s", path, strerror(errno));
62 if ((s.st_mode & S_IFMT) != S_IFREG) {
63 snprintf(file_error_string, sizeof(file_error_string), "'%s' is not a file", path);
70 int file_read_content(const char *path, uint8_t *mem, size_t beg, size_t len)
72 char file_name[PATH_MAX];
75 resolve_path_cfg_dir(file_name, sizeof(file_name), path);
76 f = fopen(file_name, "r");
78 file_set_error("Failed to read '%s': %s", path, strerror(errno));
82 fseek(f, beg, SEEK_SET);
84 size_t ret = fread(mem, 1, len, f);
85 if ((uint32_t)ret != len) {
86 file_set_error("Failed to read '%s:%zu' for %zu bytes: got %zu\n", file_name, beg, len, ret);