public static CookedFilename ( string fileName, string extensionName, bool trusted = false ) : string | ||
fileName | string | Filename to maybe change. Can be full path or just the filename. |
extensionName | string | Extension to add if there is none already. |
trusted | bool | True if the filename is internally generated (and therefore allowed to /// have paths in it). False if the filename is from a user-land string (and therefore allowing /// a name that walks the directory tree is a security hole.) |
Результат | string |
/// <summary> /// Get the file from the OS. /// </summary> /// <param name="name">filename to look for</param> /// <param name="ksmDefault">if true, it prefers to use the KSM filename over the KS. The default is to prefer KS.</param> /// <returns>the full fileinfo of the filename if found</returns> private System.IO.FileInfo FileSearch(string name, bool ksmDefault = false) { var path = ArchiveFolder + name; if (Path.HasExtension(path)) { return(File.Exists(path) ? new System.IO.FileInfo(path) : null); } var kerboscriptFile = new System.IO.FileInfo(PersistenceUtilities.CookedFilename(path, KERBOSCRIPT_EXTENSION, true)); var kosMlFile = new System.IO.FileInfo(PersistenceUtilities.CookedFilename(path, KOS_MACHINELANGUAGE_EXTENSION, true)); if (kerboscriptFile.Exists && kosMlFile.Exists) { return(ksmDefault ? kosMlFile : kerboscriptFile); } if (kerboscriptFile.Exists) { return(kerboscriptFile); } if (kosMlFile.Exists) { return(kosMlFile); } return(null); }
/// <summary> /// Get the file from the OS. /// </summary> /// <param name="name">filename to look for</param> /// <param name="ksmDefault">if true, it prefers to use the KSM filename over the KS. The default is to prefer KS.</param> /// <returns>the full fileinfo of the filename if found</returns> private FileInfo FileSearch(string name, bool ksmDefault = false) { var path = Path.Combine(ArchiveFolder, name); if (File.Exists(path)) { return(new FileInfo(path)); } var kerboscriptFile = new FileInfo(PersistenceUtilities.CookedFilename(path, KERBOSCRIPT_EXTENSION, true)); var kosMlFile = new FileInfo(PersistenceUtilities.CookedFilename(path, KOS_MACHINELANGUAGE_EXTENSION, true)); if (kerboscriptFile.Exists && kosMlFile.Exists) { return(ksmDefault ? kosMlFile : kerboscriptFile); } if (kerboscriptFile.Exists) { return(kerboscriptFile); } if (kosMlFile.Exists) { return(kosMlFile); } return(null); }
private ProgramFile FileSearch(string name, bool ksmDefault = false) { SafeHouse.Logger.SuperVerbose("Volume: FileSearch: " + files.Count); var kerboscriptFilename = PersistenceUtilities.CookedFilename(name, KERBOSCRIPT_EXTENSION, true); var kosMlFilename = PersistenceUtilities.CookedFilename(name, KOS_MACHINELANGUAGE_EXTENSION, true); ProgramFile kerboscriptFile; ProgramFile kosMlFile; bool kerboscriptFileExists = files.TryGetValue(kerboscriptFilename, out kerboscriptFile); bool kosMlFileExists = files.TryGetValue(kosMlFilename, out kosMlFile); if (kerboscriptFileExists && kosMlFileExists) { return(ksmDefault ? kosMlFile : kerboscriptFile); } if (kerboscriptFile != null) { return(kerboscriptFile); } if (kosMlFile != null) { return(kosMlFile); } return(null); }
/// <summary> /// Get the file from the OS. /// </summary> /// <param name="name">filename to look for</param> /// <param name="ksmDefault">if true, it prefers to use the KSM filename over the KS. The default is to prefer KS.</param> /// <returns>the full fileinfo of the filename if found</returns> private FileSystemInfo Search(VolumePath volumePath, bool ksmDefault) { var path = GetArchivePath(volumePath); if (Directory.Exists(path)) { return(new DirectoryInfo(path)); } if (File.Exists(path)) { return(new FileInfo(path)); } var kerboscriptFile = new FileInfo(PersistenceUtilities.CookedFilename(path, KERBOSCRIPT_EXTENSION, true)); var kosMlFile = new FileInfo(PersistenceUtilities.CookedFilename(path, KOS_MACHINELANGUAGE_EXTENSION, true)); if (kerboscriptFile.Exists && kosMlFile.Exists) { return(ksmDefault ? kosMlFile : kerboscriptFile); } if (kerboscriptFile.Exists) { return(kerboscriptFile); } if (kosMlFile.Exists) { return(kosMlFile); } return(null); }
private VolumeItem Search(string name, bool ksmDefault = false) { object item = items.ContainsKey(name) ? items[name] : null; if (item is FileContent) { return(new HarddiskFile(this, name)); } else if (item is HarddiskDirectory) { return(item as HarddiskDirectory); } else { var kerboscriptFilename = PersistenceUtilities.CookedFilename(name, Volume.KERBOSCRIPT_EXTENSION, true); var kosMlFilename = PersistenceUtilities.CookedFilename(name, Volume.KOS_MACHINELANGUAGE_EXTENSION, true); bool kerboscriptFileExists = items.ContainsKey(kerboscriptFilename) && items[kerboscriptFilename] is FileContent; bool kosMlFileExists = items.ContainsKey(kosMlFilename) && items[kosMlFilename] is FileContent; if (kerboscriptFileExists && kosMlFileExists) { return(ksmDefault ? new HarddiskFile(this, kosMlFilename) : new HarddiskFile(this, kerboscriptFilename)); } if (kerboscriptFileExists) { return(new HarddiskFile(this, kerboscriptFilename)); } if (kosMlFileExists) { return(new HarddiskFile(this, kosMlFilename)); } } return(null); }
private VolumeFile FileSearch(string name, bool ksmDefault = false) { if (files.ContainsKey(name)) { return(new HarddiskFile(this, name)); } else { var kerboscriptFilename = PersistenceUtilities.CookedFilename(name, KERBOSCRIPT_EXTENSION, true); var kosMlFilename = PersistenceUtilities.CookedFilename(name, KOS_MACHINELANGUAGE_EXTENSION, true); bool kerboscriptFileExists = files.ContainsKey(kerboscriptFilename); bool kosMlFileExists = files.ContainsKey(kosMlFilename); if (kerboscriptFileExists && kosMlFileExists) { if (ksmDefault) { return(new HarddiskFile(this, kosMlFilename)); } else { return(new HarddiskFile(this, kerboscriptFilename)); } } if (kerboscriptFileExists) { return(new HarddiskFile(this, kerboscriptFilename)); } if (kosMlFileExists) { return(new HarddiskFile(this, kosMlFilename)); } } return(null); }
public override void AppendToFile(string name, byte[] bytesToAppend) { SafeHouse.Logger.SuperVerbose("Archive: AppendToFile: " + name); System.IO.FileInfo info = FileSearch(name); string fullPath = info == null?string.Format("{0}{1}", ArchiveFolder, PersistenceUtilities.CookedFilename(name, KERBOSCRIPT_EXTENSION, true)) : info.FullName; // Deliberately not catching potential I/O exceptions from this, so they will percolate upward and be seen by the user: using (var outfile = new BinaryWriter(File.Open(fullPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))) { outfile.Write(bytesToAppend); } }
public override bool SaveFile(ProgramFile file) { base.SaveFile(file); Directory.CreateDirectory(ArchiveFolder); try { SafeHouse.Logger.Log("Archive: Saving File Name: " + file.Filename); byte[] fileBody; string fileExtension; switch (file.Category) { case FileCategory.OTHER: case FileCategory.TOOSHORT: case FileCategory.ASCII: case FileCategory.KERBOSCRIPT: string tempString = file.StringContent; if (SafeHouse.IsWindows) { // Only evil windows gets evil windows line breaks, and only if this is some sort of ASCII: tempString = tempString.Replace("\n", "\r\n"); } fileBody = System.Text.Encoding.UTF8.GetBytes(tempString.ToCharArray()); fileExtension = KERBOSCRIPT_EXTENSION; break; case FileCategory.KSM: fileBody = file.BinaryContent; fileExtension = KOS_MACHINELANGUAGE_EXTENSION; break; default: throw new ArgumentOutOfRangeException(); } var fileName = string.Format("{0}{1}", ArchiveFolder, PersistenceUtilities.CookedFilename(file.Filename, fileExtension, true)); using (var outfile = new BinaryWriter(File.Open(fileName, FileMode.Create))) { outfile.Write(fileBody); } } catch (Exception e) { SafeHouse.Logger.Log(e); return(false); } return(true); }
public override void AppendToFile(string name, string textToAppend) { SafeHouse.Logger.SuperVerbose("Archive: AppendToFile: " + name); System.IO.FileInfo info = FileSearch(name); string fullPath = info == null?string.Format("{0}{1}", ArchiveFolder, PersistenceUtilities.CookedFilename(name, KERBOSCRIPT_EXTENSION, true)) : info.FullName; // Using binary writer so we can bypass the OS behavior about ASCII end-of-lines and always use \n's no matter the OS: // Deliberately not catching potential I/O exceptions from this, so they will percolate upward and be seen by the user: using (var outfile = new BinaryWriter(File.Open(fullPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))) { byte[] binaryLine = System.Text.Encoding.UTF8.GetBytes((textToAppend + "\n").ToCharArray()); outfile.Write(binaryLine); } }