Пример #1
0
        public virtual bool SaveFile(ProgramFile file)
        {
            SafeHouse.Logger.SuperVerbose("Volume: SaveFile: " + file.Filename);

            Add(file, true);
            return(true);
        }
Пример #2
0
        public virtual void AppendToFile(string name, byte[] bytesToAppend)
        {
            SafeHouse.Logger.SuperVerbose("Volume: AppendToFile: " + name);
            ProgramFile file = GetByName(name) ?? new ProgramFile(name);

            file.BinaryContent = new byte[file.BinaryContent.Length + bytesToAppend.Length];
            Array.Copy(bytesToAppend, 0, file.BinaryContent, file.BinaryContent.Length, bytesToAppend.Length);
            SaveFile(file);
        }
Пример #3
0
 public ProgramFile(ProgramFile copy)
 {
     Filename = copy.Filename;
     Category = copy.Category;
     if (Category == FileCategory.KSM)
         BinaryContent = copy.BinaryContent;
     else
         StringContent = copy.StringContent;
 }
Пример #4
0
        public virtual bool SaveObjectFile(string fileNameOut, List <CodePart> parts)
        {
            var newFile = new ProgramFile(fileNameOut)
            {
                BinaryContent = CompiledObject.Pack(parts)
            };

            return(SaveFile(newFile));
        }
Пример #5
0
        public override bool IsRoomFor(ProgramFile newFile)
        {
            int         usedSpace    = GetUsedSpace();
            ProgramFile existingFile = GetByName(newFile.Filename);

            if (existingFile != null)
            {
                usedSpace -= existingFile.GetSize();
            }

            return((Capacity - usedSpace) >= newFile.GetSize());
        }
Пример #6
0
        public override bool IsRoomFor(ProgramFile newFile)
        {
            int usedSpace = GetUsedSpace();
            ProgramFile existingFile = GetByName(newFile.Filename);

            if (existingFile != null)
            {
                usedSpace -= existingFile.GetSize();
            }

            return ((Capacity - usedSpace) >= newFile.GetSize());
        }
Пример #7
0
 public virtual void Add(ProgramFile file, bool withReplacement = false)
 {
     SafeHouse.Logger.SuperVerbose("Volume: Add: " + file.Filename);
     if (withReplacement)
     {
         files[file.Filename] = file;
     }
     else
     {
         files.Add(file.Filename, file);
     }
 }
Пример #8
0
        public virtual void AppendToFile(string name, string textToAppend)
        {
            SafeHouse.Logger.SuperVerbose("Volume: AppendToFile: " + name);
            ProgramFile file = GetByName(name) ?? new ProgramFile(name);

            if (file.StringContent.Length > 0 && !file.StringContent.EndsWith("\n"))
            {
                textToAppend = "\n" + textToAppend;
            }

            file.StringContent = file.StringContent + textToAppend;
            SaveFile(file);
        }
Пример #9
0
 public ProgramFile(ProgramFile copy)
 {
     Filename = copy.Filename;
     Category = copy.Category;
     if (Category == FileCategory.KSM)
     {
         BinaryContent = copy.BinaryContent;
     }
     else
     {
         StringContent = copy.StringContent;
     }
 }
Пример #10
0
        public virtual bool RenameFile(string name, string newName)
        {
            SafeHouse.Logger.SuperVerbose("Volume: RenameFile: From: " + name + " To: " + newName);
            ProgramFile file = GetByName(name);

            if (file != null)
            {
                DeleteByName(name);
                file.Filename = newName;
                Add(file);
                return(true);
            }
            return(false);
        }
Пример #11
0
        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;
        }
Пример #12
0
        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);
        }
Пример #13
0
        /// <summary>
        /// Get a file given its name
        /// </summary>
        /// <param name="name">filename to get.  if it has no filename extension, one will be guessed at, ".ks" usually.</param>
        /// <param name="ksmDefault">true if a filename of .ksm is preferred in contexts where the extension was left off.  The default is to prefer .ks</param>
        /// <returns>the file</returns>
        public override ProgramFile GetByName(string name, bool ksmDefault = false)
        {
            try
            {
                SafeHouse.Logger.Log("Archive: Getting File By Name: " + name);
                var fileInfo = FileSearch(name, ksmDefault);
                if (fileInfo == null)
                {
                    return(null);
                }

                using (var infile = new BinaryReader(File.Open(fileInfo.FullName, FileMode.Open)))
                {
                    byte[] fileBody = ProcessBinaryReader(infile);

                    var          retFile  = new ProgramFile(fileInfo.Name);
                    FileCategory whatKind = PersistenceUtilities.IdentifyCategory(fileBody);
                    if (whatKind == FileCategory.KSM)
                    {
                        retFile.BinaryContent = fileBody;
                    }
                    else
                    {
                        retFile.StringContent = System.Text.Encoding.UTF8.GetString(fileBody);
                    }

                    if (retFile.Category == FileCategory.ASCII || retFile.Category == FileCategory.KERBOSCRIPT)
                    {
                        retFile.StringContent = retFile.StringContent.Replace("\r\n", "\n");
                    }

                    base.Add(retFile, true);

                    return(retFile);
                }
            }
            catch (Exception e)
            {
                SafeHouse.Logger.Log(e);
                return(null);
            }
        }
