Flat File System

This is a discussion on Flat File System within the Linux Programming forums, part of the Platform Specific Boards category; I am sorry, I have already did a thread about this but for some reason ie (it's a public library) ...

  1. #1
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68

    Flat File System

    I am sorry, I have already did a thread about this but for some reason ie (it's a public library) doesnt let me make a reply so I did another one, I apologise.

    So I have an assignment to do a very simple flat file system on unix which supports some basic functions i.e ls, cp, mv, shownig file id and etc...

    I asked for some guide and I was given some pages and I think I realised the basic concept but I honestly dont know how to start. I know that I will create header files (library) and link them with c files which will have the functions that I want supported. I think that I have to allocate the space from my disk in the header files while specifying the block size and number of blocks and I probably have to think of it as a big file which contains other files so I guess I will nead a head pointer of sorts which will show the start of my file system and will move while I create files and save the functions (or a copy of it I guess so I wont lose the beginning). I dont know if my thought trail is correct but this is all I have. I dont know how to start writing code. In the lesson they just gave as some functions of c and the library the are in (open, close, creat, lseek, read and write) but I alredy knew them and I suppose that I will use them in the implementation of the functions of the file system...

    So basicly I am lost... I know that I cant ask for code, so any help is gladly appreciated...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Code:
    typedef struct {
        char name[10];  // name[0] = '\0' is a free entry
        int size;
        int blocks[100];  // max file size is 100 * 256 bytes
    } dirEnt;
    Each file has
    - a name
    - a size
    - a number of blocks of 'storage' associated with it.

    You then have
    Code:
    dirEnt files[MAXFILES];
    So very crudely, you fread() and fwrite() the files array to the start of some large file.

    Say the first file had blocks 2 and 7 associated with it.
    To get the first block, you would fseek( sizeof(files) + 256 * 2 ); and read 256 bytes
    To get the second block, you would fseek( sizeof(files) + 256 * 7 ); and read 256 bytes

    You might want to keep a separate structure which is a list of all the free blocks in the file system, so whenever you create files (or append to files), you have a ready source of blocks to use.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    Ok, I think I understand it, but wouldnt
    fseek( sizeof(files) + 256 * 2 );
    read the third block since it sets the seek pointer two blocks after the files array so if read after that I would read the third block, or not?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Phoenix_Rebirth View Post
    Ok, I think I understand it, but wouldnt
    fseek( sizeof(files) + 256 * 2 );
    read the third block since it sets the seek pointer two blocks after the files array so if read after that I would read the third block, or not?
    Yes, but if you count blocks as 0, 1, 2 then the third block is what you want.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    Ah, yes. If you look at it like this yes (classic c always starting from 0... :P ).
    Oh another dumb question, If I remember correct for allocating memory I can use malloc. Can I also use it for space on a hard drive or must I go another way (probably more complicated)

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Depends on how much seeking you want to do.

    At a minimum, 1 directory entry and one block of file data.
    At a maximum (since this is just a simulation), the whole virtual file system.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Malloc can only allocate memory and not hard drive space, if that's what you're asking.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Phoenix_Rebirth View Post
    Ah, yes. If you look at it like this yes (classic c always starting from 0... :P ).
    Yes, there's a difference between the second sector [index 1] and sector number 2 [index 2].
    Oh another dumb question, If I remember correct for allocating memory I can use malloc. Can I also use it for space on a hard drive or must I go another way (probably more complicated)
    Not really - if we assume that you have "a large file that is your disk", then you have to keep track of which block you use for what in some sense. There are many methods which all works more or less well under various circumstances. Two of the more popular methods are:
    1. Bitmap - one bit for each block, 0 -> free, 1 -> used, then search the bitmap until you find a free block. Drawback is that the list gets very large if you have large disks [e.g. 4GB of 512byte sectors -> 1MB of bitmap to search within].
    2. List of free blocks - Use some sort of "linked list" where you store a (set of) block(s) that are free. If you use a structure like this:
    Code:
    struct freelist
    {
        int nextfreelistblock;   /* disk block that contains the next list of free blocks. 0 means no more list */
        struct freeblocks {     
           int nblocks;              /* Number of contiguous blocks that are free */
           int firstblock;           /* First block in this particular free section */
        } freeblocks[1];
    };
    You can put multiple structs in one block, so you don't take up that much space.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 10:13 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

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