1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2016 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
// version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
// licence you accepted on initial access to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project generally, you agree to be
// bound by the terms of the MaidSafe Contributor Agreement.  This, along with the Licenses can be
// found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
//
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.
//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.

use {App, AppError};
use ffi_utils::{FFI_RESULT_OK, FfiResult, OpaqueCtx, SafePtr, catch_unwind_cb, from_c_str};
use futures::Future;
use object_cache::MDataInfoHandle;
use safe_core::FutureExt;
use safe_core::ipc::req::containers_into_vec;
use safe_core::ipc::req::ffi::ContainerPermissions;
use std::os::raw::{c_char, c_void};

/// Fetch access info from the network.
#[no_mangle]
pub unsafe extern "C" fn access_container_refresh_access_info(
    app: *const App,
    user_data: *mut c_void,
    o_cb: extern "C" fn(*mut c_void, FfiResult),
) {
    catch_unwind_cb(user_data, o_cb, || {
        let user_data = OpaqueCtx(user_data);

        (*app).send(move |client, context| {
            context
                .refresh_access_info(client)
                .then(move |res| {
                    call_result_cb!(res, user_data, o_cb);
                    Ok(())
                })
                .into_box()
                .into()
        })
    })
}

/// Retrieve a list of container names that an app has access to.
#[no_mangle]
pub unsafe extern "C" fn access_container_fetch(
    app: *const App,
    user_data: *mut c_void,
    o_cb: extern "C" fn(*mut c_void, FfiResult, *const ContainerPermissions, usize),
) {
    catch_unwind_cb(user_data, o_cb, || {
        let user_data = OpaqueCtx(user_data);

        (*app).send(move |client, context| {
            context
                .get_access_info(client)
                .and_then(move |containers| {
                    let ffi_containers = containers_into_vec(containers.into_iter().map(
                        |(key, (_, value))| (key, value),
                    ))?;
                    o_cb(
                        user_data.0,
                        FFI_RESULT_OK,
                        ffi_containers.as_safe_ptr(),
                        ffi_containers.len(),
                    );
                    Ok(())
                })
                .map_err(move |e| {
                    call_result_cb!(Err::<(), _>(e), user_data, o_cb);
                })
                .into_box()
                .into()
        })
    })
}

/// Retrieve `MDataInfo` for the given container name from the access container.
#[no_mangle]
pub unsafe extern "C" fn access_container_get_container_mdata_info(
    app: *const App,
    name: *const c_char,
    user_data: *mut c_void,
    o_cb: extern "C" fn(*mut c_void, FfiResult, MDataInfoHandle),
) {
    catch_unwind_cb(user_data, o_cb, || {
        let user_data = OpaqueCtx(user_data);
        let name = from_c_str(name)?;

        (*app).send(move |client, context| {
            let context = context.clone();

            context
                .get_access_info(client)
                .map(move |containers| if let Some(&(ref mdata_info, _)) =
                    containers.get(&name)
                {
                    let handle = context.object_cache().insert_mdata_info(mdata_info.clone());
                    o_cb(user_data.0, FFI_RESULT_OK, handle);
                } else {
                    call_result_cb!(Err::<(), _>(AppError::NoSuchContainer), user_data, o_cb);
                })
                .map_err(move |err| {
                    call_result_cb!(Err::<(), _>(err), user_data, o_cb);
                })
                .into_box()
                .into()
        })
    })
}

#[cfg(test)]
mod tests {
    use errors::AppError;
    use ffi::access_container::*;
    use ffi_utils::{ReprC, from_c_str};
    use ffi_utils::test_utils::{call_1, call_vec};
    use safe_core::DIR_TAG;
    use safe_core::ipc::req::{Permission, container_perms_from_repr_c};
    use std::collections::{BTreeSet, HashMap};
    use std::ffi::CString;
    use test_utils::{create_app_with_access, run_now};

    #[test]
    fn get_access_info() {
        let mut container_permissions = HashMap::new();
        let _ = container_permissions.insert("_videos".to_string(), btree_set![Permission::Read]);
        let app = create_app_with_access(container_permissions);

        // Get access container info
        let perms: Vec<PermSet> =
            unsafe { unwrap!(call_vec(|ud, cb| access_container_fetch(&app, ud, cb))) };

        let perms: HashMap<String, BTreeSet<Permission>> =
            perms.into_iter().map(|val| (val.0, val.1)).collect();

        assert_eq!(perms["_videos"], btree_set![Permission::Read]);
        assert_eq!(perms.len(), 2);

        // Get MD info
        let md_info_h = {
            let videos_str = unwrap!(CString::new("_videos"));
            unsafe {
                unwrap!(call_1(|ud, cb| {
                    access_container_get_container_mdata_info(&app, videos_str.as_ptr(), ud, cb)
                }))
            }
        };

        run_now(&app, move |_, context| {
            let info = unwrap!(context.object_cache().get_mdata_info(md_info_h));
            assert_eq!(info.type_tag, DIR_TAG);
        })
    }

    struct PermSet(String, BTreeSet<Permission>);

    impl ReprC for PermSet {
        type C = *const ContainerPermissions;
        type Error = AppError;

        unsafe fn clone_from_repr_c(c_repr: Self::C) -> Result<Self, Self::Error> {
            Ok(PermSet(
                from_c_str((*c_repr).cont_name)?,
                container_perms_from_repr_c((*c_repr).access)?,
            ))
        }
    }

}