webdesus / fs_extra

Expanding opportunities standard library std::fs and std::io
MIT License
242 stars 46 forks source link

Add timestamp preservation mode (cp -p) #22

Open awakecoding opened 5 years ago

awakecoding commented 5 years ago

Add support for copying files while preserving timestamps (do not change the last modified time), just like the cp command -p or --preserve option: http://man7.org/linux/man-pages/man1/cp.1.html

       -p     same as --preserve=mode,ownership,timestamps

       --preserve[=ATTR_LIST]
              preserve the specified attributes (default:
              mode,ownership,timestamps), if possible additional attributes:
              context, links, xattr, all

I am currently working on a tool to reuse cargo build artifacts from the "target" directory, and it turns out that cargo checks the last modified time on files and triggers a rebuild if it changed.

JWorthe commented 2 weeks ago

I also ran into needing this functionality for a similar purpose (I have a static site generator for personal use, and if the modified timestamp isn't preserved it breaks caching for static assets that haven't changed).

I found that being able to set times was added fairly recently to the Rust std library, only in Rust 1.75.0. Check out the set_times and related functions on File: https://doc.rust-lang.org/std/fs/struct.File.html#method.set_times

I was only using a small subset of fs_extra, so unfortunately this lead me to replacing fs_extra with my own recursive copy that included the modified timestamp.