Diff
Not logged in

Differences From Artifact [9cb52c0328]:

To Artifact [145a1beb35]:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
..
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
..
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
// Copyright 2015 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 rust_sodium::crypto::box_;
use std::sync::{Arc, Mutex};

const DNS_CONFIG_DIR_NAME: &'static str = "DnsReservedDirectory";
const DNS_CONFIG_FILE_NAME: &'static str = "DnsConfigurationFile";

/// Dns configuration. For internal use by the `dns` module.
#[derive(Clone, Debug, Eq, PartialEq, RustcEncodable, RustcDecodable)]
pub struct DnsConfiguration {
    /// Dns long name
    pub long_name: String,
    /// Encryption keys
    pub encryption_keypair: (box_::PublicKey, box_::SecretKey),
}

/// Initialise dns configuration.
pub fn initialise_dns_configuaration(client: Arc<Mutex<Client>>) -> Result<(), DnsError> {
    trace!("Initialise dns configuration if not already done.");

    let dir_helper = DirectoryHelper::new(client.clone());
    let dir_listing =
        try!(dir_helper.get_configuration_directory_listing(DNS_CONFIG_DIR_NAME.to_string()));
    let mut file_helper = FileHelper::new(client.clone());
    match file_helper.create(DNS_CONFIG_FILE_NAME.to_string(), vec![], dir_listing) {
        Ok(writer) => {
            trace!("Dns configuration not found - initialising.");

            let _ = try!(writer.close());
            Ok(())
        }
        Err(NfsError::FileAlreadyExistsWithSameName) => {
            trace!("Dns configuration is already initialised.");
            Ok(())
        }
        Err(error) => Err(DnsError::from(error)),
................................................................................

/// Get dns configuration data.
pub fn get_dns_configuration_data(client: Arc<Mutex<Client>>)
                                  -> Result<Vec<DnsConfiguration>, DnsError> {
    trace!("Retrieve dns configuration data from a previously initialised dns configuration.");

    let dir_helper = DirectoryHelper::new(client.clone());
    let dir_listing =
        try!(dir_helper.get_configuration_directory_listing(DNS_CONFIG_DIR_NAME.to_string()));

    let file = try!(dir_listing.get_files()
        .iter()
        .find(|file| file.get_name() == DNS_CONFIG_FILE_NAME)
        .ok_or(DnsError::DnsConfigFileNotFoundOrCorrupted));
    let mut file_helper = FileHelper::new(client.clone());
    let mut reader = try!(file_helper.read(file));
    let size = reader.size();
    if size == 0 {
        Ok(vec![])
    } else {
        Ok(try!(deserialise(&try!(reader.read(0, size)))))
    }
}

/// Write dns configuration data.
pub fn write_dns_configuration_data(client: Arc<Mutex<Client>>,
                                    config: &[DnsConfiguration])
                                    -> Result<(), DnsError> {
    trace!("Write new dns configuration data to the previously initialised dns configuration.");

    let dir_helper = DirectoryHelper::new(client.clone());
    let dir_listing =
        try!(dir_helper.get_configuration_directory_listing(DNS_CONFIG_DIR_NAME.to_string()));

    let file = try!(dir_listing.get_files()
            .iter()
            .find(|file| file.get_name() == DNS_CONFIG_FILE_NAME)
            .ok_or(DnsError::DnsConfigFileNotFoundOrCorrupted))
        .clone();
    let mut file_helper = FileHelper::new(client.clone());

    let mut writer = try!(file_helper.update_content(file, Mode::Overwrite, dir_listing));
    try!(writer.write(&try!(serialise(&config))));
    let _ = try!(writer.close());
    Ok(())
}

#[cfg(test)]
mod test {

    use core::utility;
    use core::utility::test_utils;
    use rust_sodium::crypto::box_;
    use std::sync::{Arc, Mutex};
    use super::*;

    #[test]
    fn read_write_dns_configuration_file() {
        let client = Arc::new(Mutex::new(unwrap!(test_utils::get_client())));

        // Initialise Dns Configuration File
        unwrap!(initialise_dns_configuaration(client.clone()));







|
|







 







|












|
|





|







 







|
|
>
|


|

|




|










|
|
>
|
|
|
|


>
|
|
|





>




<







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
..
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
..
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
// Copyright 2015 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 rust_sodium::crypto::box_;
use std::sync::{Arc, Mutex};

const DNS_CONFIG_DIR_NAME: &'static str = "DnsReservedDirectory";
const DNS_CONFIG_FILE_NAME: &'static str = "DnsConfigurationFile";

/// Dns configuration. For internal use by the `dns` module.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct DnsConfiguration {
    /// Dns long name
    pub long_name: String,
    /// Encryption keys
    pub encryption_keypair: (box_::PublicKey, box_::SecretKey),
}

/// Initialise dns configuration.
pub fn initialise_dns_configuaration(client: Arc<Mutex<Client>>) -> Result<(), DnsError> {
    trace!("Initialise dns configuration if not already done.");

    let dir_helper = DirectoryHelper::new(client.clone());
    let dir_listing = dir_helper
        .get_configuration_directory_listing(DNS_CONFIG_DIR_NAME.to_string())?;
    let mut file_helper = FileHelper::new(client.clone());
    match file_helper.create(DNS_CONFIG_FILE_NAME.to_string(), vec![], dir_listing) {
        Ok(writer) => {
            trace!("Dns configuration not found - initialising.");

            let _ = writer.close()?;
            Ok(())
        }
        Err(NfsError::FileAlreadyExistsWithSameName) => {
            trace!("Dns configuration is already initialised.");
            Ok(())
        }
        Err(error) => Err(DnsError::from(error)),
................................................................................

/// Get dns configuration data.
pub fn get_dns_configuration_data(client: Arc<Mutex<Client>>)
                                  -> Result<Vec<DnsConfiguration>, DnsError> {
    trace!("Retrieve dns configuration data from a previously initialised dns configuration.");

    let dir_helper = DirectoryHelper::new(client.clone());
    let dir_listing = dir_helper
        .get_configuration_directory_listing(DNS_CONFIG_DIR_NAME.to_string())?;
    let file = dir_listing
        .get_files()
        .iter()
        .find(|file| file.get_name() == DNS_CONFIG_FILE_NAME)
        .ok_or(DnsError::DnsConfigFileNotFoundOrCorrupted)?;
    let mut file_helper = FileHelper::new(client.clone());
    let mut reader = file_helper.read(file)?;
    let size = reader.size();
    if size == 0 {
        Ok(vec![])
    } else {
        Ok(deserialise(&reader.read(0, size)?)?)
    }
}

/// Write dns configuration data.
pub fn write_dns_configuration_data(client: Arc<Mutex<Client>>,
                                    config: &[DnsConfiguration])
                                    -> Result<(), DnsError> {
    trace!("Write new dns configuration data to the previously initialised dns configuration.");

    let dir_helper = DirectoryHelper::new(client.clone());
    let dir_listing = dir_helper
        .get_configuration_directory_listing(DNS_CONFIG_DIR_NAME.to_string())?;
    let file = dir_listing
        .get_files()
        .iter()
        .find(|file| file.get_name() == DNS_CONFIG_FILE_NAME)
        .ok_or(DnsError::DnsConfigFileNotFoundOrCorrupted)?
        .clone();
    let mut file_helper = FileHelper::new(client.clone());
    let mut writer = file_helper
        .update_content(file, Mode::Overwrite, dir_listing)?;
    writer.write(&serialise(&config)?)?;
    let _ = writer.close()?;
    Ok(())
}

#[cfg(test)]
mod test {
    use super::*;
    use core::utility;
    use core::utility::test_utils;
    use rust_sodium::crypto::box_;
    use std::sync::{Arc, Mutex};


    #[test]
    fn read_write_dns_configuration_file() {
        let client = Arc::new(Mutex::new(unwrap!(test_utils::get_client())));

        // Initialise Dns Configuration File
        unwrap!(initialise_dns_configuaration(client.clone()));