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
//! File-system related resource types.
use std::any;
use std::fmt;
use std::fs;
use std::io;
use std::os;
use std::path;

use error;
use resource;
use util;

/// A file, which can be absent, a regular file, a directory, or a symlink.
#[derive(Debug, Eq, PartialEq)]
pub struct File {
    path: path::PathBuf,
    file_type: FileType,
}

#[derive(Debug, Eq, PartialEq)]
enum FileType {
    Absent,
    File { contents: Option<Vec<u8>> },
    Dir,
    Symlink { target: path::PathBuf },
}

impl File {
    /// Starts reasoning about a file at a certain path. The resulting resource
    /// will only ensure that the file exists and is a regular file.
    pub fn at<P>(path: P) -> File
    where
        P: Into<path::PathBuf>,
    {
        File {
            path: path.into(),
            file_type: FileType::File { contents: None },
        }
    }

    /// The file should contain the specified byte contents. It is recommended
    /// to call this method with either a binary literal (`contains(b"abc")`) or
    /// with an embedded external file (`contains(include_bytes!("my/file"))`).
    pub fn contains<B>(mut self, contents: B) -> File
    where
        B: Into<Vec<u8>>,
    {
        self.file_type = FileType::File { contents: Some(contents.into()) };
        self
    }

    /// The file should contain the specified string contents, encoded as UTF-8.
    /// It is recommended to call this method with either a string literal
    /// (`contains("abc")`) or with an embedded external file
    /// (`contains(include_str!("my/file"))`).
    pub fn contains_str<S>(self, contents: S) -> File
    where
        S: Into<String>,
    {
        self.contains(contents.into().into_bytes())
    }

    /// The file is a regular file, with any contents.
    pub fn is_file(mut self) -> File {
        self.file_type = FileType::File { contents: None };
        self
    }

    /// The file is a directory.
    pub fn is_dir(mut self) -> File {
        self.file_type = FileType::Dir;
        self
    }

    /// The file is a symlink that points to the supplied path.
    pub fn points_to<P>(mut self, path: P) -> File
    where
        P: Into<path::PathBuf>,
    {
        self.file_type = FileType::Symlink { target: path.into() };
        self
    }

    /// The file should be absent, and will be deleted if it exists.
    pub fn is_absent(mut self) -> File {
        self.file_type = FileType::Absent;
        self
    }
}

impl resource::Resource for File {
    fn key(&self) -> resource::Key {
        resource::Key::Path(self.path.clone())
    }

    fn realize(&self, &resource::Context { log, .. }: &resource::Context) -> error::Result<()> {
        use error::ResultExt;
        let path = self.path.to_string_lossy().into_owned();
        let log = log.new(o!("path" => path));

        match self.file_type {
            FileType::Absent => {
                if self.path.is_dir() {
                    trace!(log, "Deleting directory");
                    fs::remove_dir(&self.path).chain_err(|| {
                        format!("Failed to delete directory {:?}", self.path)
                    })?;
                }
                if self.path.is_file() {
                    trace!(log, "Deleting file");
                    fs::remove_file(&self.path).chain_err(|| {
                        format!("Failed to delete file {:?}", self.path)
                    })?;
                }
            }
            FileType::File { ref contents } => {
                if let Some(ref contents) = *contents {
                    use std::io::Write;

                    trace!(log, "Updating file contents");
                    let mut f = fs::File::create(&self.path).chain_err(|| {
                        format!("Failed to create file {:?}", self.path)
                    })?;
                    f.write_all(contents).chain_err(|| {
                        format!("Failed to write to file {:?}", self.path)
                    })?;
                }
            }
            FileType::Dir => {
                fs::create_dir_all(&self.path).chain_err(|| {
                    format!("Failed to create directory {:?}", self.path)
                })?;
            }
            FileType::Symlink { ref target } => {
                // TODO: add support for other OSes
                os::unix::fs::symlink(target, &self.path).chain_err(|| {
                    format!("Failed to create symlink {:?}", self.path)
                })?;
            }
        }
        Ok(())
    }

    fn verify(&self, &resource::Context { log, .. }: &resource::Context) -> error::Result<bool> {
        use error::ResultExt;

        let log = log.new(o!("path" => self.path.to_string_lossy().into_owned()));

        if !self.path.exists() {
            debug!(log, "Path does not exist");
            return Ok(false);
        }

        let metadata = fs::metadata(&self.path).chain_err(|| {
            format!("Failed to gather metadata about path {:?}", self.path)
        })?;
        match self.file_type {
            FileType::File { ref contents } => {
                if !metadata.file_type().is_file() {
                    debug!(log, "Path doesn't point to a regular file");
                    return Ok(false);
                }

                if let Some(ref contents) = *contents {
                    let file = fs::File::open(&self.path).chain_err(|| {
                        format!("Failed to open file {:?} for hashing", self.path)
                    })?;
                    let old_sha1 = util::sha1(file)
                        .chain_err(|| {
                            format!("Failed to compute SHA-1 digest of file {:?}", self.path)
                        })?
                        .to_string();
                    let new_sha1 = util::sha1(io::Cursor::new(contents))
                        .chain_err(|| "Failed to compute SHA-1 digest")?
                        .to_string();
                    if old_sha1 != new_sha1 {
                        debug!(log, "File has wrong contents";
                               "old_sha1" => old_sha1, "new_sha1" => new_sha1);
                        return Ok(false);
                    }
                }
            }
            FileType::Dir => {
                if !metadata.file_type().is_dir() {
                    debug!(log, "Path doesn't point to a directory");
                    return Ok(false);
                }
            }
            FileType::Symlink { target: ref new_target } => {
                if !metadata.file_type().is_symlink() {
                    debug!(log, "Path doesn't point to a symlink");
                    return Ok(false);
                }

                let old_target = fs::read_link(&self.path).chain_err(|| {
                    format!("Failed to read link target of {:?}", self.path)
                })?;
                if old_target != *new_target {
                    let old_target = old_target.to_string_lossy().into_owned();
                    let new_target = new_target.to_string_lossy().into_owned();
                    debug!(log, "Symlink target is wrong";
                           "old_target" => old_target, "new_target" => new_target);
                    return Ok(false);
                }
            }
            FileType::Absent => {}
        }

        trace!(log, "File is up to date");
        Ok(true)
    }

    fn as_any(&self) -> &any::Any {
        self
    }
}

impl resource::UnresolvedResource for File {
    fn implicit_ensure<E>(&self, ensurer: &mut E)
    where
        E: resource::Ensurer,
    {
        if let Some(parent) = self.path.parent() {
            ensurer.ensure(File::at(parent).is_dir());
        }
    }
}

impl fmt::Display for File {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.file_type {
            FileType::Absent { .. } => write!(f, "absent")?,
            FileType::File { .. } => write!(f, "file")?,
            FileType::Dir { .. } => write!(f, "directory")?,
            FileType::Symlink { .. } => write!(f, "symlink")?,
        }

        write!(f, " {:?}", self.path)?;

        match self.file_type {
            FileType::File { contents: Some(ref contents) } => {
                let sha1 = util::sha1(io::Cursor::new(contents)).unwrap();
                write!(f, " with sha1 {}", &format!("{}", sha1)[..8])?
            }
            FileType::Symlink { ref target } => write!(f, " with target {:?}", target)?,
            _ => (),
        }
        Ok(())
    }
}