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