creating file space via mmap

Don Faulkner faulkner%jmullins at husc6.harvard.edu
Wed May 17 23:55:18 AEST 1989


I've been working with mmap to create file space.  There are some problems
in letting mmap set the size of the file.

>From "man mmap":
     It should be noted that the system will always zero-fill any
     partial  pages at the end of an object.  Further, the system
     will never write out any modified portions of the last  page
     of  an object which are beyond its end.  References to whole
     pages following the end of an  object  will  result  in  the
     delivery  of  a  SIGBUS  signal.  SIGBUS signals may also be
     delivered on various filesystem conditions, including  quota
     exceeded errors.


(the System Services Overview chapter "Memory Management" doesn't
add any info for my question ...)

So --- you can't just "mmap" with a "len" greater than the length
of the file, and let it grow ...

Experimentally, the following DOES work:

fd = open ("file", O_RDWR | O_CREAT, 0666)
lseek (fd, 10000000, L_SET)  /*  max file size reasonable  */
write (fd, "", 1)
This will create a file with an enormous hole.
Then, you can mmap with len=10000000
if you address and write into the middle of a file, those blocks
touched get allocated and filled in automagically ...
(I'm trying to avoid actually POPULATING a huge block if it's not needed...)

Afterwards, you could open a new file, and copy only the elements that
were actually created...

This DOES work; the question is:  can one DEPEND on it working?
Is there a better way?
--

 Don Faulkner                                       
 Building 1, Room 803
 Harvard University, School of Public Health
 665 Huntington Avenue
 Boston, MA  02115

 ARPA:      faulkner%jmullins at harvard.harvard.edu                
 BITNET:    faulkner at harvard
 Telephone: (617) 732-2297



More information about the Comp.sys.sun mailing list