CookedFilename() 공개 정적인 메소드

This is both for error checking and blessing of user-created filenames, and to tack on a filename extension if there is none present.


Returns a version of the filename in which it has had the file extension added unless the filename already has any sort of file extension, in which case nothing is changed about it. If every place where the auto- extension-appending is attempted is done via this method, then it will never end up adding an extension when an explicit one exists already.
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
예제 #1
0
파일: Archive.cs 프로젝트: jenden0/KOS
        /// <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);
        }
예제 #2
0
파일: Archive.cs 프로젝트: jonored/KOS
        /// <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);
        }
예제 #3
0
파일: Volume.cs 프로젝트: jenden0/KOS
        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);
        }
예제 #4
0
        /// <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);
        }
예제 #5
0
        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);
        }
예제 #6
0
파일: Harddisk.cs 프로젝트: jonored/KOS
 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);
 }
예제 #7
0
파일: Archive.cs 프로젝트: jenden0/KOS
        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);
            }
        }
예제 #8
0
파일: Archive.cs 프로젝트: jenden0/KOS
        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);
        }
예제 #9
0
파일: Archive.cs 프로젝트: jenden0/KOS
        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);
            }
        }