Thread: writing a utility for format a file

  1. #1
    powinda
    Guest

    writing a utility for format a file

    Hi,
    I am trying to write a utility for formating a file of 1 Mbyte to be used as a disk in an OS simulater so that I can have my file system running on it. I am trying to use a bitmap to manage the free blocks on the disk. How can I write a bitmap, in reality an array of bits to my file and than read from that file in another program. Trying for days but no luck. Hope some one has done something like writing a format utility and file system stuff so they can better explain me this.

    Thank you

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post some of what you've written and we can see where you're going with it, and maybe help you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    I'm not really getting the question but it "intriges" me actually.
    Do you just want a file with lots of crap in to be cleansed so that there arent any characters left in it (if im wrong correct me).

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    12
    Well I am writing an Operatiing system that is used with a os simulator called Brown simulator. I am now left with implementing the file system for it. Their is a regular file named disk0 which inside the simulator will appear as a normal hard disk. I have to format the file outside the simulator so that inside the simulator I can read the first block and get necessary information to be loaded into some data structures for managing the file system. I will try to post the code I have written for the formatting utility by tommorrow. Thanks

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: writing a utility for format a file

    Originally posted by powinda
    How can I write a bitmap, in reality an array of bits to my file and than read from that file in another program.
    Code:
    struct os_bitmap
    {
        unsigned char array[SIZE];
    };
    
    FILE * mydisk = fopen( "mydisk", "w" );
    fwrite( &my_os_bitmap, sizeof struct os_bitmap, 1, mydisk );
    Vola!

    Reading is naturally done in the same way. It's up to you to provide implementation / toggling of specific bits / making it actually mean something.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    12
    The way you can logically represnt the layout of the disk here is

    --------------------------------------------------
    | block 0 | block 1 | . . . . . . . . .|block 128|
    ---------------------------------------------------

    now the bitmap for the free blocks will be written in the block 0. As the operating system boots it will read the first block. Now I extract the bitmap from it using fread() call. Since each block is 8192 bytes and I have 128 blocks my bitmap will be 128 bits long so my bitmap will take 16 bytes of storage. Now I divide each block into inodes of size say 128 bytes each. So each block will have 64 inodes. I need to manage these indoes and write into the first block the informtion about this, which will be written right after the bitmap for the free blocks. I need to read that into a seperate structure. my stuctture for inode looks like this
    Code:
    struct inode {
             int     i_num;
             int     IS_REGF | IS_DIR;
             char   fname[MAX_SIZE];
             int      first_block_num;
    } inode_t;
    how can i use fread() to first read the bitmap which is 16 bytes and than read the rest for initializing the inode structs? A bit confused about that.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    As the operating system boots it will read the first block. Now I extract the bitmap from it using fread() call.
    If the OS is "reading" this, then it is already doing your fread. You just need to access the first N bytes of data from where ever it's stored at. memcpy or something similar should be all you need.

    If you are actually reading from disk, then simply:
    a) read the whole block and extract the first N bytes that you need, as per above
    b) read just the N bytes you need, and then do whatever with the rest

    PS: Your structure definition looks fishy. I doubt this will compile:

    int IS_REGF | IS_DIR;

    You shouldn't have an OR in your definition of your structure like that.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    12
    Hi,
    I am getting a wierd error of BUS ERROR I found that it is during this call
    Code:
    init_bitmap(imap->bitmap,num_of_inodes);
    the same function is working just an instruction before for a different bitmap. here is the code for the program

    Code:
    int main(int argc, char *argv[])
    {
      FILE *stream;
      b_map *bmap;
      i_map *imap;
      sb_t *sb = (sb_t *)malloc(sizeof(sb_t));
      inode_t *ino = (inode_t *)malloc(sizeof(inode_t));
      
      if((stream = fopen("disk0","r+")) ==  NULL)
        printf("Can not open [disk0] for formatting ... \n");
      else
        printf("[disk0] open for formatting ... \n\n");
    
      init_bitmap(bmap->bitmap,num_of_blocks);
      init_bitmap(imap->bitmap,num_of_inodes);   <===== this line 
      printf("Block Bitmap\n");
      for (i = 0; i < num_of_blocks; i++) 
        printf("[%d]",bmap->bitmap[i]);	  
      printf("\n\n");
    
    
      printf("[%d] - Block bitmap structure size in bytes.\n",sizeof(*bmap));
      printf("[%d] - Inode bitmap structure size in bytes.\n",sizeof(*imap));
      
      sb->s_inode_count = num_of_inodes;
      sb->s_block_count = num_of_blocks;
      sb->s_first_data_block = 2;
      n = allocate_bit(bmap->bitmap,sizeof(bmap->bitmap)/sizeof(int));
      printf("\n[%d] offset into the bitmap\n",n);
      for (i = 0; i < num_of_blocks; i++) 
        printf("[%d]",bmap->bitmap[i]);
      printf("\n");
    
      n = sizeof(*sb)+sizeof(*bmap)+sizeof(*imap);
      printf("[%d] total bytes to be written to disk\n",n);
    
      printf("[%d,%d] - [Total inodes,Total blocks] on disk\n",sb->s_inode_count,sb->s_block_count);
      printf("[%d] - size of super block structure\n",sizeof(*sb));
      printf("[%d] - size of inode data structure\n",sizeof(*ino));
      fwrite(sb,sizeof(*sb),1,stream);
      fwrite(ino,sizeof(*ino),1,stream);
      fwrite(bmap,sizeof(*bmap),1,stream);
    
        
      if(!(close(stream)))
        printf("Error closing disk0\n");
      else
        printf("\n[disk0] formatted successfully ...\n");
      return 0;
    }

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>i_map *imap;
    >>init_bitmap(imap->bitmap,num_of_inodes);
    How can you derefence that pointer when it's uninitialised? That'll be a problem

    (the same goes for the other pointer too)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM