Patchwork [2,of,2] rust: convert Unix path to CString transparently

login
register
mail settings
Submitter Yuya Nishihara
Date Jan. 12, 2018, 2:22 p.m.
Message ID <4ca3b26ca272cf8c24b3.1515766929@mimosa>
Download mbox | patch
Permalink /patch/26704/
State Accepted
Headers show

Comments

Yuya Nishihara - Jan. 12, 2018, 2:22 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1515763122 -32400
#      Fri Jan 12 22:18:42 2018 +0900
# Node ID 4ca3b26ca272cf8c24b34deee8bc2530a8eccada
# Parent  44289d88954aaaa2a3c559c424fa1f2d85cb7e16
rust: convert Unix path to CString transparently

On Unix, path is just a sequence of bytes. We shouldn't convert it to UTF-8
string.

Patch

diff --git a/rust/hgcli/src/main.rs b/rust/hgcli/src/main.rs
--- a/rust/hgcli/src/main.rs
+++ b/rust/hgcli/src/main.rs
@@ -16,7 +16,7 @@  use std::env;
 use std::path::PathBuf;
 use std::ffi::{CString, OsStr};
 #[cfg(target_family = "unix")]
-use std::os::unix::ffi::OsStringExt;
+use std::os::unix::ffi::{OsStrExt, OsStringExt};
 
 #[derive(Debug)]
 struct Environment {
@@ -62,6 +62,14 @@  fn get_environment() -> Environment {
     }
 }
 
+// On UNIX, platform string is just bytes and should not contain NUL.
+#[cfg(target_family = "unix")]
+fn cstring_from_os<T: AsRef<OsStr>>(s: T) -> CString {
+    CString::new(s.as_ref().as_bytes()).unwrap()
+}
+
+// TODO convert to ANSI characters?
+#[cfg(target_family = "windows")]
 fn cstring_from_os<T: AsRef<OsStr>>(s: T) -> CString {
     CString::new(s.as_ref().to_str().unwrap()).unwrap()
 }