Beispiel #1
0
        /// <summary>
        /// 将文件内容拷贝到node里,返回node
        /// </summary>
        public Node CopyRealFileToNode(string srcPath, Node destPath, Disk disk)
        {
            if (destPath == null)
            {
                return(null);
            }

            //判断源文件是二进制还是文本
            bool isBin = false;

            if (System.IO.File.Exists(srcPath))
            {
                FileInfo fileinfo = new FileInfo(srcPath);
                isBin = !(fileinfo.Extension == ".txt");
            }
            else
            {
                Console.WriteLine("源文件不存在");
                disk.RemoveNode(destPath);
                return(null);
            }

            if (isBin)
            {
                if (destPath is File f)
                {
                    List <byte[]> bs = ReadBinFile(srcPath);
                    if (bs != null)
                    {
                        f.SetBinData(bs);
                    }
                    else
                    {
                        disk.RemoveNode(destPath);
                        return(null);
                    }
                }
            }
            else
            {
                if (destPath is File f)
                {
                    string str = ReadStrFile(srcPath);
                    if (str != null)
                    {
                        f.SetStrData(str);
                    }
                    else
                    {
                        disk.RemoveNode(destPath);
                        return(null);
                    }
                }
            }

            return(destPath);
        }
Beispiel #2
0
        public override Disk Execute(Disk disk, string param)
        {
            string path   = "";
            string addPar = "";

            CmdStrTool.SplitParam(param, addParam, out path, out addPar);

            if (string.IsNullOrEmpty(path))
            {
                CmdStrTool.ShowTips(1);
                return(null);
            }
            else
            {
                string[] paths  = CmdStrTool.SplitPathParamToPathList(path);
                bool     alldel = addPar == "/s";
                if (paths.Length > 0)
                {
                    for (int i = 0; i < paths.Length; i++)
                    {
                        string[] namelist = CmdStrTool.SplitPathToNameList(paths[i]);
                        Node     n        = disk.NameListToNode(namelist, IsSupportWildcard);
                        if (n != null)
                        {
                            List <Node> dels = new List <Node>();
                            Del(DelNode(n, alldel, ref dels));
                        }
                        else
                        {
                            CmdStrTool.ShowTips(2);
                        }
                    }
                }
            }

            List <Node> DelNode(Node d, bool alldel, ref List <Node> dels)
            {
                if (d is File fl)
                {
                    dels.Add(fl);
                }
                else if (d is Floder f)
                {
                    foreach (Node item in f.childs)
                    {
                        if (item is Floder)
                        {
                            if (alldel)
                            {
                                DelNode(item, alldel, ref dels);
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            dels.Add(item);
                        }
                    }
                }
                else if (d is Symlink s)
                {
                    dels.Add(s);
                }
                return(dels);
            }

            void Del(List <Node> indexs)
            {
                for (int i = 0; i < indexs.Count; i++)
                {
                    disk.RemoveNode(indexs[i]);
                }
            }

            GC.Collect(); //手动gc

            return(null);
        }
Beispiel #3
0
        public override Disk Execute(Disk disk, string param)
        {
            string path   = "";
            string addPar = "";

            CmdStrTool.SplitParam(param, addParam, out path, out addPar);

            if (string.IsNullOrEmpty(path))
            {
                CmdStrTool.ShowTips(1);
                return(null);
            }
            else
            {
                string[] paths  = CmdStrTool.SplitPathParamToPathList(path);
                bool     alldel = addPar == "/s";
                if (paths.Length > 0)
                {
                    for (int i = 0; i < paths.Length; i++)
                    {
                        string[] namelist = CmdStrTool.SplitPathToNameList(paths[i]);
                        Node     n        = disk.NameListToNode(namelist, IsSupportWildcard);
                        if (n.index == disk.current.index)
                        {
                            Console.WriteLine("无法删除正在使用的当前路径");
                            return(null);
                        }
                        if (n != null)
                        {
                            List <Node> dels = new List <Node>();
                            Rd(RdNode(n, alldel, ref dels), alldel);
                        }
                        else
                        {
                            CmdStrTool.ShowTips(2);
                        }
                    }
                }
            }

            List <Node> RdNode(Node d, bool alldel, ref List <Node> dels)
            {
                if (d is File fl)
                {
                    if (alldel)
                    {
                        dels.Add(fl);
                    }
                }
                else if (d is Floder f)
                {
                    if (!alldel && f.childs.Count != 0)
                    {
                        Console.WriteLine("目录非空");
                        return(dels);
                    }
                    dels.Add(f);
                    foreach (Node item in f.childs)
                    {
                        if (item is Floder)
                        {
                            if (alldel)
                            {
                                RdNode(item, alldel, ref dels);
                            }
                            else
                            {
                                dels.Add(item);
                            }
                        }
                        else if (alldel)
                        {
                            dels.Add(item);
                        }
                    }
                }
                else if (d is Symlink s)
                {
                    if (alldel)
                    {
                        dels.Add(s);
                    }
                }
                return(dels);
            }

            void Rd(List <Node> indexs, bool alldel)
            {
                for (int i = 0; i < indexs.Count; i++)
                {
                    if (alldel)
                    {
                        disk.RemoveNode(indexs[i]);
                    }
                    else if (indexs[i] is Floder f)
                    {
                        if (f.childs.Count == 0)
                        {
                            disk.RemoveNode(f);
                        }
                        else
                        {
                            Console.WriteLine("目录非空");
                            return;
                        }
                    }
                }
            }

            GC.Collect(); //手动gc

            return(null);
        }