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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//

//! The Ref object is a helper over the link functionality, so one is able to create references to
//! files outside of the imag store.

use std::path::PathBuf;
use std::fs::File;
use std::fs::Permissions;

use libimagstore::store::Entry;

use toml::Value;
use toml_query::read::TomlValueReadExt;
use toml_query::set::TomlValueSetExt;

use error::RefErrorKind as REK;
use error::RefError as RE;
use error::ResultExt;
use error::Result;
use hasher::*;

pub trait Ref {

    /// Get the hash from the path of the ref
    fn get_path_hash(&self) -> Result<String>;

    /// Get the hash of the link target which is stored in the ref object
    fn get_stored_hash(&self) -> Result<String>;

    /// Get the hahs of the link target which is stored in the ref object, which is hashed with a
    /// custom Hasher instance.
    fn get_stored_hash_with_hasher<H: Hasher>(&self, h: &H) -> Result<String>;

    /// Get the hash of the link target by reading the link target and hashing the contents
    fn get_current_hash(&self) -> Result<String>;

    /// Get the hash of the link target by reading the link target and hashing the contents with the
    /// custom hasher
    fn get_current_hash_with_hasher<H: Hasher>(&self, h: H) -> Result<String>;

    /// check whether the pointer the Ref represents still points to a file which exists
    fn fs_link_exists(&self) -> Result<bool>;

    /// Alias for `r.fs_link_exists() && r.deref().is_file()`
    fn is_ref_to_file(&self) -> Result<bool>;

    /// Alias for `r.fs_link_exists() && r.deref().is_dir()`
    fn is_ref_to_dir(&self) -> Result<bool>;

    /// Alias for `!Ref::fs_link_exists()`
    fn is_dangling(&self) -> Result<bool>;

    /// check whether the pointer the Ref represents is valid
    /// This includes:
    ///     - Hashsum of the file is still the same as stored in the Ref
    ///     - file permissions are still valid
    fn fs_link_valid(&self) -> Result<bool>;

    /// Check whether the file permissions of the referenced file are equal to the stored
    /// permissions
    fn fs_link_valid_permissions(&self) -> Result<bool>;

    /// Check whether the Hashsum of the referenced file is equal to the stored hashsum
    fn fs_link_valid_hash(&self) -> Result<bool>;

    /// Update the Ref by re-checking the file from FS
    /// This errors if the file is not present or cannot be read()
    fn update_ref(&mut self) -> Result<()>;

    /// Update the Ref by re-checking the file from FS using the passed Hasher instance
    /// This errors if the file is not present or cannot be read()
    fn update_ref_with_hasher<H: Hasher>(&mut self, h: &H) -> Result<()>;

    /// Get the path of the file which is reffered to by this Ref
    fn fs_file(&self) -> Result<PathBuf>;

    /// Re-find a referenced file
    ///
    /// This function tries to re-find a ref by searching all directories in `search_roots` recursively
    /// for a file which matches the hash of the Ref.
    ///
    /// If `search_roots` is `None`, it starts at the filesystem root `/`.
    ///
    /// If the target cannot be found, this yields a RefTargetDoesNotExist error kind.
    ///
    /// # Warning
    ///
    /// This option causes heavy I/O as it recursively searches the Filesystem.
    fn refind(&self, search_roots: Option<Vec<PathBuf>>) -> Result<PathBuf>;

    /// See documentation of `Ref::refind()`
    fn refind_with_hasher<H: Hasher>(&self, search_roots: Option<Vec<PathBuf>>, h: H)
        -> Result<PathBuf>;

    /// Get the permissions of the file which are present
    fn get_current_permissions(&self) -> Result<Permissions>;
}


impl Ref for Entry {

    /// Get the hash from the path of the ref
    fn get_path_hash(&self) -> Result<String> {
        self.get_location()
            .clone()
            .into_pathbuf()
            .map_err(From::from)
            .and_then(|pb| {
                pb.file_name()
                    .and_then(|osstr| osstr.to_str())
                    .and_then(|s| s.split("~").next())
                    .map(String::from)
                    .ok_or(String::from("String splitting error"))
                    .map_err(From::from)
            })
    }

