Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibCore: Port Directory to Windows #2188

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions AK/LexicalPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ bool LexicalPath::is_absolute() const
return m_string.starts_with('/');
}

bool LexicalPath::is_root() const
{
return m_string == "/";
}

Vector<ByteString> LexicalPath::parts() const
{
Vector<ByteString> vector;
Expand Down
1 change: 1 addition & 0 deletions AK/LexicalPath.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class LexicalPath {
explicit LexicalPath(ByteString);

bool is_absolute() const;
bool is_root() const;
ByteString const& string() const { return m_string; }

StringView dirname() const { return m_dirname; }
Expand Down
9 changes: 7 additions & 2 deletions AK/LexicalPathWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ bool LexicalPath::is_absolute() const
return is_absolute_path(m_string);
}

bool LexicalPath::is_root() const
{
return AK::is_root(m_parts);
}

Vector<ByteString> LexicalPath::parts() const
{
Vector<ByteString> vector;
Expand Down Expand Up @@ -86,7 +91,7 @@ ByteString LexicalPath::canonicalized_path(ByteString path)
continue;
if (part == ".." && !canonical_parts.is_empty()) {
// At the root, .. does nothing.
if (is_root(canonical_parts))
if (AK::is_root(canonical_parts))
continue;
// A .. and a previous non-.. part cancel each other.
if (canonical_parts.last() != "..") {
Expand All @@ -100,7 +105,7 @@ ByteString LexicalPath::canonicalized_path(ByteString path)
StringBuilder builder;
builder.join('\\', canonical_parts);
// "X:" -> "X:\"
if (is_root(canonical_parts))
if (AK::is_root(canonical_parts))
builder.append('\\');
path = builder.to_byte_string();
return path == "" ? "." : path;
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibCore/AnonymousBufferWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ AnonymousBufferImpl::~AnonymousBufferImpl()

ErrorOr<NonnullRefPtr<AnonymousBufferImpl>> AnonymousBufferImpl::create(size_t size)
{
HANDLE map_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, HIWORD(size), LOWORD(size), NULL);
HANDLE map_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, size >> 31 >> 1, size & 0xFFFFFFFF, NULL);
if (!map_handle)
return Error::from_windows_error(GetLastError());

Expand Down
2 changes: 0 additions & 2 deletions Libraries/LibCore/ConfigFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include <LibCore/Directory.h>
#include <LibCore/StandardPaths.h>
#include <LibCore/System.h>
#include <pwd.h>
#include <sys/types.h>

namespace Core {

Expand Down
10 changes: 5 additions & 5 deletions Libraries/LibCore/Directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
* SPDX-License-Identifier: BSD-2-Clause
*/

#include "Directory.h"
#include "DirIterator.h"
#include "System.h"
#include <dirent.h>
#include <LibCore/Directory.h>
#include <LibCore/System.h>

namespace Core {

Expand All @@ -31,13 +29,15 @@ Directory::~Directory()
MUST(System::close(m_directory_fd));
}

#ifndef AK_OS_WINDOWS
ErrorOr<void> Directory::chown(uid_t uid, gid_t gid)
{
if (m_directory_fd == -1)
return Error::from_syscall("fchown"sv, -EBADF);
TRY(Core::System::fchown(m_directory_fd, uid, gid));
return {};
}
#endif

ErrorOr<bool> Directory::is_valid_directory(int fd)
{
Expand Down Expand Up @@ -69,7 +69,7 @@ ErrorOr<Directory> Directory::create(LexicalPath path, CreateDirectories create_

ErrorOr<void> Directory::ensure_directory(LexicalPath const& path, mode_t creation_mode)
{
if (path.basename() == "/" || path.basename() == ".")
if (path.is_root() || path.string() == ".")
return {};

TRY(ensure_directory(path.parent(), creation_mode));
Expand Down
5 changes: 2 additions & 3 deletions Libraries/LibCore/Directory.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@
#include <AK/Error.h>
#include <AK/Format.h>
#include <AK/Function.h>
#include <AK/IterationDecision.h>
#include <AK/LexicalPath.h>
#include <AK/Noncopyable.h>
#include <AK/Optional.h>
#include <LibCore/DirIterator.h>
#include <LibCore/DirectoryEntry.h>
#include <LibCore/File.h>
#include <dirent.h>
#include <sys/stat.h>

namespace Core {
Expand Down Expand Up @@ -51,7 +48,9 @@ class Directory {
static ErrorOr<void> for_each_entry(StringView path, DirIterator::Flags, ForEachEntryCallback);
ErrorOr<void> for_each_entry(DirIterator::Flags, ForEachEntryCallback);

#ifndef AK_OS_WINDOWS
ErrorOr<void> chown(uid_t, gid_t);
#endif

static ErrorOr<bool> is_valid_directory(int fd);

Expand Down
41 changes: 37 additions & 4 deletions Libraries/LibCore/SystemWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,30 @@
#include <AK/ByteString.h>
#include <AK/ScopeGuard.h>
#include <LibCore/System.h>
#include <WinSock2.h>
#include <Windows.h>
#include <direct.h>
#include <io.h>

namespace Core::System {

ErrorOr<int> open(StringView path, int options, mode_t mode)
{
ByteString string_path = path;
int rc = _open(string_path.characters(), options, mode);
if (rc < 0)
return Error::from_syscall("open"sv, -errno);
auto sz_path = string_path.characters();
int rc = _open(sz_path, options, mode);
if (rc < 0) {
int error = errno;
struct stat st = {};
if (::stat(sz_path, &st) == 0 && (st.st_mode & S_IFDIR)) {
HANDLE dir_handle = CreateFile(sz_path, GENERIC_ALL, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (dir_handle == INVALID_HANDLE_VALUE)
return Error::from_windows_error(GetLastError());
int dir_fd = _open_osfhandle((intptr_t)dir_handle, 0);
if (dir_fd != -1)
return dir_fd;
}
return Error::from_syscall("open"sv, -error);
}
return rc;
}

Expand Down Expand Up @@ -88,4 +101,24 @@ ErrorOr<void> ioctl(int, unsigned, ...)
VERIFY_NOT_REACHED();
}

ErrorOr<void> mkdir(StringView path, mode_t)
{
ByteString str = path;
if (_mkdir(str.characters()) < 0)
return Error::from_syscall("mkdir"sv, -errno);
return {};
}

ErrorOr<int> openat(int, StringView, int, mode_t)
{
dbgln("Core::System::openat() is not implemented");
VERIFY_NOT_REACHED();
}

ErrorOr<struct stat> fstatat(int, StringView, int)
{
dbgln("Core::System::fstatat() is not implemented");
VERIFY_NOT_REACHED();
}

}
Loading