Represents the location of a directory or a file inside a volume.
Exemple #1
0
        public VolumeItem(Volume volume, VolumePath parentPath, String name)
        {
            Volume = volume;
            Path = VolumePath.FromString(name, parentPath);

            InitializeSuffixes();
        }
Exemple #2
0
        public VolumeItem(Volume volume, VolumePath path)
        {
            Volume = volume;
            Path = path;

            InitializeSuffixes();
        }
Exemple #3
0
        public override bool Delete(VolumePath path, bool ksmDefault = false)
        {
            if (path.Depth == 0)
            {
                throw new KOSPersistenceException("Can't delete root directory");
            }

            HarddiskDirectory directory = ParentDirectoryForPath(path);

            return directory.Delete(path.Name, ksmDefault);
        }
Exemple #4
0
        public override VolumeFile CreateFile(VolumePath path)
        {
            if (path.Depth == 0)
            {
                throw new KOSPersistenceException("Can't create a file over root directory");
            }

            HarddiskDirectory directory = ParentDirectoryForPath(path, true);

            return directory.CreateFile(path.Name);
        }
Exemple #5
0
        public override bool Exists(VolumePath path, bool ksmDefault = false)
        {
            if (path.Depth == 0)
            {
                return true;
            }

            HarddiskDirectory directory = ParentDirectoryForPath(path);

            if (directory == null)
            {
                return false;
            }

            return directory.Exists(path.Name, ksmDefault);
        }
Exemple #6
0
 public static GlobalPath FromVolumePath(VolumePath volumePath, int volumeId)
 {
     return new GlobalPath(volumeId, new List<string>(volumePath.Segments));
 }
Exemple #7
0
        public bool IsRoomFor(VolumePath path, FileContent fileContent)
        {
            VolumeItem existing = Open(path);

            if (existing is VolumeDirectory)
            {
                throw new KOSPersistenceException("'" + path + "' is a directory");
            }

            VolumeFile existingFile = existing as VolumeFile;

            int usedByThisFile = 0;

            if (existingFile != null)
            {
                usedByThisFile = existingFile.ReadAll().Size;
            }

            return INFINITE_CAPACITY == FreeSpace || FreeSpace + usedByThisFile >= fileContent.Size;
        }
Exemple #8
0
        public override VolumeDirectory CreateDirectory(VolumePath path)
        {
            string archivePath = GetArchivePath(path);

            if (Directory.Exists(archivePath))
            {
                throw new KOSPersistenceException("Already exists: " + path);
            }

            try
            {
                Directory.CreateDirectory(archivePath);
            }
            catch (IOException)
            {
                throw new KOSPersistenceException("Could not create directory: " + path);
            }

            return new ArchiveDirectory(this, path);
        }
Exemple #9
0
 public ArchiveFile(Archive archive, FileInfo fileInfo, VolumePath path)
     : base(archive, path)
 {
     this.fileInfo = fileInfo;
 }
Exemple #10
0
 private GlobalPath(object volumeId, VolumePath path) : this(volumeId, new List <string>(path.Segments))
 {
 }
Exemple #11
0
        public override VolumeFile SaveFile(VolumePath path, FileContent content, bool verifyFreeSpace = true)
        {
            Directory.CreateDirectory(ArchiveFolder);

            string archivePath = GetArchivePath(path);
            if (Directory.Exists(archivePath))
            {
                throw new KOSPersistenceException("Can't save file over a directory: " + path);
            }

            string parentPath = Directory.GetParent(archivePath).FullName;

            if (!Directory.Exists(parentPath))
            {
                Directory.CreateDirectory(parentPath);
            }

            byte[] fileBody = ConvertToWindowsNewlines(content.Bytes);

            using (var outfile = new BinaryWriter(File.Open(archivePath, FileMode.Create)))
            {
                outfile.Write(fileBody);
            }

            return Open(path) as VolumeFile;
        }
Exemple #12
0
 public abstract VolumeFile SaveFile(VolumePath path, FileContent content, bool verifyFreeSpace = true);
Exemple #13
0
 public ArchiveFile(Archive archive, FileInfo fileInfo, VolumePath path)
     : base(archive, path)
 {
     this.fileInfo = fileInfo;
 }
