Exemplo n.º 1
0
        private static void CreateDirectory(string directory)
        {
            string[] directories = directory.Split('/');

            uint currentcluster = 0;
            int  current        = 0;

            while (true)
            {
StartLoop:
                ReadCluster(currentcluster);

                for (int i = 0; i < filesystemdefinition.ClusterSizeInBytes; i += 64)
                {
                    if ((cluster[i] & 0x3) == 1)
                    {
                        string str = ASCIIEncoding.ASCII.GetString(cluster, i + 32, 32);
                        str = str.TrimEnd('\0');

                        if (str == directories[current])
                        {
                            current++;

                            if (current > directories.Length - 1)
                            {
                                return;
                            }

                            currentcluster = BitConverter.ToUInt32(cluster, i + 1);
                            goto StartLoop;
                        }
                    }
                }

                uint nextcluster = GetNextClusterInChain(currentcluster);

                if (nextcluster == (uint)0xFFFFFFFE)
                {
                    uint free = FindFreeCluster();
                    ClearCluster(free);
                    WriteClusterChain(free);

                    DirectoryEntry entry = new DirectoryEntry();
                    entry.Attributes = 0x01;
                    //entry.FileName = directories[current];
                    entry.FirstCluster = free;

                    WriteDirectoryEntryToDirectory(currentcluster, ref entry);

                    currentcluster = free;
                    current++;

                    if (current > directories.Length - 1)
                    {
                        return;
                    }
                }
                else
                {
                    currentcluster = nextcluster;
                }
            }
        }
Exemplo n.º 2
0
        private static void AddFile(string diskFileName, string fileName)
        {
            string[] path      = fileName.Split('/');
            string   directory = null;

            if (path.Length > 1)
            {
                directory = fileName.Substring(0, fileName.LastIndexOf('/'));
            }

            FileStream file = File.Open(diskFileName, FileMode.Open, FileAccess.Read);

            byte[] data = new byte[file.Length];
            file.Read(data, 0, data.Length);

            byte[] sector  = new byte[filesystemdefinition.SectorSizeInBytes];
            byte[] cluster = new byte[filesystemdefinition.ClusterSizeInBytes];

            //Find free clusters for the file
            int remainder;
            int clustersneeded = Math.DivRem(data.Length, filesystemdefinition.ClusterSizeInBytes, out remainder);

            if (remainder > 0)
            {
                clustersneeded++;
            }

            uint[] clustersused = FindFreeClusters(clustersneeded);

            //Write the file data to the free clusters
            WriteData(data, clustersused);

            //Create cluster chain
            WriteClusterChain(clustersused);

            //Write the filename
            uint filenamecluster = FindFreeCluster();

            byte[] filenamedata = ASCIIEncoding.ASCII.GetBytes(fileName);
            WriteData(filenamedata, filenamecluster);

            WriteClusterChain(filenamecluster);

            //Create the directory entry
            DirectoryEntry entry = new DirectoryEntry();

            entry.Attributes      = 0x03;
            entry.FirstCluster    = clustersused[0];
            entry.FileSize        = (uint)file.Length;
            entry.FilenameCluster = filenamecluster;
            //entry.FileName = path[path.Length - 1];

            //Write directory entry to root directory
            if (path.Length == 1)
            {
                WriteDirectoryEntryToDirectory(0, ref entry);
            }
            else
            {
                uint firstcluster = GetDirectoryFirstCluster(directory);
                WriteDirectoryEntryToDirectory(firstcluster, ref entry);
            }

            //Close the file
            file.Close();
        }
Exemplo n.º 3
0
        private static void WriteDirectoryEntryToDirectory(uint firstDirectoryCluster, ref DirectoryEntry entry)
        {
            uint currentcluster = firstDirectoryCluster;

            while (true)
            {
                ReadCluster(currentcluster);

                for (int i = 0; i < filesystemdefinition.ClusterSizeInBytes; i += 64)
                {
                    if (cluster[i] == 0)
                    {
                        Array.Copy(entry.ToBytes(), 0, cluster, i, 64);
                        WriteCluster(currentcluster, cluster);

                        return;
                    }
                }

                uint nextcluster = GetNextClusterInChain(currentcluster);

                if (nextcluster == EndOfClusterChain)
                {
                    currentcluster = ExpandDirectoryCluster(currentcluster);
                }
                else
                {
                    currentcluster = nextcluster;
                }
            }
        }