#include <linux/err.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/types.h>
Go to the source code of this file.
|
| int | _read_file (const char *path, char **out_buf) |
| |
| int | _write_file (const char *path, const char *buf, size_t len) |
| |
| void | build_cfg_path (const char *fname, char *out, size_t sz) |
| |
| char * | read_file (char *filename, int *readed_size) |
| |
◆ _read_file()
| int _read_file |
( |
const char * |
path, |
|
|
char ** |
out_buf |
|
) |
| |
_read_file - Reads the contents of a file into a dynamically allocated buffer.
- Parameters
-
| path | Path to the file to be read. |
| out_buf | Pointer to a char pointer that will receive the allocated buffer containing the file contents. Return: On success, returns the number of bytes read (excluding the null terminator). On failure, returns a negative error code. |
Definition at line 12 of file io.c.
12 {
13 size_t tot = 0;
16 loff_t pos = 0;
17 char *buf;
18 int ret;
19
20 buf = kzalloc(buf_size, GFP_KERNEL);
21 if (!buf)
22 return -ENOMEM;
23
24 f = filp_open(path, O_RDONLY, 0);
25 if (IS_ERR(f)) {
26 ret = PTR_ERR(f);
27 kfree(buf);
28 return ret;
29 }
30
31
32 while ((ret = kernel_read(f, buf + tot, buf_size - tot - 1, &pos)) > 0) {
33 tot += ret;
34
35 if (tot >= buf_size - 1) {
36 size_t new_size = buf_size * 2;
37 char *resized = krealloc(buf, new_size, GFP_KERNEL);
38 if (!resized) {
39 kfree(buf);
40 filp_close(f, NULL);
41 return -ENOMEM;
42 }
43
44 buf = resized;
45 buf_size = new_size;
46 }
47 }
48
49 filp_close(f, NULL);
50 if (ret < 0) {
51 kfree(buf);
52 return ret;
53 }
54
55 buf[tot] = '\0';
56 *out_buf = buf;
57 return tot;
58}
static struct dentry * file
◆ _write_file()
| int _write_file |
( |
const char * |
path, |
|
|
const char * |
buf, |
|
|
size_t |
len |
|
) |
| |
_write_file - Write a buffer to a file in the kernel space.
- Parameters
-
| path | Path to the file to write. |
| buf | Pointer to the buffer containing data to write. |
| len | Number of bytes to write from the buffer. Return: Number of bytes written on success, or a negative error code on failure. |
Definition at line 67 of file io.c.
67 {
69 loff_t pos = 0;
70 int ret;
71
72 f = filp_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
73 if (IS_ERR(f))
74 return PTR_ERR(f);
75
76 ret = kernel_write(f, buf, len, &pos);
77 filp_close(f, NULL);
78 return ret;
79}
◆ build_cfg_path()
| void build_cfg_path |
( |
const char * |
fname, |
|
|
char * |
out, |
|
|
size_t |
sz |
|
) |
| |
build_cfg_path - Constructs a full path for a configuration file in the hidden directory.
- Parameters
-
| fname | Name of the configuration file. |
| out | Output buffer to store the full path. |
| sz | Size of the output buffer. |
Definition at line 87 of file io.c.
◆ read_file()
| char * read_file |
( |
char * |
filename, |
|
|
int * |
readed_size |
|
) |
| |