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
//! A module which defines evaluating any kind of redirection.

use {Fd, POLLED_TWICE, STDIN_FILENO, STDOUT_FILENO};
use env::{AsyncIoEnvironment, FileDescEnvironment, IsInteractiveEnvironment, StringWrapper,
          WorkingDirectoryEnvironment};
use eval::{Fields, TildeExpansion, WordEval, WordEvalConfig};
use error::RedirectionError;
use future::{Async, EnvFuture, Poll};
use io::{FileDesc, Permissions, Pipe};
use std::borrow::Cow;
use std::path::Path;
use std::fs::OpenOptions;
use std::io::Result as IoResult;

/// Indicates what changes should be made to the environment as a result
/// of a successful `Redirect` evaluation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RedirectAction<T> {
    /// Indicates that a descriptor should be closed.
    Close(Fd),
    /// Indicates that a descriptor should be opened with
    /// a given file handle and permissions.
    Open(Fd, T, Permissions),
    /// Indicates that the body of a heredoc should be asynchronously written
    /// to a file handle on a best effor basis (i.e. write as much of the body
    /// as possible but give up on appropriate errors such as broken pipes).
    HereDoc(Fd, Vec<u8>),
}

impl<T> RedirectAction<T> {
    /// Applies changes to a given environment as appropriate.
    pub fn apply<E: ?Sized>(self, env: &mut E) -> IoResult<()>
        where T: From<FileDesc>,
              E: AsyncIoEnvironment + FileDescEnvironment<FileHandle = T>,
    {
        match self {
            RedirectAction::Close(fd) => env.close_file_desc(fd),
            RedirectAction::Open(fd, file_desc, perms) => env.set_file_desc(fd, file_desc, perms),
            RedirectAction::HereDoc(fd, body) => {
                let pipe = try!(Pipe::new());
                env.set_file_desc(fd, pipe.reader.into(), Permissions::Read);
                env.write_all_best_effort(pipe.writer, body);
            },
        }

        Ok(())
    }
}

/// A trait for evaluating file descriptor redirections.
pub trait RedirectEval<E: ?Sized> {
    /// The type of handle that should be added to the environment.
    type Handle;
    /// An error that can arise during evaluation.
    type Error;
    /// A future which will carry out the evaluation (but will not update the
    /// environment with the result).
    type EvalFuture: EnvFuture<E, Item = RedirectAction<Self::Handle>, Error = Self::Error>;

    /// Evaluates a redirection path and opens the appropriate redirect.
    ///
    /// Newly opened/closed/duplicated/heredoc file descriptors are NOT
    /// updated in the environment, and thus it is up to the caller to
    /// update the environment as appropriate.
    fn eval(self, env: &E) -> Self::EvalFuture;
}

fn eval_path<W, E: ?Sized>(path: W, env: &E) -> W::EvalFuture
    where W: WordEval<E>,
          E: IsInteractiveEnvironment,
{
    path.eval_with_config(env, WordEvalConfig {
        tilde_expansion: TildeExpansion::First,
        split_fields_further: env.is_interactive(),
    })
}

fn redirect<W, E: ?Sized>(fd: Fd, path: W, opts: OpenOptions, perms: Permissions, env: &E)
    -> Redirect<W::EvalFuture>
    where W: WordEval<E>,
          E: IsInteractiveEnvironment,
{
    Redirect {
        state: State::Open(fd, eval_path(path, env), Some(opts), perms),
    }
}

/// Evaluate a redirect which will open a file for reading.
///
/// If `fd` is not specified, then `STDIN_FILENO` will be used.
pub fn redirect_read<W, E: ?Sized>(fd: Option<Fd>, path: W, env: &E)
    -> Redirect<W::EvalFuture>
    where W: WordEval<E>,
          E: IsInteractiveEnvironment,
{
    let fd = fd.unwrap_or(STDIN_FILENO);
    let perms = Permissions::Read;

    redirect(fd, path, perms.into(), perms, env)
}

