Diff
Not logged in

Differences From Artifact [eb578e9cdd]:

To Artifact [664ebb15f1]:


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
..
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
...
128
129
130
131
132
133
134

135
136
137
138
139
140
141
142
...
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180


181
182
183
184
185
186
187
188
...
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208


209


210
211
212
213
214
215
216
217
// 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, version 1.0.
// 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;
use errors::AppError;
use ffi::helper::send_with_mdata_info;
use ffi_utils::{OpaqueCtx, ReprC, catch_unwind_cb, from_c_str};
use futures::Future;
use object_cache::MDataInfoHandle;
use safe_core::FutureExt;
use safe_core::nfs::File as NativeFile;
use safe_core::nfs::ffi::File;
use safe_core::nfs::file_helper;
use std::os::raw::{c_char, c_void};
................................................................................

/// Retrieve file with the given name, and its version, from the directory.
#[no_mangle]
pub unsafe extern "C" fn file_fetch(app: *const App,
                                    parent_h: MDataInfoHandle,
                                    file_name: *const c_char,
                                    user_data: *mut c_void,
                                    o_cb: extern "C" fn(*mut c_void, i32, *const File, u64)) {
    catch_unwind_cb(user_data, o_cb, || {
        let file_name = from_c_str(file_name)?;
        let user_data = OpaqueCtx(user_data);

        (*app).send(move |client, context| {
            let parent = try_cb!(context.object_cache().get_mdata_info(parent_h),
                                 user_data.0,
                                 o_cb);

            file_helper::fetch(client.clone(), parent.clone(), file_name)
                .map(move |(version, file)| {
                    let ffi_file = file.into_repr_c();
                    o_cb(user_data.0, 0, &ffi_file, version)
                })
                .map_err(AppError::from)
                .map_err(move |err| o_cb(user_data.0, ffi_error_code!(err), ptr::null(), 0))









                .into_box()
                .into()
        })
    })
}

/// Insert the file into the parent directory.
#[no_mangle]
pub unsafe extern "C" fn file_insert(app: *const App,
                                     parent_h: MDataInfoHandle,
                                     file_name: *const c_char,
                                     file: *const File,
                                     user_data: *mut c_void,
                                     o_cb: extern "C" fn(*mut c_void, i32)) {
    catch_unwind_cb(user_data, o_cb, || {
        let file = NativeFile::clone_from_repr_c(file)?;
        let file_name = from_c_str(file_name)?;

        send_with_mdata_info(app, parent_h, user_data, o_cb, move |client, _, parent| {
            file_helper::insert(client.clone(), parent.clone(), file_name, file)
        })
    })
}

/// Update (replace) file at the given name with the new file.
/// If `version` is 0, the correct version is obtained automatically.
#[no_mangle]
pub unsafe extern "C" fn file_update(app: *const App,
                                     parent_h: MDataInfoHandle,
                                     file_name: *const c_char,
                                     file: *const File,
                                     version: u64,
                                     user_data: *mut c_void,
                                     o_cb: extern "C" fn(*mut c_void, i32)) {
    catch_unwind_cb(user_data, o_cb, || {
        let file = NativeFile::clone_from_repr_c(file)?;
        let file_name = from_c_str(file_name)?;

        send_with_mdata_info(app, parent_h, user_data, o_cb, move |client, _, parent| {
            file_helper::update(client.clone(), parent.clone(), file_name, file, version)
        })
    })
}

#[cfg(test)]
mod tests {
    use super::*;
................................................................................
                                                        Permission::Delete]);

        let app = create_app_with_access(container_permissions);

        let container_info_h = run(&app, move |client, context| {
            let context = context.clone();


            context.get_container_mdata_info(client, "_videos")
                .map(move |info| context.object_cache().insert_mdata_info(info))
        });

        let file_name0 = "file0.txt";
        let ffi_file_name0 = unwrap!(CString::new(file_name0));

        // fetching non-existing file fails.
