-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.c
More file actions
executable file
·55 lines (47 loc) · 1004 Bytes
/
files.c
File metadata and controls
executable file
·55 lines (47 loc) · 1004 Bytes
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 "files.h"
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
int web_open(const char *path, int *length, time_t *mtime) {
assert(path);
assert(length);
assert(mtime);
const char *default_file = "index.html";
if(!path[0]) {
path = default_file;
}
struct stat sb;
int fd = open(path, O_RDONLY);
int dirfd;
if(fd == -1) {
//TODO: differentiate between errors
//ex: EACCES vs ENOMEM
return -1;
}
//check if path is directory or file
if(fstat(fd, &sb)) {
close(fd);
return -1;
}
//if path is directory, see if it contains an index.html file
if(S_ISDIR(sb.st_mode)) {
dirfd = fd;
fd = openat(dirfd, default_file, O_RDONLY);
close(dirfd);
if(fd == -1) return -1;
if(fstat(fd, &sb)) {
close(fd);
return -1;
}
}
//must get a regular file (otherwise sendfile does not work)
if(!S_ISREG(sb.st_mode)) {
close(fd);
return -1;
}
*length = (int)sb.st_size;
*mtime = sb.st_mtime;
return fd;
}