Serenity Operating System
at portability 109 lines 3.3 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 <LibCore/File.h> 28#include <errno.h> 29#include <fcntl.h> 30#include <stdio.h> 31#include <sys/stat.h> 32#include <unistd.h> 33 34namespace Core { 35 36File::File(const StringView& filename, Object* parent) 37 : IODevice(parent) 38 , m_filename(filename) 39{ 40} 41 42File::~File() 43{ 44 if (m_should_close_file_descriptor == ShouldCloseFileDescription::Yes && mode() != NotOpen) 45 close(); 46} 47 48bool File::open(int fd, IODevice::OpenMode mode, ShouldCloseFileDescription should_close) 49{ 50 set_fd(fd); 51 set_mode(mode); 52 m_should_close_file_descriptor = should_close; 53 return true; 54} 55 56bool File::open(IODevice::OpenMode mode) 57{ 58 ASSERT(!m_filename.is_null()); 59 int flags = 0; 60 if ((mode & IODevice::ReadWrite) == IODevice::ReadWrite) { 61 flags |= O_RDWR | O_CREAT; 62 } else if (mode & IODevice::ReadOnly) { 63 flags |= O_RDONLY; 64 } else if (mode & IODevice::WriteOnly) { 65 flags |= O_WRONLY | O_CREAT; 66 bool should_truncate = !((mode & IODevice::Append) || (mode & IODevice::MustBeNew)); 67 if (should_truncate) 68 flags |= O_TRUNC; 69 } 70 if (mode & IODevice::Append) 71 flags |= O_APPEND; 72 if (mode & IODevice::Truncate) 73 flags |= O_TRUNC; 74 if (mode & IODevice::MustBeNew) 75 flags |= O_EXCL; 76 int fd = ::open(m_filename.characters(), flags, 0666); 77 if (fd < 0) { 78 set_error(errno); 79 return false; 80 } 81 82 set_fd(fd); 83 set_mode(mode); 84 return true; 85} 86 87bool File::is_directory() const 88{ 89 struct stat stat; 90 if (fstat(fd(), &stat) < 0) 91 return false; 92 return S_ISDIR(stat.st_mode); 93} 94 95bool File::is_directory(const String& filename) 96{ 97 struct stat st; 98 if (stat(filename.characters(), &st) < 0) 99 return false; 100 return S_ISDIR(st.st_mode); 101} 102 103bool File::exists(const String& filename) 104{ 105 struct stat st; 106 return stat(filename.characters(), &st) == 0; 107} 108 109}