2015-01-10 21:00:15 +00:00
|
|
|
#![crate_name = "sync"]
|
2015-04-27 18:06:13 +00:00
|
|
|
#![feature(rustc_private)]
|
2015-01-10 19:31:55 +00:00
|
|
|
|
2014-06-15 14:25:00 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the uutils coreutils package.
|
|
|
|
*
|
|
|
|
* (c) Alexander Fomin <xander.fomin@ya.ru>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Last synced with: sync (GNU coreutils) 8.13 */
|
|
|
|
|
|
|
|
extern crate getopts;
|
|
|
|
extern crate libc;
|
|
|
|
|
|
|
|
use getopts::{optflag, getopts, usage};
|
|
|
|
|
2015-01-08 12:54:22 +00:00
|
|
|
#[path = "../common/util.rs"] #[macro_use] mod util;
|
2014-06-20 12:44:35 +00:00
|
|
|
|
2014-08-15 22:53:23 +00:00
|
|
|
static NAME: &'static str = "sync";
|
|
|
|
static VERSION: &'static str = "1.0.0";
|
|
|
|
|
2014-06-20 12:44:35 +00:00
|
|
|
#[cfg(unix)]
|
|
|
|
mod platform {
|
|
|
|
use super::libc;
|
|
|
|
|
|
|
|
extern {
|
|
|
|
fn sync() -> libc::c_void;
|
|
|
|
}
|
|
|
|
|
2015-01-10 18:07:08 +00:00
|
|
|
pub unsafe fn do_sync() -> isize {
|
2014-06-20 12:44:35 +00:00
|
|
|
sync();
|
|
|
|
0
|
|
|
|
}
|
2014-06-15 14:25:00 +00:00
|
|
|
}
|
|
|
|
|
2014-06-20 12:44:35 +00:00
|
|
|
#[cfg(windows)]
|
|
|
|
mod platform {
|
|
|
|
pub use super::libc;
|
2014-09-24 05:28:07 +00:00
|
|
|
use std::{mem, string};
|
2014-08-15 22:53:23 +00:00
|
|
|
use std::ptr::null;
|
2014-06-20 12:44:35 +00:00
|
|
|
|
|
|
|
extern "system" {
|
2014-06-30 11:13:38 +00:00
|
|
|
fn CreateFileA(lpFileName: *const libc::c_char,
|
2014-06-20 12:44:35 +00:00
|
|
|
dwDesiredAccess: libc::uint32_t,
|
|
|
|
dwShareMode: libc::uint32_t,
|
2014-06-30 11:13:38 +00:00
|
|
|
lpSecurityAttributes: *const libc::c_void, // *LPSECURITY_ATTRIBUTES
|
2014-06-20 12:44:35 +00:00
|
|
|
dwCreationDisposition: libc::uint32_t,
|
|
|
|
dwFlagsAndAttributes: libc::uint32_t,
|
2014-06-30 11:13:38 +00:00
|
|
|
hTemplateFile: *const libc::c_void) -> *const libc::c_void;
|
|
|
|
fn GetDriveTypeA(lpRootPathName: *const libc::c_char) -> libc::c_uint;
|
2014-06-20 12:44:35 +00:00
|
|
|
fn GetLastError() -> libc::uint32_t;
|
2014-06-30 11:13:38 +00:00
|
|
|
fn FindFirstVolumeA(lpszVolumeName: *mut libc::c_char,
|
|
|
|
cchBufferLength: libc::uint32_t) -> *const libc::c_void;
|
|
|
|
fn FindNextVolumeA(hFindVolume: *const libc::c_void,
|
|
|
|
lpszVolumeName: *mut libc::c_char,
|
2014-06-20 12:44:35 +00:00
|
|
|
cchBufferLength: libc::uint32_t) -> libc::c_int;
|
2014-06-30 11:13:38 +00:00
|
|
|
fn FindVolumeClose(hFindVolume: *const libc::c_void) -> libc::c_int;
|
|
|
|
fn FlushFileBuffers(hFile: *const libc::c_void) -> libc::c_int;
|
2014-06-20 12:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(unused_unsafe)]
|
|
|
|
unsafe fn flush_volume(name: &str) {
|
2014-09-24 05:28:07 +00:00
|
|
|
let name_buffer = name.to_c_str().as_ptr();
|
|
|
|
if 0x00000003 == GetDriveTypeA(name_buffer) { // DRIVE_FIXED
|
2015-01-25 06:46:55 +00:00
|
|
|
let sliced_name = &name[..name.len() - 1]; // eliminate trailing backslash
|
2014-09-24 05:28:07 +00:00
|
|
|
let sliced_name_buffer = sliced_name.to_c_str().as_ptr();
|
|
|
|
match CreateFileA(sliced_name_buffer,
|
|
|
|
0xC0000000, // GENERIC_WRITE
|
|
|
|
0x00000003, // FILE_SHARE_WRITE,
|
|
|
|
null(),
|
|
|
|
0x00000003, // OPEN_EXISTING
|
|
|
|
0,
|
|
|
|
null()) {
|
2015-01-25 06:46:55 +00:00
|
|
|
-1 => { // INVALID_HANDLE_VALUE
|
2014-09-24 05:28:07 +00:00
|
|
|
crash!(GetLastError(), "failed to create volume handle");
|
|
|
|
}
|
2015-01-25 06:46:55 +00:00
|
|
|
handle => {
|
2014-09-24 05:28:07 +00:00
|
|
|
if FlushFileBuffers(handle) == 0 {
|
|
|
|
crash!(GetLastError(), "failed to flush file buffer");
|
2014-06-20 12:44:35 +00:00
|
|
|
}
|
2014-09-24 05:28:07 +00:00
|
|
|
}
|
2014-06-20 12:44:35 +00:00
|
|
|
}
|
2014-09-24 05:28:07 +00:00
|
|
|
}
|
2014-06-20 12:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(unused_unsafe)]
|
2014-06-30 11:13:38 +00:00
|
|
|
unsafe fn find_first_volume() -> (String, *const libc::c_void) {
|
2015-01-08 12:58:23 +00:00
|
|
|
let mut name: [libc::c_char; 260] = mem::uninitialized(); // MAX_PATH
|
2014-08-15 22:53:23 +00:00
|
|
|
match FindFirstVolumeA(name.as_mut_ptr(),
|
|
|
|
name.len() as libc::uint32_t) {
|
2015-01-25 06:46:55 +00:00
|
|
|
-1 => { // INVALID_HANDLE_VALUE
|
2014-06-20 12:44:35 +00:00
|
|
|
crash!(GetLastError(), "failed to find first volume");
|
|
|
|
}
|
2015-01-25 06:46:55 +00:00
|
|
|
handle => {
|
2014-09-24 05:28:07 +00:00
|
|
|
(string::raw::from_buf(name.as_ptr() as *const u8), handle)
|
2014-06-20 12:44:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(unused_unsafe)]
|
|
|
|
unsafe fn find_all_volumes() -> Vec<String> {
|
|
|
|
match find_first_volume() {
|
|
|
|
(first_volume, next_volume_handle) => {
|
2015-01-08 20:59:07 +00:00
|
|
|
let mut volumes = vec![first_volume];
|
2014-06-20 12:44:35 +00:00
|
|
|
loop {
|
2015-01-08 12:58:23 +00:00
|
|
|
let mut name: [libc::c_char; 260] = mem::uninitialized(); // MAX_PATH
|
2014-08-15 22:53:23 +00:00
|
|
|
match FindNextVolumeA(next_volume_handle,
|
|
|
|
name.as_mut_ptr(),
|
|
|
|
name.len() as libc::uint32_t) {
|
2014-06-20 12:44:35 +00:00
|
|
|
0 => {
|
|
|
|
match GetLastError() {
|
|
|
|
0x12 => { // ERROR_NO_MORE_FILES
|
|
|
|
FindVolumeClose(next_volume_handle); // ignore FindVolumeClose() failures
|
|
|
|
break;
|
|
|
|
}
|
2015-01-25 06:46:55 +00:00
|
|
|
err => {
|
2014-06-20 12:44:35 +00:00
|
|
|
crash!(err, "failed to find next volume");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2014-09-24 05:28:07 +00:00
|
|
|
volumes.push(string::raw::from_buf(name.as_ptr() as *const u8));
|
2014-06-20 12:44:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
volumes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn do_sync() -> int {
|
|
|
|
let volumes = find_all_volumes();
|
|
|
|
for vol in volumes.iter() {
|
2015-01-25 06:46:55 +00:00
|
|
|
flush_volume(&vol);
|
2014-06-20 12:44:35 +00:00
|
|
|
}
|
|
|
|
0
|
|
|
|
}
|
2014-06-15 14:25:00 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 13:48:07 +00:00
|
|
|
pub fn uumain(args: Vec<String>) -> i32 {
|
2015-02-22 11:55:30 +00:00
|
|
|
let program = &args[0][..];
|
2014-06-15 14:25:00 +00:00
|
|
|
|
|
|
|
let options = [
|
|
|
|
optflag("h", "help", "display this help and exit"),
|
|
|
|
optflag("V", "version", "output version information and exit")
|
|
|
|
];
|
|
|
|
|
2015-04-27 18:06:13 +00:00
|
|
|
let matches = match getopts(&args[1..], &options) {
|
2014-06-15 14:25:00 +00:00
|
|
|
Ok(m) => { m }
|
2015-01-25 06:46:55 +00:00
|
|
|
_ => { help(program, &options); return 1 }
|
2014-06-15 14:25:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if matches.opt_present("h") {
|
2015-01-25 06:46:55 +00:00
|
|
|
help(program, &options);
|
2014-06-15 14:25:00 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if matches.opt_present("V") {
|
|
|
|
version();
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-01-10 21:00:15 +00:00
|
|
|
sync();
|
2014-06-15 14:25:00 +00:00
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn version() {
|
2014-09-17 03:08:40 +00:00
|
|
|
println!("{} (uutils) {}", NAME, VERSION);
|
2014-06-15 14:25:00 +00:00
|
|
|
println!("The MIT License");
|
|
|
|
println!("");
|
|
|
|
println!("Author -- Alexander Fomin.");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn help(program: &str, options: &[getopts::OptGroup]) {
|
2014-11-21 09:09:43 +00:00
|
|
|
println!("Usage: {} [OPTION]", program);
|
|
|
|
print!("{}", usage("Force changed blocks to disk, update the super block.", options));
|
2014-06-15 14:25:00 +00:00
|
|
|
}
|
2014-06-20 12:44:35 +00:00
|
|
|
|
2015-01-10 21:00:15 +00:00
|
|
|
fn sync() -> isize {
|
2014-06-20 12:44:35 +00:00
|
|
|
unsafe {
|
|
|
|
platform::do_sync()
|
|
|
|
}
|
2014-06-26 05:56:03 +00:00
|
|
|
}
|