From 44b4d324a0176033107789d1421549b24898ac8a Mon Sep 17 00:00:00 2001 From: Andy Wortman Date: Sat, 11 Nov 2017 03:25:04 -0800 Subject: graceful shutdown, fix profile corruption bug profile corruption was trailing json left by not truncating when profile shrinks in size --- src/tw/mod.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'src/tw') diff --git a/src/tw/mod.rs b/src/tw/mod.rs index 767aae9..e2b8b03 100644 --- a/src/tw/mod.rs +++ b/src/tw/mod.rs @@ -23,6 +23,7 @@ pub mod user; use self::user::User; pub enum AppState { + Shutdown, Reconnect, Compose, View @@ -349,18 +350,20 @@ impl TwitterCache { } } pub fn store_cache(&mut self) { - if Path::new(TwitterCache::PROFILE_DIR).is_dir() { - let profile = OpenOptions::new() - .write(true) - .create(true) - .append(false) - .open(TwitterCache::PROFILE_CACHE) - .unwrap(); - serde_json::to_writer(profile, self).unwrap(); - } else { - self.display_info.status("No cache dir exists...".to_owned()); + if self.caching_permitted { + if Path::new(TwitterCache::PROFILE_DIR).is_dir() { + let profile = OpenOptions::new() + .write(true) + .create(true) + .append(false) + .truncate(true) // since this one can become smaller, lop off trailing characters + .open(TwitterCache::PROFILE_CACHE) + .unwrap(); + serde_json::to_writer(profile, self).unwrap(); + } else { + self.display_info.status("No cache dir exists...".to_owned()); + } } - // store cache } fn number_and_insert_tweet(&mut self, mut tw: Tweet) { if !self.tweets.contains_key(&tw.id.to_owned()) { -- cgit v1.1