-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 674101a
Showing
9 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
target/ | ||
**/*.rs.bk | ||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "fsync" | ||
version = "0.1.0" | ||
authors = ["Erle Carrara <[email protected]>"] | ||
repository = "https://github.com/ecarrara/fsync-rs.git" | ||
homepage = "https://github.com/ecarrara/fsync-rs" | ||
license = "MIT" | ||
readme = "README.md" | ||
description = "Keeps files and directories synchronized." | ||
|
||
[dependencies] | ||
libc = "0.2.21" | ||
error-chain = "0.10.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (C) 2017 Erle Carrara | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
of the Software, and to permit persons to whom the Software is furnished to do | ||
so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# fsync | ||
|
||
Keeps files or directories in sync. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
reorder_imports = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
error_chain! { | ||
foreign_links { | ||
Io(::std::io::Error); | ||
SystemTimeError(::std::time::SystemTimeError); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//! Syncronize files and directories. | ||
#![deny(missing_docs, | ||
missing_debug_implementations, missing_copy_implementations, | ||
trivial_casts, trivial_numeric_casts, | ||
unused_import_braces, unused_qualifications)] | ||
|
||
#[macro_use] | ||
extern crate error_chain; | ||
extern crate libc; | ||
|
||
mod errors; | ||
mod utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use errors::Result; | ||
use libc::{futimens, timespec, c_long, time_t}; | ||
use std::fs::File; | ||
use std::io::Error as IOError; | ||
use std::os::unix::io::AsRawFd; | ||
use std::path::Path; | ||
use std::time::{SystemTime, UNIX_EPOCH}; | ||
|
||
|
||
/// Set file acessed and modified time. | ||
pub fn set_file_times<P: AsRef<Path>>(path: P, | ||
atime: &SystemTime, | ||
mtime: &SystemTime) | ||
-> Result<()> { | ||
let atime_since_epoch = atime.duration_since(UNIX_EPOCH)?; | ||
let mtime_since_epoch = mtime.duration_since(UNIX_EPOCH)?; | ||
|
||
let times = [timespec { | ||
tv_sec: atime_since_epoch.as_secs() as time_t, | ||
tv_nsec: atime_since_epoch.subsec_nanos() as c_long, | ||
}, | ||
timespec { | ||
tv_sec: mtime_since_epoch.as_secs() as time_t, | ||
tv_nsec: mtime_since_epoch.subsec_nanos() as c_long, | ||
}]; | ||
|
||
let file = File::open(path)?; | ||
let ret = unsafe { futimens(file.as_raw_fd(), times.as_ptr()) }; | ||
if ret == 0 { | ||
Ok(()) | ||
} else { | ||
bail!(IOError::last_os_error()) | ||
} | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::time::{Duration, UNIX_EPOCH}; | ||
use super::set_file_times; | ||
|
||
#[test] | ||
fn test_set_file_times() { | ||
// Oct, 27 -> a special date! :D | ||
let atime = UNIX_EPOCH + Duration::from_secs(1509062400); | ||
let mtime = UNIX_EPOCH + Duration::from_secs(1509105600); | ||
set_file_times("./test_files/a.txt", &atime, &mtime).unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a |