os: add os.path_devnull (containing /dev/null on POSIX and \\.\nul on Windows) (#20439)

This commit is contained in:
Delyan Angelov 2024-01-08 18:00:36 +02:00 committed by GitHub
parent 34da4c97ea
commit 252540a1ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 3 deletions

View file

@ -477,3 +477,18 @@ fn test_open_file_crlf_binary_mode() {
assert fcont_wb == teststr assert fcont_wb == teststr
} }
fn test_path_devnull() {
dump(os.path_devnull)
content := os.read_file(os.path_devnull)!
// dump(content)
// dump(content.len)
os.write_file(os.path_devnull, 'something')!
content_after := os.read_file(os.path_devnull)!
// dump(content_after)
// dump(content_after.len)
assert content.len == 0
assert content_after.len == 0
}

View file

@ -8,6 +8,8 @@ $if js_node {
pub const path_delimiter = get_path_delimiter() pub const path_delimiter = get_path_delimiter()
pub const path_separator = get_path_separator() pub const path_separator = get_path_separator()
pub const path_devnull = '/dev/null' // TODO
pub const args = []string{} pub const args = []string{}
const executable_suffixes = [''] const executable_suffixes = ['']

View file

@ -9,9 +9,18 @@ import strings
#include <sys/types.h> #include <sys/types.h>
#include <utime.h> #include <utime.h>
// path_separator is the platform specific separator string, used between the folders
// and filenames in a path. It is '/' on POSIX, and '\\' on Windows.
pub const path_separator = '/' pub const path_separator = '/'
// path_delimiter is the platform specific delimiter string, used between the paths
// in environment variables like PATH. It is ':' on POSIX, and ';' on Windows.
pub const path_delimiter = ':' pub const path_delimiter = ':'
// path_devnull is a platform-specific file path of the null device.
// It is '/dev/null' on POSIX, and r'\\.\nul' on Windows.
pub const path_devnull = '/dev/null'
const executable_suffixes = [''] const executable_suffixes = ['']
const stdin_value = 0 const stdin_value = 0

View file

@ -6,6 +6,18 @@ import strings
#include <process.h> #include <process.h>
#include <sys/utime.h> #include <sys/utime.h>
// path_separator is the platform specific separator string, used between the folders
// and filenames in a path. It is '/' on POSIX, and '\\' on Windows.
pub const path_separator = '\\'
// path_delimiter is the platform specific delimiter string, used between the paths
// in environment variables like PATH. It is ':' on POSIX, and ';' on Windows.
pub const path_delimiter = ';'
// path_devnull is a platform-specific file path of the null device.
// It is '/dev/null' on POSIX, and r'\\.\nul' on Windows.
pub const path_devnull = r'\\.\nul'
// See https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createsymboliclinkw // See https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createsymboliclinkw
fn C.CreateSymbolicLinkW(&u16, &u16, u32) int fn C.CreateSymbolicLinkW(&u16, &u16, u32) int
@ -16,9 +28,6 @@ fn C._getpid() int
const executable_suffixes = ['.exe', '.bat', '.cmd', ''] const executable_suffixes = ['.exe', '.bat', '.cmd', '']
pub const path_separator = '\\'
pub const path_delimiter = ';'
// Ref - https://docs.microsoft.com/en-us/windows/desktop/winprog/windows-data-types // Ref - https://docs.microsoft.com/en-us/windows/desktop/winprog/windows-data-types
// A handle to an object. // A handle to an object.
pub type HANDLE = voidptr pub type HANDLE = voidptr