linux& c-通过地址访问磁盘上的文件吗? [英] linux & c - access file on disk.. by address?

查看:122
本文介绍了linux& c-通过地址访问磁盘上的文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c程序或终端机(linux)中是否可以通过地址获取文件?我知道这听起来很奇怪,实际上对我来说,但我只是在尝试.

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

例如,替换

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

绝对令人赞叹的东西

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

或将获取相关文件部分的echo& 0x545f6f5(是否需要像echo& 0x545f6f5 20这样的长度才能读取地址旁边的20个字节)?

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)?

我了解mmap,但同样,我的问题还更多是实验.

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

那么,总体情况是:一个人可以用地址(最好是长度)访问linux文件系统上文件的任何部分吗?

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

说我的分区ID/dev/sda1,我想用地址而不是名称来访问原始内存值(可读或不可读). 如果我寻找地址& 0x545f6f5并且恰好是myfile.txt,偏移量为64,则我将在此位置读取该字节.我希望它可以使它更清晰:)

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 :)

推荐答案

假设myfile.txt位于挂载在/ext3文件系统上,而该文件系统位于分区/dev/sda1上.如果您知道文件在磁盘上的偏移量,可以想象打开设备/dev/sda1(分区)或/dev/sda(整个驱动器)并访问文件的字节.

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.

例如,如果您以某种方式确定文件的内容在第一个硬盘驱动器的第一个分区上的偏移量为0xDEADBEEF,则可以执行以下操作:

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);

以这种方式访问​​原始设备会绕过文件系统.文件系统是文件元数据的存储位置,例如文件系统的名称,位置和大小.如果您戳/dev/sda1的开头,可以想像自己可以读取原始文件系统数据,而不必依靠内核文件系统驱动程序来完成.

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.

这篇关于linux& c-通过地址访问磁盘上的文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