Пример #14
0
        /// <summary>
        /// Get a file given its name
        /// </summary>
        /// <param name="name">filename to get.  if it has no filename extension, one will be guessed at, ".ks" usually.</param>
        /// <param name="ksmDefault">true if a filename of .ksm is preferred in contexts where the extension was left off.  The default is to prefer .ks</param>
        /// <returns>the file</returns>
        public override ProgramFile GetByName(string name, bool ksmDefault = false)
        {
            try
            {
                SafeHouse.Logger.Log("Archive: Getting File By Name: " + name);
                var fileInfo = FileSearch(name, ksmDefault);
                if (fileInfo == null)
                {
                    return null;
                }

                using (var infile = new BinaryReader(File.Open(fileInfo.FullName, FileMode.Open)))
                {
                    byte[] fileBody = ProcessBinaryReader(infile);

                    var retFile = new ProgramFile(fileInfo.Name);
                    FileCategory whatKind = PersistenceUtilities.IdentifyCategory(fileBody);
                    if (whatKind == FileCategory.KSM)
                        retFile.BinaryContent = fileBody;
                    else
                        retFile.StringContent = System.Text.Encoding.UTF8.GetString(fileBody);

                    if (retFile.Category == FileCategory.ASCII || retFile.Category == FileCategory.KERBOSCRIPT)
                        retFile.StringContent = retFile.StringContent.Replace("\r\n", "\n");

                    base.Add(retFile, true);

                    return retFile;
                }
            }
            catch (Exception e)
            {
                SafeHouse.Logger.Log(e);
                return null;
            }
        }
Пример #15
0
 public virtual bool SaveFile(ProgramFile file)
 {
     SafeHouse.Logger.SuperVerbose("Volume: SaveFile: " + file.Filename);
     
     Add(file, true);
     return true;
 }
Пример #16
0
 public override bool IsRoomFor(ProgramFile newFile)
 {
     return(true);
 }
Пример #17
0
 public override bool IsRoomFor(ProgramFile newFile)
 {
     return true;
 }
Пример #18
0
 public override bool SaveFile(ProgramFile file)
 {
     SafeHouse.Logger.Log("HardDisk: SaveFile: " + file.Filename);
     return(IsRoomFor(file) && base.SaveFile(file));
 }
Пример #19
0
        public void SaveContents()
        {
            var file = new ProgramFile(fileName)
            {
                StringContent = contents
            };

            if (! volume.SaveFile(file) )
            {
                // For some reason the normal trap that prints exceptions on
                // the terminal doesn't work here in this part of the code,
                // thus the two messages:
                term.Print("[File Save failed. Check space on device?]");
                throw new Exception( "File Save Failed from Text Editor.");
            }
            isDirty = false;
            term.Print("[Saved changes to " + fileName + "]");
        }
Пример #20
0
 public virtual bool IsRoomFor(ProgramFile newFile)
 {
     return(true);
 }
Пример #21
0
 public virtual bool SaveObjectFile(string fileNameOut, List<CodePart> parts)
 {
     var newFile = new ProgramFile(fileNameOut) {BinaryContent = CompiledObject.Pack(parts)};
     return SaveFile(newFile);
 }
Пример #22
0
 public override bool SaveFile(ProgramFile file)
 {
     SafeHouse.Logger.Log("HardDisk: SaveFile: " + file.Filename);
     return IsRoomFor(file) && base.SaveFile(file);
 }
Пример #23
0
 public virtual void Add(ProgramFile file, bool withReplacement = false)
 {
     SafeHouse.Logger.SuperVerbose("Volume: Add: " + file.Filename);
     if (withReplacement)
     {
         files[file.Filename] = file;
     }
     else
     {
         files.Add(file.Filename, file);
     }
 }
Пример #24
0
 public virtual bool IsRoomFor(ProgramFile newFile) { return true; }