Added pull request #1189: Fix os::native_is_{file,dir} bug. Add tests.

This commit is contained in:
Christoffer Lerno
2024-07-15 03:02:54 +02:00
parent 24041ed80d
commit bc0d52142a
3 changed files with 19 additions and 2 deletions

View File

@@ -95,7 +95,7 @@ fn bool native_is_file(String path)
$case env::DARWIN:
$case env::LINUX:
Stat stat;
return @ok(native_stat(&stat, path)) && stat.st_mode & libc::S_IFREG;
return @ok(native_stat(&stat, path)) && libc_S_ISTYPE(stat.st_mode, libc::S_IFREG);
$default:
File! f = file::open(path, "r");
defer (void)f.close();
@@ -107,7 +107,7 @@ fn bool native_is_dir(String path)
{
$if env::DARWIN || env::LINUX:
Stat stat;
return @ok(native_stat(&stat, path)) && stat.st_mode & libc::S_IFDIR;
return @ok(native_stat(&stat, path)) && libc_S_ISTYPE(stat.st_mode, libc::S_IFDIR);
$else
return native_file_or_dir_exists(path) && !native_is_file(path);
$endif

View File

@@ -360,6 +360,7 @@ const int EOF = -1;
const int FOPEN_MAX = 20;
const int FILENAME_MAX = 1024;
macro bool libc_S_ISTYPE(value, mask) @builtin => (value & S_IFMT) == mask;
const S_IFMT = 0o170000; // type of file mask
const S_IFIFO = 0o010000; // named pipe (fifo)
const S_IFCHR = 0o020000; // character special

View File

@@ -0,0 +1,16 @@
module std::io::fileinfo @test;
import std::io::os;
fn void test_native_is_file() @if(env::LINUX || env::DARWIN)
{
assert(!os::native_is_file("/dev/loop0"));
assert(!os::native_is_file("/dev/null"));
}
fn void test_native_is_dir() @if(env::LINUX || env::DARWIN)
{
assert(os::native_is_dir("/"));
assert(!os::native_is_file("/"));
assert(!os::native_is_dir("/dev/loop0"));
assert(!os::native_is_dir("/dev/null"));
}