OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Apr 24, 2024 11:44 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: fat12 simple question
PostPosted: Thu Jan 10, 2008 8:11 pm 
Offline

Joined: Thu Jan 10, 2008 6:09 pm
Posts: 11
Hi all.

I am trying to write fat12 img reader, but i have problem.

I read http://www.osdev.org/osfaq2/index.php/FAT12%20document FAT12 documentation, but i can't figure out how directories and subdirectories are organized in fat12 file system .. maybe i misted something ?

For example simpe directories tree:

root
/ \
foo bar
|
test.txt

and the Directory structure :

Bytes Meaning
0 - 10 File name with extension
11 Attributes of the file
12 - 21 Reserved Bytes
22 - 23 Indicate the time
24 - 25 Indicate the date
26 - 27 Indicate the entry cluster value
28 - 31 Indicate the size of the file

And can't see any pointers that will show how the directories tree is created ?

How foo subdirectory will know that she belongs to root directory ? How the file test.txt will now that he belongs to foo subdirectory .. and etc ..


kestaz


Top
 Profile  
 
 Post subject: Re: fat12 simple question
PostPosted: Fri Jan 11, 2008 1:39 am 
Offline
Member
Member

Joined: Sun Jan 06, 2008 8:41 am
Posts: 174
kestaz wrote:
Hi all.
and the Directory structure :

Bytes Meaning
0 - 10 File name with extension
11 Attributes of the file
12 - 21 Reserved Bytes
22 - 23 Indicate the time
24 - 25 Indicate the date
26 - 27 Indicate the entry cluster value
28 - 31 Indicate the size of the file

And can't see any pointers that will show how the directories tree is created ?

How foo subdirectory will know that she belongs to root directory ? How the file test.txt will now that he belongs to foo subdirectory .. and etc ..

kestaz

Simple it doesn't. The knowledge is that the directory entry knows what type of file each entry corresponds to (the directory knows it contents, but the contents does not know which directory it belongs to). If the subdirectory bit is set (0x10) the directory entry corresponds to a directory (and the it's size is determined via the FAT).

In your case the root directory contains two entries (foo and bar), foo having subdir bit set. Folowing the entry cluster you find it's contents - clusters containing directory entries, one of them being test.txt.

Having the files (and directories) not knowing which directory they belong to is important when implementing hard links since then the files can belong to multiple directories.


Top
 Profile  
 
 Post subject: Re: fat12 simple question
PostPosted: Fri Jan 11, 2008 5:55 am 
Offline
Member
Member

Joined: Wed Oct 31, 2007 9:09 am
Posts: 1385
skyking wrote:
Having the files (and directories) not knowing which directory they belong to is important when implementing hard links since then the files can belong to multiple directories.


However, totally unimportant for FAT, since it doesn't have hard links. In fact, subdirectories actually have the "." and ".." as part of the directory contents (unlike Unix file systems, which rely on the shell to interpret these), and ".." actually links to the parent directory.


JAL


Top
 Profile  
 
 Post subject: Re: fat12 simple question
PostPosted: Fri Jan 11, 2008 6:00 am 
Offline
Member
Member

Joined: Wed Oct 31, 2007 9:09 am
Posts: 1385
kestaz wrote:
How foo subdirectory will know that she belongs to root directory ? How the file test.txt will now that he belongs to foo subdirectory .. and etc ..


Directories and files do not know anything about their location. I thinks there are very little file systems that store this knowledge together with the file. For FAT, all file information is stored in the directory 'file' (as opposed to e.g. the Unix-like file systems where the file information is stored in a global structure called 'i-node'). To build a complete directory tree, you start with the root directory. In the root directory, there are entries that indicate they are subdirectories. Read these subdirectories to discover their contents, etc.


JAL


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 11, 2008 7:37 am 
Offline

Joined: Thu Jan 10, 2008 6:09 pm
Posts: 11
no i can't figure out .. how directory entry can point to many list of files ? how it's possible ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 11, 2008 8:24 am 
Offline
Member
Member
User avatar

Joined: Wed Nov 02, 2005 12:00 am
Posts: 110
Location: The Netherlands
Skyking allready gave you the answer: Bits 26 and 27 gives you the starting cluster of the directory.
B.t.w. The directory is just a file, but operating systems tend to use the directory for another purpose... :wink:

You have to parse the fat-chain for the directory and read the cluster that belongs to that fat entry.

