1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-12-11 21:13:00 +01:00
libcproject/lib/filesystem.c

90 lines
2.2 KiB
C
Raw Normal View History

2023-01-05 19:28:05 +01:00
#include "filesystem.h"
int filesystem_read(string_t path, byte_t **file_content, off_t *file_size) {
2023-01-05 19:28:05 +01:00
int file_descriptor = open(path, O_RDONLY);
if (file_descriptor == -1) {
return -1;
}
(*file_size) = lseek(file_descriptor, 0, SEEK_END);
lseek(file_descriptor, 0, SEEK_SET);
(*file_content) = malloc(*file_size);
if (read(file_descriptor, *file_content, *file_size) == -1) {
return -1;
}
2023-01-05 19:28:05 +01:00
close(file_descriptor);
return 0;
}
int filesystem_write(string_t path, byte_t *file_content, off_t file_size) {
2023-01-05 19:28:05 +01:00
int file_descriptor = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (file_descriptor == -1) {
return -1;
}
if (write(file_descriptor, file_content, file_size) == -1) {
return -1;
}
2023-01-05 19:28:05 +01:00
close(file_descriptor);
return 0;
}
2023-06-25 15:17:46 +02:00
string_t filesystem_get_mimetype(string_t path) {
2023-01-05 19:28:05 +01:00
if (string_ends_with(path, ".html")) {
return "text/html";
}
if (string_ends_with(path, ".css")) {
return "text/css";
}
if (string_ends_with(path, ".js")) {
return "text/javascript";
}
if (string_ends_with(path, ".png")) {
return "image/png";
}
if (string_ends_with(path, ".jpg") || string_ends_with(path, ".jpeg")) {
return "image/jpeg";
}
if (string_ends_with(path, ".gif")) {
return "image/gif";
}
if (string_ends_with(path, ".ico")) {
return "image/x-icon";
}
if (string_ends_with(path, ".svg")) {
return "image/svg+xml";
}
if (string_ends_with(path, ".json")) {
return "application/json";
}
if (string_ends_with(path, ".pdf")) {
return "application/pdf";
}
if (string_ends_with(path, ".xml")) {
return "application/xml";
}
if (string_ends_with(path, ".csv")) {
return "text/csv";
}
if (string_ends_with(path, ".zip")) {
return "application/zip";
}
if (string_ends_with(path, ".tar")) {
return "application/x-tar";
}
if (string_ends_with(path, ".gz")) {
return "application/x-gzip";
}
if (string_ends_with(path, ".mp3")) {
return "audio/mpeg";
}
if (string_ends_with(path, ".wav")) {
return "audio/wav";
}
if (string_ends_with(path, ".mp4")) {
return "video/mp4";
}
if (string_ends_with(path, ".bin")) {
return "application/octet-stream";
}
2023-01-05 19:28:05 +01:00
return "text/plain";
}