Skip to content

Commit

Permalink
rust/treefile: Use the c_utf8 crate
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cgwalters authored and rh-atomic-bot committed Sep 28, 2018
1 parent 1966167 commit 7595bce
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 8 additions & 10 deletions rust/src/treefile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand Down Expand Up @@ -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###"
Expand Down Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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");
}
Expand Down

0 comments on commit 7595bce

Please sign in to comment.