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
// Aldaron's Device Interface / Storage
// Copyright (c) 2017 Plop Grizzly, Jeron Lau <jeron.lau@plopgrizzly.com>
// Licensed under the MIT LICENSE
//
// src/lib.rs

//! Aldaron's Device Interface - Storage (adi_storage) is a Rust library for
//! interfacing with a persistent storage device (ie: hard drive, solid state
//! drive, sd card, flash drive, etc.).

#![doc(
	html_logo_url =
		"https://rawgit.com/aldarons-tech/adi_storage/master/res/icon.png",
	html_favicon_url =
		"https://rawgit.com/aldarons-tech/adi_storage/master/res/symbol.png",
	html_root_url = "http://at.plopgrizzly.tech/utem/"
)]

extern crate whoami;

use std::fs;
use std::path::Path;
use std::io::{ Read, Write };

/// Whether a path is a file or a folder.
pub enum PathType {
	Folder,
	File,
}

/// A persistent storage device.
pub struct Storage {
	// "/app_name.developer/" on Aldaron's OS
	// "/usr/local/share/at_root/app_name.developer/" on Linux
	// "c:/program files/at_root/app_name.developer/" on Windows
	app_root: String,
	// The user currently logged in
	user: String,
}

impl Storage {
	/// Use the `storage!()` macro instead.
	pub fn new(app_name: &str, developer: &str) -> Storage {
		let mut app_root = if cfg!(target_os = "linux") {
			"/usr/local/share/at_root/"
		} else if cfg!(target_os = "windows") {
			"C:/Program Files/at_root/"
		} else if cfg!(target_os = "aldarons_os") {
			"/"
		} else {
			panic!("AA!")
		}.to_string();

		// "/app_name.developer/"
		app_root.push_str(&app_name.to_lowercase());
		app_root.push_str(".");
		app_root.push_str(&developer);
		app_root.push_str("/");

		// "/app_name.developer/user.main/"
		// TODO: Only works/compiles for UNIX currently.
		let mut user = String::new();

		user.push_str(&whoami::username().to_lowercase());
		user.push_str(".");
		user.push_str(&whoami::computer());

		Storage {
			app_root: app_root,
			user: user,
		}
	}

	pub fn sync(&self) {
	}
}

impl ::std::fmt::Display for Storage {
	fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
		write!(f, "{{ app_root: \"{}\", user: \"{}\" }}", self.app_root,
			self.user)
	}
}

/// Storage
#[macro_export] macro_rules! storage {
	() => ( {
		let app_name = env!("CARGO_PKG_NAME");
		let developer = include!(concat!(env!("CARGO_MANIFEST_DIR"),
			"/target/res/src/developer.rs"));

		adi_storage::Storage::new(app_name, developer)
	} )
}

/// Save a file.
#[macro_export] macro_rules! file_save {
	(file_name) => ( {
		let app_name = include!(concat!(env!("CARGO_MANIFEST_DIR"),
			"/target/res/src/name.rs"));
		let developer = include!(concat!(env!("CARGO_MANIFEST_DIR"),
			"/target/res/src/developer.rs"));
		let mut app_root = STORAGE_MAIN.to_string();

		// "/app_name.developer/"
		app_root.push_str(app_name);
		app_root.push_str(".");
		app_root.push_str(developer);
		app_root.push_str("/");

		// "/app_name.developer/user.main/"
		// TODO: Only works/compiles for UNIX currently.
		let user = users::get_user_by_uid(users::get_current_uid());
		app_root.push_str(user.unwrap());
		app_root.push_str(".");
		app_root.push_str(hostname::get_hostname().unwrap());
	} )
}

/// Save a file.
pub fn save<P: AsRef<Path>, B: AsRef<[u8]>>(filename: P, data: B) -> () {
	let path = filename.as_ref();
	let parent = path.parent().unwrap();

	if parent.exists() == false {
		mkdir(parent);
	}

	fs::File::create(path).unwrap().write_all(data.as_ref()).unwrap();
}

/// Load a file.
pub fn load<P: AsRef<Path>>(filename: P) -> Vec<u8> {
	let mut file = fs::File::open(filename).unwrap();
	let mut contents = Vec::new();

	file.read_to_end(&mut contents).unwrap();

	contents
}

/// Delete a file.
pub fn rm<P: AsRef<Path>>(path: P) {
	fs::remove_file(path).unwrap();
}

/// Delete a folder and all of it's contents ( use carefully ).
pub fn rmdir<P: AsRef<Path>>(path: P) {
	fs::remove_dir_all(path).unwrap();
}

/// Make a folder.
pub fn mkdir<P: AsRef<Path>>(path: P) {
	fs::create_dir_all(path).unwrap();
}

// Because: https://doc.rust-lang.org/std/fs/fn.rename.html Platform-Specifc...
fn mv_ll<P: AsRef<Path>>(old: P, new: P) {
	rmdir(&new);
	mkdir(&new);
	fs::rename(old, new).unwrap();
}

/// Move or rename a file ( change it's path ).
pub fn mv<P: AsRef<Path>>(old: P, new: P) {
	mv_ll(old, new);
}

/// Get the permissions of a file.
pub fn get_permissions<P: AsRef<Path>>(name: P) -> fs::Permissions {
	let file = fs::File::open(name).unwrap();
	
	file.metadata().unwrap().permissions()
}

/// Set the permissions of a file.
pub fn set_permissions<P: AsRef<Path>>(name: P, permissions: fs::Permissions) -> () {
	let file = fs::File::open(name).unwrap();

	file.set_permissions(permissions).unwrap()
}

// Remove first folder in relative path
fn fnrm_first<P: AsRef<Path>>(input: P) -> String {
	let input = input.as_ref().to_str().unwrap();
	let index = input.find('/').unwrap();
	let mut input = input.to_string();
	let t: String = input.drain(index+1..).collect();

	t
}

/// Duplicate a file.
pub fn copy<P: AsRef<Path>>(src: P, dst: P) -> Result<(), String> {
	let src = src.as_ref();
	let dst = dst.as_ref();

	if let Some(pt) = path_type(src) {
		match pt {
			PathType::File => {
				let permissions = get_permissions(src);
				let data = load(src);

				save(dst, data.as_slice());
				set_permissions(dst, permissions);
				Ok(())
			}
			PathType::Folder => {
				if let Ok(dir_iter) = fs::read_dir(src) {
					for entry in dir_iter {
						if let Ok(entry) = entry {
							let path = entry.path();
							let apnd = fnrm_first(&path);
							let dest = dst.join(&apnd);

							copy(path, dest)?;
						} else {
							return Err("intermitten\
								t io".to_string())
						}
					}
					Ok(())
				} else {
					Err(format!("Couldn't copy folder {:?} \
						because it lacks read \
						permission", src))
				}
			}
		}
	} else {
		Err(format!("Couldn't copy {:?} because it doesn't exist.", src))
	}
}

/// Returns true only if `filepath` exists.
pub fn get_exists(filepath: &str) -> bool {
	Path::new(filepath).exists()
}

/// Get the type of file at `path`, or `None` if there is no file at `path`.
pub fn path_type<P: AsRef<Path>>(path: P) -> Option<PathType> {
	let path = path.as_ref();

	if path.exists() == false {
		None
	} else if path.is_file() == true {
		Some(PathType::File)
	} else if path.is_dir() == true {
		Some(PathType::Folder)
	} else {
		panic!("Filesystem contains mysterious entity (Not a file or a \
			folder)!");
	}
}