Serenity Operating System
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
36RefPtr<File> File::open(const String& filename, IODevice::OpenMode mode, mode_t permissions)
37{
38 auto file = File::construct(filename);
39 if (!file->open_impl(mode, permissions))
40 return nullptr;
41 return file;
42}
43
44File::File(const StringView& filename, Object* parent)
45 : IODevice(parent)
46 , m_filename(filename)
47{
48}
49
50File::~File()
51{
52 if (m_should_close_file_descriptor == ShouldCloseFileDescription::Yes && mode() != NotOpen)
53 close();
54}
55
56bool File::open(int fd, IODevice::OpenMode mode, ShouldCloseFileDescription should_close)
57{
58 set_fd(fd);
59 set_mode(mode);
60 m_should_close_file_descriptor = should_close;
61 return true;
62}
63
64bool File::open(IODevice::OpenMode mode)
65{
66 return open_impl(mode, 0666);
67}
68
69bool File::open_impl(IODevice::OpenMode mode, mode_t permissions)
70{
71 ASSERT(!m_filename.is_null());
72 int flags = 0;
73 if ((mode & IODevice::ReadWrite) == IODevice::ReadWrite) {
74 flags |= O_RDWR | O_CREAT;
75 } else if (mode & IODevice::ReadOnly) {
76 flags |= O_RDONLY;
77 } else if (mode & IODevice::WriteOnly) {
78 flags |= O_WRONLY | O_CREAT;
79 bool should_truncate = !((mode & IODevice::Append) || (mode & IODevice::MustBeNew));
80 if (should_truncate)
81 flags |= O_TRUNC;
82 }
83 if (mode & IODevice::Append)
84 flags |= O_APPEND;
85 if (mode & IODevice::Truncate)
86 flags |= O_TRUNC;
87 if (mode & IODevice::MustBeNew)
88 flags |= O_EXCL;
89 int fd = ::open(m_filename.characters(), flags, permissions);
90 if (fd < 0) {
91 set_error(errno);
92 return false;
93 }
94
95 set_fd(fd);
96 set_mode(mode);
97 return true;
98}
99
100bool File::is_directory() const
101{
102 struct stat stat;
103 if (fstat(fd(), &stat) < 0)
104 return false;
105 return S_ISDIR(stat.st_mode);
106}
107
108bool File::is_directory(const String& filename)
109{
110 struct stat st;
111 if (stat(filename.characters(), &st) < 0)
112 return false;
113 return S_ISDIR(st.st_mode);
114}
115
116bool File::exists(const String& filename)
117{
118 struct stat st;
119 return stat(filename.characters(), &st) == 0;
120}
121
122}