コード例 #1
0
 void RenameNode(Node n, string name)
 {
     //先看父节点有没有已经存在这个name
     if (n.parent != null && n.parent is Floder f)
     {
         Node has = f.GetChildByName(name);
         if (has != null)  //父节点已经存在这个儿子
         {
             CmdStrTool.ShowTips(4);
         }
         else
         {
             //name参数对不对
             if (CmdStrTool.NameIsGood(name))
             {
                 CmdStrTool.ShowTips(5);
             }
             else
             {
                 Console.WriteLine(n.GetPath() + "-->" + name);
                 n.SetName(name);
                 n.SetDate();
             }
         }
     }
     else
     {
         CmdStrTool.ShowTips(3);
     }
 }
コード例 #2
0
ファイル: MdCommand.cs プロジェクト: AHappyFun/VirtualDisk
        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);
                if (paths.Length > 0)
                {
                    for (int i = 0; i < paths.Length; i++)  //创建多个目录
                    {
                        string[] namelist = CmdStrTool.SplitPathToNameList(paths[i]);
                        disk.CreateNodesWithNameList(namelist);

                        Console.WriteLine("已创建{0}{1}", disk.current.GetPath(), GetStrNames(namelist));
                    }
                }
            }


            return(null);
        }
コード例 #3
0
 public Disk DeserializeDisk(string realPath)
 {
     try
     {
         FileStream      fs    = new FileStream(realPath, FileMode.Open);
         BinaryFormatter bf    = new BinaryFormatter();
         List <Node>     nodes = bf.Deserialize(fs) as List <Node>;
         fs.Close();
         if (nodes != null && nodes.Count > 0)
         {
             Disk d = new Disk(nodes[0]);
             d.nodeCount = nodes.Count;
             d.allNodes  = nodes;
             //List<Node> ns = new List<Node>();
             foreach (Node item in nodes)
             {
                 item.SetDisk(d);
             }
             return(d);
         }
         else
         {
             Console.WriteLine("加载二进制序列化文件失败");
             return(null);
         }
     }
     catch (Exception)
     {
         CmdStrTool.ShowTips(6);
         return(null);
     }
 }
コード例 #4
0
ファイル: CdCommand.cs プロジェクト: AHappyFun/VirtualDisk
        public override Disk Execute(Disk disk, string param)
        {
            string path   = "";
            string addPar = "";

            if (param == ".")
            {
                Console.WriteLine(disk.current.GetPath() + ">");
                return(null);
            }
            else if (param == "/" || param == "\\")
            {
                disk.current = disk.root as Floder;
                Console.WriteLine(disk.current.GetPath() + ">");
                return(null);
            }

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

            if (path == "")
            {
                Console.WriteLine(disk.current.GetPath() + ">");
                return(null);
            }
            else
            {
                string[] paths = CmdStrTool.SplitPathParamToPathList(path);
                if (paths.Length != 1) //多个路径命令不正确
                {
                    CmdStrTool.ShowTips(2);
                }
                else
                {
                    string[] namelist = CmdStrTool.SplitPathToNameList(paths[0]);
                    Node     n        = disk.NameListToNode(namelist, IsSupportWildcard);
                    SetCurrentDir(n);
                }
            }

            void SetCurrentDir(Node n)
            {
                if (n != null && n is Floder f)   //正常情况
                {
                    disk.current = f as Floder;
                    Console.WriteLine(disk.current.GetPath() + ">");
                }
                else if (n != null && n is Symlink s)  //找到的是个符号链接
                {
                    Node tar = s.target;
                    SetCurrentDir(tar);
                }
                else   //没找到结点或找到了个文件不能cd
                {
                    CmdStrTool.ShowTips(2);
                }
            }

            return(null);
        }
コード例 #5
0
ファイル: CopyCommand.cs プロジェクト: AHappyFun/VirtualDisk
        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);
                if (paths.Length != 2) //参数不对
                {
                    CmdStrTool.ShowTips(1);
                }
                else
                {
                    bool cover = addPar == "/y";
                    bool isRealFile = paths[0].First() == '@';
                    Node n1, n2;
                    //n1为拷贝前结点,真路径就创建一个
                    if (isRealFile)  //真实源路径,
                    {
                        string[] namelist1 = CmdStrTool.SplitPathToNameList(paths[0]);
                        string   newname   = namelist1.Last(); //最后一个是name
                        n1 = disk.CreateNode(0, newname, disk.root);
                        n1 = RealDiskTool.Instance.CopyRealFileToNode(paths[0].Substring(1), n1, disk);
                    }
                    else
                    {
                        string[] namelist1 = CmdStrTool.SplitPathToNameList(paths[0]);
                        n1 = disk.NameListToNode(namelist1, IsSupportWildcard);
                    }
                    //n2为目标目录,需要存在
                    string[] namelist2 = CmdStrTool.SplitPathToNameList(paths[1]);
                    n2 = disk.NameListToNode(namelist2, IsSupportWildcard);

                    if (n1 == null || n2 == null || n2.nodeType != 1)
                    {
                        CmdStrTool.ShowTips(2);
                    }
                    else
                    {
                        if (isRealFile)
                        {
                            disk.MoveNode(n1, n2, cover);
                        }
                        else
                        {
                            disk.CopyNode(n1, n2, cover);
                        }
                    }
                }
            }
            return(null);
        }