Exemple #14
0
        public VolumeFile OpenOrCreateFile(VolumePath path, bool ksmDefault = false)
        {
            VolumeFile file = Open(path, ksmDefault) as VolumeFile;

            if (file == null)
            {
                file = CreateFile(path);
            }

            return file;
        }
Exemple #15
0
        public VolumeDirectory OpenOrCreateDirectory(VolumePath path)
        {
            VolumeDirectory directory = Open(path) as VolumeDirectory;

            if (directory == null)
            {
                directory = CreateDirectory(path);
            }

            return directory;
        }
Exemple #16
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">in the scenario where there is no filename extension, do we prefer the .ksm over the .ks?  The default is to prefer .ks</param>
 /// <returns>VolumeFile or VolumeDirectory. Null if not found.</returns>
 public abstract VolumeItem Open(VolumePath path, bool ksmDefault = false);
Exemple #17
0
        private static HarddiskDirectory ToHarddiskDirectory(this ConfigNode configNode, Harddisk harddisk, VolumePath path)
        {
            HarddiskDirectory directory = new HarddiskDirectory(harddisk, path);

            foreach (ConfigNode fileNode in configNode.GetNodes("file"))
            {
                directory.CreateFile(fileNode.GetValue(FilenameValueString), fileNode.ToHarddiskFile(harddisk, directory));
            }

            foreach (ConfigNode dirNode in configNode.GetNodes("directory"))
            {
                string dirName = dirNode.GetValue(DirnameValueString);

                directory.CreateDirectory(dirName, dirNode.ToHarddiskDirectory(harddisk, VolumePath.FromString(dirName, path)));
            }

            return directory;
        }
Exemple #18
0
 public static GlobalPath FromVolumePath(VolumePath volumePath, int volumeId)
 {
     return(new GlobalPath(volumeId, new List <string>(volumePath.Segments)));
 }
Exemple #19
0
 public HarddiskDirectory CreateDirectory(string name)
 {
     return(CreateDirectory(name, new HarddiskDirectory(Volume as Harddisk, VolumePath.FromString(name, Path))));
 }
Exemple #20
0
        public static VolumePath FromString(string pathString, VolumePath basePath)
        {
            if (IsAbsolute(pathString))
            {
                throw new KOSInvalidPathException("Relative path expected", pathString);
            }

            List<string> mergedSegments = new List<string>();
            mergedSegments.AddRange(basePath.Segments);
            mergedSegments.AddRange(GetSegmentsFromString(pathString));

            return new VolumePath(mergedSegments);
        }
Exemple #21
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;
        }
Exemple #22
0
 public ArchiveDirectory(Archive archive, VolumePath path) : base(archive, path)
 {
     this.archive     = archive;
     this.archivePath = archive.GetArchivePath(path);
 }
Exemple #23
0
 public ArchiveDirectory(Archive archive, VolumePath path)
     : base(archive, path)
 {
     this.archive = archive;
     this.archivePath = archive.GetArchivePath(path);
 }
Exemple #24
0
        public override VolumeFile SaveFile(VolumePath path, FileContent content, bool verifyFreeSpace = true)
        {
            try
            {
                if (verifyFreeSpace && !IsRoomFor(path, content))
                {
                    return null;
                }
            }
            catch (KOSPersistenceException)
            {
                throw new KOSPersistenceException("Can't save file over a directory: " + path);
            }

            HarddiskDirectory directory = ParentDirectoryForPath(path, true);

            return directory.Save(path.Name, content) as VolumeFile;
        }
Exemple #25
0
 public bool IsParent(VolumePath path)
 {
     return path.Segments.Count > Segments.Count && path.Segments.GetRange(0, Segments.Count).SequenceEqual(Segments);
 }
 public bool IsParent(VolumePath path)
 {
     return(path.Segments.Count > Segments.Count && path.Segments.GetRange(0, Segments.Count).SequenceEqual(Segments));
 }
Exemple #27
0
        public override VolumeItem Open(VolumePath path, bool ksmDefault = false)
        {
            if (path.Depth == 0) {
                return Root;
            }

            HarddiskDirectory directory = ParentDirectoryForPath(path);

            return directory == null ? null : directory.Open(path.Name, ksmDefault);
        }
