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

//! Utilities for common filesystem operations.
//!
//! **fsutils** provides an API based on Bash commands and includes a number
//! of utility functions to make interacting with the filesystem simpler and more
//! ergonomic.

#[macro_use]
extern crate log;

use std::fs;
use std::path::Path;
use std::io::Write;

/// Creates a directory recursively at passed path
/// and returns a boolean based on success or failure.
///
/// ## Usage:
///
/// ```
/// assert_eq!(fsutils::mkdir("testdir"), true);
///
/// # // Cleanup
/// # fsutils::rmdir("testdir");
/// ```
pub fn mkdir(path: &str) -> bool {
    if !path_exists(path) {
        match fs::create_dir_all(path) {
            Ok(_) => {
                info!("Created {}", path);
                true
            }
            Err(e) => {
                info!("Error creating file: {}", e);
                false
            }
        }
    } else {
        false
    }
}

/// Removes a file at passed path
/// and returns a boolean based on success or failure.
///
/// ## Usage:
///
/// ```
/// fsutils::create_file("testfile.txt");
/// assert_eq!(fsutils::rm("testfile.txt"), true);
/// ```
pub fn rm(path: &str) -> bool {
    // str to Path
    let new_path = Path::new(path);
    if new_path.exists() {
        match fs::remove_file(path) {
            Ok(_) => {
                info!("Removed file {}", path);
                true
            },
            Err(e) => {
                info!("Error removing {} {}", path, e);
                false
            }
        }
    } else {
        false
    }
}

/// Removes an empty directory
/// and returns a boolean based on success or failure.
///
/// This does not remove a directory recursively. Use `fsutils::rm_r`
/// if you need recursive directory deletion.
///
/// # Usage:
///
/// ```
/// use fsutils::rmdir;
/// fsutils::mkdir("testdir");
/// assert_eq!(rmdir("testdir"), true);
/// ```
pub fn rmdir(path: &str) -> bool {
    // Turn str path into Path
    let new_path = Path::new(path);
    if new_path.exists() {
        match fs::remove_dir(path) {
            Ok(_) => {
                info!("Removed directory at {}", path);
                true
            },
            Err(_e) => {
                info!("The directory {} is not empty", path);
                false
            }
        }
    } else {
        info!("Directory does not exist");
        true
    }
}

/// Removes a directory recursively
/// and returns a boolean based on success or failure.
///
/// You should use this carefully.
///
/// ## Usage:
///
/// ```
/// fsutils::mkdir("testdir");
/// fsutils::create_file("testdir/testfile");
///
/// assert_eq!(fsutils::rm_r("testdir"), true);
/// ```
pub fn rm_r(path: &str) -> bool {
    // Turn str path into Path
    let new_path = Path::new(path);
    if new_path.exists() {
        match fs::remove_dir_all(path) {
            Ok(_) => {
                info!("Removed directory at {}", path);
                true
            },
            Err(_e) => {
                info!("The directory {} is not empty", path);
                false
            }
        }
    } else {
        info!("Directory does not exist");
        true
    }
}

/// Checks if a path exists
/// and returns a boolean based on success or failure.
///
/// # Usage:
///
/// ```
/// fsutils::create_file("testfile");
/// assert_eq!(fsutils::path_exists("testfile"), true);
/// assert_eq!(fsutils::path_exists("a_very_1234_unlikely_9876_filename"), false);
/// 
/// # // Cleanup
/// # fsutils::rm("testfile");
/// ```
pub fn path_exists(path: &str) -> bool {
    // Turn str path into Path
    let new_path = Path::new(path);
    if new_path.exists() {
        info!("{} exists", path);
        true
    } else {
        info!("{} does not exist", path);
        false
    }
}

/// Checks if a directory is empty
/// and returns a boolean based on success or failure.
///
/// # Usage:
///
/// ```
/// fsutils::mkdir("empty_directory");
/// fsutils::mkdir("full_directory");
/// fsutils::create_file("full_directory/a_file.txt");
/// fsutils::create_file("full_directory/another_file.txt");
///
/// assert_eq!(fsutils::directory_is_empty("full_directory"), false);
/// assert_eq!(fsutils::directory_is_empty("empty_directory"), true);
///
/// # // Cleanup
/// # fsutils::rmdir("empty_directory");
/// # fsutils::rm_r("full_directory");
/// ```
pub fn directory_is_empty(path: &str) -> bool {
    // Turn str path into Path
    let new_path = Path::new(path);
    if new_path.exists() {
        if new_path.is_dir() {
            let mut i = 0;
            // iterate through entries and count them
            // `fs::read_dir` returns type `ReadDir`
            for entry in fs::read_dir(path) {
                // Iterating over `ReadDir` returns a Result<DirEntry>`
                // which is what we want to give us the count.
                for _ in entry {
                    i += 1;
                }
            }
            // if the count of directory entries is 1 (it counts itself), it is empty
            if i == 0 {
                true
            } else {
                false
            }
        } else {
            info!("The path {} passed is not a directory", path);
            false
        }
    } else {
        info!("The path {} passed does not exist.", path);
        false
    }
}

/// Moves a file from `path_one` to `path_two`
/// and returns a boolean based on success or failure.
///
/// # Usage:
///
/// ```
/// fsutils::mkdir("directory_one");
/// fsutils::mkdir("directory_two");
/// fsutils::create_file("directory_one/the_file");
///
/// assert_eq!(fsutils::mv("directory_one/the_file", "directory_two/the_file"), true);
///
/// # // Cleanup
/// # fsutils::rm_r("directory_one");
/// # fsutils::rm_r("directory_two");
/// ```
pub fn mv(path_one: &str, path_two: &str) -> bool {
    let p1 = Path::new(path_one);
    if p1.exists() {
        match fs::rename(path_one, path_two) {
            Ok(_) => {
                info!("Moved from {} to {}.", path_one, path_two);
                true
            },
            Err(e) => {
                info!("File moving error: {}", e);
                false
            }
        }
    } else {
        false
    }
}

/// Creates a file and returns a boolean based on success or failure.
///
/// ## Usage:
///
/// ```
/// assert_eq!(fsutils::create_file("the_file"), true);
///
/// # // Cleanup
/// # fsutils::rm("the_file");
/// ```
pub fn create_file(path: &str) -> bool {
    match fs::File::create(path) {
        Ok(_f) => {
            info!("Successfully wrote file to {}", path);
            true
        }
        Err(e) => {
            info!("{}", e);
            false
        }
    }
}

/// Creates a file from bytes
/// and returns a boolean based on success or failure.
///
/// # Usage:
///
/// ```
/// let binary_file: &'static [u8] = b"01001000 01100101 01101100 01101100 01101111 00100001";
///
/// assert_eq!(fsutils::create_file_bytes("a_binary_file", binary_file), true);
///
/// # // Cleanup
/// # fsutils::rm("a_binary_file");
/// ```
pub fn create_file_bytes(path: &str, bytes_to_write: &[u8]) -> bool {
    match fs::File::create(path) {
        Ok(mut buffer) => {
            match buffer.write_all(bytes_to_write) {
                Ok(_) => {
                    info!("Wrote buffer to {}", path);
                    true
                },
                Err(e) => {
                    info!("{}", e);
                    false
                }
            }
        }
        Err(e) => {
            info!("{}", e);
            false
        }
    }
}