3

I'm downloading with KTorrent, and have noticed that when I select "allocate disk space before starting torrent", it creates all the files at start, presumably empty, to avoid fragmentation.

When I'm in the folder and use ls, it shows the allocated sizes, but also the total size, and those two don't add up. df also reports only the really used space.

ls -lH

total 745M
-rw-r--r-- 1 ##### ##### 542M Feb  8 15:55 Star Trek - 1x01 - The Cage.avi
-rw-r--r-- 1 ##### ##### 351M Feb  8 14:37 Star Trek - 1x02 - Where No Man Has Gone Before.avi
-rw-r--r-- 1 ##### ##### 346M Feb  8 16:04 Star Trek - 1x03 - The Corbomite Maneuver.avi
-rw-r--r-- 1 ##### ##### 350M Feb  8 16:02 Star Trek - 1x04 - Mudd's Women.avi
-rw-r--r-- 1 ##### ##### 350M Feb  8 15:35 Star Trek - 1x05 - The Enemy Within.avi
-rw-r--r-- 1 ##### ##### 350M Feb  8 15:37 Star Trek - 1x06 - The Man Trap.avi
... (many more)

However, with the -s option, I get real sizes:

ls -s

total 728M
   0 Star Trek - 1x01 - The Cage.avi
351M Star Trek - 1x02 - Where No Man Has Gone Before.avi
 42M Star Trek - 1x03 - The Corbomite Maneuver.avi
195M Star Trek - 1x04 - Mudd's Women.avi
5.8M Star Trek - 1x05 - The Enemy Within.avi
9.7M Star Trek - 1x06 - The Man Trap.avi
.... (size matches)

How is that possible? Is there some "file size" setting in the filesystem (ext4), that ls reads (sometimes) and df ignores?

1
  • Smells like sparse files to me. – user Feb 8 '15 at 15:18
3

Ktorrent is using sparse files. Basically, a sparse file is a file with "holes" : no actual disk space is used for areas of the file which are "empty" (ie contains only zeroes).

Your file thus appears to be of a certain size (in your case, the size of your Star Trek video), but is actually not using disk space for those areas that are not yet "filled" (in your case, the parts that Ktorrent has yet to download), thus avoiding wasting disk space until it is really needed.

Instead, metadata is used by the filesystem to indicate that those areas are empty. If you were to read from them, you would get only zeroes.

You can see the real disk usage of a such a file with du or ls -s, and its apparent disk usage with du -b or ls -l.

df and the total size reported by ls -l are only showing real disk usage.

2
  • Note that these sparse files do nothing at all for combatting fragmentation, so the option to create the files this way is quite useless. It would need to really allocate space e.g. with the fallocate() system call on those filesystems that support it. – wurtel Feb 9 '15 at 9:06
  • There actually is a second checkbox called "Fully allocate disk space" which probably does just that. I'm not sure what the "sparse files" option is good for though.. maybe just so the user can see where the files will go? – MightyPork Feb 9 '15 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.