Diff
Not logged in

Differences From Artifact [9cb52c0328]:

To Artifact [145a1beb35]:


     1      1   // Copyright 2015 MaidSafe.net limited.
     2      2   //
     3      3   // This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
     4      4   // version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
     5      5   // licence you accepted on initial access to the Software (the "Licences").
     6      6   //
     7      7   // By contributing code to the SAFE Network Software, or to this project generally, you agree to be
     8         -// bound by the terms of the MaidSafe Contributor Agreement, version 1.0.  This, along with the
     9         -// Licenses can be found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
            8  +// bound by the terms of the MaidSafe Contributor Agreement.  This, along with the Licenses can be
            9  +// found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
    10     10   //
    11     11   // Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
    12     12   // under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    13     13   // KIND, either express or implied.
    14     14   //
    15     15   // Please review the Licences for the specific language governing permissions and limitations
    16     16   // relating to use of the SAFE Network Software.
................................................................................
    28     28   use rust_sodium::crypto::box_;
    29     29   use std::sync::{Arc, Mutex};
    30     30   
    31     31   const DNS_CONFIG_DIR_NAME: &'static str = "DnsReservedDirectory";
    32     32   const DNS_CONFIG_FILE_NAME: &'static str = "DnsConfigurationFile";
    33     33   
    34     34   /// Dns configuration. For internal use by the `dns` module.
    35         -#[derive(Clone, Debug, Eq, PartialEq, RustcEncodable, RustcDecodable)]
           35  +#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
    36     36   pub struct DnsConfiguration {
    37     37       /// Dns long name
    38     38       pub long_name: String,
    39     39       /// Encryption keys
    40     40       pub encryption_keypair: (box_::PublicKey, box_::SecretKey),
    41     41   }
    42     42   
    43     43   /// Initialise dns configuration.
    44     44   pub fn initialise_dns_configuaration(client: Arc<Mutex<Client>>) -> Result<(), DnsError> {
    45     45       trace!("Initialise dns configuration if not already done.");
    46     46   
    47     47       let dir_helper = DirectoryHelper::new(client.clone());
    48         -    let dir_listing =
    49         -        try!(dir_helper.get_configuration_directory_listing(DNS_CONFIG_DIR_NAME.to_string()));
           48  +    let dir_listing = dir_helper
           49  +        .get_configuration_directory_listing(DNS_CONFIG_DIR_NAME.to_string())?;
    50     50       let mut file_helper = FileHelper::new(client.clone());
    51     51       match file_helper.create(DNS_CONFIG_FILE_NAME.to_string(), vec![], dir_listing) {
    52     52           Ok(writer) => {
    53     53               trace!("Dns configuration not found - initialising.");
    54     54   
    55         -            let _ = try!(writer.close());
           55  +            let _ = writer.close()?;
    56     56               Ok(())
    57     57           }
    58     58           Err(NfsError::FileAlreadyExistsWithSameName) => {
    59     59               trace!("Dns configuration is already initialised.");
    60     60               Ok(())
    61     61           }
    62     62           Err(error) => Err(DnsError::from(error)),
................................................................................
    65     65   
    66     66   /// Get dns configuration data.
    67     67   pub fn get_dns_configuration_data(client: Arc<Mutex<Client>>)
    68     68                                     -> Result<Vec<DnsConfiguration>, DnsError> {
    69     69       trace!("Retrieve dns configuration data from a previously initialised dns configuration.");
    70     70   
    71     71       let dir_helper = DirectoryHelper::new(client.clone());
    72         -    let dir_listing =
    73         -        try!(dir_helper.get_configuration_directory_listing(DNS_CONFIG_DIR_NAME.to_string()));
    74         -    let file = try!(dir_listing.get_files()
           72  +    let dir_listing = dir_helper
           73  +        .get_configuration_directory_listing(DNS_CONFIG_DIR_NAME.to_string())?;
           74  +    let file = dir_listing
           75  +        .get_files()
    75     76           .iter()
    76     77           .find(|file| file.get_name() == DNS_CONFIG_FILE_NAME)
    77         -        .ok_or(DnsError::DnsConfigFileNotFoundOrCorrupted));
           78  +        .ok_or(DnsError::DnsConfigFileNotFoundOrCorrupted)?;
    78     79       let mut file_helper = FileHelper::new(client.clone());
    79         -    let mut reader = try!(file_helper.read(file));
           80  +    let mut reader = file_helper.read(file)?;
    80     81       let size = reader.size();
    81     82       if size == 0 {
    82     83           Ok(vec![])
    83     84       } else {
    84         -        Ok(try!(deserialise(&try!(reader.read(0, size)))))
           85  +        Ok(deserialise(&reader.read(0, size)?)?)
    85     86       }
    86     87   }
    87     88   
    88     89   /// Write dns configuration data.
    89     90   pub fn write_dns_configuration_data(client: Arc<Mutex<Client>>,
    90     91                                       config: &[DnsConfiguration])
    91     92                                       -> Result<(), DnsError> {
    92     93       trace!("Write new dns configuration data to the previously initialised dns configuration.");
    93     94   
    94     95       let dir_helper = DirectoryHelper::new(client.clone());
    95         -    let dir_listing =
    96         -        try!(dir_helper.get_configuration_directory_listing(DNS_CONFIG_DIR_NAME.to_string()));
    97         -    let file = try!(dir_listing.get_files()
    98         -            .iter()
    99         -            .find(|file| file.get_name() == DNS_CONFIG_FILE_NAME)
   100         -            .ok_or(DnsError::DnsConfigFileNotFoundOrCorrupted))
           96  +    let dir_listing = dir_helper
           97  +        .get_configuration_directory_listing(DNS_CONFIG_DIR_NAME.to_string())?;
           98  +    let file = dir_listing
           99  +        .get_files()
          100  +        .iter()
          101  +        .find(|file| file.get_name() == DNS_CONFIG_FILE_NAME)
          102  +        .ok_or(DnsError::DnsConfigFileNotFoundOrCorrupted)?
   101    103           .clone();
   102    104       let mut file_helper = FileHelper::new(client.clone());
   103         -    let mut writer = try!(file_helper.update_content(file, Mode::Overwrite, dir_listing));
   104         -    try!(writer.write(&try!(serialise(&config))));
   105         -    let _ = try!(writer.close());
          105  +    let mut writer = file_helper
          106  +        .update_content(file, Mode::Overwrite, dir_listing)?;
          107  +    writer.write(&serialise(&config)?)?;
          108  +    let _ = writer.close()?;
   106    109       Ok(())
   107    110   }
   108    111   
   109    112   #[cfg(test)]
   110    113   mod test {
          114  +    use super::*;
   111    115       use core::utility;
   112    116       use core::utility::test_utils;
   113    117       use rust_sodium::crypto::box_;
   114    118       use std::sync::{Arc, Mutex};
   115         -    use super::*;
   116    119   
   117    120       #[test]
   118    121       fn read_write_dns_configuration_file() {
   119    122           let client = Arc::new(Mutex::new(unwrap!(test_utils::get_client())));
   120    123   
   121    124           // Initialise Dns Configuration File
   122    125           unwrap!(initialise_dns_configuaration(client.clone()));