acpi: Add IORT helper functions
[coreboot2.git] / tests / helpers / file.c
blob53f4f12dc6c8b001d1d49622207e9ba697f1110d
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <fcntl.h>
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <unistd.h>
12 * Get the file size of a given file
14 * @params fname name of the file relative to the __TEST_DATA_DIR__ directory
16 * @return On success file size in bytes is returned. On failure -1 is returned.
18 int test_get_file_size(const char *fname)
20 char path[strlen(__TEST_DATA_DIR__) + strlen(fname) + 2];
21 sprintf(path, "%s/%s", __TEST_DATA_DIR__, fname);
23 struct stat st;
24 if (stat(path, &st) == -1)
25 return -1;
26 return st.st_size;
30 * Read a file and write its contents into a buffer
32 * @params fname name of the file relative to the __TEST_DATA_DIR__ directory
33 * @params buf buffer to write file contents into
34 * @params size size of buf
36 * @return On success number of bytes read is returned. On failure -1 is returned.
38 int test_read_file(const char *fname, uint8_t *buf, size_t size)
40 char path[strlen(__TEST_DATA_DIR__) + strlen(fname) + 2];
41 sprintf(path, "%s/%s", __TEST_DATA_DIR__, fname);
43 int f = open(path, O_RDONLY);
44 if (f == -1)
45 return -1;
47 int read_size = read(f, buf, size);
49 close(f);
50 return read_size;