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

Add Libc.mkfifo #34587

Merged
merged 19 commits into from
Oct 1, 2023
Merged
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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Build system changes

New library functions
---------------------

* The new `Libc.mkfifo` function wraps the `mkfifo` C function on Unix platforms ([#34587]).
* `hardlink(src, dst)` can be used to create hard links. ([#41639])
* `diskstat(path=pwd())` can be used to return statistics about the disk. ([#42248])
* `copyuntil(out, io, delim)` and `copyline(out, io)` copy data into an `out::IO` stream ([#48273]).

New library features
Expand Down
29 changes: 28 additions & 1 deletion base/libc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Core.Intrinsics: bitcast

export FILE, TmStruct, strftime, strptime, getpid, gethostname, free, malloc, memcpy,
memmove, memset, calloc, realloc, errno, strerror, flush_cstdio, systemsleep, time,
transcode
transcode, mkfifo
if Sys.iswindows()
export GetLastError, FormatMessage
end
Expand Down Expand Up @@ -437,6 +437,33 @@ function srand(seed::Integer=_make_uint64_seed())
ccall(:jl_srand, Cvoid, (UInt64,), seed % UInt64)
end

"""
mkfifo(path::AbstractString, [mode::Integer]) -> path

Make a FIFO special file (a named pipe) at `path`. Return `path` as-is on success.
tkf marked this conversation as resolved.
Show resolved Hide resolved

`mkfifo` is supported only in Unix platforms.

!!! compat "Julia 1.8"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has this been corrected? Obviously this merge was a bit late for 1.8.

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Please PR to fix

`mkfifo` requires at least Julia 1.8.
"""
function mkfifo(
path::AbstractString,
mode::Integer = Base.S_IRUSR | Base.S_IWUSR | Base.S_IRGRP | Base.S_IWGRP |
Base.S_IROTH | Base.S_IWOTH,
)
@static if Sys.isunix()
# Default `mode` is compatible with `mkfifo` CLI in coreutils.
ret = ccall(:mkfifo, Cint, (Cstring, Base.Cmode_t), path, mode)
systemerror("mkfifo", ret == -1)
return path
else
# Using normal `error` because `systemerror("mkfifo", ENOTSUP)` does not
# seem to work on Windows.
error("mkfifo: Operation not supported")
end
end

struct Cpasswd
username::Cstring
uid::Culong
Expand Down
1 change: 1 addition & 0 deletions doc/src/base/libc.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ Base.Libc.TmStruct
Base.Libc.FILE
Base.Libc.flush_cstdio
Base.Libc.systemsleep
Base.Libc.mkfifo
```
22 changes: 22 additions & 0 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1636,6 +1636,28 @@ end
end
end

if Sys.isunix()
@testset "mkfifo" begin
mktempdir() do dir
path = Libc.mkfifo(joinpath(dir, "fifo"))
@sync begin
@async write(path, "hello")
cat_exec = `$(Base.julia_cmd()) --startup-file=no -e "write(stdout, read(ARGS[1]))"`
@test read(`$cat_exec $path`, String) == "hello"
end

existing_file = joinpath(dir, "existing")
write(existing_file, "")
@test_throws SystemError Libc.mkfifo(existing_file)
end
end
else
@test_throws(
"mkfifo: Operation not supported",
Libc.mkfifo(joinpath(pwd(), "dummy_path")),
)
end

@testset "chmod/isexecutable" begin
mktempdir() do dir
mkdir(joinpath(dir, "subdir"))
Expand Down