@@ -7,6 +7,8 @@ use libc::{self, mode_t};
77use std:: mem;
88use std:: os:: unix:: io:: RawFd ;
99
10+ pub use self :: linux:: * ;
11+
1012mod ffi {
1113 use libc:: { c_char, c_int, mode_t, dev_t} ;
1214 pub use libc:: { stat, fstat, lstat} ;
@@ -179,3 +181,73 @@ pub fn fchmodat<P: ?Sized + NixPath>(dirfd: RawFd, pathname: &P, mode: Mode, fla
179181
180182 Errno :: result ( res) . map ( drop)
181183}
184+
185+ #[ cfg( target_os = "linux" ) ]
186+ mod linux {
187+ use { Errno , Result , NixPath } ;
188+ use std:: os:: unix:: io:: RawFd ;
189+ use libc;
190+ use fcntl:: AtFlags ;
191+ use sys:: time:: TimeSpec ;
192+
193+ /// A file timestamp.
194+ pub enum UtimeSpec {
195+ /// File timestamp is set to the current time.
196+ Now ,
197+ /// The corresponding file timestamp is left unchanged.
198+ Omit ,
199+ /// File timestamp is set to value
200+ Time ( TimeSpec )
201+ }
202+
203+ impl < ' a > From < & ' a UtimeSpec > for libc:: timespec {
204+ fn from ( time : & ' a UtimeSpec ) -> libc:: timespec {
205+ match time {
206+ & UtimeSpec :: Now => libc:: timespec {
207+ tv_sec : 0 ,
208+ tv_nsec : libc:: UTIME_NOW ,
209+ } ,
210+ & UtimeSpec :: Omit => libc:: timespec {
211+ tv_sec : 0 ,
212+ tv_nsec : libc:: UTIME_OMIT ,
213+ } ,
214+ & UtimeSpec :: Time ( spec) => * spec. as_ref ( )
215+ }
216+ }
217+ }
218+
219+ /// Change file timestamps with nanosecond precision
220+ /// (see [utimensat(2)](http://man7.org/linux/man-pages/man2/utimensat.2.html)).
221+ pub fn utimensat < P : ?Sized + NixPath > ( dirfd : RawFd ,
222+ pathname : & P ,
223+ atime : & UtimeSpec ,
224+ mtime : & UtimeSpec ,
225+ flags : AtFlags ) -> Result < ( ) > {
226+ let time = [ atime. into ( ) , mtime. into ( ) ] ;
227+ let res = try!( pathname. with_nix_path ( |cstr| {
228+ unsafe {
229+ libc:: utimensat ( dirfd,
230+ cstr. as_ptr ( ) ,
231+ time. as_ptr ( ) as * const libc:: timespec ,
232+ flags. bits ( ) )
233+ }
234+ } ) ) ;
235+
236+ Errno :: result ( res) . map ( drop)
237+ }
238+
239+ /// Change file timestamps with nanosecond precision
240+ /// (see [futimens(2)](http://man7.org/linux/man-pages/man2/futimens.2.html)).
241+ pub fn futimens ( fd : RawFd ,
242+ atime : & UtimeSpec ,
243+ mtime : & UtimeSpec ) -> Result < ( ) > {
244+ let time = [ atime. into ( ) , mtime. into ( ) ] ;
245+ let res = unsafe {
246+ libc:: futimens ( fd, time. as_ptr ( ) as * const libc:: timespec )
247+ } ;
248+
249+ Errno :: result ( res) . map ( drop)
250+ }
251+ }
252+ #[ cfg( not( target_os = "linux" ) ) ]
253+ mod linux { }
0 commit comments