How do I get the disk addresses of files in C/C++?

725 Views Asked by At

When a file is saved into a drive, its contents are written & then indexed. I want to get the indexes and to access the raw contents of the files.

Any idea on the method how to do it, especially for ex4 & btrfs?

UPDATE: I want to get the addresses of the extents of a file. The information about the addresses must be stored somewhere onto the disk. I want to retrieve this info, in order to map the physical location of the file contents. Any methods in order to achieve that?

UPDATE: Hello, all! Thanks for your replies. What I want is a function/command which returns me a list of extent addresses. debugfs seems the function/command with the most-relevant functionality.

2

There are 2 best solutions below

1
On

It depends of the filesystem you are using. If you are running Linux you can use debufs to seek the file in the filesystem.

I have to say that all FSs are mounted through a VFS, a virtual filesystem that is like a simplified interface with the standard operations (open, close, read...). What is the meaning of that? No filesystem nor its contents(files, dirs) are opened directly from disk, when you open something, you move it to the main memory(your RAM) you do your operations and when you close something it returns to the disk drive.

Now, the question is: Can I get the absolute address in a FS? Yes, if you open your whole filesystem like open ("/dev/sdaX", 0_RDONLY); so you get the address relative to your filesystem using lseek in C for example.

And then... Can I get the same in the whole drive? No, that is because you cannot open the whole drive as a file descriptor. Remember /dev/sdaXin UNIX? Partitions and its can be opened like files because they have a virtual interface running on them.

Your last answer: Can I read really raw contents? All files are read as they appear on disk, the only thing that changes is the descriptor used by the OS and some data about how is indexed, all this as a "file header".

I hope all your questions are answered.

1
On

The current solution/workaround is to call these functions with popen:

filefrag -e /path/to/file

hdparm --fibmap /path/to/filename

Then one should simply parse the stringoutputs of these programs. It is not a real solution (i.e.: outputs at C/C++ level), but I'll accept it for now.

Sources:

https://unix.stackexchange.com/questions/106802/what-command-do-i-use-to-see-the-start-and-end-block-of-a-file-in-the-file-syste

https://serverfault.com/questions/29886/how-do-i-list-a-files-data-blocks-on-linux