Skip to content

Commit

Permalink
Ensure we flush data when it changes
Browse files Browse the repository at this point in the history
In various cases changeing data in the cache would not cause a flush to
the json file. Writes are rare so even for the very inefficient json
file storage it is ok to just flush (and therefore rewrite the entire
file) all the changes as soon as any modification operation happens.

Signed-off-by: Simo Sorce <[email protected]>
  • Loading branch information
simo5 committed Mar 13, 2024
1 parent 6192f61 commit daa9061
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/storage/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,15 @@ impl Storage for JsonStorage {
self.cache.get_by_unique_id_mut(uid)
}
fn store(&mut self, uid: String, obj: Object) -> KResult<()> {
self.cache.store(uid, obj)
self.cache.store(uid, obj)?;
self.flush()
}
fn search(&self, template: &[CK_ATTRIBUTE]) -> Vec<&Object> {
self.cache.search(template)
}
fn remove_by_unique_id(&mut self, uid: &String) -> KResult<()> {
self.cache.remove_by_unique_id(uid)
self.cache.remove_by_unique_id(uid)?;
self.flush()
}
fn get_rough_size_by_unique_id(&self, uid: &String) -> KResult<usize> {
let obj = self.cache.get_by_unique_id(uid)?;
Expand Down
15 changes: 3 additions & 12 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ pub struct Token {
storage: Box<dyn Storage>,
handles: HashMap<CK_OBJECT_HANDLE, String>,
next_handle: CK_OBJECT_HANDLE,
dirty: bool,
so_login: LoginData,
user_login: LoginData,
}
Expand Down Expand Up @@ -143,7 +142,6 @@ impl Token {
attempts: 0,
logged_in: false,
},
dirty: false,
};

/* register mechanisms and factories */
Expand Down Expand Up @@ -239,7 +237,6 @@ impl Token {
},
Err(_) => return CKR_GENERAL_ERROR,
}
self.dirty = true;

/* add pin to so_object */
match self.store_pin_object(
Expand All @@ -251,7 +248,7 @@ impl Token {
Err(_) => return CKR_GENERAL_ERROR,
}

match self.save() {
match self.storage.flush() {
Ok(_) => {
self.info.flags |= CKF_TOKEN_INITIALIZED;
CKR_OK
Expand Down Expand Up @@ -541,17 +538,13 @@ impl Token {
/* If we set a PIN it means we switched to require Logins */
self.info.flags |= CKF_LOGIN_REQUIRED;

self.dirty = true;
match self.save() {
match self.storage.flush() {
Ok(()) => CKR_OK,
Err(_) => CKR_GENERAL_ERROR,
}
}

pub fn save(&mut self) -> KResult<()> {
if !self.dirty {
return Ok(());
}
self.storage.flush()
}

Expand All @@ -569,7 +562,6 @@ impl Token {
if !self.is_logged_in(KRY_UNSPEC) {
return err_rv!(CKR_USER_NOT_LOGGED_IN);
}
self.dirty = true;
} else {
obj.set_session(s_handle);
}
Expand Down Expand Up @@ -642,8 +634,7 @@ impl Token {
None => return err_rv!(CKR_OBJECT_HANDLE_INVALID),
};
self.object_factories.set_object_attributes(obj, template)?;
self.dirty = true;
Ok(())
self.storage.flush()
}

pub fn drop_session_objects(&mut self, handle: CK_SESSION_HANDLE) {
Expand Down

0 comments on commit daa9061

Please sign in to comment.