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

            InitializeSuffixes();
        }
Esempio n. 2
0
        public VolumeItem(Volume volume, VolumePath path)
        {
            Volume = volume;
            Path = path;

            InitializeSuffixes();
        }
Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 6
0
 public static GlobalPath FromVolumePath(VolumePath volumePath, int volumeId)
 {
     return new GlobalPath(volumeId, new List<string>(volumePath.Segments));
 }
Esempio n. 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;
        }
Esempio n. 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);
        }
Esempio n. 9
0
 public ArchiveFile(Archive archive, FileInfo fileInfo, VolumePath path)
     : base(archive, path)
 {
     this.fileInfo = fileInfo;
 }
Esempio n. 10
0
 private GlobalPath(object volumeId, VolumePath path) : this(volumeId, new List <string>(path.Segments))
 {
 }
Esempio n. 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;
        }
Esempio n. 12
0
 public abstract VolumeFile SaveFile(VolumePath path, FileContent content, bool verifyFreeSpace = true);
Esempio n. 13
0
 public ArchiveFile(Archive archive, FileInfo fileInfo, VolumePath path)
     : base(archive, path)
 {
     this.fileInfo = fileInfo;
 }
Esempio n. 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;
        }
Esempio n. 15
0
        public VolumeDirectory OpenOrCreateDirectory(VolumePath path)
        {
            VolumeDirectory directory = Open(path) as VolumeDirectory;

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

            return directory;
        }
Esempio n. 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);
Esempio n. 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;
        }
Esempio n. 18
0
 public static GlobalPath FromVolumePath(VolumePath volumePath, int volumeId)
 {
     return(new GlobalPath(volumeId, new List <string>(volumePath.Segments)));
 }
Esempio n. 19
0
 public HarddiskDirectory CreateDirectory(string name)
 {
     return(CreateDirectory(name, new HarddiskDirectory(Volume as Harddisk, VolumePath.FromString(name, Path))));
 }
Esempio n. 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);
        }
Esempio n. 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;
        }
Esempio n. 22
0
 public ArchiveDirectory(Archive archive, VolumePath path) : base(archive, path)
 {
     this.archive     = archive;
     this.archivePath = archive.GetArchivePath(path);
 }
Esempio n. 23
0
 public ArchiveDirectory(Archive archive, VolumePath path)
     : base(archive, path)
 {
     this.archive = archive;
     this.archivePath = archive.GetArchivePath(path);
 }
Esempio n. 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;
        }
Esempio n. 25
0
 public bool IsParent(VolumePath path)
 {
     return path.Segments.Count > Segments.Count && path.Segments.GetRange(0, Segments.Count).SequenceEqual(Segments);
 }
Esempio n. 26
0
 public bool IsParent(VolumePath path)
 {
     return(path.Segments.Count > Segments.Count && path.Segments.GetRange(0, Segments.Count).SequenceEqual(Segments));
 }
Esempio n. 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);
        }
Esempio n. 28
0
 public override bool Exists(VolumePath path, bool ksmDefault = false)
 {
     return(Search(path, ksmDefault) != null);
 }
Esempio n. 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");
     }
 }
Esempio n. 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;
        }
Esempio n. 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;
        }
Esempio n. 32
0
 public override bool Exists(VolumePath path, bool ksmDefault = false)
 {
     return Search(path, ksmDefault) != null;
 }
Esempio n. 33
0
 protected VolumeFile(Volume volume, VolumePath path)
     : base(volume, path)
 {
     InitializeSuffixes();
 }
Esempio n. 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;
        }
Esempio n. 35
0
 private GlobalPath(object volumeId, VolumePath path)
     : this(volumeId, new List<string>(path.Segments))
 {
 }
Esempio n. 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);
            }
        }
Esempio n. 37
0
 public PathValue FromPath(VolumePath volumePath, string volumeId)
 {
     return new PathValue(GlobalPath.FromVolumePath(volumePath, volumeId), sharedObjects);
 }
Esempio n. 38
0
 public HarddiskDirectory(Harddisk harddisk, VolumePath path) : base(harddisk, path)
 {
     items = new Dictionary <string, Structure>(StringComparer.InvariantCultureIgnoreCase);
 }