Exemplo n.º 1
0
        public static List <LogInfo> FileCreateBlank(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

            Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_FileCreateBlank));
            CodeInfo_FileCreateBlank info = cmd.Info as CodeInfo_FileCreateBlank;

            string filePath = StringEscaper.Preprocess(s, info.FilePath);

            Encoding encoding = Encoding.Default;

            if (info.Encoding != null)
            {
                encoding = info.Encoding;
            }

            if (File.Exists(filePath))
            {
                if (info.Preserve)
                {
                    logs.Add(new LogInfo(info.NoWarn ? LogState.Ignore : LogState.Warning, $"Cannot overwrite [{filePath}]", cmd));
                    return(logs);
                }
                else
                {
                    logs.Add(new LogInfo(info.NoWarn ? LogState.Ignore : LogState.Warning, $"[{filePath}] will be overwritten", cmd));
                }
            }

            if (StringEscaper.PathSecurityCheck(filePath, out string errorMsg) == false)
            {
                logs.Add(new LogInfo(LogState.Error, errorMsg));
                return(logs);
            }

            Directory.CreateDirectory(FileHelper.GetDirNameEx(filePath));
            FileHelper.WriteTextBOM(filePath, encoding);
            logs.Add(new LogInfo(LogState.Success, $"Created blank text file [{filePath}]", cmd));

            return(logs);
        }
Exemplo n.º 2
0
        public static List <LogInfo> FileCreateBlank(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

            CodeInfo_FileCreateBlank info = cmd.Info.Cast <CodeInfo_FileCreateBlank>();

            string filePath = StringEscaper.Preprocess(s, info.FilePath);

            Encoding encoding = EncodingHelper.DefaultAnsi;

            if (info.Encoding != null)
            {
                encoding = info.Encoding;
            }

            if (File.Exists(filePath))
            {
                if (info.Preserve)
                {
                    logs.Add(new LogInfo(info.NoWarn ? LogState.Ignore : LogState.Overwrite, $"[{filePath}] will not be overwritten", cmd));
                    return(logs);
                }

                logs.Add(new LogInfo(info.NoWarn ? LogState.Ignore : LogState.Overwrite, $"[{filePath}] will be overwritten", cmd));
            }

            if (!StringEscaper.PathSecurityCheck(filePath, out string errorMsg))
            {
                return(LogInfo.LogErrorMessage(logs, errorMsg));
            }

            Directory.CreateDirectory(FileHelper.GetDirNameEx(filePath));
            EncodingHelper.WriteTextBom(filePath, encoding);
            logs.Add(new LogInfo(LogState.Success, $"Created blank text file [{filePath}]", cmd));

            return(logs);
        }