................................................................................
        // Create empty file.
        let user_metadata = b"metadata".to_vec();
        let file = NativeFile::new(user_metadata.clone());
        let ffi_file = file.into_repr_c();

        unsafe {
            unwrap!(call_0(|ud, cb| {
                file_insert(&app,
                            container_info_h,
                            ffi_file_name0.as_ptr(),
                            &ffi_file,
                            ud,
                            cb)
            }))
        }

        // Fetch it back.
        let (retrieved_file, retrieved_version): (NativeFile, u64) = unsafe {
            unwrap!(call_2(|ud, cb| {
                file_fetch(&app, container_info_h, ffi_file_name0.as_ptr(), ud, cb)
            }))
        };
        assert_eq!(retrieved_file.user_metadata(), &user_metadata[..]);
        assert_eq!(retrieved_file.size(), 0);
        assert_eq!(retrieved_version, 0);

        // Create non-empty file.
        let cipher_opt_h = run_now(&app, |_, context| {


            context.object_cache().insert_cipher_opt(CipherOpt::PlainText)
        });

        let content = b"hello world";
        let content_name = unsafe { put_file_content(&app, content, cipher_opt_h) };

        let mut file = NativeFile::new(Vec::new());
        file.set_data_map_name(content_name);
................................................................................
        let ffi_file = file.into_repr_c();

        let file_name1 = "file1.txt";
        let ffi_file_name1 = unwrap!(CString::new(file_name1));

        unsafe {
            unwrap!(call_0(|ud, cb| {
                file_insert(&app,
                            container_info_h,
                            ffi_file_name1.as_ptr(),
                            &ffi_file,
                            ud,
                            cb)
            }))
        }

        // Fetch it back.
        let (file, _version): (NativeFile, u64) = {
            unsafe {
                unwrap!(call_2(|ud, cb| {


                    file_fetch(&app, container_info_h, ffi_file_name1.as_ptr(), ud, cb)


                }))
            }
        };

        // Read the content.
        let retrieved_content = unsafe { get_file_content(&app, file.data_map_name().0) };
        assert_eq!(retrieved_content, content);
    }


|
|
<
|

|
|
<
<
|

|
|
|
<

|
|




|







 







|











|
|
|

|
>
>
>
>
>
>
>
>
>













|





|













|





|







 







>
|







 







|
|
|
|
|
|
|





|
|







>
>
|







 







|
|
|
|
|
|
|






>
>
|
>
>
|







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
..
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
...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
...
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// 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;
use errors::AppError;
use ffi::helper::send_with_mdata_info;
use ffi_utils::{FFI_RESULT_OK, FfiResult, OpaqueCtx, ReprC, catch_unwind_cb, from_c_str};
use futures::Future;
use object_cache::MDataInfoHandle;
use safe_core::FutureExt;
use safe_core::nfs::File as NativeFile;
use safe_core::nfs::ffi::File;
use safe_core::nfs::file_helper;
use std::os::raw::{c_char, c_void};
................................................................................

/// Retrieve file with the given name, and its version, from the directory.
#[no_mangle]
pub unsafe extern "C" fn file_fetch(app: *const App,
                                    parent_h: MDataInfoHandle,
                                    file_name: *const c_char,
                                    user_data: *mut c_void,
                                    o_cb: extern "C" fn(*mut c_void, FfiResult, *const File, u64)) {
    catch_unwind_cb(user_data, o_cb, || {
        let file_name = from_c_str(file_name)?;
        let user_data = OpaqueCtx(user_data);

        (*app).send(move |client, context| {
            let parent = try_cb!(context.object_cache().get_mdata_info(parent_h),
                                 user_data.0,
                                 o_cb);

            file_helper::fetch(client.clone(), parent.clone(), file_name)
                .map(move |(version, file)| {
                         let ffi_file = file.into_repr_c();
                         o_cb(user_data.0, FFI_RESULT_OK, &ffi_file, version)
                     })
                .map_err(AppError::from)
                .map_err(move |err| {
                    let (error_code, description) = ffi_error!(err);
                    o_cb(user_data.0,
                         FfiResult {
                             error_code,
                             description: description.as_ptr(),
                         },
                         ptr::null(),
                         0)
                })
                .into_box()
                .into()
        })
    })
}

