Serenity Operating System
at portability 201 lines 6.0 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/Assertions.h> 28#include <AK/StdLibExtras.h> 29#include <Kernel/Syscall.h> 30#include <dirent.h> 31#include <errno.h> 32#include <fcntl.h> 33#include <stdio.h> 34#include <stdlib.h> 35#include <string.h> 36#include <sys/stat.h> 37#include <unistd.h> 38 39extern "C" { 40 41DIR* opendir(const char* name) 42{ 43 int fd = open(name, O_RDONLY | O_DIRECTORY); 44 if (fd == -1) 45 return nullptr; 46 DIR* dirp = (DIR*)malloc(sizeof(DIR)); 47 dirp->fd = fd; 48 dirp->buffer = nullptr; 49 dirp->buffer_size = 0; 50 dirp->nextptr = nullptr; 51 return dirp; 52} 53 54int closedir(DIR* dirp) 55{ 56 if (!dirp || dirp->fd == -1) 57 return -EBADF; 58 if (dirp->buffer) 59 free(dirp->buffer); 60 int rc = close(dirp->fd); 61 if (rc == 0) 62 dirp->fd = -1; 63 free(dirp); 64 return rc; 65} 66 67struct [[gnu::packed]] sys_dirent 68{ 69 ino_t ino; 70 u8 file_type; 71 size_t namelen; 72 char name[]; 73 size_t total_size() 74 { 75 return sizeof(ino_t) + sizeof(u8) + sizeof(size_t) + sizeof(char) * namelen; 76 } 77}; 78 79static void create_struct_dirent(sys_dirent* sys_ent, struct dirent* str_ent) 80{ 81 str_ent->d_ino = sys_ent->ino; 82 str_ent->d_type = sys_ent->file_type; 83 str_ent->d_off = 0; 84 str_ent->d_reclen = sys_ent->total_size(); 85 for (size_t i = 0; i < sys_ent->namelen; ++i) 86 str_ent->d_name[i] = sys_ent->name[i]; 87 // FIXME: I think this null termination behavior is not supposed to be here. 88 str_ent->d_name[sys_ent->namelen] = '\0'; 89} 90 91static int allocate_dirp_buffer(DIR* dirp) 92{ 93 if (dirp->buffer) { 94 return 0; 95 } 96 97 struct stat st; 98 // preserve errno since this could be a reentrant call 99 int old_errno = errno; 100 int rc = fstat(dirp->fd, &st); 101 if (rc < 0) { 102 int new_errno = errno; 103 errno = old_errno; 104 return new_errno; 105 } 106 size_t size_to_allocate = max(st.st_size, 4096); 107 dirp->buffer = (char*)malloc(size_to_allocate); 108 ssize_t nread = syscall(SC_get_dir_entries, dirp->fd, dirp->buffer, size_to_allocate); 109 if (nread < 0) { 110 // uh-oh, the syscall returned an error 111 free(dirp->buffer); 112 dirp->buffer = nullptr; 113 return -nread; 114 } 115 dirp->buffer_size = nread; 116 dirp->nextptr = dirp->buffer; 117 return 0; 118} 119 120dirent* readdir(DIR* dirp) 121{ 122 if (!dirp) 123 return nullptr; 124 if (dirp->fd == -1) 125 return nullptr; 126 127 if (int new_errno = allocate_dirp_buffer(dirp)) { 128 // readdir is allowed to mutate errno 129 errno = new_errno; 130 return nullptr; 131 } 132 133 if (dirp->nextptr >= (dirp->buffer + dirp->buffer_size)) 134 return nullptr; 135 136 auto* sys_ent = (sys_dirent*)dirp->nextptr; 137 create_struct_dirent(sys_ent, &dirp->cur_ent); 138 139 dirp->nextptr += sys_ent->total_size(); 140 return &dirp->cur_ent; 141} 142 143static bool compare_sys_struct_dirent(sys_dirent* sys_ent, struct dirent* str_ent) 144{ 145 size_t namelen = min((size_t)256, sys_ent->namelen); 146 // These fields are guaranteed by create_struct_dirent to be the same 147 return sys_ent->ino == str_ent->d_ino 148 && sys_ent->file_type == str_ent->d_type 149 && sys_ent->total_size() == str_ent->d_reclen 150 && strncmp(sys_ent->name, str_ent->d_name, namelen) == 0; 151} 152 153int readdir_r(DIR* dirp, struct dirent* entry, struct dirent** result) 154{ 155 if (!dirp || dirp->fd == -1) { 156 *result = nullptr; 157 return EBADF; 158 } 159 160 if (int new_errno = allocate_dirp_buffer(dirp)) { 161 *result = nullptr; 162 return new_errno; 163 } 164 165 // This doesn't care about dirp state; seek until we find the entry. 166 // Unfortunately, we can't just compare struct dirent to sys_dirent, so 167 // manually compare the fields. This seems a bit risky, but could work. 168 auto* buffer = dirp->buffer; 169 auto* sys_ent = (sys_dirent*)buffer; 170 bool found = false; 171 while (!(found || buffer >= dirp->buffer + dirp->buffer_size)) { 172 found = compare_sys_struct_dirent(sys_ent, entry); 173 174 // Make sure if we found one, it's the one after (end of buffer or not) 175 buffer += sys_ent->total_size(); 176 sys_ent = (sys_dirent*)buffer; 177 } 178 179 // If we found one, but hit end of buffer, then EOD 180 if (found && buffer >= dirp->buffer + dirp->buffer_size) { 181 *result = nullptr; 182 return 0; 183 } 184 // If we never found a match for entry in buffer, start from the beginning 185 else if (!found) { 186 buffer = dirp->buffer; 187 sys_ent = (sys_dirent*)buffer; 188 } 189 190 *result = entry; 191 create_struct_dirent(sys_ent, entry); 192 193 return 0; 194} 195 196int dirfd(DIR* dirp) 197{ 198 ASSERT(dirp); 199 return dirp->fd; 200} 201}