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 EnvCryptDat Load(DatFromFileLoaderOptions options) { if (!_myFile.Exists(options.DatFilePath)) { return(new EnvCryptDat() { Categories = new List <Category>() }); } var xmlFromFile = _xmlReader.ReadAllText(options.DatFilePath); if (string.IsNullOrWhiteSpace(xmlFromFile)) { throw new EnvCryptException("key file is empty: {0}", options.DatFilePath); } var xmlPoco = _xmlSerializationUtils.Deserialize(xmlFromFile); return(_xmlToPocoMapper.Map(xmlPoco)); }
public TKey Load(TLoadDetails keyFileDetails) { if (keyFileDetails == null || string.IsNullOrWhiteSpace(keyFileDetails.FilePath)) { throw new EnvCryptException("key file path cannot be empty"); } if (!_myFile.Exists(keyFileDetails.FilePath)) { throw new EnvCryptException("key file does not exist: {0}", keyFileDetails); } var xmlFromFile = _xmlReader.ReadAllText(keyFileDetails.FilePath); if (string.IsNullOrWhiteSpace(xmlFromFile)) { throw new EnvCryptException("key file is empty: {0}", keyFileDetails); } var xmlPoco = _xmlSerializationUtils.Deserialize(xmlFromFile); return(MapToKeyPoco(xmlPoco)); }
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(); }