/// Insert the file into the parent directory.
#[no_mangle]
pub unsafe extern "C" fn file_insert(app: *const App,
                                     parent_h: MDataInfoHandle,
                                     file_name: *const c_char,
                                     file: *const File,
                                     user_data: *mut c_void,
                                     o_cb: extern "C" fn(*mut c_void, FfiResult)) {
    catch_unwind_cb(user_data, o_cb, || {
        let file = NativeFile::clone_from_repr_c(file)?;
        let file_name = from_c_str(file_name)?;

        send_with_mdata_info(app, parent_h, user_data, o_cb, move |client, _, parent| {
            file_helper::insert(client.clone(), parent.clone(), file_name, &file)
        })
    })
}

/// Update (replace) file at the given name with the new file.
/// If `version` is 0, the correct version is obtained automatically.
#[no_mangle]
pub unsafe extern "C" fn file_update(app: *const App,
                                     parent_h: MDataInfoHandle,
                                     file_name: *const c_char,
                                     file: *const File,
                                     version: u64,
                                     user_data: *mut c_void,
                                     o_cb: extern "C" fn(*mut c_void, FfiResult)) {
    catch_unwind_cb(user_data, o_cb, || {
        let file = NativeFile::clone_from_repr_c(file)?;
        let file_name = from_c_str(file_name)?;

        send_with_mdata_info(app, parent_h, user_data, o_cb, move |client, _, parent| {
            file_helper::update(client.clone(), parent.clone(), file_name, &file, version)
        })
    })
}

#[cfg(test)]
mod tests {
    use super::*;
................................................................................
                                                        Permission::Delete]);

        let app = create_app_with_access(container_permissions);

        let container_info_h = run(&app, move |client, context| {
            let context = context.clone();

            context
                .get_container_mdata_info(client, "_videos")
                .map(move |info| context.object_cache().insert_mdata_info(info))
        });

        let file_name0 = "file0.txt";
        let ffi_file_name0 = unwrap!(CString::new(file_name0));

        // fetching non-existing file fails.
................................................................................
        // Create empty file.
        let user_metadata = b"metadata".to_vec();
        let file = NativeFile::new(user_metadata.clone());
        let ffi_file = file.into_repr_c();

        unsafe {
            unwrap!(call_0(|ud, cb| {
                               file_insert(&app,
                                           container_info_h,
                                           ffi_file_name0.as_ptr(),
                                           &ffi_file,
                                           ud,
                                           cb)
                           }))
        }

        // Fetch it back.
        let (retrieved_file, retrieved_version): (NativeFile, u64) = unsafe {
            unwrap!(call_2(|ud, cb| {
                               file_fetch(&app, container_info_h, ffi_file_name0.as_ptr(), ud, cb)
                           }))
        };
        assert_eq!(retrieved_file.user_metadata(), &user_metadata[..]);
        assert_eq!(retrieved_file.size(), 0);
        assert_eq!(retrieved_version, 0);

        // Create non-empty file.
        let cipher_opt_h = run_now(&app, |_, context| {
            context
                .object_cache()
                .insert_cipher_opt(CipherOpt::PlainText)
        });

        let content = b"hello world";
        let content_name = unsafe { put_file_content(&app, content, cipher_opt_h) };

        let mut file = NativeFile::new(Vec::new());
        file.set_data_map_name(content_name);
................................................................................
        let ffi_file = file.into_repr_c();

        let file_name1 = "file1.txt";
        let ffi_file_name1 = unwrap!(CString::new(file_name1));

        unsafe {
            unwrap!(call_0(|ud, cb| {
                               file_insert(&app,
                                           container_info_h,
                                           ffi_file_name1.as_ptr(),
                                           &ffi_file,
                                           ud,
                                           cb)
                           }))
        }

        // Fetch it back.
        let (file, _version): (NativeFile, u64) = {
            unsafe {
                unwrap!(call_2(|ud, cb| {
                                   file_fetch(&app,
                                              container_info_h,
                                              ffi_file_name1.as_ptr(),
                                              ud,
                                              cb)
                               }))
            }
        };

        // Read the content.
        let retrieved_content = unsafe { get_file_content(&app, file.data_map_name().0) };
        assert_eq!(retrieved_content, content);
    }