コード例 #6
0
ファイル: Floder.cs プロジェクト: AHappyFun/VirtualDisk
        public Node GetChildByName(string name, bool isMatch = false)
        {
            //1.先对特殊名进行字符进行转换
            string tmp = string.Empty;

            if (name == ".")  //文件name不能是. / \ 这种
            {
                //name转换成当前结点
                return(disk.current);
            }
            else if (name == "..")
            {
                //name转换成上一节点
                Node f = this.parent;
                if (f != null)
                {
                    return(f);
                }
                else
                {
                    return(this);
                }
            }
            else
            {
                //如果是中间有空格的
                if (name.Contains("\""))
                {
                    name = name.Trim(new char[] { '"' }).Replace('_', ' ');
                }
                //通配符
                if (isMatch && name.Contains("*") || name.Contains("?"))
                {
                    var           rex   = CmdStrTool.WildCardToRegex(name);
                    List <string> names = new List <string>();
                    foreach (Node item in childs)
                    {
                        names.Add(item.name);
                    }
                    List <string> list = names.Where(ex => Regex.IsMatch(ex, rex, RegexOptions.IgnoreCase)).ToList();
                    if (list.Count > 0)
                    {
                        name = list[0];
                    }
                }
            }

            //2.find node
            foreach (Node node in childs)
            {
                if (node.name == name)
                {
                    return(node);
                }
            }
            return(null);
        }
コード例 #7
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);
                if (paths.Length != 2) //参数不对
                {
                    CmdStrTool.ShowTips(1);
                }
                else
                {
                    string[] namelist1 = CmdStrTool.SplitPathToNameList(paths[0]);
                    string   newname   = namelist1.Last(); //最后一个是新name
                    Node     n1;                           //n1为新建文件的父节点
                    if (namelist1.Length > 1)
                    {
                        string[] namelist = new string[namelist1.Length - 1];
                        Array.Copy(namelist1, namelist, namelist1.Length - 1);

                        n1 = disk.NameListToNode(namelist, IsSupportWildcard);
                    }
                    else
                    {
                        n1 = disk.current;
                    }

                    //n2为link的目标结点
                    string[] namelist2 = CmdStrTool.SplitPathToNameList(paths[1]);
                    Node     n2        = disk.NameListToNode(namelist2, IsSupportWildcard);
                    if (n1 == null || n2 == null)
                    {
                        CmdStrTool.ShowTips(2);
                    }
                    else
                    {
                        Symlink s = disk.CreateNode(2, newname, n1) as Symlink;
                        s.SetLinkTarget(n2);
                        Console.WriteLine("创建链接{0}--->{1}", s.GetPath(), n2.GetPath());
                    }
                }
            }


            return(null);
        }
コード例 #8
0
 public bool SerializeDisk(List <Node> nodes, string realPath)
 {
     try
     {
         FileStream      fs = new FileStream(realPath, FileMode.Create);
         BinaryFormatter bf = new BinaryFormatter();
         bf.Serialize(fs, nodes);
         fs.Close();
         return(true);
     }
     catch (Exception)
     {
         CmdStrTool.ShowTips(6);
         return(false);
     }
 }
コード例 #9
0
ファイル: MoveCommand.cs プロジェクト: AHappyFun/VirtualDisk
        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);
                if (paths.Length != 2) //参数不对
                {
                    CmdStrTool.ShowTips(1);
                }
                else
                {
                    bool cover = addPar == "/y";
                    Node n1, n2;
                    //n1为移动前结点,需要存在
                    string[] namelist1 = CmdStrTool.SplitPathToNameList(paths[0]);
                    n1 = disk.NameListToNode(namelist1, IsSupportWildcard);

                    //n2为目标目录,需要存在
                    string[] namelist2 = CmdStrTool.SplitPathToNameList(paths[1]);
                    n2 = disk.NameListToNode(namelist2, IsSupportWildcard);

                    if (n1 == null || n2 == null || n2.nodeType != 1)
                    {
                        CmdStrTool.ShowTips(2);
                    }
                    else
                    {
                        disk.MoveNode(n1, n2, cover);
                        Console.WriteLine("移动:{0}--->{1}", n1.name, n2.GetPath());
                    }
                }
            }
            return(null);
        }
