Skip to content

Commit

Permalink
os: return the long path for os.temp_dir() on windows, even for folde…
Browse files Browse the repository at this point in the history
…rs like `c:\someth~1` (#17623)
  • Loading branch information
spytheman committed Mar 13, 2023
1 parent d1d2689 commit daa9034
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions vlib/os/os.v
Expand Up @@ -716,6 +716,7 @@ pub fn temp_dir() string {
path = 'C:/tmp'
}
}
path = get_long_path(path) or { path }
}
$if macos {
// avoid /var/folders/6j/cmsk8gd90pd.... on macs
Expand Down
6 changes: 6 additions & 0 deletions vlib/os/os_js.js.v
Expand Up @@ -183,3 +183,9 @@ pub fn is_readable(path string) bool {
return false
}
}

// get_long_path has no meaning for *nix, but has for windows, where `c:\folder\some~1` for example
// can be the equivalent of `c:\folder\some spa ces`. On *nix, it just returns a copy of the input path.
fn get_long_path(path string) !string {
return path
}
6 changes: 6 additions & 0 deletions vlib/os/os_nix.c.v
Expand Up @@ -500,3 +500,9 @@ pub fn posix_set_permission_bit(path_s string, mode u32, enable bool) {
}
C.chmod(path, int(new_mode))
}

// get_long_path has no meaning for *nix, but has for windows, where `c:\folder\some~1` for example
// can be the equivalent of `c:\folder\some spa ces`. On *nix, it just returns a copy of the input path.
fn get_long_path(path string) !string {
return path
}
21 changes: 21 additions & 0 deletions vlib/os/os_windows.c.v
Expand Up @@ -558,3 +558,24 @@ pub fn (mut c Command) read_line() string {
pub fn (mut c Command) close() ! {
panic('not implemented')
}

fn C.GetLongPathName(short_path &u16, long_path &u16, long_path_bufsize u32) u32

// get_long_path has no meaning for *nix, but has for windows, where `c:\folder\some~1` for example
// can be the equivalent of `c:\folder\some spa ces`. On *nix, it just returns a copy of the input path.
fn get_long_path(path string) !string {
if !path.contains('~') {
return path
}
input_short_path := path.to_wide()
defer {
unsafe { free(input_short_path) }
}
long_path_buf := [4096]u16{}
res := C.GetLongPathName(input_short_path, &long_path_buf[0], sizeof(long_path_buf))
if res == 0 {
return error(get_error_msg(int(C.GetLastError())))
}
long_path := unsafe { string_from_wide(&long_path_buf[0]) }
return long_path
}
2 changes: 1 addition & 1 deletion vlib/os/signal.js.v
Expand Up @@ -115,7 +115,7 @@ fn signal_from_str(str JS.String) Signal {
// - Browser: Will use `window.addEventListener` for handling signal
//
// TODO: Add signal events implementation for browser backend
pub fn signal_opt(signum Signal, handler SignalHandler) ?SignalHandler {
pub fn signal_opt(signum Signal, handler SignalHandler) !SignalHandler {
signame := signal_str(signum)
_ := signame
$if js_node {
Expand Down

0 comments on commit daa9034

Please sign in to comment.