Serenity Operating System
at portability 100 lines 3.5 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 <Kernel/FileSystem/FileDescription.h> 28#include <Kernel/FileSystem/Inode.h> 29#include <Kernel/FileSystem/InodeFile.h> 30#include <Kernel/FileSystem/VirtualFileSystem.h> 31#include <Kernel/Process.h> 32 33namespace Kernel { 34 35InodeFile::InodeFile(NonnullRefPtr<Inode>&& inode) 36 : m_inode(move(inode)) 37{ 38} 39 40InodeFile::~InodeFile() 41{ 42} 43 44ssize_t InodeFile::read(FileDescription& description, u8* buffer, ssize_t count) 45{ 46 ssize_t nread = m_inode->read_bytes(description.offset(), count, buffer, &description); 47 if (nread > 0) 48 Thread::current->did_file_read(nread); 49 return nread; 50} 51 52ssize_t InodeFile::write(FileDescription& description, const u8* data, ssize_t count) 53{ 54 ssize_t nwritten = m_inode->write_bytes(description.offset(), count, data, &description); 55 if (nwritten > 0) { 56 m_inode->set_mtime(kgettimeofday().tv_sec); 57 Thread::current->did_file_write(nwritten); 58 } 59 return nwritten; 60} 61 62KResultOr<Region*> InodeFile::mmap(Process& process, FileDescription& description, VirtualAddress preferred_vaddr, size_t offset, size_t size, int prot) 63{ 64 ASSERT(offset == 0); 65 // FIXME: If PROT_EXEC, check that the underlying file system isn't mounted noexec. 66 auto* region = process.allocate_file_backed_region(preferred_vaddr, size, inode(), description.absolute_path(), prot); 67 if (!region) 68 return KResult(-ENOMEM); 69 return region; 70} 71 72String InodeFile::absolute_path(const FileDescription& description) const 73{ 74 ASSERT_NOT_REACHED(); 75 ASSERT(description.custody()); 76 return description.absolute_path(); 77} 78 79KResult InodeFile::truncate(u64 size) 80{ 81 auto truncate_result = m_inode->truncate(size); 82 if (truncate_result.is_error()) 83 return truncate_result; 84 int mtime_result = m_inode->set_mtime(kgettimeofday().tv_sec); 85 if (mtime_result != 0) 86 return KResult(mtime_result); 87 return KSuccess; 88} 89 90KResult InodeFile::chown(uid_t uid, gid_t gid) 91{ 92 return VFS::the().chown(*m_inode, uid, gid); 93} 94 95KResult InodeFile::chmod(mode_t mode) 96{ 97 return VFS::the().chmod(*m_inode, mode); 98} 99 100}