/// Evaluate a redirect which will open a file for writing, failing if the
/// `noclobber` option is set.
///
/// If `fd` is not specified, then `STDOUT_FILENO` will be used.
///
/// > *Note*: checks for `noclobber` are not yet implemented.
pub fn redirect_write<W, E: ?Sized>(fd: Option<Fd>, path: W, env: &E)
    -> Redirect<W::EvalFuture>
    where W: WordEval<E>,
          E: IsInteractiveEnvironment,
{
    // FIXME: check for and fail if noclobber option is set
    redirect_clobber(fd, path, env)
}

/// Evaluate a redirect which will open a file for reading and writing.
///
/// If `fd` is not specified, then `STDIN_FILENO` will be used.
pub fn redirect_readwrite<W, E: ?Sized>(fd: Option<Fd>, path: W, env: &E)
    -> Redirect<W::EvalFuture>
    where W: WordEval<E>,
          E: IsInteractiveEnvironment,
{
    let fd = fd.unwrap_or(STDIN_FILENO);
    let perms = Permissions::ReadWrite;

    redirect(fd, path, perms.into(), perms, env)
}

/// Evaluate a redirect which will open a file for writing, regardless if the
/// `noclobber` option is set.
///
/// If `fd` is not specified, then `STDOUT_FILENO` will be used.
pub fn redirect_clobber<W, E: ?Sized>(fd: Option<Fd>, path: W, env: &E)
    -> Redirect<W::EvalFuture>
    where W: WordEval<E>,
          E: IsInteractiveEnvironment,
{
    let fd = fd.unwrap_or(STDOUT_FILENO);
    let perms = Permissions::Write;

    redirect(fd, path, perms.into(), perms, env)
}

/// Evaluate a redirect which will open a file in append mode.
///
/// If `fd` is not specified, then `STDOUT_FILENO` will be used.
pub fn redirect_append<W, E: ?Sized>(fd: Option<Fd>, path: W, env: &E)
    -> Redirect<W::EvalFuture>
    where W: WordEval<E>,
          E: IsInteractiveEnvironment,
{
    let fd = fd.unwrap_or(STDOUT_FILENO);
    let mut opts = OpenOptions::new();
    opts.append(true);

    redirect(fd, path, opts, Permissions::Write, env)
}

fn redirect_dup<W, E: ?Sized>(dst_fd: Fd, src_fd: W, readable: bool, env: &E)
    -> Redirect<W::EvalFuture>
    where W: WordEval<E>,
          E: IsInteractiveEnvironment,
{
    Redirect {
        state: State::Dup(dst_fd, eval_path(src_fd, env), readable),
    }
}

/// Evaluate a redirect which will either duplicate a readable file descriptor
/// as specified by `src_fd` into `dst_fd`, or close `dst_fd` if `src_fd`
/// evaluates to `-`.
///
/// If `fd` is not specified, then `STDIN_FILENO` will be used.
pub fn redirect_dup_read<W, E: ?Sized>(dst_fd: Option<Fd>, src_fd: W, env: &E)
    -> Redirect<W::EvalFuture>
    where W: WordEval<E>,
          E: IsInteractiveEnvironment,
{
    redirect_dup(dst_fd.unwrap_or(STDIN_FILENO), src_fd, true, env)
}

/// Evaluate a redirect which will either duplicate a writeable file descriptor
/// as specified by `src_fd` into `dst_fd`, or close `dst_fd` if `src_fd`
/// evaluates to `-`.
///
/// If `fd` is not specified, then `STDOUT_FILENO` will be used.
pub fn redirect_dup_write<W, E: ?Sized>(dst_fd: Option<Fd>, src_fd: W, env: &E)
    -> Redirect<W::EvalFuture>
    where W: WordEval<E>,
          E: IsInteractiveEnvironment,
{
    redirect_dup(dst_fd.unwrap_or(STDOUT_FILENO), src_fd, false, env)
}

/// Evaluate a redirect which write the body of a *here-document* into `fd`.
///
/// If `fd` is not specified, then `STDIN_FILENO` will be used.
pub fn redirect_heredoc<W, E: ?Sized>(fd: Option<Fd>, heredoc: W, env: &E)
    -> Redirect<W::EvalFuture>
    where W: WordEval<E>,
          E: IsInteractiveEnvironment,
{
    Redirect {
        state: State::HereDoc(fd.unwrap_or(STDIN_FILENO), eval_path(heredoc, env)),
    }
}

