diff --git a/vlib/os/os.v b/vlib/os/os.v index 286121994d..ce7da1d335 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -23,7 +23,6 @@ struct C.dirent { fn C.readdir(voidptr) C.dirent - pub const ( args = []string MAX_PATH = 4096 diff --git a/vlib/os/os_nix.v b/vlib/os/os_nix.v index 68f8108831..c6e6101535 100644 --- a/vlib/os/os_nix.v +++ b/vlib/os/os_nix.v @@ -6,6 +6,8 @@ pub const ( path_separator = '/' ) +fn C.symlink(charptr, charptr) int + pub fn init_os_args(argc int, argv &byteptr) []string { mut args := []string for i in 0 .. argc { @@ -98,3 +100,8 @@ pub fn exec(cmd string) ?Result { } } +pub fn symlink(origin, target string) ?bool { + res := C.symlink(origin.str, target.str) + if res == 0 { return true } + return error(get_error_msg(C.errno)) +} diff --git a/vlib/os/os_test.v b/vlib/os/os_test.v index 9c91bf312e..ac9b3d306a 100644 --- a/vlib/os/os_test.v +++ b/vlib/os/os_test.v @@ -237,3 +237,14 @@ fn cleanup_leftovers(){ os.rm('ex1.txt') os.rm('ex2.txt') } + +fn test_symlink() { + $if windows { return } + os.mkdir('symlink') or { panic(err) } + os.symlink('symlink', 'symlink2') or { panic(err) } + assert os.exists('symlink2') + + // cleanup + os.rm('symlink') + os.rm('symlink2') +} diff --git a/vlib/os/os_windows.v b/vlib/os/os_windows.v index 6533063165..d67da058c7 100644 --- a/vlib/os/os_windows.v +++ b/vlib/os/os_windows.v @@ -285,3 +285,13 @@ pub fn exec(cmd string) ?Result { exit_code: int(exit_code) } } + +fn C.CreateSymbolicLinkW(&u16, &u16, u32) int + +pub fn symlink(origin, target string) ?bool { + flags := if os.is_dir(origin) { 1 } else { 0 } + if C.CreateSymbolicLinkW(origin.to_wide(), target.to_wide(), u32(flags)) != 0 { + return true + } + return error(get_error_msg(int(C.GetLastError()))) +}