    /// Get the hash of the link target which is stored in the ref object
    fn get_stored_hash(&self) -> Result<String> {
        self.get_stored_hash_with_hasher(&DefaultHasher::new())
    }

    /// Get the hahs of the link target which is stored in the ref object, which is hashed with a
    /// custom Hasher instance.
    fn get_stored_hash_with_hasher<H: Hasher>(&self, h: &H) -> Result<String> {
        match self.get_header().read(&format!("ref.content_hash.{}", h.hash_name())[..])? {
            // content hash stored...
            Some(&Value::String(ref s)) => Ok(s.clone()),

            // content hash header field has wrong type
            Some(_) => Err(RE::from_kind(REK::HeaderTypeError)),

            // content hash not stored
            None => Err(RE::from_kind(REK::HeaderFieldMissingError)),

        }
    }

    /// Get the hash of the link target by reading the link target and hashing the contents
    fn get_current_hash(&self) -> Result<String> {
        self.get_current_hash_with_hasher(DefaultHasher::new())
    }

    /// Get the hash of the link target by reading the link target and hashing the contents with the
    /// custom hasher
    fn get_current_hash_with_hasher<H: Hasher>(&self, mut h: H) -> Result<String> {
        self.fs_file()
            .and_then(|pb| File::open(pb.clone()).map(|f| (pb, f)).map_err(From::from))
            .and_then(|(path, mut file)| h.create_hash(&path, &mut file))
    }

    /// check whether the pointer the Ref represents still points to a file which exists
    fn fs_link_exists(&self) -> Result<bool> {
        self.fs_file().map(|pathbuf| pathbuf.exists())
    }

    /// Alias for `r.fs_link_exists() && r.deref().is_file()`
    fn is_ref_to_file(&self) -> Result<bool> {
        self.fs_file().map(|pathbuf| pathbuf.is_file())
    }

    /// Alias for `r.fs_link_exists() && r.deref().is_dir()`
    fn is_ref_to_dir(&self) -> Result<bool> {
        self.fs_file().map(|pathbuf| pathbuf.is_dir())
    }

    /// Alias for `!Ref::fs_link_exists()`
    fn is_dangling(&self) -> Result<bool> {
        self.fs_link_exists().map(|b| !b)
    }

    /// check whether the pointer the Ref represents is valid
    /// This includes:
    ///     - Hashsum of the file is still the same as stored in the Ref
    ///     - file permissions are still valid
    fn fs_link_valid(&self) -> Result<bool> {
        match (self.fs_link_valid_permissions(), self.fs_link_valid_hash()) {
            (Ok(true) , Ok(true)) => Ok(true),
            (Ok(_)    , Ok(_))    => Ok(false),
            (Err(e)   , _)        => Err(e),
            (_        , Err(e))   => Err(e),
        }
    }

    /// Check whether the file permissions of the referenced file are equal to the stored
    /// permissions
    fn fs_link_valid_permissions(&self) -> Result<bool> {
        self
            .get_header()
            .read("ref.permissions.ro")
            .chain_err(|| REK::HeaderFieldReadError)
            .and_then(|ro| {
                match ro {
                    Some(&Value::Boolean(b)) => Ok(b),
                    Some(_)                 => Err(RE::from_kind(REK::HeaderTypeError)),
                    None                    => Err(RE::from_kind(REK::HeaderFieldMissingError)),
                }
            })
            .and_then(|ro| self.get_current_permissions().map(|perm| ro == perm.readonly()))
            .chain_err(|| REK::RefTargetCannotReadPermissions)
    }

    /// Check whether the Hashsum of the referenced file is equal to the stored hashsum
    fn fs_link_valid_hash(&self) -> Result<bool> {
        let stored_hash  = self.get_stored_hash()?;
        let current_hash = self.get_current_hash()?;
        Ok(stored_hash == current_hash)
    }

