mkdir/rmdir would not work properly with substring paths on non-windows platforms.

This commit is contained in:
Christoffer Lerno
2025-06-30 21:19:15 +02:00
committed by Christoffer Lerno
parent 13bb2b6690
commit fad87b294b
8 changed files with 77 additions and 66 deletions

View File

@@ -16,6 +16,7 @@ faultdef
BUSY,
CANNOT_READ_DIR,
DIR_NOT_EMPTY,
PARENT_DIR_MISSING,
EOF,
FILE_CANNOT_DELETE,
FILE_IS_DIR,

View File

@@ -2,10 +2,12 @@ module std::io::os;
import std::io::path, libc, std::os;
macro void? native_chdir(Path path)
{
@pool()
{
$switch:
$case env::POSIX:
if (posix::chdir(path.as_zstr()))
if (posix::chdir(path.str_view().zstr_tcopy()))
{
switch (libc::errno())
{
@@ -18,13 +20,11 @@ macro void? native_chdir(Path path)
}
}
$case env::WIN32:
@pool()
{
// TODO improve with better error handling.
if (win32::setCurrentDirectoryW(path.str_view().to_temp_utf16()!!)) return;
};
return io::GENERAL_ERROR?;
$default:
return io::UNSUPPORTED_OPERATION?;
$endswitch
};
}

View File

@@ -5,7 +5,11 @@ fn PathList? native_ls(Path dir, bool no_dirs, bool no_symlinks, String mask, Al
{
PathList list;
list.init(allocator);
DIRPtr directory = posix::opendir(dir.str_view() ? dir.as_zstr() : (ZString)".");
DIRPtr directory @noinit;
@pool()
{
directory = posix::opendir(dir.str_view() ? dir.str_view().zstr_tcopy() : (ZString)".");
};
defer if (directory) posix::closedir(directory);
if (!directory) return (path::is_dir(dir) ? io::CANNOT_READ_DIR : io::FILE_NOT_DIR)?;
Posix_dirent* entry;

View File

@@ -6,10 +6,12 @@ import std::os::posix;
macro bool? native_mkdir(Path path, MkdirPermissions permissions)
{
@pool()
{
$switch:
$case env::POSIX:
if (!posix::mkdir(path.as_zstr(), permissions == NORMAL ? 0o777 : 0o700)) return true;
if (!posix::mkdir(path.str_view().zstr_tcopy(), permissions == NORMAL ? 0o777 : 0o700)) return true;
switch (libc::errno())
{
case errno::EACCES:
@@ -23,11 +25,11 @@ macro bool? native_mkdir(Path path, MkdirPermissions permissions)
case errno::EEXIST: return false;
case errno::ELOOP: return io::SYMLINK_FAILED?;
case errno::ENOTDIR: return io::FILE_NOT_FOUND?;
default: return io::GENERAL_ERROR?;
case errno::ENOENT: return io::PARENT_DIR_MISSING?;
default:
return io::GENERAL_ERROR?;
}
$case env::WIN32:
@pool()
{
// TODO security attributes
if (win32::createDirectoryW(path.str_view().to_temp_utf16()!!, null)) return true;
switch (win32::getLastError())
@@ -43,8 +45,8 @@ macro bool? native_mkdir(Path path, MkdirPermissions permissions)
default:
return io::GENERAL_ERROR?;
}
};
$default:
return io::UNSUPPORTED_OPERATION?;
$endswitch
};
}

View File

@@ -5,10 +5,12 @@ import std::os::win32;
import std::os::posix;
macro bool? native_rmdir(Path path)
{
@pool()
{
$switch:
$case env::POSIX:
if (!posix::rmdir(path.as_zstr())) return true;
if (!posix::rmdir(path.str_view().zstr_tcopy())) return true;
switch (libc::errno())
{
case errno::EBUSY: return io::BUSY?;
@@ -24,8 +26,6 @@ macro bool? native_rmdir(Path path)
default: return io::GENERAL_ERROR?;
}
$case env::WIN32:
@pool()
{
if (win32::removeDirectoryW(path.str_view().to_temp_utf16()!!)) return true;
switch (win32::getLastError())
{
@@ -41,8 +41,8 @@ macro bool? native_rmdir(Path path)
default:
return io::GENERAL_ERROR?;
}
};
$default:
return io::UNSUPPORTED_OPERATION?;
$endswitch
};
}

View File

@@ -6,7 +6,9 @@ import std::io, std::os, libc;
*>
fn void? native_rmtree(Path dir)
{
DIRPtr directory = posix::opendir(dir.as_zstr());
@pool()
{
DIRPtr directory = posix::opendir(dir.str_view().zstr_tcopy());
defer if (directory) posix::closedir(directory);
if (!directory) return path::is_dir(dir) ? io::CANNOT_READ_DIR? : io::FILE_NOT_DIR?;
Posix_dirent* entry;
@@ -22,7 +24,7 @@ fn void? native_rmtree(Path dir)
native_rmtree(new_path)!;
continue;
}
if (libc::remove(new_path.as_zstr()))
if (libc::remove(new_path.str_view().zstr_tcopy()))
{
// TODO improve
return io::GENERAL_ERROR?;
@@ -30,6 +32,7 @@ fn void? native_rmtree(Path dir)
};
}
os::native_rmdir(dir)!;
};
}
module std::io::os @if(env::WIN32);

View File

@@ -527,7 +527,7 @@ fn String? normalize(String path_str, PathEnv path_env = DEFAULT_ENV)
return path_str[:len];
}
fn ZString Path.as_zstr(self) => (ZString)self.path_string.ptr;
fn ZString Path.as_zstr(self) @deprecated => (ZString)self.path_string.ptr;
fn String Path.root_directory(self)
{

View File

@@ -8,6 +8,7 @@
- Inline associated enum values are deprecated, use `--use-old-enums` to re-enable them.
### Fixes
- mkdir/rmdir would not work properly with substring paths on non-windows platforms.
### Stdlib changes