Resize() public method

修改并释放数据占用空间,删除第 newSize 字节开始直到原文件大小的数据块
public Resize ( UInt32 newSize ) : void
newSize System.UInt32
return void
Exemplo n.º 1
0
        /// <summary>
        /// 删除一个目录项
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Boolean Delete(String name)
        {
            if (!Contains(name))
            {
                return(false);
            }
            if (name == "." || name == "..")
            {
                return(false);
            }

            var inodeIndex = entries[name];

            INode inode = INode.Load(vfs, inodeIndex);

            if (inode.IsDirectory())
            {
                // 删除非空目录项:递归删除
                INodeDirectory id = INodeDirectory.Load(vfs, inodeIndex);
                if (id.Size() > 2)
                {
                    var l = id.List();
                    foreach (var pair in l)
                    {
                        id.Delete(pair.Key);
                    }
                }
            }

            inode.data.linkCount--;

            if (inode.data.linkCount == 0)
            {
                inode.Resize(0);
                vfs.DeAllocateINode(inode.index);
            }
            else
            {
                inode.Save();
            }

            entries.Remove(name);
            Save();

            return(true);
        }
Exemplo n.º 2
0
            public File(VFSCore vfs, String path, FileMode fileMode)
            {
                this.vfs = vfs;

                var directory = VFS.GetPathDirectory(path);
                var name      = VFS.GetPathName(path);

                VFS.AssertNameValid(name);

                INodeDirectory dir = INodeDirectory.Resolve(vfs, directory);

                if (dir == null)
                {
                    throw new Exception("无法访问目录");
                }
                switch (fileMode)
                {
                case FileMode.CreateNew:
                    if (dir.Contains(name))
                    {
                        throw new Exception("文件已存在");
                    }
                    CreateFile(dir, name);
                    break;

                case FileMode.Create:
                    if (dir.Contains(name))
                    {
                        OpenFile(dir, name);
                        inode.Resize(0);
                    }
                    else
                    {
                        CreateFile(dir, name);
                    }
                    break;

                case FileMode.Open:
                    if (!dir.Contains(name))
                    {
                        throw new Exception("文件未找到");
                    }
                    OpenFile(dir, name);
                    break;

                case FileMode.OpenOrCreate:
                    if (dir.Contains(name))
                    {
                        OpenFile(dir, name);
                    }
                    else
                    {
                        CreateFile(dir, name);
                    }
                    break;

                case FileMode.Truncate:
                    if (!dir.Contains(name))
                    {
                        throw new Exception("文件未找到");
                    }
                    OpenFile(dir, name);
                    inode.Resize(0);
                    break;

                case FileMode.Append:
                    if (!dir.Contains(name))
                    {
                        CreateFile(dir, name);
                    }
                    else
                    {
                        OpenFile(dir, name);
                        position = inode.data.sizeByte;
                    }
                    break;

                default:
                    throw new ArgumentException();
                }
            }