Example #1
0
        public void DeleteFile(MFPHandle handle, string filename)
        {
            FileStream fs = handle.fs;

            Assert(fs != null, "The MFPHandle has been CLOSED or something WRONG with it");
            Assert(handle.config.ContainsKey(filename), "The file with name '" + filename + "' does NOT EXISTS");
            MFPSeek seek = handle.config[filename];

            handle.config.Remove(filename);
            List <MFPSeek> space = handle.space;

            if (space.Count > 0)
            {
                MFPSeek head    = new MFPSeek();
                MFPSeek tail    = new MFPSeek();
                int     headIdx = 0;
                int     tailIdx = 0;
                for (int i = 0; i < space.Count; ++i)
                {
                    MFPSeek s = space[i];
                    if (s.tail == seek.head)
                    {
                        s.tail  = seek.tail;
                        head    = s;
                        headIdx = i;
                    }
                    if (s.head == seek.tail)
                    {
                        s.head  = seek.head;
                        tail    = s;
                        tailIdx = i;
                    }
                }
                if (head.tail != 0 && tail.tail != 0)
                {
                    head.tail      = tail.tail;
                    space[headIdx] = head;
                    space.RemoveAt(tailIdx);
                }
                if (head.tail != 0 || tail.tail != 0)
                {
                    if (head.tail != 0 && seek.tail == handle.seek_pos)
                    {
                        handle.seek_pos = head.head;
                        space.RemoveAt(headIdx);
                    }
                    return;
                }
            }
            if (seek.tail == handle.seek_pos)
            {
                handle.seek_pos = seek.head;
                return;
            }
            space.Add(seek);
        }
Example #2
0
        private void InsertFile(MFPHandle handle, MFPSeek s, byte[] file, string filename)
        {
            FileStream fs = handle.fs;

            Assert(fs != null, "The MFPHandle has been CLOSED or something WRONG with it");
            Assert(!handle.config.ContainsKey(filename), "The file with name '" + filename + "' has EXISTS");
            fs.Seek(s.head, SeekOrigin.Begin);
            fs.Write(file, 0, file.Length);
            handle.config.Add(filename, s);
        }
Example #3
0
        public byte[] ReadFileBytes(MFPHandle handle, string filename)
        {
            FileStream fs = handle.fs;

            Assert(fs != null, "The MFPHandle has been CLOSED or something WRONG with it");
            Assert(handle.config.ContainsKey(filename), "The file with name '" + filename + "' does NOT EXISTS");
            MFPSeek seek = handle.config[filename];

            fs.Seek(seek.head, SeekOrigin.Begin);
            byte[] file_byte = new byte[seek.tail - seek.head];
            fs.Read(file_byte, 0, file_byte.Length);
            return(file_byte);
        }
Example #4
0
        public void AddFileBytes(MFPHandle handle, byte[] file, string filename)
        {
            List <MFPSeek> space = handle.space;

            if (space.Count > 0)
            {
                for (int i = 0; i < space.Count; ++i)
                {
                    MFPSeek s       = space[i];
                    int     slength = s.tail - s.head;
                    float   r       = (float)file.Length / slength;
                    if (r >= MFP_CONFIG_PRIVATE.REUSE_FILE_PERCENT && r <= 1.0f)
                    {
                        MFPSeek sf = new MFPSeek()
                        {
                            head = s.head,
                            tail = s.head + file.Length
                        };
                        InsertFile(handle, sf, file, filename);
                        s.head += file.Length;
                        if (s.head == s.tail)
                        {
                            space.RemoveAt(i);
                        }
                        else
                        {
                            space[i] = s;
                        }
                        return;
                    }
                }
            }
            MFPSeek seek = new MFPSeek()
            {
                head = handle.seek_pos,
                tail = handle.seek_pos + file.Length
            };

            handle.seek_pos = seek.tail;
            InsertFile(handle, seek, file, filename);
        }