All files and directories don't have any clue of their parent whatsoever. A file doesn't know to which directory it belongs and this is also true for a subdirectory. Subdirectories however, have 2 entries: dot (.) and dotdot (..). These entries point to itself and its parent respectively. That is, byte 26 and 27 contain the starting cluster.

Hope this helps.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 11, 2008 8:33 am 
Offline
Member
Member

Joined: Wed Oct 31, 2007 9:09 am
Posts: 1385
kestaz wrote:
no i can't figure out .. how directory entry can point to many list of files ? how it's possible ?


A directory is the same as a regular file, as far as the FAT file system is concerned. That means that it occupies disk sectors that you can read. A directory contains entries describing files that are 'inside' that directory. A directory is an array of 32-byte structures, each structure describing one file. The attributes inside the structure tell you whether it is a regular file or a directory (or a volume label, but you can ignore that one).

Perhaps you should read and try to understand the official FAT specification that you can find here. It's all in there.


JAL


Top
 Profile  
 
 Post subject: Re: fat12 simple question
PostPosted: Fri Jan 11, 2008 11:17 am 
Offline
Member
Member

Joined: Sun Jan 06, 2008 8:41 am
Posts: 174
jal wrote:
skyking wrote:
Having the files (and directories) not knowing which directory they belong to is important when implementing hard links since then the files can belong to multiple directories.


However, totally unimportant for FAT, since it doesn't have hard links. In fact, subdirectories actually have the "." and ".." as part of the directory contents (unlike Unix file systems, which rely on the shell to interpret these), and ".." actually links to the parent directory.


JAL


I meant to design a VFS to rely on files to know where they are located is a bad idea for that reason (not that it matters much for FAT12).


Top
 Profile  
 
 Post subject: Re: fat12 simple question
PostPosted: Fri Jan 11, 2008 2:48 pm 
Offline
Member
Member
User avatar

Joined: Wed Feb 07, 2007 1:45 pm
Posts: 1401
Location: Eugene, OR, US
jal wrote:
"." and ".." ... Unix file systems, which rely on the shell to interpret these ...

JAL


Perhaps the Linuxes use shell interpretation for . and .., but original Unix always used hardlinks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 11, 2008 6:04 pm 
Offline

Joined: Thu Jan 10, 2008 6:09 pm
Posts: 11
thanks everybody for replies. Still don't understand ;) So I am wrote some code to make ROOT STRUCTURE dump ..

You can see it at http://www.pastebin.ca/851490

ok i will try to explain what i don't understand

Name: UNTITLED 2 (
Ext: 2 (
starting_sector: 0

Ok everything clear at this point.. I made image of usb flash .. so it was named UNTITLED ..

Name: LABAS TXT !¨π)8)8
Ext: TXT !¨π)8)8
starting_sector: 23

And there's file.. And i see it's points to 23 .. OK .. it's points to FAT

Name: LABAS -wπ)8)8
Attribute: directory
Ext: -wπ)8)8
starting_sector: 22

Ok there's directory .. hmm but what 22 shows ? Where this pointer points ? To the FAT ? TO the ROOT DIRECTORY STRUCTURE ? I still i don't understand ...

OK .. As you said there's ".." , "." but i can't see them ..

I see mane junk in my dump .. How to avoid it ?

You can see my code: http://www.pastebin.ca/851504 hope it helps..

any help will be appropriate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 14, 2008 3:42 am 
Offline
Member
Member

Joined: Wed Oct 31, 2007 9:09 am
Posts: 1385
kestaz wrote:
Ok there's directory .. hmm but what 22 shows ? Where this pointer points ? To the FAT ? TO the ROOT DIRECTORY STRUCTURE ? I still i don't understand ...


Please, PLEASE, PLEASE read the offical FAT specification I linked to somewhere earlier in the thread. It is really all in there. If you don't understand details of it, fine, let's discuss those, but it seems you just want us to provide all the answers that you can easily find yourself!

Quote:
OK .. As you said there's ".." , "." but i can't see them ..


No, they are not in the root directory, as mentioned.

Quote:
I see mane junk in my dump .. How to avoid it ?


The strings aren't zero-terminated, so you cannot use %s without specifying a width when printing. And that's something else easily available from the specification. Use %.8s and %.3s instead!

Quote:
any help will be appropriate


I (and no doubt many others) are more than willing to help, but like I said, please check the documentation first before asking questions, as we are not here to spell it out for you.


JAL


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: rdos and 227 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group