-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.c
150 lines (123 loc) · 3.56 KB
/
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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#define _DEFAULT_SOURCE
#include <dirent.h>
#include <openssl/ssl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include "config.h"
#include "file.h"
extern struct gem_config cfg;
const char *pfs_kilo = "KB";
const char *pfs_mega = "MB";
const char *pfs_giga = "GB";
/* convert n bytes to a pretty file size format */
/* eg 3453554 -> "3.45 MB" */
struct pfs_data pretty_filesize(const size_t siz) {
struct pfs_data p;
if (siz >= 1000000000UL) {
p.type = pfs_giga;
p.value = (float)siz / 1000000000UL;
} else if (siz >= 1000000UL) {
p.type = pfs_mega;
p.value = (float)siz / 1000000UL;
} else if (siz >= 1000UL) {
p.type = pfs_kilo;
p.value = (float)siz / 1000UL;
} else {
p.type = NULL;
p.value = (float)siz;
}
return p;
}
/* return file size */
size_t filesize(const char *path) {
struct stat st;
return (path && stat(path, &st) == 0) ? (size_t)st.st_size : 0;
}
/* check if a file is a directory */
int file_is_dir(const char *path) {
struct stat st;
return (path && stat(path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFDIR);
}
/* check if a directory contains an index file */
/* as specified with -i flag, or default as "index.gmi" */
int dir_has_index(const char *path) {
char buf[2048];
if (!path || strlen(path) + strlen(cfg.index) + 2 >= sizeof(buf)) {
return 0;
}
strcpy(buf, path);
if (buf[strlen(buf) - 1] != '/') {
strcat(buf, "/");
}
strcat(buf, cfg.index);
return file_exists(buf);
}
/* check if file exists at all */
int file_exists(const char *path) {
struct stat st;
return (path && stat(path, &st) == 0);
}
/* read N bytes from a file */
static int file_read_n(const char *path, char *buf, const size_t siz) {
int fd;
size_t bytes;
fd = open(path, O_RDONLY);
if (fd == -1) {
fprintf(stderr, "unable to open %s\n", path);
return 1;
}
bytes = (size_t)read(fd, buf, siz);
if (bytes == (size_t)-1) {
fprintf(stderr, "err reading %s\n", path);
return 1;
}
if (close(fd) == -1) {
fprintf(stderr, "failed close file %s\n", path);
return 1;
}
return 0;
}
/* takes a path ex: "/main/en-US/hello.gmi" */
/* finds the base directory of the file */
/* ex: "/main/en-US/hello.gmi" -> "/main/en-US/" */
/* ex: "/main/asdsa.txt" -> "/main/" */
/* ex: "/index.gmi" -> "/" */
/* ex: "/" -> "/" */
/* checks if this directory contains a file (arg2) */
/* if so, writes <=n bytes of this file to a dest buffer and returns 0 */
/* if not, returns 1 */
int file_read_dir_meta(const char *path, const char *file, char *buf, const size_t bufsiz) {
char dir_buf[512]= {0};
size_t dir_length, i;
char *slash;
if (!path || !file || !buf) {
return 1;
}
if (!strcmp(path, "/") || path[strlen(path)-1] == '/') {
strncpy(dir_buf, path, 512);
strcat(dir_buf, file);
} else {
slash = strrchr(path, '/');
dir_length = (size_t)(slash - path +1);
strncpy(dir_buf, path, dir_length);
strcat(dir_buf, file);
}
/* check if file exists */
if (!file_exists(dir_buf)) {
return 1;
}
if (file_read_n(dir_buf, buf, bufsiz) != 0) {
return 1;
}
/* trim newline at the end */
for(i=bufsiz-1; i; i--) {
if (buf[i] == '\n') {
buf[i] = '\0';
}
}
return 0;
}