summaryrefslogtreecommitdiffstats
path: root/openpgp/src/packet/literal.rs
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-03-22 11:33:09 +0100
committerJustus Winter <justus@sequoia-pgp.org>2019-03-22 11:33:09 +0100
commita533d1e81d683eb0e1679f084c604d0674b5e130 (patch)
tree8284e393a0e91f1d46cbbbd0fe8141282d270ab1 /openpgp/src/packet/literal.rs
parent2ac59632525d4997bfc2b9ce08fa9cf75d57d8a7 (diff)
openpgp, core: Return old value in setters.
- Fixes #147.
Diffstat (limited to 'openpgp/src/packet/literal.rs')
-rw-r--r--openpgp/src/packet/literal.rs29
1 files changed, 19 insertions, 10 deletions
diff --git a/openpgp/src/packet/literal.rs b/openpgp/src/packet/literal.rs
index 85b90be4..e892ea2b 100644
--- a/openpgp/src/packet/literal.rs
+++ b/openpgp/src/packet/literal.rs
@@ -86,8 +86,8 @@ impl Literal {
}
/// Sets the Literal packet's body to the provided byte string.
- pub fn set_body(&mut self, data: Vec<u8>) {
- self.common.set_body(data);
+ pub fn set_body(&mut self, data: Vec<u8>) -> Vec<u8> {
+ self.common.set_body(data)
}
/// Gets the Literal packet's content disposition.
@@ -117,15 +117,15 @@ impl Literal {
/// Note: when a literal data packet is protected by a signature,
/// only the literal data packet's body is protected, not the
/// meta-data. As such, this field should not be used.
- pub fn set_filename_from_bytes(&mut self, filename: &[u8]) -> Result<()> {
- self.filename = match filename.len() {
+ pub fn set_filename_from_bytes(&mut self, filename: &[u8])
+ -> Result<Option<Vec<u8>>> {
+ Ok(::std::mem::replace(&mut self.filename, match filename.len() {
0 => None,
1...255 => Some(filename.to_vec()),
n => return
Err(Error::InvalidArgument(
format!("filename too long: {} bytes", n)).into()),
- };
- Ok(())
+ }))
}
/// Sets the literal packet's filename field from a UTF-8 encoded
@@ -137,7 +137,8 @@ impl Literal {
/// Note: when a literal data packet is protected by a signature,
/// only the literal data packet's body is protected, not the
/// meta-data. As such, this field should not be used.
- pub fn set_filename(&mut self, filename: &str) -> Result<()> {
+ pub fn set_filename(&mut self, filename: &str)
+ -> Result<Option<Vec<u8>>> {
self.set_filename_from_bytes(filename.as_bytes())
}
@@ -159,9 +160,17 @@ impl Literal {
/// Note: when a literal data packet is protected by a signature,
/// only the literal data packet's body is protected, not the
/// meta-data. As such, this field should not be used.
- pub fn set_date(&mut self, timestamp: Option<time::Tm>) {
- self.date = timestamp.map(|t| t.canonicalize())
- .unwrap_or(time::Tm::from_pgp(0));
+ pub fn set_date(&mut self, timestamp: Option<time::Tm>) -> Option<time::Tm>
+ {
+ let old = ::std::mem::replace(
+ &mut self.date,
+ timestamp.map(|t| t.canonicalize())
+ .unwrap_or(time::Tm::from_pgp(0)));
+ if old == time::Tm::from_pgp(0) {
+ None
+ } else {
+ Some(old)
+ }
}
}