Exemple #28
0
 public override bool Exists(VolumePath path, bool ksmDefault = false)
 {
     return(Search(path, ksmDefault) != null);
 }
Exemple #29
0
 private HarddiskDirectory ParentDirectoryForPath(VolumePath path, bool create = false)
 {
     HarddiskDirectory directory = RootHarddiskDirectory;
     if (path.Depth > 0)
     {
         return RootHarddiskDirectory.GetSubdirectory(path.GetParent(), create);
     }
     else
     {
         throw new Exception("This directory does not have a parent");
     }
 }
Exemple #30
0
        public override VolumeFile CreateFile(VolumePath path)
        {
            if (path.Depth == 0)
            {
                throw new KOSPersistenceException("Can't create a file over root directory");
            }

            string archivePath = GetArchivePath(path);

            if (File.Exists(archivePath))
            {
                throw new KOSPersistenceException("Already exists: " + path);
            }

            try
            {
                Directory.CreateDirectory(GetArchivePath(path.GetParent()));
            }
            catch (IOException)
            {
                throw new KOSPersistenceException("Parent directory for path does not exist: " + path.ToString());
            }

            try
            {
                File.Create(archivePath).Dispose();
            }
            catch (UnauthorizedAccessException)
            {
                throw new KOSPersistenceException("Could not create file: " + path);
            }

            return Open(path) as VolumeFile;
        }
Exemple #31
0
        public override bool Delete(VolumePath path, bool ksmDefault = false)
        {
            if (path.Depth == 0)
            {
                throw new KOSPersistenceException("Can't delete root directory");
            }

            var fileSystemInfo = Search(path, ksmDefault);

            if (fileSystemInfo == null)
            {
                return false;
            }
            else if (fileSystemInfo is FileInfo)
            {
                File.Delete(fileSystemInfo.FullName);
            }
            else
            {
                Directory.Delete(fileSystemInfo.FullName, true);
            }

            return true;
        }
Exemple #32
0
 public override bool Exists(VolumePath path, bool ksmDefault = false)
 {
     return Search(path, ksmDefault) != null;
 }
Exemple #33
0
 protected VolumeFile(Volume volume, VolumePath path)
     : base(volume, path)
 {
     InitializeSuffixes();
 }
Exemple #34
0
        public string GetArchivePath(VolumePath path)
        {
            if (path.PointsOutside)
            {
                throw new KOSInvalidPathException("Path refers to parent directory", path.ToString());
            }

            string mergedPath = ArchiveFolder;

            foreach (string segment in path.Segments)
            {
                mergedPath = Path.Combine(mergedPath, segment);
            }

            return mergedPath;
        }
Exemple #35
0
 private GlobalPath(object volumeId, VolumePath path)
     : this(volumeId, new List<string>(path.Segments))
 {
 }
Exemple #36
0
        public override VolumeItem Open(VolumePath path, bool ksmDefault = false)
        {
            try
            {
                var fileSystemInfo = Search(path, ksmDefault);

                if (fileSystemInfo == null) {
                    return null;
                }
                else if (fileSystemInfo is FileInfo)
                {
                    VolumePath filePath = VolumePath.FromString(fileSystemInfo.FullName.Substring(ArchiveFolder.Length).Replace(Path.DirectorySeparatorChar, VolumePath.PathSeparator));
                    return new ArchiveFile(this, fileSystemInfo as FileInfo, filePath);
                }
                else {
                    // we can use 'path' here, default extensions are not added to directories
                    return new ArchiveDirectory(this, path);
                }
            }
            catch (Exception e)
            {
                throw new KOSPersistenceException("Could not open path: " + path, e);
            }
        }
Exemple #37
0
 public PathValue FromPath(VolumePath volumePath, string volumeId)
 {
     return new PathValue(GlobalPath.FromVolumePath(volumePath, volumeId), sharedObjects);
 }
Exemple #38
0
 public HarddiskDirectory(Harddisk harddisk, VolumePath path) : base(harddisk, path)
 {
     items = new Dictionary <string, Structure>(StringComparer.InvariantCultureIgnoreCase);
 }