Use cxx-rs for utils.rs download_to_fd

A step towards converting all of utils.
This commit is contained in:
Colin Walters 2020-12-19 14:38:47 +00:00 committed by OpenShift Merge Robot
parent 9565c19ef0
commit 8ab6a1f4f0
3 changed files with 23 additions and 26 deletions

View File

@ -19,6 +19,11 @@ mod ffi {
fn prepare_tempetc_guard(rootfs: i32) -> Result<Box<TempEtcGuard>>;
fn undo(self: &TempEtcGuard) -> Result<()>;
}
// utils.rs
extern "Rust" {
fn download_to_fd(url: &str) -> Result<i32>;
}
}
mod cliwrap;

View File

@ -206,19 +206,8 @@ mod ffi {
use std::os::unix::io::IntoRawFd;
use std::ptr;
#[no_mangle]
pub extern "C" fn ror_download_to_fd(
url: *const libc::c_char,
gerror: *mut *mut glib_sys::GError,
) -> libc::c_int {
let url: Borrowed<glib::GString> = unsafe { from_glib_borrow(url) };
match download_url_to_tmpfile(url.as_str()) {
Ok(f) => f.into_raw_fd(),
Err(e) => {
error_to_glib(&e, gerror);
-1
}
}
pub(crate) fn download_to_fd(url: &str) -> Result<i32> {
download_url_to_tmpfile(url).map(|f| f.into_raw_fd())
}
#[no_mangle]

View File

@ -35,6 +35,7 @@
#include "rpmostree-util.h"
#include "rpmostree-rpm-util.h"
#include "rpmostree-rust.h"
#include "rpmostree-cxxrs.h"
#define RPMOSTREE_CLI_ID "cli"
@ -1056,18 +1057,20 @@ rpmostree_sort_pkgs_strv (const char *const* pkgs,
g_auto(GVariantBuilder) builder;
g_variant_builder_init (&builder, G_VARIANT_TYPE ("ah"));
for (const char *const* pkg = pkgs; pkg && *pkg; pkg++)
for (const char *const* pkgiter = pkgs; pkgiter && *pkgiter; pkgiter++)
{
if (g_str_has_prefix (*pkg, "http://") ||
g_str_has_prefix (*pkg, "https://"))
auto pkg = *pkgiter;
if (g_str_has_prefix (pkg, "http://") ||
g_str_has_prefix (pkg, "https://"))
{
g_print ("Downloading '%s'... ", *pkg);
glnx_autofd int fd = ror_download_to_fd (*pkg, error);
if (fd < 0)
{
g_print ("failed!\n");
return FALSE;
}
g_print ("Downloading '%s'... ", pkg);
glnx_autofd int fd = -1;
try {
fd = rpmostreecxx::download_to_fd (pkg);
} catch (std::exception& e) {
g_print ("failed!\n");
throw;
}
g_print ("done!\n");
int idx = g_unix_fd_list_append (fd_list, fd, error);
@ -1076,12 +1079,12 @@ rpmostree_sort_pkgs_strv (const char *const* pkgs,
g_variant_builder_add (&builder, "h", idx);
}
else if (!g_str_has_suffix (*pkg, ".rpm"))
g_ptr_array_add (repo_pkgs, g_strdup (*pkg));
else if (!g_str_has_suffix (pkg, ".rpm"))
g_ptr_array_add (repo_pkgs, g_strdup (pkg));
else
{
glnx_autofd int fd = -1;
if (!glnx_openat_rdonly (AT_FDCWD, *pkg, TRUE, &fd, error))
if (!glnx_openat_rdonly (AT_FDCWD, pkg, TRUE, &fd, error))
return FALSE;
int idx = g_unix_fd_list_append (fd_list, fd, error);