Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
johnschug committed Dec 12, 2023
1 parent 27b5441 commit df77a98
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 102 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

## Using

To use the crate, add it to your depedencies:
To use the crate, add it to your dependencies:
```sh
$ cargo add gpgme
```
Expand All @@ -36,6 +36,11 @@ On MacOS systems:
$ brew install gnupg
```

On Windows 10 (1709 or later) systems:
```pwsh
$ winget install --id GnuPG.Gpg4win
```

On Windows systems, download and install the official [Gpg4win] installer. Only
the `i686-pc-windows-gnu` target is supported.

Expand Down
2 changes: 0 additions & 2 deletions gpgme-sys/src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use libc;

pub mod errors {
pub use libgpg_error_sys::consts::*;
}
Expand Down
2 changes: 0 additions & 2 deletions src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use std::{
thread,
};

use ffi;
use libc;
use static_assertions::assert_obj_safe;

use crate::{edit, utils::FdWriter, Data, Error, Result};
Expand Down
166 changes: 102 additions & 64 deletions src/data.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(unix)]
use std::os::unix::io::AsRawFd;
use std::os::fd::{AsRawFd, BorrowedFd};
use std::{
borrow::BorrowMut,
error::Error as StdError,
Expand All @@ -17,7 +17,7 @@ use ffi;
use libc;
use static_assertions::{assert_impl_all, assert_not_impl_any};

use crate::{error::return_err, utils::CStrArgument, Error, NonNull, Result};
use crate::{utils::convert_err, utils::CStrArgument, Error, NonNull, Result};

assert_impl_all!(Data<'_>: Send);
assert_not_impl_any!(Data<'_>: Sync);
Expand Down Expand Up @@ -141,7 +141,7 @@ impl<'data> Data<'data> {
crate::init();
unsafe {
let mut data = ptr::null_mut();
return_err!(ffi::gpgme_data_new(&mut data));
convert_err(ffi::gpgme_data_new(&mut data))?;
Ok(Data::from_raw(data))
}
}
Expand All @@ -157,11 +157,11 @@ impl<'data> Data<'data> {
let path = path.into_cstr();
unsafe {
let mut data = ptr::null_mut();
return_err!(ffi::gpgme_data_new_from_file(
convert_err(ffi::gpgme_data_new_from_file(
&mut data,
path.as_ref().as_ptr(),
1,
));
))?;
Ok(Data::from_raw(data))
}
}
Expand All @@ -177,7 +177,7 @@ impl<'data> Data<'data> {
unsafe {
let (buf, len) = (bytes.as_ptr(), bytes.len());
let mut data = ptr::null_mut();
return_err!(ffi::gpgme_data_new_from_mem(&mut data, buf.cast(), len, 1));
convert_err(ffi::gpgme_data_new_from_mem(&mut data, buf.cast(), len, 1))?;
Ok(Data::from_raw(data))
}
}
Expand All @@ -193,7 +193,7 @@ impl<'data> Data<'data> {
unsafe {
let (buf, len) = (buf.as_ptr(), buf.len());
let mut data = ptr::null_mut();
return_err!(ffi::gpgme_data_new_from_mem(&mut data, buf.cast(), len, 0));
convert_err(ffi::gpgme_data_new_from_mem(&mut data, buf.cast(), len, 0))?;
Ok(Data::from_raw(data))
}
}
Expand All @@ -202,11 +202,26 @@ impl<'data> Data<'data> {
/// [`gpgme_data_new_from_fd`](https://www.gnupg.org/documentation/manuals/gpgme/File-Based-Data-Buffers.html#index-gpgme_005fdata_005fnew_005ffrom_005ffd)
#[inline]
#[cfg(unix)]
pub fn from_borrowed_fd(fd: &'data (impl AsFd + ?Sized)) -> Result<Self> {
crate::init();
let fd = fd.as_fd();
unsafe {
let mut data = ptr::null_mut();
convert_err(ffi::gpgme_data_new_from_fd(&mut data, fd.as_raw_fd()))?;
Ok(Data::from_raw(data))
}
}

/// Upstream documentation:
/// [`gpgme_data_new_from_fd`](https://www.gnupg.org/documentation/manuals/gpgme/File-Based-Data-Buffers.html#index-gpgme_005fdata_005fnew_005ffrom_005ffd)
#[inline]
#[cfg(unix)]
#[deprecated(reason = "Use from_borrowed_fd")]
pub fn from_fd(file: &'data (impl AsRawFd + ?Sized)) -> Result<Self> {
crate::init();
unsafe {
let mut data = ptr::null_mut();
return_err!(ffi::gpgme_data_new_from_fd(&mut data, file.as_raw_fd()));
convert_err(ffi::gpgme_data_new_from_fd(&mut data, file.as_raw_fd()));
Ok(Data::from_raw(data))
}
}
Expand All @@ -217,7 +232,7 @@ impl<'data> Data<'data> {
pub unsafe fn from_raw_file(file: *mut libc::FILE) -> Result<Self> {
crate::init();
let mut data = ptr::null_mut();
return_err!(ffi::gpgme_data_new_from_stream(&mut data, file));
convert_err(ffi::gpgme_data_new_from_stream(&mut data, file))?;
Ok(Data::from_raw(data))
}

Expand All @@ -229,11 +244,9 @@ impl<'data> Data<'data> {
let src = Box::into_raw(Box::new(CallbackWrapper { inner: src, cbs }));
let cbs = ptr::addr_of_mut!((*src).cbs);
let mut data = ptr::null_mut();
let result = ffi::gpgme_data_new_from_cbs(&mut data, cbs, src.cast());
if result == 0 {
Ok(Data::from_raw(data))
} else {
Err(WrappedError(Error::new(result), Box::from_raw(src).inner))
match convert_err(ffi::gpgme_data_new_from_cbs(&mut data, cbs, src.cast())) {
Ok(()) => Ok(Data::from_raw(data)),
Err(e) => Err(WrappedError(e, Box::from_raw(src).inner)),
}
}

Expand Down Expand Up @@ -345,7 +358,7 @@ impl<'data> Data<'data> {
#[inline]
pub fn clear_filename(&mut self) -> Result<()> {
unsafe {
return_err!(ffi::gpgme_data_set_file_name(self.as_raw(), ptr::null()));
convert_err(ffi::gpgme_data_set_file_name(self.as_raw(), ptr::null()))?;
}
Ok(())
}
Expand All @@ -356,10 +369,10 @@ impl<'data> Data<'data> {
pub fn set_filename(&mut self, name: impl CStrArgument) -> Result<()> {
let name = name.into_cstr();
unsafe {
return_err!(ffi::gpgme_data_set_file_name(
convert_err(ffi::gpgme_data_set_file_name(
self.as_raw(),
name.as_ref().as_ptr(),
));
))?;
}
Ok(())
}
Expand All @@ -375,7 +388,9 @@ impl<'data> Data<'data> {
/// [`gpgme_data_set_encoding`](https://www.gnupg.org/documentation/manuals/gpgme/Data-Buffer-Meta_002dData.html#index-gpgme_005fdata_005fset_005fencoding)
#[inline]
pub fn set_encoding(&mut self, enc: Encoding) -> Result<()> {
unsafe { return_err!(ffi::gpgme_data_set_encoding(self.as_raw(), enc.raw())) }
unsafe {
convert_err(ffi::gpgme_data_set_encoding(self.as_raw(), enc.raw()))?;
}
Ok(())
}

Expand All @@ -386,11 +401,11 @@ impl<'data> Data<'data> {
let name = name.into_cstr();
let value = value.into_cstr();
unsafe {
return_err!(ffi::gpgme_data_set_flag(
convert_err(ffi::gpgme_data_set_flag(
self.as_raw(),
name.as_ref().as_ptr(),
value.as_ref().as_ptr(),
));
))?;
}
Ok(())
}
Expand Down Expand Up @@ -546,90 +561,113 @@ impl<'a> IntoData<'a> for &mut Data<'a> {
}
}

impl<'a> IntoData<'a> for Data<'a> {
type Output = Self;
impl<'a, T> IntoData<'a> for T
where
T: TryInto<Data<'a>, Error = Error>,
{
type Output = Data<'a>;

fn into_data(self) -> Result<Self> {
Ok(self)
fn into_data(self) -> Result<Self::Output> {
self.try_into()
}
}

impl<'a> IntoData<'a> for &'a [u8] {
type Output = Data<'a>;
impl<'a> TryFrom<&'a [u8]> for Data<'a> {
type Error = Error;

#[inline]
fn try_from(value: &'a [u8]) -> Result<Self> {
Self::from_buffer(value)
}
}

fn into_data(self) -> Result<Data<'a>> {
Data::from_seekable_reader(Cursor::new(self)).map_err(|e| e.error())
impl<'a> TryFrom<&'a mut [u8]> for Data<'a> {
type Error = Error;

#[inline]
fn try_from(value: &'a mut [u8]) -> Result<Self> {
Self::from_seekable_stream(Cursor::new(value)).map_err(|e| e.error())
}
}

impl<'a> IntoData<'a> for &'a mut [u8] {
type Output = Data<'a>;
impl<'a> TryFrom<&'a str> for Data<'a> {
type Error = Error;

fn into_data(self) -> Result<Data<'a>> {
Data::from_seekable_stream(Cursor::new(self)).map_err(|e| e.error())
#[inline]
fn try_from(value: &'a str) -> Result<Self> {
value.as_bytes().try_into()
}
}

impl<'a> IntoData<'a> for &'a Vec<u8> {
type Output = Data<'a>;
impl<'a> TryFrom<&'a Vec<u8>> for Data<'a> {
type Error = Error;

fn into_data(self) -> Result<Data<'a>> {
self.as_slice().into_data()
#[inline]
fn try_from(value: &'a Vec<u8>) -> Result<Self> {
value.as_slice().try_into()
}
}

impl<'a> IntoData<'a> for &'a mut Vec<u8> {
type Output = Data<'a>;
impl<'a> TryFrom<&'a mut Vec<u8>> for Data<'a> {
type Error = Error;

fn into_data(self) -> Result<Data<'a>> {
Data::from_seekable_stream(Cursor::new(self)).map_err(|e| e.error())
#[inline]
fn try_from(value: &'a mut Vec<u8>) -> std::result::Result<Self, Self::Error> {
value.as_mut_slice().try_into()
}
}

impl IntoData<'static> for Vec<u8> {
type Output = Data<'static>;
impl<'a> TryFrom<Vec<u8>> for Data<'a> {
type Error = Error;

fn into_data(self) -> Result<Data<'static>> {
Data::from_seekable_stream(Cursor::new(self)).map_err(|e| e.error())
#[inline]
fn try_from(value: Vec<u8>) -> std::result::Result<Self, Self::Error> {
Self::from_seekable_stream(Cursor::new(value)).map_err(|e| e.error())
}
}

impl<'a> IntoData<'a> for &'a str {
type Output = Data<'a>;
impl<'a> TryFrom<String> for Data<'a> {
type Error = Error;

fn into_data(self) -> Result<Data<'a>> {
self.as_bytes().into_data()
#[inline]
fn try_from(value: String) -> std::result::Result<Self, Self::Error> {
value.into_bytes().try_into()
}
}

impl IntoData<'static> for String {
type Output = Data<'static>;
impl<'a> TryFrom<&'a File> for Data<'a> {
type Error = Error;

fn into_data(self) -> Result<Data<'static>> {
self.into_bytes().into_data()
#[inline]
fn try_from(value: &'a File) -> std::result::Result<Self, Self::Error> {
Self::from_seekable_stream(value).map_err(|e| e.error())
}
}

impl<'a> IntoData<'a> for &'a File {
type Output = Data<'a>;
impl<'a> TryFrom<&'a mut File> for Data<'a> {
type Error = Error;

fn into_data(self) -> Result<Data<'a>> {
Data::from_seekable_stream(self).map_err(|e| e.error())
#[inline]
fn try_from(value: &'a mut File) -> std::result::Result<Self, Self::Error> {
Self::try_from(&*value)
}
}

impl<'a> IntoData<'a> for &'a mut File {
type Output = Data<'a>;
impl<'a> TryFrom<File> for Data<'a> {
type Error = Error;

fn into_data(self) -> Result<Data<'a>> {
Data::from_seekable_stream(self).map_err(|e| e.error())
#[inline]
fn try_from(value: File) -> std::result::Result<Self, Self::Error> {
Self::from_seekable_stream(value).map_err(|e| e.error())
}
}

impl IntoData<'static> for File {
type Output = Data<'static>;
#[cfg(unix)]
impl<'a> TryFrom<BorrowedFd<'a>> for Data<'a> {
type Error = Error;

fn into_data(self) -> Result<Data<'static>> {
Data::from_seekable_stream(self).map_err(|e| e.error())
#[inline]
fn try_from(value: BorrowedFd<'a>) -> Result<Self> {
Data::from_borrowed_fd(self)
}
}
22 changes: 11 additions & 11 deletions src/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,18 @@ ffi_enum_wrapper! {

impl StatusCode {
pub fn needs_response(&self) -> bool {
match self {
matches!(
self,
Self::AlreadySigned
| Self::Error
| Self::GetBool
| Self::GetLine
| Self::KeyCreated
| Self::NeedPassphraseSym
| Self::ScOpFailure
| Self::CardCtrl
| Self::BackupKeyCreated => true,
_ => false,
}
| Self::Error
| Self::GetBool
| Self::GetLine
| Self::KeyCreated
| Self::NeedPassphraseSym
| Self::ScOpFailure
| Self::CardCtrl
| Self::BackupKeyCreated
)
}

pub fn into_result(self) -> Result<()> {
Expand Down
2 changes: 0 additions & 2 deletions src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
use std::{ffi::CStr, fmt, str::Utf8Error};

use bitflags::bitflags;
use ffi;
use libc;

bitflags! {
/// Upstream documentation:
Expand Down
2 changes: 1 addition & 1 deletion src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ impl fmt::Debug for UserId<'_> {
impl fmt::Display for UserId<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(
&*self
&self
.id_raw()
.map(|s| s.to_string_lossy())
.unwrap_or("".into()),
Expand Down
Loading

0 comments on commit df77a98

Please sign in to comment.