The library analogy is a good one.
Paul, the two approaches you mention to locating files on disk are in fact two legitimate storage allocation strategies used in file systems. They're called "First (or Next) Fit" and "Best Fit". Each has advantages and disadvantages, the most obvious being the one you've observed - the conflicting outcome regarding file fragmentation and drive (free-space) fragmentation.
First fit is faster to allocate locations. Next fit should be faster again as it works the same way, but the next available physical location is used instead of continually re-scanning from the beginning.
Best fit is slower because potentially all of the available empty slots have to be assessed in order to find the one closest to the required size.
File/operating systems tend to use faster strategies to enhance performance.
The extension of an existing file (concatenation) you've mentioned is another consideration. Adding content to a file like a Word .doc file may or may not change its size. A plain text file enlarges in direct response to its content, a file like a .doc file is a database in which adding a word may not affect the filesize, or may make a disproportionate adjustment in space allocation. And this is complicated by Word creating a backup file while an existing file is being edited. So that will also move the location of available space for the active file.
The choice is made to aviod the complexity and processing overhead of fragmentation reduction during file access, as computers are productivity orientated, especially in commercial environments. The "time-wasting" overhead is offloaded into the separate process of defragmentation, which can then be done in otherwise unproductive "offline" time. The strategies actually trace right back to early computing days when processing time was very expensive and clients paid for it by the minute.
Edited by Platypus, 17 May 2010 - 01:08 AM.