コード例 #10
0
ファイル: LoadCommand.cs プロジェクト: AHappyFun/VirtualDisk
        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);
                if (paths.Length != 1) //多个路径命令不正确
                {
                    CmdStrTool.ShowTips(2);
                    return(null);
                }
                else
                {
                    if (paths[0].First() == '@')
                    {
                        string real    = paths[0].Substring(1);
                        Disk   newDisk = RealDiskTool.Instance.DeserializeDisk(real);
                        if (newDisk != null)
                        {
                            disk.Clear(); //先归一
                            //disk = newDisk;
                            return(newDisk);
                        }
                    }
                    else
                    {
                        CmdStrTool.ShowTips(2);
                        return(null);
                    }
                }
            }

            return(null);
        }
コード例 #11
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);
                if (paths.Length != 1) //多个路径命令不正确
                {
                    CmdStrTool.ShowTips(2);
                    return(null);
                }
                else
                {
                    if (paths[0].Contains("@"))
                    {
                        string real = paths[0].Substring(1);
                        if (disk.SaveToDisk(real))
                        {
                            Console.WriteLine("已保存虚拟磁盘文件至: " + real);
                        }
                    }
                    else
                    {
                        CmdStrTool.ShowTips(2);
                        return(null);
                    }
                }
            }

            return(null);
        }
コード例 #12
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);
                if (paths.Length != 2) //参数数不对
                {
                    CmdStrTool.ShowTips(1);
                }
                else
                {
                    string[] namelist = CmdStrTool.SplitPathToNameList(paths[0]);
                    Node     n        = disk.NameListToNode(namelist, IsSupportWildcard);
                    if (n != null)
                    {
                        RenameNode(n, paths[1]);
                    }
                    else
                    {
                        CmdStrTool.ShowTips(2);
                    }
                }
            }

            return(null);
        }
コード例 #13
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);
        }
コード例 #14
0
ファイル: DirCommand.cs プロジェクト: AHappyFun/VirtualDisk
        public override Disk Execute(Disk disk, string param)
        {
            string path   = "";
            string addPar = "";

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

            bool onlyDir   = addPar == "/ad";
            bool allChilds = addPar == "/s";

            if (string.IsNullOrEmpty(path))
            {
                if (onlyDir)
                {
                    ShowChildsFloderInfo(disk.current);
                }
                else if (allChilds)
                {
                    ShowNodeInfo(disk.current);
                }
                else
                {
                    ShowChildsInfo(disk.current);
                }
            }
            else
            {
                string[] paths = CmdStrTool.SplitPathParamToPathList(path);
                if (paths.Length == 1)   //必须一个路径
                {
                    string[] namelist = CmdStrTool.SplitPathToNameList(paths[0]);
                    Node     n        = disk.NameListToNode(namelist, IsSupportWildcard);
                    if (n == null)
                    {
                        CmdStrTool.ShowTips(2);
                        return(null);
                    }

                    if (allChilds)
                    {
                        ShowNodeInfo(n);
                    }
                    else if (onlyDir)
                    {
                        ShowChildsFloderInfo(n);
                    }
                    else
                    {
                        ShowChildsInfo(n);
                    }
                }
                else
                {
                    CmdStrTool.ShowTips(2);
                }
            }



            return(null);
        }
コード例 #15
0
ファイル: RdCommand.cs プロジェクト: AHappyFun/VirtualDisk
        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);
        }
コード例 #16
0
        public Disk ExecuteCmd(string cmdStr, Disk disk)
        {
            string   cmdParam = String.Empty;
            ICommand cmd      = null;

            //-------
            string[] ar  = cmdStr.ToLower().Trim().Split(new char[] { ' ' }, 2);
            string   tmp = ar[0];

            if (ar.Length > 1)
            {
                cmdParam = ar[1];
            }
            else
            {
                cmdParam = "";
            }

            if (string.IsNullOrEmpty(tmp) || tmp == "")
            {
                CmdStrTool.ShowTips(1);
                return(null);
            }
            else
            {
                switch (tmp)
                {
                case "dir":
                    cmd = new DirCommand();
                    break;

                case "md":
                    cmd = new MdCommand();
                    break;

                case "cd":
                    cmd = new CdCommand();
                    break;

                case "copy":
                    cmd = new CopyCommand();
                    break;

                case "del":
                    cmd = new DelCommand();
                    break;

                case "rd":
                    cmd = new RdCommand();
                    break;

                case "ren":
                    cmd = new RenCommand();
                    break;

                case "move":
                    cmd = new MoveCommand();
                    break;

                case "mklink":
                    cmd = new MklinkCommand();
                    break;

                case "save":
                    cmd = new SaveCommand();
                    break;

                case "load":
                    cmd = new LoadCommand();
                    break;

                case "cls":
                    cmd = new ClsCommand();
                    break;

                default:
                    CmdStrTool.ShowTips(1);
                    break;
                }
            }

            if (cmd != null)
            {
                return(cmd.Execute(disk, cmdParam));
            }
            else
            {
                return(null);
            }
        }