/// <summary>
        /// Writes a block into a specific partition
        /// </summary>
        /// <param name="p">The partition to write to</param>
        /// <param name="b">The block to write</param>
        public static void Write(Partition p, NoobFSBlock b)
        {
            Byte[] data  = new Byte[p.BlockSize];
            int    index = 0;

            if (b.Used)
            {
                data[index++] = 0x01;
            }
            else
            {
                data[index++] = 0x00;
            }
            Byte[] x = BitConverter.GetBytes(b.ContentSize);
            for (int i = 0; i < x.Length; i++)
            {
                data[index++] = x[i];
            }
            x = BitConverter.GetBytes(b.TotalSize);
            for (int i = 0; i < x.Length; i++)
            {
                data[index++] = x[i];
            }
            x = BitConverter.GetBytes(b.NextBlock);
            for (int i = 0; i < x.Length; i++)
            {
                data[index++] = x[i];
            }
            x = b.Content;
            for (int i = 0; i < x.Length; i++)
            {
                data[index++] = x[i];
            }
            p.WriteBlock(b.BlockNumber, 1, data);
        }
Beispiel #2
0
        /// <summary>
        /// Gets the last NoobFSBlock of the directory
        /// </summary>
        private NoobFSBlock GetBlockToEdit()
        {
            NoobFSBlock ret = _StartBlock;

            while (ret.NextBlock != 0)
            {
                ret = NoobFSBlock.Read(_StartBlock.Partition, _StartBlock.NextBlock);
            }
            if (ret.BlockNumber == _StartBlock.BlockNumber)
            {
                ret                   = NoobFSBlock.GetFreeBlock(part);
                ret.Used              = true;
                ret.ContentSize       = 0;
                ret.NextBlock         = 0;
                _StartBlock.NextBlock = ret.BlockNumber;
                NoobFSBlock.Write(part, _StartBlock);
                NoobFSBlock.Write(part, ret);
            }
            if (part.NewBlockArray(1).Length - ret.ContentSize < 12)
            {
                NoobFSBlock b = NoobFSBlock.GetFreeBlock(part);
                if (b == null)
                {
                    return(null);
                }
                ret.NextBlock = b.BlockNumber;
                NoobFSBlock.Write(part, ret);
                b.Used = true;
                ret    = b;
            }
            return(ret);
        }
Beispiel #3
0
 /// <summary>
 /// Creates a new NoobDirectory Object
 /// </summary>
 /// <param name="p">The partition to use</param>
 /// <param name="bn">The block number we want to use</param>
 /// <param name="pa">The path of the new directory</param>
 public NoobDirectory(Partition p, ulong bn, String pa)
 {
     _Path       = pa;
     part        = p;
     _StartBlock = NoobFSBlock.Read(p, bn);
     if (bn == 1 && pa == "/" && _StartBlock.Content[0] != '/')
     {
         Char[] nm = "/".ToCharArray();
         for (int i = 0; i < nm.Length; i++)
         {
             _StartBlock.Content[i] = (byte)nm[i];
         }
         _StartBlock.Used      = true;
         _StartBlock.NextBlock = 0;
         NoobFSBlock.Write(p, _StartBlock);
     }
     if (!_StartBlock.Used)
     {
         _StartBlock.Used = true;
         String n = "New Directory";
         if (pa == NoobFileSystem.separator)
         {
             _Path = "";
             n     = pa;
         }
         CreateEntry(part, _StartBlock, n);
     }
 }
Beispiel #4
0
        /// <summary>
        /// Gets all NoobEntries contained in this NoobDirectory
        /// </summary>
        public NoobEntry[] GetEntries()
        {
            NoobFSBlock      curb = _StartBlock;
            List <NoobEntry> d    = new List <NoobEntry>();

            while (curb.NextBlock != 0)
            {
                int index = 0;
                curb = NoobFSBlock.Read(_StartBlock.Partition, _StartBlock.NextBlock);
                while (index < curb.ContentSize)
                {
                    ulong a = BitConverter.ToUInt64(curb.Content, index);
                    index += 8;
                    uint sep = BitConverter.ToUInt32(curb.Content, index);
                    index += 4;
                    if (sep == 1)
                    {
                        d.Add(new NoobDirectory(part, a, NoobFileSystem.CombineDir(_Path, Name)));
                    }
                    else if (sep == 2)
                    {
                        d.Add(new NoobFile(part, a, NoobFileSystem.CombineDir(_Path, Name)));
                    }
                }
            }
            return(d.ToArray());
        }
