rust/treefile: Use the c_utf8 crate

The advantage of this over CStr is that Rust knows it's UTF-8
too.  I also tweaked our path code to use String, and only
view it as a `Path`.  This avoids having to `unwrap()` later
back to a `str`.

Closes: #1588
Approved by: jlebon
This commit is contained in:
Colin Walters 2018-09-27 13:31:50 +00:00 committed by Atomic Bot
parent 1966167839
commit 7595bce228
3 changed files with 10 additions and 10 deletions

View File

@ -15,6 +15,7 @@ glib = "0.5.0"
tempfile = "3.0.3"
openat = "0.1.15"
curl = "0.4.14"
c_utf8 = "0.1.0"
[lib]
name = "rpmostree_rust"

View File

@ -16,6 +16,7 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
extern crate c_utf8;
extern crate curl;
extern crate gio_sys;
extern crate glib;

View File

@ -20,13 +20,12 @@
* https://github.com/cgwalters/coreos-assembler
* */
use c_utf8::CUtf8Buf;
use openat;
use serde_json;
use serde_yaml;
use std::ffi::{CStr, CString};
use std::io::prelude::*;
use std::os::unix::ffi::OsStringExt;
use std::path::{Path, PathBuf};
use std::path::Path;
use std::{fs, io};
use tempfile;
@ -35,7 +34,7 @@ const ARCH_X86_64: &'static str = "x86_64";
pub struct Treefile {
pub workdir: openat::Dir,
pub parsed: TreeComposeConfig,
pub rojig_spec: Option<Box<CStr>>,
pub rojig_spec: Option<CUtf8Buf>,
}
enum InputFormat {
@ -140,15 +139,15 @@ impl Treefile {
Ok(tmpf)
}
fn write_rojig_spec<'a, 'b>(workdir: &'a openat::Dir, r: &'b Rojig) -> io::Result<Box<CStr>> {
fn write_rojig_spec<'a, 'b>(workdir: &'a openat::Dir, r: &'b Rojig) -> io::Result<CUtf8Buf> {
let description = r
.description
.as_ref()
.and_then(|v| if v.len() > 0 { Some(v.as_str()) } else { None })
.unwrap_or(r.summary.as_str());
let name: PathBuf = format!("{}.spec", r.name).into();
let name: String = format!("{}.spec", r.name);
{
let mut f = workdir.write_file(&name, 0644)?;
let mut f = workdir.write_file(name.as_str(), 0644)?;
write!(
f,
r###"
@ -183,8 +182,7 @@ for x in *; do mv ${{x}} %{{buildroot}}%{{_prefix}}/lib/ostree-jigdo/%{{name}};
rpmostree_rojig_description = description,
)?;
}
let c_name = CString::new(name.into_os_string().into_vec())?;
Ok(c_name.into_boxed_c_str())
Ok(CUtf8Buf::from_string(name))
}
}
@ -513,7 +511,7 @@ rojig:
let tf = &t.tf;
let rojig = tf.parsed.rojig.as_ref().unwrap();
assert!(rojig.name == "exampleos");
let rojig_spec_str = tf.rojig_spec.as_ref().unwrap().to_str().unwrap();
let rojig_spec_str = tf.rojig_spec.as_ref().unwrap().as_str();
let rojig_spec = Path::new(rojig_spec_str);
assert!(rojig_spec.file_name().unwrap() == "exampleos.spec");
}