summaryrefslogtreecommitdiff
path: root/tests/helpers/file.c
blob: 53f4f12dc6c8b001d1d49622207e9ba697f1110d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* SPDX-License-Identifier: GPL-2.0-only */

#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

/*
 * Get the file size of a given file
 *
 * @params fname  name of the file relative to the __TEST_DATA_DIR__ directory
 *
 * @return  On success file size in bytes is returned. On failure -1 is returned.
 */
int test_get_file_size(const char *fname)
{
	char path[strlen(__TEST_DATA_DIR__) + strlen(fname) + 2];
	sprintf(path, "%s/%s", __TEST_DATA_DIR__, fname);

	struct stat st;
	if (stat(path, &st) == -1)
		return -1;
	return st.st_size;
}

/*
 * Read a file and write its contents into a buffer
 *
 * @params fname  name of the file relative to the __TEST_DATA_DIR__ directory
 * @params buf    buffer to write file contents into
 * @params size   size of buf
 *
 * @return  On success number of bytes read is returned. On failure -1 is returned.
 */
int test_read_file(const char *fname, uint8_t *buf, size_t size)
{
	char path[strlen(__TEST_DATA_DIR__) + strlen(fname) + 2];
	sprintf(path, "%s/%s", __TEST_DATA_DIR__, fname);

	int f = open(path, O_RDONLY);
	if (f == -1)
		return -1;

	int read_size = read(f, buf, size);

	close(f);
	return read_size;
}