Beispiel #5
0
        /// <summary>
        /// Clears every block starting from this one
        /// </summary>
        /// <param name="StartBlock">The block to start from</param>
        public static void ClearBlocks(NoobFSBlock StartBlock)
        {
            NoobFSBlock b = StartBlock;

            while (b.NextBlock != 0)
            {
                b      = NoobFSBlock.Read(b.Partition, b.NextBlock);
                b.Used = false;
                NoobFSBlock.Write(mFS.Partition, b);
            }
        }
 /// <summary>
 /// Get the free block from the selected partition (TO-DO: Implement something that runs faster)
 /// </summary>
 /// <param name="p">The partition to get the block from</param>
 public static NoobFSBlock GetFreeBlock(Partition p)
 {
     for (ulong i = 1; i < p.BlockCount; i++)
     {
         NoobFSBlock b = Read(p, i);
         if (!b.Used)
         {
             return(b);
         }
     }
     return(null);
 }
 /// <summary>
 /// Creates a new NoobFile Object
 /// </summary>
 /// <param name="p">The partition to use</param>
 /// <param name="bn">The block number we want to use</param>
 /// <param name="pa">The path of the new directory</param>
 public NoobFile(Partition p, ulong bn, String pa)
 {
     _Path       = pa;
     part        = p;
     _StartBlock = NoobFSBlock.Read(p, bn);
     if (!_StartBlock.Used)
     {
         _StartBlock.Used = true;
         String n = "New File";
         CreateEntry(part, _StartBlock, n);
     }
 }
Beispiel #8
0
        /// <summary>
        /// Permits to remove a NoobFSBlock by passing it
        /// </summary>
        /// <param name="noobDirectory">The NoobFSBlock to remove</param>
        private void DeleteBlock(NoobFSBlock noobFSBlock)
        {
            NoobFSBlock curb = _StartBlock;

            while (curb.NextBlock != 0)
            {
                int         index = 0;
                bool        found = false;
                List <Byte> cont  = new List <Byte>();
                curb = NoobFSBlock.Read(_StartBlock.Partition, _StartBlock.NextBlock);
                while (index < curb.ContentSize)
                {
                    ulong  a   = BitConverter.ToUInt64(curb.Content, index);
                    Byte[] app = BitConverter.GetBytes(a);
                    for (int i = 0; i < app.Length; i++)
                    {
                        cont.Add(app[i]);
                    }
                    index += 8;
                    uint sep = BitConverter.ToUInt32(curb.Content, index);
                    index += 4;
                    if (a == noobFSBlock.BlockNumber)
                    {
                        app = BitConverter.GetBytes((uint)0);
                        for (int i = 0; i < app.Length; i++)
                        {
                            cont.Add(app[i]);
                        }
                        found = true;
                    }
                    else
                    {
                        app = BitConverter.GetBytes(sep);
                        for (int i = 0; i < app.Length; i++)
                        {
                            cont.Add(app[i]);
                        }
                    }
                }
                if (found)
                {
                    curb.Content     = cont.ToArray();
                    curb.ContentSize = (uint)cont.Count;
                    NoobFSBlock.Write(part, curb);
                }
            }
        }
        /// <summary>
        /// Return's all the bytes contained in the file
        /// </summary>
        public Byte[] ReadAllBytes()
        {
            if (_StartBlock.NextBlock == 0)
            {
                return(new Byte[0]);
            }
            NoobFSBlock b    = _StartBlock;
            List <Byte> lret = new List <Byte>();

            while (b.NextBlock != 0)
            {
                b = NoobFSBlock.Read(b.Partition, b.NextBlock);
                for (int i = 0; i < b.ContentSize; i++)
                {
                    lret.Add(b.Content[i]);
                }
            }
            EditEntryInfo(EntryInfoPosition.DateTimeLastAccess, Environment.DateTime.Now.TimeStamp);
            return(lret.ToArray());
        }
 /// <summary>
 /// Creates a new NoobEntry in the current directory
 /// </summary>
 /// <param name="p">The partition where to create the new NoobEntry</param>
 /// <param name="b">The block to write on</param>
 /// <param name="name">The new NoobEntry's name</param>
 protected static NoobFSBlock CreateEntry(Partition p, NoobFSBlock b, String n)
 {
     if (b != null && ((!Utils.StringContains(n, InvalidChars)) || b.BlockNumber == 0))
     {
         b.Used      = true;
         b.NextBlock = 0;
         b.TotalSize = 0;
         Char[] nm = n.ToCharArray();
         for (int i = 0; i < nm.Length; i++)
         {
             b.Content[i] = (byte)nm[i];
         }
         if (b.BlockNumber != 0)
         {
             Utils.CopyByteToByte(BitConverter.GetBytes(Environment.DateTime.Now.TimeStamp), 0, b.Content, (int)MaxNameSize + (int)EntryInfoPosition.DateTimeCreated, 8, false);
         }
         b.Content[nm.Length] = 0;
         b.ContentSize        = (uint)nm.Length;
         NoobFSBlock.Write(p, b);
         return(b);
     }
     return(null);
 }
