/>
file system
 jbwyatt.com

. File System


.. intro

File System ---------------

Keeps track of allocated and free space Maintains directories and filenames Tracks the sector location of each file and directory
Cluster: Smallest unit of space used by a file Consists of one or more adjacent sectors Size depends on both the type of file system A file is a linked sequence of clusters. Example: A file always uses at least one cluster A high percentage of space may be wasted Example: 8,200-byte file requires three 4K clusters:

DOS Directory Structure: keeping track of files ----------------------------------------------------


Time field equals 4DBDh (9:45:58) Date field equals 247Ah (March 26, 1998). Attribute is normal 16 bit field (4 hex digits) 16 bit field (4 hex digits) What type of file has attribute 00100111 . . . ?
Filenames =========
FAT === A map of all clusters on the disk, showing their ownership by specific files Each entry corresponds to a cluster number Each cluster contains one or more sectors Each file is represented as a linked list, called a cluster chain. Three types of FAT's, named after the length of each FAT entry: FAT-12 FAT-16 FAT-32 Each entry contains an n-bit integer that identifies the next entry. (n=12,16, or 32) Two cluster chains are shown in the following diagram one for File1 another for File2
DiskIO Data Structure ===================== Used by system to access disk files .data buffer BYTE 512 DUP(?) diskStruct DISKIO <> .code mov ax,7305h ; absolute Read/Write mov cx,0FFFFh ; always this value mov dl,3 ; drive C mov bx,OFFSET diskStruct mov si,0 ; read sector int 21h

.. example code:

IO - get and put stream pointers
================================

All i/o streams objects have, at least, one internal stream pointer:

   ifstream has a pointer known as the get pointer that points to 
   the element to be read in the next input operation.
   
   ofstream has a pointer known as the put pointer that points to 
   the location where the next element has to be written.
   
   fstream io; // file descriptor for input and output - has BOTH pointers
                  Although two pointers,they move together.  
                  Changing one, changes BOTH.

   Can manipulate pointers with:  
         tellg() // gets position of get and put file pointer 
         tellp()
        
         seekg() // positions get and put pointers
         seekp() // in relation to ios::end, ios::beg 
                  ex: inout.seekp(0, ios::end);


Buffers and Synchronization When we operate with file streams, these are associated to an internal buffer of type streambuf. This buffer is a memory block that acts as an intermediary between the stream and the physical file. For example, with an ofstream, each time the member function put (which writes a single character) is called, the character is not written directly to the physical file - the character is inserted in that stream's intermediate buffer. When the buffer is flushed, all the data contained in it is written to the physical medium (if it is an output stream) or simply freed (if it is an input stream). This process is called synchronization and happens: * When file is closed: before closing a file all buffers that have not yet been flushed are synchronized and pending data is written or read * When the buffer is full: Buffers have a certain size. When the buffer is full it is automatically synchronized. * Explicitly, with manipulators: When certain manipulators are used on, an explicit synchronization takes place. These are: flush and endl. * Explicitly, with member function sync(): Calling stream's member function sync(), (no parameters), causes an immediate synchronization.