public static List <LogInfo> DirMove(EngineState s, CodeCommand cmd) { List <LogInfo> logs = new List <LogInfo>(); CodeInfo_DirMove info = cmd.Info.Cast <CodeInfo_DirMove>(); string srcDir = StringEscaper.Preprocess(s, info.SrcDir); string destPath = StringEscaper.Preprocess(s, info.DestPath); // Path Security Check if (!StringEscaper.PathSecurityCheck(destPath, out string errorMsg)) { return(LogInfo.LogErrorMessage(logs, errorMsg)); } // SrcPath must be directory // WB082 does not check this, so file can be moved with DirMove if (File.Exists(srcDir)) { return(LogInfo.LogErrorMessage(logs, $"[{srcDir}] is a file, not a directory")); } // DestPath must be directory if (File.Exists(destPath)) { return(LogInfo.LogErrorMessage(logs, $"[{destPath}] is a file, not a directory")); } if (Directory.Exists(destPath)) { string srcDirName = Path.GetFileName(srcDir); if (srcDirName == null) { throw new InternalException("Internal Logic Error at DirMove"); } string destFullPath = Path.Combine(destPath, srcDirName); Directory.Move(srcDir, destFullPath); logs.Add(new LogInfo(LogState.Success, $"Directory [{srcDir}] moved to [{destFullPath}]")); } else { Directory.Move(srcDir, destPath); logs.Add(new LogInfo(LogState.Success, $"Directory [{srcDir}] moved to [{destPath}]")); } return(logs); }
public static List <LogInfo> DirMove(EngineState s, CodeCommand cmd) { List <LogInfo> logs = new List <LogInfo>(); Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_DirMove)); CodeInfo_DirMove info = cmd.Info as CodeInfo_DirMove; string srcDir = StringEscaper.Preprocess(s, info.SrcDir); string destPath = StringEscaper.Preprocess(s, info.DestPath); // Path Security Check if (StringEscaper.PathSecurityCheck(destPath, out string errorMsg) == false) { logs.Add(new LogInfo(LogState.Error, errorMsg)); return(logs); } // SrcPath must be directory // WB082 does not check this, so file can be moved with DirMove if (File.Exists(srcDir)) { logs.Add(new LogInfo(LogState.Error, $"[{srcDir}] is a file, not a directory")); return(logs); } // DestPath must be directory if (File.Exists(destPath)) { logs.Add(new LogInfo(LogState.Error, $"[{destPath}] is a file, not a directory")); return(logs); } if (Directory.Exists(destPath)) { string destFullPath = Path.Combine(destPath, Path.GetFileName(srcDir)); Directory.Move(srcDir, destFullPath); logs.Add(new LogInfo(LogState.Success, $"Directory [{srcDir}] moved to [{destFullPath}]")); } else { Directory.Move(srcDir, destPath); logs.Add(new LogInfo(LogState.Success, $"Directory [{srcDir}] moved to [{destPath}]")); } return(logs); }