    /// Update the Ref by re-checking the file from FS
    /// This errors if the file is not present or cannot be read()
    fn update_ref(&mut self) -> Result<()> {
        self.update_ref_with_hasher(&DefaultHasher::new())
    }

    /// Update the Ref by re-checking the file from FS using the passed Hasher instance
    /// This errors if the file is not present or cannot be read()
    fn update_ref_with_hasher<H: Hasher>(&mut self, h: &H) -> Result<()> {
        let current_hash = self.get_current_hash()?; // uses the default hasher
        let current_perm = self.get_current_permissions()?;

        self
            .get_header_mut()
            .set("ref.permissions.ro", Value::Boolean(current_perm.readonly()))
        ?;

        self
            .get_header_mut()
            .set(&format!("ref.content_hash.{}", h.hash_name())[..], Value::String(current_hash))
        ?;

        Ok(())
    }

    /// Get the path of the file which is reffered to by this Ref
    fn fs_file(&self) -> Result<PathBuf> {
        match self.get_header().read("ref.path")? {
            Some(&Value::String(ref s)) => Ok(PathBuf::from(s)),
            Some(_) => Err(RE::from_kind(REK::HeaderTypeError)),
            None    => Err(RE::from_kind(REK::HeaderFieldMissingError)),
        }
    }

    /// Re-find a referenced file
    ///
    /// This function tries to re-find a ref by searching all directories in `search_roots` recursively
    /// for a file which matches the hash of the Ref.
    ///
    /// If `search_roots` is `None`, it starts at the filesystem root `/`.
    ///
    /// If the target cannot be found, this yields a RefTargetDoesNotExist error kind.
    ///
    /// # Warning
    ///
    /// This option causes heavy I/O as it recursively searches the Filesystem.
    fn refind(&self, search_roots: Option<Vec<PathBuf>>) -> Result<PathBuf> {
        self.refind_with_hasher(search_roots, DefaultHasher::new())
    }

    /// See documentation of `Ref::refind()`
    fn refind_with_hasher<H: Hasher>(&self, search_roots: Option<Vec<PathBuf>>, mut h: H)
        -> Result<PathBuf>
    {
        use itertools::Itertools;
        use walkdir::WalkDir;

        self.get_stored_hash()
            .and_then(|stored_hash| {
                search_roots
                    .unwrap_or(vec![PathBuf::from("/")])
                    .into_iter()
                    .map(|root| {
                        WalkDir::new(root)
                            .follow_links(false)
                            .into_iter()
                            .map(|entry| {
                                entry
                                    .map_err(From::from)
                                    .and_then(|entry| {
                                        let pb = PathBuf::from(entry.path());
                                        File::open(entry.path())
                                            .map(|f| (pb, f))
                                            .map_err(From::from)
                                    })
                                    .and_then(|(p, mut f)| {
                                        h.create_hash(&p, &mut f)
                                            .map(|h| (p, h))
                                            .map_err(From::from)
                                    })
                                    .map(|(path, hash)| {
                                        if hash == stored_hash {
                                            Some(path)
                                        } else {
                                            None
                                        }
                                    })
                            })
                            .filter_map(Result::ok)
                            .filter_map(|e| e)
                            .next()
                    })
                    .flatten()
                    .next()
                    .ok_or(RE::from_kind(REK::RefTargetDoesNotExist))
            })
    }

    /// Get the permissions of the file which are present
    fn get_current_permissions(&self) -> Result<Permissions> {
        self.fs_file()
            .and_then(|pb| {
                File::open(pb)
                    .chain_err(|| REK::HeaderFieldReadError)
            })
            .and_then(|file| {
                file
                    .metadata()
                    .map(|md| md.permissions())
                    .chain_err(|| REK::RefTargetCannotReadPermissions)
            })
    }

}