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
// Copyright 2017 dmg Developers
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

//! Simple attaching/detaching of macOS disk images.
//!
//! # Example
//!
//! Attach a disk image until dropped:
//!
//! ```rust
//! use dmg::Attach;
//!
//! let info = Attach::new("Test.dmg").with().expect("could not attach");
//! println!("Mounted at {:?}", info.mount_point);
//! // Detched when 'info' dropped
//! ```
//!
//! If you prefer to handle detaching yourself simply use [`attach()`](struct.Attach.html#method.attach):
//!
//! ```rust
//! use dmg::Attach;
//!
//! let info = Attach::new("Test.dmg").attach().expect("could not attach");
//! println!("Device node {:?}", info.device);
//! info.detach().expect("could not detach"); // There is also .force_detach()
//! ```
//!
//! If you know the device node or mount point, you can detach it like this too:
//!
//! ```rust,no_run
//! use dmg;
//! dmg::detach("/Volumes/Test", false).expect("could not detach"); // Do not force detach
//! ```
//!
//! For more examples see [`src/tests.rs`][1] and [`src/bin/demo.rs`][2]
//!
//!
//! [1]: https://github.com/mgoszcz2/dmg/blob/master/src/tests.rs
//! [2]: https://github.com/mgoszcz2/dmg/blob/master/src/bin/demo.rs

extern crate plist;
#[macro_use]
extern crate log;

use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::io::{self, ErrorKind, Cursor};
use std::ops::Deref;
use std::env;

use plist::Plist;

#[cfg(test)]
mod tests;

static DISK_COMMAND: &str = "hdiutil";

enum Mount {
    Default,
    Random(PathBuf),
    Root(PathBuf),
    Point(PathBuf)
}

/// Builder to attach a disk image.
pub struct Attach {
    image: PathBuf,
    mount: Mount,
    hidden: bool,
    force_readonly: bool,
}

/// Data associated with an attached disk image.
#[derive(Debug)]
pub struct Info {
    /// Path at which the disk image is mounted.
    pub mount_point: PathBuf,

    /// Device node path for this disk image.
    pub device: PathBuf,
}

/// Convinience handle for detaching an attached disk image.
///
/// Created with [`attach()`](struct.Attach.html#method.attach)
#[derive(Debug)]
pub struct Handle(Info);

/// An attached disk image handle that detaches it when dropped.
///
/// Created with [`with()`](struct.Attach.html#method.with)
#[derive(Debug)]
pub struct With(Info);

macro_rules! check {
    ($opt:expr) => {
        match $opt {
            Some(res) => res,
            None => return Err(io::Error::new(ErrorKind::InvalidData, "could not find property")),
        }
    }
}

macro_rules! deref_info {
    ($name:ident) => {
        /// Access the [`Info`](struct.Info.html) struct associated with this handle.
        impl Deref for $name {
            type Target = Info;
            fn deref(&self) -> &Info {
                &self.0
            }
        }
    }
}

deref_info!(Handle);
deref_info!(With);

impl Handle {
    /// Detach the image, ignoring any open files.
    pub fn force_detach(self) -> io::Result<()> {
        detach(&self.device, true)
    }

    /// Detach the image.
    pub fn detach(self) -> io::Result<()> {
        detach(&self.device, false)
    }
}

/// Detach the disk image on drop
impl Drop for With {
    fn drop(&mut self) {
        detach(&self.device, false).expect("could not detach");
    }
}

macro_rules! mount_fn {
    ($doc:expr, $name:ident, $variant:ident) => {
        #[doc=$doc]
        pub fn $name<P: Into<PathBuf>>(mut self, path: P) -> Attach {
            self.mount = Mount::$variant(path.into());
            self
        }
    }
}

macro_rules! enable_fn {
    ($doc:expr, $name:ident) => {
        #[doc=$doc]
        pub fn $name(mut self) -> Attach {
            self.$name = true;
            self
        }
    }
}

impl Attach {
    /// Creates a new attach builder for the given disk image.
    pub fn new<P: Into<PathBuf>>(path: P) -> Attach {
        Attach {
            image: path.into(),
            mount: Mount::Default,
            hidden: false,
            force_readonly: false,
        }
    }


    mount_fn!("Mount volumes on subdirectories of path instead of under `/Volumes`.", mount_root, Root);
    mount_fn!("Asuming only one volume, mount it at path instead of in `/Volumes`.", mount_point, Point);
    mount_fn!("Mount under `path` with a random unique mount point directory name.", mount_random, Random);
    enable_fn!("Render the volume invisible in applications like Finder.", hidden);
    enable_fn!("Force the device to be read-only.", force_readonly);

    /// Mount in a random folder inside the temporary directory.
    ///
    /// Equivalent to `mount_random(std::env::temp_dir())`
    pub fn mount_temp(self) -> Attach {
        self.mount_random(env::temp_dir())
    }

    fn attach_info(self) -> io::Result<Info> {
        let mut cmd = Command::new(DISK_COMMAND);
        cmd.arg("attach");

        match self.mount {
            Mount::Default => {},
            Mount::Random(ref path) => {
                cmd.arg("-mountrandom");
                cmd.arg(path);
            },
            Mount::Root(ref path) => {
                cmd.arg("-mountroot");
                cmd.arg(path);
            },
            Mount::Point(ref path) => {
                cmd.arg("-mountpoint");
                cmd.arg(path);
            }
        }

        if self.force_readonly {
            cmd.arg("-readonly");
        }

        if self.hidden {
            cmd.arg("-nobrowse");
        }

        cmd.arg("-plist");
        cmd.arg(&self.image);

        info!("Attaching {:?}", cmd);
        let output = cmd.output()?;
        info!("Status {:?}", output.status);

        if !output.status.success() {
            // This is not as informative as I wish it would be
            // .. but neither is hdiutil
            return Err(io::Error::new(ErrorKind::Other, "hdiutil failed"));
        }

        if let Ok(plist) = Plist::read(Cursor::new(output.stdout)) {
            let entities = check!(check!(check!(plist.as_dictionary()).get("system-entities")).as_array());
            for entity in entities {
                let properties = check!(entity.as_dictionary());
                if let Some(mount_point) = properties.get("mount-point") {
                    return Ok(Info {
                        mount_point: PathBuf::from(check!(mount_point.as_string())),
                        // If we don't have this something has gonne _really_ wrong
                        device: PathBuf::from(check!(properties["dev-entry"].as_string())),
                    });
                }
            }
            return Err(io::Error::new(ErrorKind::Other, "could not extract data"));
        }
        return Err(io::Error::new(ErrorKind::InvalidData, "could not parse plist"));
    }

    /// Attach the disk image
    pub fn attach(self) -> io::Result<Handle> {
        self.attach_info().map(Handle)
    }

    /// Attach the disk image, detaching when dropped
    pub fn with(self) -> io::Result<With> {
        self.attach_info().map(With)
    }
}

/// Detach an image using a path.
///
/// The path can be either a device node path or a mount point.
pub fn detach<P: AsRef<Path>>(path: P, force: bool) -> io::Result<()> {
    let mut cmd = Command::new(DISK_COMMAND);
    cmd.stdout(Stdio::null());
    cmd.stderr(Stdio::null());

    cmd.arg("detach");
    if force {
        cmd.arg("-force");
    }
    cmd.arg(path.as_ref());

    info!("Detaching (force: {:?}): {:?}", force, cmd);
    let status = cmd.status()?;
    info!("Status {:?}", status);

    if status.success() {
        Ok(())
    } else {
        Err(io::Error::new(ErrorKind::Other, "non-zero exit status for detach"))
    }
}