-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* stat: fmt code * Basic mkfifo implementation
- Loading branch information
Showing
7 changed files
with
121 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import os | ||
import common | ||
|
||
const name = 'mkfifo' | ||
const default_mode = u32(0o600) | ||
|
||
struct Options { | ||
mode u32 | ||
} | ||
|
||
fn mkfifo_cmd(list []string, opts &Options) { | ||
for path in list { | ||
ret := mkfifo(path, opts.mode) | ||
if ret != 0 { | ||
common.exit_with_error_message(name, os.posix_get_error_msg(ret)) | ||
} | ||
} | ||
} | ||
|
||
fn run_mkfifo(args []string) { | ||
mut fp := common.flag_parser(args) | ||
fp.application(name) | ||
fp.usage_example('[OPTION]... NAME...') | ||
fp.description('Create named pipes (FIFOs) with the given NAMEs.') | ||
fp.description('Mandatory arguments to long options are mandatory for short options too.') | ||
|
||
mut opts := Options{ | ||
mode: u32(fp.int('mode', `m`, int(default_mode), 'set file permission bits to MODE, not a=rw - umask')) | ||
} | ||
|
||
help := fp.bool('help', 0, false, 'display this help and exit') | ||
version := fp.bool('version', 0, false, 'output version information and exit') | ||
if help { | ||
println(fp.usage()) | ||
exit(0) | ||
} | ||
if version { | ||
println('${name} ${common.coreutils_version()}') | ||
exit(0) | ||
} | ||
|
||
file_args := fp.finalize() or { common.exit_with_error_message(name, err.msg()) } | ||
if file_args.len == 0 { | ||
common.exit_with_error_message(name, 'missing operand') | ||
} | ||
|
||
mkfifo_cmd(file_args, &opts) | ||
} | ||
|
||
fn main() { | ||
run_mkfifo(os.args) | ||
exit(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// mkfifo on unsupported platforms | ||
fn mkfifo(pathname string, mode int) int { | ||
return -1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
fn C.mkfifo(&char, int) int | ||
|
||
fn mkfifo(pathname string, mode u32) int { | ||
return C.mkfifo(pathname.str, mode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import os | ||
import common.testing | ||
|
||
const rig = testing.prepare_rig(util: util) | ||
const tfolder = os.join_path(os.temp_dir(), 'coreutils', 'mkfifo_test') | ||
|
||
const util = 'mkfifo' | ||
const platform_util = util | ||
|
||
const executable_under_test = testing.prepare_executable(util) | ||
const cmd = testing.new_paired_command(platform_util, executable_under_test) | ||
|
||
fn test_help_and_version() { | ||
rig.assert_help_and_version_options_work() | ||
} | ||
|
||
fn testsuite_begin() { | ||
rig.assert_platform_util() | ||
os.chdir(testing.temp_folder)! | ||
eprintln('testsuite_begin, tfolder = ${tfolder}') | ||
os.rmdir_all(tfolder) or {} | ||
assert !os.is_dir(tfolder) | ||
os.mkdir_all(tfolder) or { panic(err) } | ||
os.chdir(tfolder) or {} | ||
assert os.is_dir(tfolder) | ||
} | ||
|
||
fn testsuite_end() { | ||
os.chdir(os.wd_at_startup) or {} | ||
os.rmdir_all(tfolder) or {} | ||
assert !os.is_dir(tfolder) | ||
eprintln('testsuite_end , tfolder = ${tfolder} removed.') | ||
} | ||
|
||
fn test_default_create_single_pipe() { | ||
target := 'test_piped' | ||
res := os.execute('${executable_under_test} ${target}') | ||
assert res.exit_code == 0 | ||
assert res.output.trim_space() == '' | ||
assert os.is_file(target) | ||
st := os.stat(target)! | ||
assert st.get_filetype() == os.FileType.fifo | ||
} | ||
|
||
fn test_default_create_multiple_pipes() { | ||
target := 'tp1 tp2 tp3' | ||
res := os.execute('${executable_under_test} ${target}') | ||
assert res.exit_code == 0 | ||
assert res.output.trim_space() == '' | ||
for p in target.split(' ') { | ||
assert os.is_file(p) | ||
st := os.stat(p)! | ||
assert st.get_filetype() == os.FileType.fifo | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters