Filesystem implementation

This is a discussion on Filesystem implementation within the C Programming forums, part of the General Programming Boards category; Hi, I have to complete program which creates a simple filesystem by using an array of 1024 byte blocks by ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Filesystem implementation

    Hi,

    I have to complete program which creates a simple filesystem by using an array of 1024 byte blocks by contiguous allocation and the following header:

    Code:
    #include <sys/stat.h>
    
    typedef unsigned char Byte;
    
    typedef struct Tblock Block;
    struct Tblock{
      Byte sector[1024];
    };
    Block disk [4096];
    
    typedef struct Tdirentry Direntry;
    struct Tdirentry{
      int startblock;
      int length;
      char name[56];  // sizeof(Direntry) == 64
      // struct stat *buf;
    };
    
    typedef struct Tfiledata Filedata;
    struct Tfiledata{
      int blockno;
      int charno;
      Byte blockcopy[1024];
    };
    
    int        myfgetc(Filedata * src); // read one byte
    void       myfputc(Byte x, Filedata * dst); //write one byte
    Filedata * myopen ( const char * name, const char * code);  
         //open file for reading or writing -- return 0 for failure
    void       myclose(Filedata * ff);  // close the file
    also blocks 0-127 will be used to store dir entries, and i have to implement my own function which are at the end of the header. My main difficulty is with the direntries and how to store the in the disk structure which stores bytes?

    Any help appreciated.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Is your file system going to be simulated using one or two or more files? My guess is that the directory structure will be stored in one file, while the file's contents are simulated in another.

    You would create/write your filled-in structures to those files. The thing to do is to sketch out an algorithm... what to do for "create", "open", etc.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    As far as I could tell,since the description says to use an array of 1024 byte blocks to simulate the filesystem, and then goes on to say use blocks 0..127 of the disk(which is the array) that I have to use one file.As I said before, the difficulty seems to be the writing and reading from the array cause its a byte array.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    But the Tfiledata is 1024 + two ints for overhead. So I don't know how this is supposed to fit into one 1024 byte block.

    I'm not sure where your problem is. Are you asking how to write a struct?

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    Well i moved on from the initial problem of writing to the byte array
    i get a segmentation fault from the following function, ipossibly from the fgetc but i dont know why


    Code:
    int readfs(char * filename){
      FILE * file ;
      if((file=fopen(filename,"r"))==NULL)
        return -1;  
      int c=1, i=0,j=0;
      while(i<=4095 && (c!=EOF)){
        while(i<=1023 && ((c = fgetc(file))!=EOF)){
          disk[i].sector[j] = c;
          j++;
        }
        i++;
      }
      fclose(file);
      if(!disk)
        return -1;
      return 0;
    }
    also heres the gdb error:

    Code:
    0x002bc7ef in getc () from /lib/libc.so.6

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I'd have to see how you allocated memory for the 1024 disk entries.
    You should probably read 4k at a time... why read 1 character at a time?
    It doesn't make sense to check (!disk) so late in the game. I hope its address is non zero before you start to populate it.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    the disk[] is declared as in my 1st post,how would you read the characters?

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    First off, fopen(filename,"rb")) so that any new-line characters aren't fiddled with.

    fread(disk[i], sizeof(Block), 1, file) would read a whole block. That would eliminate your inner loop.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,443
    Use fread, with size being sizeof(TBlock) and nmemb being 4 to read 4k at a time. Reading in multiples of blocks is also good for catching errors like your disk not having an exact multiple of blocks. That is, if it's 1.5 blocks, there's a problem, but it would be hard to catch by reading individual characters.

    As a side note, you should define BLOCK_SIZE as 1024 and N_BLOCKS as 4096. Then use them instead of hard coding magic numbers all over.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 12:18 PM
  2. Need to know the filesystem on /dev/sda1
    By amit_sahrawat in forum Linux Programming
    Replies: 7
    Last Post: 12-13-2007, 04:29 AM
  3. File System Implementation
    By dodgeviper in forum C Programming
    Replies: 9
    Last Post: 11-16-2007, 12:04 PM
  4. implementation file
    By bejiz in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2005, 12:59 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21