Skip to content

Commit

Permalink
Move RequestSourceInfo and dec related from cyfs-base to cyfs-lib
Browse files Browse the repository at this point in the history
Improve op-env's dependency on RequestSourceInfo
  • Loading branch information
lurenpluto committed Sep 27, 2022
1 parent 6f89c01 commit b53a00f
Show file tree
Hide file tree
Showing 35 changed files with 245 additions and 188 deletions.
4 changes: 0 additions & 4 deletions src/component/cyfs-base/src/base/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ mod name;
mod paths;
mod ports;
mod protocol_fields;
mod source;
mod system;
mod time;

pub use access_string::*;
Expand All @@ -22,6 +20,4 @@ pub use name::*;
pub use paths::*;
pub use ports::*;
pub use protocol_fields::*;
pub use source::*;
pub use system::*;
pub use time::*;
47 changes: 32 additions & 15 deletions src/component/cyfs-base/src/objects/object_map/op_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,33 @@ impl ObjectMapOpEnv {

use std::collections::HashMap;

pub struct OpEnvSourceInfo {
pub dec: ObjectId,
pub device: Option<DeviceId>,
}

impl std::fmt::Debug for OpEnvSourceInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self, f)
}
}

impl std::fmt::Display for OpEnvSourceInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "dec={}, device={:?}", self.dec, self.device)
}
}

struct ObjectMapOpEnvHolder {
last_access: u64,
op_env: ObjectMapOpEnv,
source: Option<RequestSourceInfo>,
source: Option<OpEnvSourceInfo>,
}

const OP_ENV_EXPIRED_DURATION: u64 = 1000 * 1000 * 60 * 60;

impl ObjectMapOpEnvHolder {
fn new(op_env: ObjectMapOpEnv, source: Option<RequestSourceInfo>) -> Self {
fn new(op_env: ObjectMapOpEnv, source: Option<OpEnvSourceInfo>) -> Self {
Self {
last_access: bucky_time_now(),
op_env,
Expand Down Expand Up @@ -244,11 +261,11 @@ impl ObjectMapOpEnvHolder {
self.last_access = bucky_time_now();
}

fn compare_source(&self, source: Option<&RequestSourceInfo>) -> bool {
fn compare_source(&self, source: Option<&OpEnvSourceInfo>) -> bool {
match &self.source {
Some(this) => match source {
Some(source) => {
if this.dec == source.dec && this.zone.device == source.zone.device {
if this.dec == source.dec && this.device == source.device {
true
} else {
false
Expand Down Expand Up @@ -313,7 +330,7 @@ impl ObjectMapOpEnvContainer {
}
}

pub fn add_env(&self, env: ObjectMapOpEnv, source: Option<RequestSourceInfo>) {
pub fn add_env(&self, env: ObjectMapOpEnv, source: Option<OpEnvSourceInfo>) {
let sid = env.sid();
let holder = ObjectMapOpEnvHolder::new(env, source);
let prev = self.all.lock().unwrap().insert(sid, holder);
Expand All @@ -323,7 +340,7 @@ impl ObjectMapOpEnvContainer {
pub fn get_op_env(
&self,
sid: u64,
source: Option<&RequestSourceInfo>,
source: Option<&OpEnvSourceInfo>,
) -> BuckyResult<ObjectMapOpEnv> {
let mut list = self.all.lock().unwrap();
let ret = list.get_mut(&sid);
Expand All @@ -349,41 +366,41 @@ impl ObjectMapOpEnvContainer {
}
}

pub fn get_path_op_env(&self, sid: u64, source: Option<&RequestSourceInfo>,) -> BuckyResult<ObjectMapPathOpEnvRef> {
pub fn get_path_op_env(&self, sid: u64, source: Option<&OpEnvSourceInfo>,) -> BuckyResult<ObjectMapPathOpEnvRef> {
let op_env = self.get_op_env(sid, source)?;
op_env.path_op_env(sid)
}

pub fn get_single_op_env(&self, sid: u64, source: Option<&RequestSourceInfo>,) -> BuckyResult<ObjectMapSingleOpEnvRef> {
pub fn get_single_op_env(&self, sid: u64, source: Option<&OpEnvSourceInfo>,) -> BuckyResult<ObjectMapSingleOpEnvRef> {
let op_env = self.get_op_env(sid, source)?;
op_env.single_op_env(sid)
}

pub async fn get_current_root(&self, sid: u64, source: Option<&RequestSourceInfo>,) -> BuckyResult<ObjectId> {
pub async fn get_current_root(&self, sid: u64, source: Option<&OpEnvSourceInfo>,) -> BuckyResult<ObjectId> {
let op_env = self.get_op_env(sid, source)?;

op_env.get_current_root().await
}

pub async fn update(&self, sid: u64, source: Option<&RequestSourceInfo>,) -> BuckyResult<ObjectId> {
pub async fn update(&self, sid: u64, source: Option<&OpEnvSourceInfo>,) -> BuckyResult<ObjectId> {
let op_env = self.get_op_env(sid, source)?;

op_env.update().await
}

pub async fn commit(&self, sid: u64, source: Option<&RequestSourceInfo>,) -> BuckyResult<ObjectId> {
pub async fn commit(&self, sid: u64, source: Option<&OpEnvSourceInfo>,) -> BuckyResult<ObjectId> {
let item = self.remove(sid, source)?;

item.into_op_env().commit().await
}

pub fn abort(&self, sid: u64, source: Option<&RequestSourceInfo>,) -> BuckyResult<()> {
pub fn abort(&self, sid: u64, source: Option<&OpEnvSourceInfo>,) -> BuckyResult<()> {
let item = self.remove(sid, source)?;

item.into_op_env().abort()
}

fn remove(&self, sid: u64, source: Option<&RequestSourceInfo>,) -> BuckyResult<ObjectMapOpEnvHolder> {
fn remove(&self, sid: u64, source: Option<&OpEnvSourceInfo>,) -> BuckyResult<ObjectMapOpEnvHolder> {
let mut all = self.all.lock().unwrap();
let ret = all.get(&sid);
if ret.is_none() {
Expand Down Expand Up @@ -494,7 +511,7 @@ impl ObjectMapRootManager {
pub async fn create_managed_op_env(
&self,
access: Option<OpEnvPathAccess>,
source: Option<RequestSourceInfo>,
source: Option<OpEnvSourceInfo>,
) -> BuckyResult<ObjectMapPathOpEnvRef> {
let env = self.create_op_env(access).await?;

Expand Down Expand Up @@ -524,7 +541,7 @@ impl ObjectMapRootManager {
pub fn create_managed_single_op_env(
&self,
access: Option<OpEnvPathAccess>,
source: Option<RequestSourceInfo>
source: Option<OpEnvSourceInfo>
) -> BuckyResult<ObjectMapSingleOpEnvRef> {
let env = self.create_single_op_env(access)?;
self.all_envs.add_env(ObjectMapOpEnv::Single(env.clone()), source);
Expand Down
66 changes: 48 additions & 18 deletions src/component/cyfs-core/src/app/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@ use super::dec_app::*;
use cyfs_base::ObjectId;


// get the default dec_app id for all the system service and system core
pub fn get_system_dec_app() -> &'static ObjectId {
cyfs_base::get_system_dec_app()
}

pub fn is_system_dec_app(dec_id: &Option<ObjectId>) -> bool {
cyfs_base::is_system_dec_app(dec_id)
}

pub struct SystemDecApp;

impl SystemDecApp {
Expand All @@ -27,20 +18,59 @@ impl SystemDecApp {
.try_into()
.unwrap()
}
}


use once_cell::sync::OnceCell;
use std::borrow::Cow;

pub(crate) static SYSTEM_DEC_APP: OnceCell<ObjectId> = OnceCell::new();
pub(crate) static ANONYMOUS_DEC_APP: OnceCell<ObjectId> = OnceCell::new();

fn init_system_dec_id() {
let dec_id = Self::gen_system_dec_id().into();
cyfs_base::init_system_dec_app(dec_id);
// get the default dec_app id for all the system service and system core
pub fn get_system_dec_app() -> &'static ObjectId {
SYSTEM_DEC_APP.get_or_init(|| {
SystemDecApp::gen_system_dec_id().into()
})
}

pub fn is_system_dec_app(dec_id: &Option<ObjectId>) -> bool {
match dec_id {
Some(id) => {
id == get_system_dec_app()
}
None => {
true
}
}
}


fn init_anonymous_dec_id() {
let dec_id = Self::gen_anonymous_dec_id().into();
cyfs_base::init_anonymous_dec_app(dec_id);
// get the default dec_app id for all the unknown service and incoming reqeust
pub fn get_anonymous_dec_app() -> &'static ObjectId {
ANONYMOUS_DEC_APP.get_or_init(|| {
SystemDecApp::gen_anonymous_dec_id().into()
})
}

pub fn is_anonymous_dec_app(dec_id: &Option<ObjectId>) -> bool {
match dec_id {
Some(id) => {
id == get_anonymous_dec_app()
}
None => {
true
}
}
}

pub fn init() {
Self::init_system_dec_id();
Self::init_anonymous_dec_id();
pub fn dec_id_to_string(dec_id: &ObjectId) -> Cow<str> {
if dec_id == get_system_dec_app() {
Cow::Borrowed("system")
} else if dec_id == get_anonymous_dec_app() {
Cow::Borrowed("anonymous")
} else {
Cow::Owned(dec_id.to_string())
}
}

Expand Down
1 change: 1 addition & 0 deletions src/component/cyfs-lib/src/acl/handler/request.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::super::{AclAccess, AclAction};
use crate::base::*;
use crate::ndn::*;
use crate::non::NONSlimObjectInfo;
use cyfs_base::*;
Expand Down
6 changes: 4 additions & 2 deletions src/component/cyfs-lib/src/base/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ mod base_requestor;
mod config;
mod exp_filter;
mod protocol;
mod range;
mod request;
mod requestor_helper;
mod select_request;
mod source;
mod tcp_listener;
mod zone;
mod range;

pub use base_requestor::*;
pub use config::*;
pub use exp_filter::*;
pub use protocol::*;
pub use range::*;
pub use request::*;
pub use requestor_helper::*;
pub use select_request::*;
pub use source::*;
pub use tcp_listener::*;
pub use zone::*;
pub use range::*;
1 change: 0 additions & 1 deletion src/component/cyfs-lib/src/base/protocol.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::str::FromStr;

pub use cyfs_base::RequestProtocol;

// pub const CYFS_OBJECT_MIME_STRING: &str = "application/cyfs-object";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::*;
use cyfs_base::*;
use cyfs_core::*;

use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::{Map, Value};
Expand Down Expand Up @@ -233,7 +234,7 @@ impl std::fmt::Display for RequestSourceInfo {
self.zone.zone_category,
self.zone.device,
self.zone.zone,
crate::dec_id_to_string(&self.dec),
cyfs_core::dec_id_to_string(&self.dec),
self.verified,
)
}
Expand Down Expand Up @@ -340,7 +341,7 @@ impl RequestSourceInfo {
true
} else {
warn!("request source pass verify but target_dec_id not match! pass={}, required={}",
dec_id_to_string(&id), dec_id_to_string(&target_dec_id));
cyfs_core::dec_id_to_string(&id), cyfs_core::dec_id_to_string(&target_dec_id));

false
}
Expand Down Expand Up @@ -519,6 +520,14 @@ impl<'de> Deserialize<'de> for DeviceZoneCategory {
}
}

impl Into<OpEnvSourceInfo> for RequestSourceInfo {
fn into(self) -> OpEnvSourceInfo {
OpEnvSourceInfo {
dec: self.dec,
device: self.zone.device,
}
}
}
#[cfg(test)]
mod test {
use super::*;
Expand Down
4 changes: 2 additions & 2 deletions src/component/cyfs-lib/src/crypto/input_request.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::output_request::*;
use crate::base::*;
use crate::non::*;
use cyfs_base::*;

Expand Down Expand Up @@ -50,8 +51,7 @@ impl fmt::Display for CryptoSignObjectInputRequest {
}
}


pub type CryptoSignObjectInputResponse = CryptoSignObjectOutputResponse;
pub type CryptoSignObjectInputResponse = CryptoSignObjectOutputResponse;

#[derive(Debug, Clone)]
pub struct CryptoVerifyObjectInputRequest {
Expand Down
11 changes: 6 additions & 5 deletions src/component/cyfs-lib/src/prelude/global_state_common.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::base::*;
use crate::GlobalStateCategory;
use cyfs_base::*;

Expand Down Expand Up @@ -60,14 +61,14 @@ impl RequestGlobalStatePath {
}

pub fn new_system_dec(req_path: Option<impl Into<String>>) -> Self {
Self::new(Some(cyfs_base::get_system_dec_app().to_owned()), req_path)
Self::new(Some(cyfs_core::get_system_dec_app().to_owned()), req_path)
}

pub fn set_root(&mut self, root: ObjectId) {
self.global_state_root = Some(RequestGlobalStateRoot::GlobalRoot(root));
self.global_state_root = Some(RequestGlobalStateRoot::GlobalRoot(root));
}
pub fn set_dec_root(&mut self, dec_root: ObjectId) {
self.global_state_root = Some(RequestGlobalStateRoot::DecRoot(dec_root));
self.global_state_root = Some(RequestGlobalStateRoot::DecRoot(dec_root));
}

pub fn category(&self) -> GlobalStateCategory {
Expand Down Expand Up @@ -283,11 +284,11 @@ mod test {
let r = RequestGlobalStatePath::parse(&s).unwrap();
assert_eq!(root, r);

root.dec_id = Some(cyfs_base::get_system_dec_app().to_owned());
root.dec_id = Some(cyfs_core::get_system_dec_app().to_owned());
let s = root.format_string();
println!("{}", s);
let r = RequestGlobalStatePath::parse(&s).unwrap();
assert_eq!(r.dec_id, Some(cyfs_base::get_system_dec_app().to_owned()));
assert_eq!(r.dec_id, Some(cyfs_core::get_system_dec_app().to_owned()));

let root = RequestGlobalStatePath {
global_state_category: None,
Expand Down
Loading

0 comments on commit b53a00f

Please sign in to comment.