andreacorbellini / rust-atomic-write-file Goto Github PK
View Code? Open in Web Editor NEWLicense: BSD 3-Clause "New" or "Revised" License
License: BSD 3-Clause "New" or "Revised" License
Hey, thanks for building this awesome crate! I have been using NamedTempFile
from the tempfile
crate for this purpose before but this seems more convenient and robust from the how-it-works description in the docs.
There's a small problem with the readme, the docs badge link is broken.
Apparently overlayfs
, used by container software, doesn't support unnamed tempfiles: launchbadge/sqlx#2908
It would be nice to be able to easily use O_TMPFILE
when it is supported, and fall back to a different behavior when it isn't. Currently, there's just the Cargo feature (and a library can't be sure that it's disabled, since some other package in an application's dependency tree might activate the it).
After reading more I found it's recommended to fsync parent also to be sure data is persisted ok.
You can see here more https://www.quora.com/Linux/When-should-you-fsync-the-containing-directory-in-addition-to-the-file-itself
Also I see other implementations are doing it https://github.com/untitaker/rust-atomicwrites/blob/master/src/lib.rs#L200
It's not a bit deal, I can fsync parent after commit but would be nice to have this included
It returns an error when commit, opening with tempfile::NamedTempFile.path()
.
Access is denied. (os error 5)
├── anyhow v1.0.81
├── atomic-write-file v0.1.3
│ └── rand v0.8.5
│ ├── rand_chacha v0.3.1
│ │ ├── ppv-lite86 v0.2.17
│ │ └── rand_core v0.6.4
│ │ └── getrandom v0.2.14
│ │ └── cfg-if v1.0.0
│ └── rand_core v0.6.4 (*)
└── tempfile v3.10.1
├── cfg-if v1.0.0
├── fastrand v2.0.2
└── windows-sys v0.52.0
└── windows-targets v0.52.4
└── windows_x86_64_msvc v0.52.4
Windows
use anyhow::Result;
use atomic_write_file::AtomicWriteFile;
use tempfile::NamedTempFile;
fn main() -> Result<()> {
let file = NamedTempFile::new()?;
let path = file.path();
let atomic = AtomicWriteFile::open(path)?;
atomic.commit()?;
Ok(())
}
Error: 拒绝访问。 (os error 5)
echo foo > filename
chattr +i filename
let mut file = AtomicWriteFile::options().open("filename")?;
writeln!(file, "{}", contents)?;
file.commit()?;
A call to fsync()
on a O_TMPFILE
does nothing. Now, the anonymous file is given a temporary name then renamed to its final name, without an fsync()
call in between. A power failure following the final rename may result in data loss -- the target file now contains no data, filesize is zero.
Take a look at the btrfs source code in the kernel, where the no-op behavior of fsync()
is described: https://github.com/torvalds/linux/blob/026e680b0a08a62b1d948e5a8ca78700bfac0e6e/fs/btrfs/tree-log.c#L2422
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.