I am writing a filesystem using C for linux os. As part of this exercise, I want a mechansim to write data to a particular sector of the hard disk. My question is how can you do that in c language?
Regards.
I am writing a filesystem using C for linux os. As part of this exercise, I want a mechansim to write data to a particular sector of the hard disk. My question is how can you do that in c language?
Regards.
Assuming no one is using the particular disk you want to do this on:
But unless you ABSOLUTELY know what you're doing, I'd suggest you only do this on a disk that doesn't contain anything valuable. And the OS isn't allowing you to do this if something has mounted the disk. You probably also need to be root to use this, as accessing devices shouldn't be allowed by "ordinary users"- although I haven't checked.Code:... h = open("/dev/hdb", O_WRONLY, 0); if (h > 0) { lseek(h, sectorno * 512, SEEK_SET); write(h, buffer, 512); } ...
Also, you can probably achieve the same thing using "dd" without writing a single line of C-code.
--
Mats
By the way, if you're writing filesystem code, you may want to take a look at "user level filesystem" or some such - but I suggest that you beef up on your linux knowledge before you embark on file-system programming, as it seems like a recipe for disaster to try to write a filesystem if you don't know the most basic things - this is not meant as disrespect, but I think you need to have a fuller understanding of how a OS works and the low-level system calls before you start writing a file-system. File-systems are complex - I've got 20 years of experience with all sorts of low-level programming, and I have no aspirations to create a file-system, not even a simple one - because even the most simple ones are hideously complex.
--
Mats