Thread: Writing Directly to Hard Disk Sectors

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    1

    Writing Directly to Hard Disk Sectors

    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.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Assuming no one is using the particular disk you want to do this on:

    Code:
    ...
        h = open("/dev/hdb", O_WRONLY, 0);
        if (h > 0) {
           lseek(h, sectorno * 512, SEEK_SET);
           write(h, buffer, 512);
        }
    ...
    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.

    Also, you can probably achieve the same thing using "dd" without writing a single line of C-code.

    --
    Mats

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hard disk space and spyware
    By oldmancan in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 01-23-2003, 01:24 AM
  2. truely Cleaning a hard disk
    By xds4lx in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 01-23-2003, 12:37 AM
  3. how to store a node on hard disk
    By ALLRIGHT in forum C Programming
    Replies: 3
    Last Post: 05-13-2002, 10:11 AM
  4. program to check hard disk transfer rate
    By shadow99er in forum C Programming
    Replies: 3
    Last Post: 03-01-2002, 05:04 PM
  5. Direct disk access in DOS
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-26-2002, 02:52 PM