views:

24

answers:

1

Hello,

I am on Linux and need to know either of the two things:

1) If i have a regular file on some file system on a partition under Linux is there a way to know the set of the physical blocks that this file occupies on the drive from user space? Or at least the set of the file system's clusters?

2) Is there a way to get the same information about the whole free space of the given file system?

In both cases i understand that if there is any possible way to extract this info it will probably be totally unsafe and racy (anything could happen to these set of blocks between the time i see them and act on them somehow). I also really don't want an implementation that will have to know a lot about every filesystem.

+1  A: 

It isn't possible in a filesystem-independent way. Filesystems aren't necessarily even built on top of block devices (simple example: NFS).

For ext2, ext3 and ext4 you can use the ext2fslib library, which is included in the e2fsprogs distribution.

caf
I am dealing with disk images which are stored as regular files and have a virtual block device for them, kind of what losetup does. The data i need above is collected from files/partitions on that looped disk image. So i always have a block device even if the backing file image is stored through NFS. But anyway, i see your point that it is impossible to do without knowing the details of the concrete filesystem.
Inso Reiges