From 9cc201fb290cade644b29bf8567c125aaf9cfc6a Mon Sep 17 00:00:00 2001 From: Max Wipfli Date: Wed, 19 May 2021 11:33:23 +0200 Subject: [PATCH] Kernel: Ignore null parent custody without error in VFS::open This modifies the error checks in VFS::open after the call to resolve_path to ignore a null parent custody if there is no error, as this is expected when the path to resolve points to "/". Rather, a null parent custody only constitutes an error if it is accompanied by ENOENT. This behavior is documented in the VFS::resolve_path_without_veil method. To accompany this change, the order of the error checks have been changed to more naturally fit the new logic. --- Kernel/FileSystem/VirtualFileSystem.cpp | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/Kernel/FileSystem/VirtualFileSystem.cpp b/Kernel/FileSystem/VirtualFileSystem.cpp index c8b2f5d8d1..fd54270833 100644 --- a/Kernel/FileSystem/VirtualFileSystem.cpp +++ b/Kernel/FileSystem/VirtualFileSystem.cpp @@ -237,19 +237,16 @@ KResultOr> VFS::open(StringView path, int options RefPtr parent_custody; auto custody_or_error = resolve_path(path, base, &parent_custody, options); - if (options & O_CREAT) { - if (!parent_custody) - return ENOENT; - if (custody_or_error.is_error()) { - if (custody_or_error.error() != -ENOENT) - return custody_or_error.error(); + if (custody_or_error.is_error()) { + // NOTE: ENOENT with a non-null parent custody signals us that the immediate parent + // of the file exists, but the file itself does not. + if ((options & O_CREAT) && custody_or_error.error() == -ENOENT && parent_custody) return create(path, options, mode, *parent_custody, move(owner)); - } - if (options & O_EXCL) - return EEXIST; - } - if (custody_or_error.is_error()) return custody_or_error.error(); + } + + if ((options & O_CREAT) && (options & O_EXCL)) + return EEXIST; auto& custody = *custody_or_error.value(); auto& inode = custody.inode();