-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_file.c
55 lines (45 loc) · 1.55 KB
/
read_file.c
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
52
53
54
55
#include "main.h"
/**
* read_file - read the content of the requested file
* @file_req: a pointer to a struct file_request_t that holds the path and the client information
*
* Return: a string containing the content of the file to be read
*/
char* read_file(file_request_t *file_req) {
char *path = file_req->path;
uv_fs_t *open_req = (uv_fs_t *)malloc(sizeof(uv_fs_t));
uv_fs_open(uv_default_loop(), open_req, path, O_RDONLY, 0, NULL);
if (open_req->result < 0)
{
fprintf(stderr, "Error opening file: %s\n", uv_strerror(open_req->result));
free(open_req);
return NULL;
}
uv_fs_t *stat_req = (uv_fs_t *)malloc(sizeof(uv_fs_t));
uv_fs_fstat(uv_default_loop(), stat_req, open_req->result, NULL);
if (stat_req->result < 0)
{
fprintf(stderr, "Error getting file info: %s\n", uv_strerror(stat_req->result));
free(open_req);
free(stat_req);
return NULL;
}
int size = stat_req->statbuf.st_size;
char *buf = (char *)malloc(size);
uv_buf_t iov = uv_buf_init(buf, size);
uv_fs_t *read_req = (uv_fs_t *)malloc(sizeof(uv_fs_t));
uv_fs_read(uv_default_loop(), read_req, open_req->result, &iov, 1, 0, NULL);
if (read_req->result < 0)
{
fprintf(stderr, "Error reading file: %s\n", uv_strerror(read_req->result));
free(open_req);
free(stat_req);
free(buf);
free(read_req);
return NULL;
}
uv_fs_req_cleanup(open_req);
uv_fs_req_cleanup(stat_req);
uv_fs_req_cleanup(read_req);
return buf;
}