/// A future representing the evaluation of a redirect.
#[must_use = "futures do nothing unless polled"]
#[derive(Debug)]
pub struct Redirect<F> {
    state: State<F>,
}

#[derive(Debug)]
enum State<F> {
    Open(Fd, F, Option<OpenOptions>, Permissions),
    Dup(Fd, F, bool /* readable dup */),
    HereDoc(Fd, F),
}

impl<T, F, E: ?Sized> EnvFuture<E> for Redirect<F>
    where T: StringWrapper,
          F: EnvFuture<E, Item = Fields<T>>,
          F::Error: From<RedirectionError>,
          E: FileDescEnvironment + IsInteractiveEnvironment + WorkingDirectoryEnvironment,
          E::FileHandle: Clone + From<FileDesc>,
{
    type Item = RedirectAction<E::FileHandle>;
    type Error = F::Error;

    fn poll(&mut self, env: &mut E) -> Poll<Self::Item, Self::Error> {
        macro_rules! poll_path {
            ($f:expr, $env:expr) => {{
                match try_ready!($f.poll($env)) {
                    Fields::Single(path) => path,
                    Fields::At(mut v)   |
                    Fields::Star(mut v) |
                    Fields::Split(mut v) => {
                        if v.len() == 1 {
                            v.pop().unwrap()
                        } else {
                            let v = v.into_iter().map(StringWrapper::into_owned).collect();
                            return Err(RedirectionError::Ambiguous(v).into());
                        }
                    },
                    Fields::Zero => return Err(RedirectionError::Ambiguous(Vec::new()).into()),
                }
            }}
        }

        let action = match self.state {
            // FIXME: on unix set file permission bits based on umask
            State::Open(fd, ref mut f, ref mut opts, perms) => {
                let path = poll_path!(f, env);

                let action = opts.take()
                    .expect(POLLED_TWICE)
                    .open(env.path_relative_to_working_dir(Cow::Borrowed(Path::new(path.as_str()))))
                    .map(FileDesc::from)
                    .map(|fdesc| RedirectAction::Open(fd, fdesc.into(), perms))
                    .map_err(|err| RedirectionError::Io(err, Some(path.into_owned())));

                try!(action)
            },

            State::Dup(dst_fd, ref mut f, readable) => {
                let src_fd = poll_path!(f, env);
                let src_fd = src_fd.as_str();

                if src_fd == "-" {
                    return Ok(Async::Ready(RedirectAction::Close(dst_fd)));
                }

                let fd_handle_perms = Fd::from_str_radix(src_fd, 10)
                    .ok()
                    .and_then(|fd| env.file_desc(fd).map(|(fdes, perms)| (fd, fdes, perms)));

                let src_fdes = match fd_handle_perms {
                    Some((fd, fdes, perms)) => {
                        if (readable && perms.readable()) || (!readable && perms.writable()) {
                            fdes.clone()
                        } else {
                            return Err(RedirectionError::BadFdPerms(fd, perms).into());
                        }
                    },

                    None => return Err(RedirectionError::BadFdSrc(src_fd.to_owned()).into()),
                };

                let perms = if readable { Permissions::Read } else { Permissions::Write };
                RedirectAction::Open(dst_fd, src_fdes, perms)
            },

            State::HereDoc(fd, ref mut f) => {
                let body = match try_ready!(f.poll(env)) {
                    Fields::Zero => Vec::new(),
                    Fields::Single(path) => path.into_owned().into_bytes(),
                    Fields::At(mut v)   |
                    Fields::Star(mut v) |
                    Fields::Split(mut v) => {
                        if v.len() == 1 {
                            v.pop().unwrap().into_owned().into_bytes()
                        } else {
                            let len = v.iter().map(|f| f.as_str().len()).sum();
                            let mut body = Vec::with_capacity(len);
                            for field in v {
                                body.extend_from_slice(field.as_str().as_bytes());
                            }
                            body
                        }
                    },
                };

                RedirectAction::HereDoc(fd, body)
            },
        };

        Ok(Async::Ready(action))
    }

    fn cancel(&mut self, env: &mut E) {
        match self.state {
            State::Open(_, ref mut f, _, _) |
            State::Dup(_, ref mut f, _) |
            State::HereDoc(_, ref mut f) => f.cancel(env),
        }
    }
}