Skip to content

Commit

Permalink
Initial commit 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
ecarrara committed Apr 1, 2017
0 parents commit 674101a
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target/
**/*.rs.bk
Cargo.lock
13 changes: 13 additions & 0 deletions Cargo.toml
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"
19 changes: 19 additions & 0 deletions LICENSE
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.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# fsync

Keeps files or directories in sync.
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
reorder_imports = true
6 changes: 6 additions & 0 deletions src/errors.rs
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);
}
}
12 changes: 12 additions & 0 deletions src/lib.rs
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;
49 changes: 49 additions & 0 deletions src/utils.rs
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();
}
}
1 change: 1 addition & 0 deletions test_files/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a

0 comments on commit 674101a

Please sign in to comment.