简体   繁体   中英

linux & c - access file on disk.. by address?

Is it possible to get to a file by address in ac program or in a terminal (linux)? I know it sounds odd, to me either actually, but I am just experimenting.

For example, replace

FILE * f = fopen("myfile.txt","r")

by something absolutely stunning like

int fd = open(&0x545f6f5,"r")

or echo &0x545f6f5 that would grab the part of the concerned file (would need a length so more like echo &0x545f6f5 20 to read the 20 bytes next to the address)?

I know about mmap, but again, my question is much more something like experimentation.

Well, the overall picture is: can one access any part of a file on the linux filesystem with an address (and ideally a length)?

UPDATE:

Say my partition id /dev/sda1 and i want to access the raw memory value (readable or not) with an address and not a name. If I seek for address &0x545f6f5 and that it happens to be myfile.txt at an offset of say 64, i would read the byte at this position. I hope it makes it clearer :)

Let's say myfile.txt is on the ext3 filesystem mounted at / , and that filesystem is on the partition /dev/sda1 . You could conceivably open the device /dev/sda1 (the partition) or /dev/sda (the entire drive) and access the file's bytes if you knew its offset on disk.

For example, if you somehow determined that the file's contents were at offset 0xDEADBEEF on the first partition of the first hard drive, you could do:

int fd = open("/dev/sda1", O_RDONLY);
lseek(fd, 0xDEADBEEF, SEEK_SET);
read(fd, buffer, 20);

Accessing the raw device this way bypasses the filesystem. Filesystems are where file metadata is stored, such as their names, locations, and sizes. If you poked around the beginning of /dev/sda1 you could conceivably read the raw filesystem data yourself rather than relying on the kernel filesystem drivers to do it for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM