Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Writing Files - Ryan Mulligan
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Las Vegas Ruby Group
May 21, 2014
100
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Writing Files - Ryan Mulligan
Las Vegas Ruby Group
May 21, 2014
More Decks by Las Vegas Ruby Group
See All by Las Vegas Ruby Group
Ruby ISO Standard - David Grayson
lvrug
0
180
Windows Automation - Howard Feldman
lvrug
0
120
Separating Your Application from Rails - Brian Hughes
lvrug
0
170
SWIG and Ruby - David Grayson
lvrug
0
110
Practical Object-Oriented Design in Ruby - Charles Jackson
lvrug
3
150
The Hamster Gem - Ryan Mulligan
lvrug
1
130
Varnish+Redis - Russ Smith
lvrug
1
150
Lambdas and Pops - Jan Hettich
lvrug
0
120
Making Good Use of Fonts - Russ Smith
lvrug
1
120
Featured
See All Featured
Abbi's Birthday
coloredviolet
3
8.9k
Six Lessons from altMBA
skipperchong
29
4.4k
Product Roadmaps are Hard
iamctodd
55
12k
Statistics for Hackers
jakevdp
799
230k
Why Our Code Smells
bkeepers
PRO
340
58k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
260
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.5k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
Done Done
chrislema
186
16k
Raft: Consensus for Rubyists
vanstee
141
7.6k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.6k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.7k
Transcript
writing files ryantm.com https://github.com/ryantm/lvrug_atomic
File.write('atomic1.txt', 'Hello world') atomic1.rb
strace (Linux) $ strace ruby atomic1.rb try dtruss on OS
X
$ strace ruby atomic1.rb …. open("atomic1.txt", O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0666) = 7
fcntl(7, F_GETFD) = 0x1 (flags FD_CLOEXEC) fstat(7, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0 ioctl(7, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, 0x7fffcafe2b60) = -1 ENOTT Y (Inappropriate ioctl for device) write(7, "Hello world", 11) = 11 close(7) = 0 ...
between open and write reading might read nothing (truncated) writes
to file erased
File.open('atomic2.txt', 'w') do |f| 1111.times do f.write 'Hello world' end
end atomic2.rb
$ strace ruby atomic2.rb open("atomic2.txt", O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0666) = 7 fcntl(7,
F_GETFD) = 0x1 (flags FD_CLOEXEC) fstat(7, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0 ioctl(7, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, 0x7fff12593520) = -1 ENOTT Y (Inappropriate ioctl for device) write(7, "Hello worldHello worldHello worl"..., 8184) = 8184 write(7, "Hello world", 11) = 11 write(7, "Hello worldHello worldHello worl"..., 4026) = 4026 close(7)
between write and write reader reads partial (corrupted?) data other
writers - truncation - interleaving
solutions use temporary files - simple use journaling - used
by most databases transactional file systems - NTFS has but deprecated - otherwise academic
require 'fileutils' File.write('temp.txt', 'hello world') FileUtils.mv('temp.txt', "atomic5.txt") atomic5.rb
$ strace ruby atomic5.rb open("temp.txt", O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0666) = 7 …
write(7, "hello world", 11) = 11 close(7) = 0 stat("atomic5.txt", {st_mode=S_IFREG|0644, st_size=11, ...}) = 0 stat("temp.txt", {st_mode=S_IFREG|0644, st_size=11, ...}) = 0 stat("atomic5.txt", {st_mode=S_IFREG|0644, st_size=11, ...}) = 0 stat("temp.txt", {st_mode=S_IFREG|0644, st_size=11, ...}) = 0 stat("atomic5.txt", {st_mode=S_IFREG|0644, st_size=11, ...}) = 0 rename("temp.txt", "atomic5.txt") = 0
requirements no one read temp file only one writer of
temp file only one file updater (file write locking)
further research how a rollback journal works http://www.sqlite.org/atomiccommit.html
further research how a rollback journal works http://www.sqlite.org/atomiccommit.html references.txt in
https://github.com/ryantm/lvrug_atomic
questions ryantm.com https://github.com/ryantm/lvrug_atomic