-
Notifications
You must be signed in to change notification settings - Fork 1
/
local.c
113 lines (96 loc) · 2.88 KB
/
local.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "pch.h"
#include "state.h"
#include "local.h"
#include "uri.h"
#include "tui.h"
int
local_request(
const struct uri *restrict uri,
int *restrict n_dirents)
{
// Get file path
static char path[FILENAME_MAX];
uri_str(uri, path, sizeof(path), URI_FLAGS_NO_PROTOCOL_BIT);
// Check what type of file the URI is
struct stat path_stat;
if (stat(path, &path_stat) < 0)
{
tui_status_say("No such file or directory");
return -1;
}
bool is_dir = path_stat.st_mode & S_IFDIR;
*n_dirents = 0;
if (!is_dir)
{
// Regular file
FILE *file = fopen(path, "r");
if (!file)
{
tui_status_say("Failed to open local file");
return -1;
}
tui_status_begin();
tui_printf("Loading local file %s", path);
tui_status_end();
// Read size of file and resize buffer if needed
fseek(file, 0, SEEK_END);
size_t len = ftell(file);
recv_buffer_check_size(len);
g_recv->size = len;
// Read file
fseek(file, 0, SEEK_SET);
bool success = fread(g_recv->b, len, 1, file) > 0;
fclose(file);
if (!success)
{
return -1;
}
// Set MIME type to gemtext for now
// TODO: determine file mimetypes
mime_parse(&g_recv->mime, MIME_GEMTEXT, strlen(MIME_GEMTEXT));
}
else
{
// Read directory and create listing
DIR *dir = opendir(path);
if (!dir)
{
tui_status_say("failed to open directory");
return -1;
}
size_t max_len = FILENAME_MAX + 128;
char *buf_tmp = malloc(max_len);
size_t bytes;
bytes = snprintf(buf_tmp, max_len,
"# Index of %s\n"
"\n"
"=> ..\n",
path);
recv_buffer_check_size(bytes);
strncpy(g_recv->b, buf_tmp, bytes);
g_recv->size = bytes;
// Iterate over the directory and generate a gemtext file of the
// directory listing
*n_dirents = 1;
for (struct dirent *entry = readdir(dir);
entry;
entry = readdir(dir))
{
// Skip '.' and '..'; we add '..' manually so it's always at top
if (strcmp(entry->d_name, ".") == 0 ||
strcmp(entry->d_name, "..") == 0) continue;
bytes = snprintf(buf_tmp, max_len,
"=> %s\n",
entry->d_name);
recv_buffer_check_size(g_recv->size + bytes);
strncpy(g_recv->b + g_recv->size, buf_tmp, bytes);
g_recv->size += bytes;
++*n_dirents;
}
(void)closedir(dir);
free(buf_tmp);
// Set MIME type to gemtext (as our directory listing is in gemtext)
mime_parse(&g_recv->mime, MIME_GEMTEXT, strlen(MIME_GEMTEXT));
}
return 0;
}