Beispiel #11
0
        /// <summary>
        /// Creates a new NoobFile to the current directory
        /// </summary>
        /// <param name="Name">The new NoobFile's name</param>
        public void AddFile(String Name)
        {
            NoobEntry[] dirs = GetEntries();
            for (int i = 0; i < dirs.Length; i++)
            {
                if (dirs[i].Name == Name)
                {
                    Helper.WriteLine("Entry with same Name already exists!");
                    return;
                }
            }
            NoobFSBlock curb     = GetBlockToEdit();
            NoobFSBlock newfileb = CreateEntry(part, Name);

            //if (newfileb != null)
            //{
            BitConverter.GetBytes(newfileb.BlockNumber).CopyTo(curb.Content, curb.ContentSize);
            BitConverter.GetBytes((uint)2).CopyTo(curb.Content, curb.ContentSize + 8);
            curb.ContentSize += 12;
            NoobFSBlock.Write(part, curb);
            EditEntryInfo(EntryInfoPosition.DateTimeModified, Environment.DateTime.Now.TimeStamp);
            EditEntryInfo(EntryInfoPosition.DateTimeLastAccess, Environment.DateTime.Now.TimeStamp);
            //}
        }
        /// <summary>
        /// Writes all the specified Bytes into the file
        /// </summary>
        /// <param name="Data">The array Byte to write into file</param>
        public void WriteAllBytes(Byte[] Data)
        {
            if (_StartBlock.NextBlock != 0)
            {
                NoobFileSystem.ClearBlocks(_StartBlock);
                _StartBlock.NextBlock = 0;
                NoobFSBlock.Write(NoobFileSystem.mFS.Partition, _StartBlock);
            }
            int         index = 0;
            NoobFSBlock curb  = NoobFSBlock.GetFreeBlock(NoobFileSystem.mFS.Partition);

            _StartBlock.NextBlock = curb.BlockNumber;
            NoobFSBlock.Write(part, _StartBlock);
            do
            {
                Byte[] arr = new Byte[NoobFSBlock.MaxBlockContentSize];
                index        = Utils.CopyByteToByte(Data, index, arr, 0, arr.Length);
                curb.Used    = true;
                curb.Content = arr;
                if (index != Data.Length)
                {
                    NoobFSBlock b = NoobFSBlock.GetFreeBlock(NoobFileSystem.mFS.Partition);
                    curb.NextBlock   = b.BlockNumber;
                    curb.ContentSize = (uint)arr.Length;
                    NoobFSBlock.Write(NoobFileSystem.mFS.Partition, curb);
                    curb = b;
                }
                else
                {
                    curb.ContentSize = (uint)(Data.Length % arr.Length);
                    NoobFSBlock.Write(NoobFileSystem.mFS.Partition, curb);
                }
            }while (index != Data.Length);
            EditEntryInfo(EntryInfoPosition.DateTimeModified, Environment.DateTime.Now.TimeStamp);
            EditEntryInfo(EntryInfoPosition.DateTimeLastAccess, Environment.DateTime.Now.TimeStamp);
        }
 /// <summary>
 /// Creates a new NoobEntry in the current directory. Uses a random Block.
 /// </summary>
 /// <param name="p">The partition where to create the new NoobEntry</param>
 /// <param name="name">The new NoobEntry's name</param>
 protected static NoobFSBlock CreateEntry(Partition p, String name)
 {
     return(CreateEntry(p, NoobFSBlock.GetFreeBlock(p), name));
 }