|
| 1 | +/*-*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | + |
| 3 | +/* Monkey HTTP Server |
| 4 | + * ================== |
| 5 | + * Copyright 2001-2015 Monkey Software LLC <eduardo@monkey.io> |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#define _GNU_SOURCE |
| 21 | + |
| 22 | +#include <sys/types.h> |
| 23 | +#include <sys/stat.h> |
| 24 | +#include <fcntl.h> |
| 25 | +#include <unistd.h> |
| 26 | +#include <stdio.h> |
| 27 | +#include <stdlib.h> |
| 28 | +#include <errno.h> |
| 29 | + |
| 30 | +#include "mk_core.h" |
| 31 | + |
| 32 | +int mk_file_get_info(const char *path, struct file_info *f_info, int mode) |
| 33 | +{ |
| 34 | + struct stat f, target; |
| 35 | + |
| 36 | + f_info->exists = MK_FALSE; |
| 37 | + |
| 38 | + /* Stat right resource */ |
| 39 | + if (lstat(path, &f) == -1) { |
| 40 | + if (errno == EACCES) { |
| 41 | + f_info->exists = MK_TRUE; |
| 42 | + } |
| 43 | + return -1; |
| 44 | + } |
| 45 | + |
| 46 | + f_info->exists = MK_TRUE; |
| 47 | + f_info->is_file = MK_TRUE; |
| 48 | + f_info->is_link = MK_FALSE; |
| 49 | + f_info->is_directory = MK_FALSE; |
| 50 | + f_info->exec_access = MK_FALSE; |
| 51 | + f_info->read_access = MK_FALSE; |
| 52 | + |
| 53 | + if (S_ISLNK(f.st_mode)) { |
| 54 | + f_info->is_link = MK_TRUE; |
| 55 | + f_info->is_file = MK_FALSE; |
| 56 | + if (stat(path, &target) == -1) { |
| 57 | + return -1; |
| 58 | + } |
| 59 | + } |
| 60 | + else { |
| 61 | + target = f; |
| 62 | + } |
| 63 | + |
| 64 | + f_info->size = target.st_size; |
| 65 | + f_info->last_modification = target.st_mtime; |
| 66 | + |
| 67 | + if (S_ISDIR(target.st_mode)) { |
| 68 | + f_info->is_directory = MK_TRUE; |
| 69 | + f_info->is_file = MK_FALSE; |
| 70 | + } |
| 71 | + |
| 72 | + /* Check read access */ |
| 73 | + if (mode & MK_FILE_READ) { |
| 74 | + if (((target.st_mode & S_IRUSR) && target.st_uid == EUID) || |
| 75 | + ((target.st_mode & S_IRGRP) && target.st_gid == EGID) || |
| 76 | + (target.st_mode & S_IROTH)) { |
| 77 | + f_info->read_access = MK_TRUE; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /* Checking execution */ |
| 82 | + if (mode & MK_FILE_EXEC) { |
| 83 | + if ((target.st_mode & S_IXUSR && target.st_uid == EUID) || |
| 84 | + (target.st_mode & S_IXGRP && target.st_gid == EGID) || |
| 85 | + (target.st_mode & S_IXOTH)) { |
| 86 | + f_info->exec_access = MK_TRUE; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /* Suggest open(2) flags */ |
| 91 | + f_info->flags_read_only = O_RDONLY | O_NONBLOCK; |
| 92 | + |
| 93 | +#if defined(__linux__) |
| 94 | + /* |
| 95 | + * If the user is the owner of the file or the user is root, it |
| 96 | + * can set the O_NOATIME flag for open(2) operations to avoid |
| 97 | + * inode updates about last accessed time |
| 98 | + */ |
| 99 | + if (target.st_uid == EUID || EUID == 0) { |
| 100 | + f_info->flags_read_only |= O_NOATIME; |
| 101 | + } |
| 102 | +#endif |
| 103 | + |
| 104 | + return 0; |
| 105 | +} |
| 106 | + |
| 107 | +/* Read file content to a memory buffer, |
| 108 | + * Use this function just for really SMALL files |
| 109 | + */ |
| 110 | +char *mk_file_to_buffer(const char *path) |
| 111 | +{ |
| 112 | + FILE *fp; |
| 113 | + char *buffer; |
| 114 | + long bytes; |
| 115 | + struct file_info finfo; |
| 116 | + |
| 117 | + if (mk_file_get_info(path, &finfo, MK_FILE_READ) != 0) { |
| 118 | + return NULL; |
| 119 | + } |
| 120 | + |
| 121 | + if (!(fp = fopen(path, "r"))) { |
| 122 | + return NULL; |
| 123 | + } |
| 124 | + |
| 125 | + buffer = mk_mem_malloc_z(finfo.size + 1); |
| 126 | + if (!buffer) { |
| 127 | + fclose(fp); |
| 128 | + return NULL; |
| 129 | + } |
| 130 | + |
| 131 | + bytes = fread(buffer, finfo.size, 1, fp); |
| 132 | + |
| 133 | + if (bytes < 1) { |
| 134 | + mk_mem_free(buffer); |
| 135 | + fclose(fp); |
| 136 | + return NULL; |
| 137 | + } |
| 138 | + |
| 139 | + fclose(fp); |
| 140 | + return (char *) buffer; |
| 141 | + |
| 142 | +} |
0 commit comments