Aleksei Poliakov
Aleksei Poliakov

Reputation: 1332

How to obtain the first cluster of the directory's data in FAT using C# (or at least C++) and Win32API?

I have a FAT drive, lets say H: and a directory 'work' (full path 'H:\work'). I need to get the NUMBER of the first cluster of that directory. The number of the first cluster is 2-bytes value, that is stored in the 26th and 27th bytes of the folder entry (which is 32 bytes).

Lets say I am doing it with file, NOT a directory. I can use code like this:

 static public string GetDirectoryPtr(string dir)
    {
        
        IntPtr ptr = CreateFile(@"H:\Work\dover.docx",
            GENERIC_READ,
            FILE_SHARE_READ | FILE_SHARE_WRITE,
            IntPtr.Zero,
            OPEN_EXISTING,
            0,//FILE_FLAG_BACKUP_SEMANTICS, 
            IntPtr.Zero);
        try
        {
            const uint bytesToRead = 2;
            byte[] readbuffer = new byte[bytesToRead];
            if (ptr.ToInt32() == -1) return String.Format("Error: cannot open directory {0}", dir);
            if (SetFilePointer(ptr, 26, 0, 0) == -1) return String.Format("Error: unable to set file pointer on file {0}", ptr);

            uint read = 0; // real count of read bytes
            if (!ReadFile(ptr, readbuffer, bytesToRead, out read, 0)) return String.Format("can't read from file {0}. Error #{1}", ptr, Marshal.GetLastWin32Error());                
            int result = readbuffer[0] + 16 * 16 * readbuffer[1];
            return result.ToString();//ASCIIEncoding.ASCII.GetString(readbuffer);

        }
        finally
        {
            CloseHandle(ptr);
        }
    }

And it will return some number, like 19 (quite real to me, this is the only file on the disk).

But I DON'T need a file, I need a folder. So I am putting FILE_FLAG_BACKUP_SEMANTICS param for CreateFile call... and don't know what to do next. Is there any way to get it working for a folder?

msdn is very clear on this issue http://msdn.microsoft.com/en-us/library/aa365258(v=VS.85).aspx

It sounds to me like: "There is no way you can get a number of the folder's first cluster". The most desperate thing is that my tutor said something like "You are going to obtain this or you wont pass this course". The true reason why he is so sure this is possible is because for 10 years (or may be more) he received the folder's first cluster number as a HASH of the folder's address (and I was stupid enough to point this to him, so now I can't do it the same way)

PS: This is the most stupid task I have ever had! This value is not really used anywhere in program, it is only a pointless integer.

Upvotes: 1

Views: 699

Answers (1)

Chris Taylor
Chris Taylor

Reputation: 53729

So if I understand correctly, you want to read the FAT and get the first cluster of a directory on the disk?

If the above is correct, then it might be better to open the volume for direct access.
http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
See the section on Phystical Disks and Volumes.

Once you have the volume open you can then read the disk sectors and scan the FAT to locate the entry for the directory you are interested in.

Here is a sample of using DeiceIoControl to read the disk geometry. http://msdn.microsoft.com/en-us/library/aa363147(VS.85).aspx

Upvotes: 2

Related Questions