Java: RandomAccessFile Mode "rws" vs "rwd"?
Asked Answered
A

2

15

The RandomAccessFile constructor accepts a mode string specifying how a file should be open.

I'm confused about the difference between "rws" and "rwd" modes.

Here's what the docs state:

"rws" Open for reading and writing, as with "rw", and also require that every update to the file's content or metadata be written synchronously to the underlying storage device.

"rwd" Open for reading and writing, as with "rw", and also require that every update to the file's content be written synchronously to the underlying storage device.

[...]

The "rwd" mode can be used to reduce the number of I/O operations performed. Using "rwd" only requires updates to the file's content to be written to storage; using "rws" requires updates to both the file's content and its metadata to be written, which generally requires at least one more low-level I/O operation.

...and no explanation about what metadata means. Does it mean that "rws" updates the last modified timestamp on the filesystem, and "rwd" doesn't ?

Autobiographical answered 9/1, 2013 at 9:58 Comment(4)
@T.J. Crowder What other such attributes?Autobiographical
@ Cristi: Dunno, changes to owner and group rights? Execute bit (on *nix filesystems). Read-only bit (on FS's that have it). Etc...Blizzard
@T.J. Crowder I can't find anything obvious in this class' API that may change those attributes.Autobiographical
Some of the following attributes docs.oracle.com/javase/tutorial/essential/io/fileAttr.htmlSchug
R
20

Does it mean that "rws" updates the last modified timestamp on the filesystem, and "rwd" doesn't ?

rws flushes the contents of the file and the modification date of the file.

rwd flushs the contents of the file, but the modification date might not change until the file is closed.

rw only flushes when you tell it to and doesn't change the modifcation date until you close the file.

BTW rwd is much slower for writes than rw, and rws is slower again.

Ribbonwood answered 9/1, 2013 at 10:4 Comment(9)
There is something I don't get: when you modify the contents of a file, ctime is modified in any case (at least with Unix{-like} systems)... I must be missing something here.Quotation
@Quotation The hints define the minimum of what must happen as it is about providing guarentees. The OS is free to do more than this e.g. set the ctime for a new file.Ribbonwood
ctime is "content modification" time (mtime is metadata modification -- each time ctime is updated, mtime is also updated since ctime is part of the metadata)Quotation
@Quotation When you change to the content, those changes are visible immediately even with "rw". I use this in a library I wrote to share data between processes and the latency can be as low at 100 ns. What it doesn't do is actually write the data to disk so even though it looks like the information has been changed the data is not yet on disk. Using "rws"/"rwd" ensures the data is written to disk before continuing e.g. to prevent data loss on an OS crash or power failure.Ribbonwood
Ah, yeah, OK, I missed the "synchronized" stuff part. Not that fsync() and friends make much of difference on most modern Unix OSes these days...Quotation
@Quotation I can tell you it's allot slower. ;)Ribbonwood
Just to make sure Peter even if vm crushes the "rw" will persist changes right? only OS crush is loosing data...Discoverer
@PeterLawrey Excuse me, what does "flushes the contents of the file" mean ? Maybe you wanted to say "flushes the stream" ?Ironware
@Ironware if you are using streams, yes. If you are using FileChannels or RandomAccessFile it is on each write.Ribbonwood
J
3

There is some info about file metadata in FileChannel API http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html

...The file may also have some associated metadata such as access permissions, content type, and last-modification time...

Besides, FileChannel.force(boolean metadata) API provides more info about the difference between rws and rwd (though the names are never mentioned)

Johannejohannes answered 9/1, 2013 at 10:20 Comment(1)
Interesting, but I had to hunt for the info. The right URL is: docs.oracle.com/javase/7/docs/api/java/nio/channels/…Autobiographical

© 2022 - 2024 — McMap. All rights reserved.