Esempio n. 1
0
        public void Write(StringToFileWriterOptions options)
        {
            if (_myFile.Exists(options.Path))
            {
                if (!options.OverwriteIfFileExists)
                {
                    throw new EnvCryptException(
                              string.Format("file already exists at the path and you have asked it not to be overwritten: {0}", options.Path));
                }
            }

            // Create directory
            var directoryOfPath = Path.GetDirectoryName(options.Path);

            _myDirectory.CreateDirectory(directoryOfPath);

            // Write string to file
            _myFile.WriteAllText(options.Path, options.Contents, options.Encoding);
        }
        public void LogDecryption(TWorkflowOptions withWorkflowOptions, EnvCryptDat ecDat, IList <TKey> usingLoadedKeys,
                                  IList <EntriesDecrypterResult <TKey> > results)
        {
            try
            {
                _myDirectory.CreateDirectory(_config.LogDirectory);
            }
            catch
            {
                return;
            }

            var fileName = string.Format(_config.FileNameFormat,
                                         _myDateTime.UtcNow().ToString(DateTimeFormatInFileName),
                                         Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName));

            var    logFilePathWithoutUidOrExt = Path.Combine(_config.LogDirectory, fileName);
            var    logFilePathWithoutUid      = logFilePathWithoutUidOrExt + _config.LogFileExtension;
            string finalLogPath = null;

            /*
             * If original file exists then try to create a unique one.
             */
            if (_myFile.Exists(logFilePathWithoutUid))
            {
                if (_config.MaxTriesToGetUniqueFileName <= 0)
                {
                    return;
                }

                var foundUniqueFileName = false;
                for (int uid = 0; uid < _config.MaxTriesToGetUniqueFileName; uid++)
                {
                    finalLogPath = string.Format("{0}-{1}{2}", logFilePathWithoutUidOrExt, uid, _config.LogFileExtension);

                    if (!_myFile.Exists(finalLogPath))
                    {
                        foundUniqueFileName = true;
                        break;
                    }
                }
                if (!foundUniqueFileName)
                {
                    return;
                }
            }
            else
            {
                finalLogPath = logFilePathWithoutUid;
            }

            Contract.Assert(finalLogPath != null, "a potentially unique final log path must be found at this point");
            var content = GetLogContent(withWorkflowOptions, results);

            try
            {
                _myFile.WriteAllText(finalLogPath, content);
            }
            catch
            {
                return;
            }


            _oldLogCleaner.Run();
        }