Skip navigation.
 
mlRe: Create NSStrings from a mapped NSData object - safe?
FROM : Jens Alfke
DATE : Tue May 13 17:08:28 2008

On 13 May '08, at 12:15 AM, Michael Vannorsdel wrote:

> Basically there's no guarantee you'll get the same data that was in 
> the file when you first mapped it if something else modifies or 
> destroys it while it's mapped.


You're correct about modifications, but not about deletions. An open 
file descriptor counts as a link to a file, so the unlink(2) system 
call will not actually delete the file from disk because there's still 
a link to it. Once you close the mapped file, the last link goes away 
and then the file is actually deleted.

This is sometimes used as a technique for creating anonymous temporary 
files — create a temp file with open(2), then unlink(2) it so it 
doesn't exist in the directory tree anymore, then its yours to write/
read until you close(2) it.

While we're on the topic, it's worth noting that memory-mapping files 
on removable or network filesystems can be dangerous. If you read/
write a mapped memory location, and the kernel has to page it in, but 
the file's filesystem is no longer accessible due to a network issue 
or a yanked USB cable ... you get a bus-error. I've seen discussion of 
ways to handle this by installing a signal handler whenever you access 
mapped memory, but it would be pretty tricky to pull off. The 
conclusion was that it's only safe to memory-map files that are either 
(a) on the boot filesystem, or (b) in the user's home directory. (The 
latter might be on a networked filesystem, but if it ever gets 
disconnected, most of the upper layers of the OS and applications will 
be hosed anyway...)

—Jens

Related mailsAuthorDate
mlCreate NSStrings from a mapped NSData object - safe? Daniel Vollmer May 13, 08:38
mlRe: Create NSStrings from a mapped NSData object - safe? Michael Vannorsdel May 13, 09:15
mlRe: Create NSStrings from a mapped NSData object - safe? Jens Alfke May 13, 17:00
mlRe: Create NSStrings from a mapped NSData object - safe? Jens Alfke May 13, 17:08
mlRe: Create NSStrings from a mapped NSData object - safe? Douglas Davidson May 13, 17:53
mlRe: Create NSStrings from a mapped NSData object - safe? Daniel Vollmer May 13, 21:55
mlRe: Create NSStrings from a mapped NSData object - safe? Jens Alfke May 14, 01:39
mlRe: Create NSStrings from a mapped NSData object - safe? Michael Vannorsdel May 14, 05:57
mlRe: Create NSStrings from a mapped NSData object - safe? Michael Vannorsdel May 14, 06:11
mlRe: Create NSStrings from a mapped NSData object - safe? Michael Vannorsdel May 14, 06:39
mlRe: Create NSStrings from a mapped NSData object - safe? Jens Alfke May 14, 07:32
mlRe: Create NSStrings from a mapped NSData object - safe? Jens Alfke May 14, 07:33
mlRe: Create NSStrings from a mapped NSData object - safe? Michael Vannorsdel May 14, 08:28
mlRe: Create NSStrings from a mapped NSData object - safe? Michael Vannorsdel May 14, 10:26
mlRe: Create NSStrings from a mapped NSData object - safe? Mike Fischer May 14, 16:32
mlRe: Create NSStrings from a mapped NSData object - safe? Jean-Daniel Dupas May 14, 17:05
mlRe: Create NSStrings from a mapped NSData object - safe? Dennis Munsie May 15, 18:42
mlRe: Create NSStrings from a mapped NSData object - safe? Andrew Farmer May 16, 07:01