public void TestWriteAndPareseFolderBlock()
        {
            var options = TestHelper.CreateFileSystemOptions("");
            var b = new BlockParser(options);

            const string name = "blubα";

            var f1 = new Folder(name);

            var bb = b.NodeToBytes(f1);
            var f2 = b.BytesToNode(bb);

            Assert.AreEqual(name, f2.Name);
            Assert.IsInstanceOfType(f2, typeof(Folder));
        }
Exemple #2
0
        public bool Exists(Folder folder, string name)
        {
            CheckDisposed();

            return GetBlockList(folder).Exists(name);
        }
 public IIndexNode Find(Folder folder, string name)
 {
     _lock.EnterUpgradeableReadLock();
     try
     {
         return _fileSystem.Find(folder, name);
     }
     finally
     {
         _lock.ExitUpgradeableReadLock();
     }
 }
 public void Import(string source, Folder destination, string name, CallbacksBase importCallbacks)
 {
     _lock.EnterWriteLock();
     try
     {
         _fileSystem.Import(source, destination, name, importCallbacks);
     }
     finally
     {
         _lock.ExitWriteLock();
     }
 }
Exemple #5
0
        /// <summary>
        /// Copies the toCopy index node, and replaces the toReplace node with the replacement
        /// </summary>
        /// <param name="toCopy">To index node to copy.</param>
        /// <param name="toReplace">To node to be replaced. Can be set to null if only a node should be appended and no one should be replaced.</param>
        /// <param name="replacement">The node to replace the node toReplace. Can be set to null for the delete action.</param>
        /// <param name="newVersion"></param>
        /// <returns></returns>
        public Folder CopyReplacingReference(Folder toCopy, IIndexNode toReplace, IIndexNode replacement, long newVersion)
        {
            var toReplaceNr = toReplace == null ? 0 : toReplace.BlockNumber;
            var replacementNr = replacement == null ? 0 : replacement.BlockNumber;

            var newFolder = new Folder(toCopy.Name)
                                   {
                                       //BlocksCount = newBlocksCount,
                                       PredecessorBlockNr = toCopy.BlockNumber, //toReplaceNr,
                                       BlockNumber = _blockAllocation.Allocate(),
                                       Version = newVersion
                                   };
            _persistence.Persist(newFolder);

            var b = new BlockList(newFolder, _blockAllocation, _options, _blockParser, _blockManipulator, _persistence);

            // This algorithm section can be improved. We don't have to copy all references, we only have to copy the references that are different.
            foreach (var reference in AsEnumerable())
            {
                var blockNumber = reference.BlockNumber == toReplaceNr ? replacementNr : reference.BlockNumber;
                if (blockNumber != 0) b.AddReference(blockNumber);
            }
            if (toReplace == null && replacement != null) b.AddReference(replacementNr);

            if (replacement != null) replacement.Parent = newFolder;

            return newFolder;
        }
 public Folder CreateFolder(Folder parentFolder, string name)
 {
     _lock.EnterWriteLock();
     try
     {
         return _fileSystem.CreateFolder(parentFolder, name);
     }
     finally
     {
         _lock.ExitWriteLock();
     }
 }
Exemple #7
0
        private Folder ArchiveAndReplaceRoot(Folder folderToCopy, IIndexNode nodeToReplace, IIndexNode nodeReplacement)
        {
            if (folderToCopy == null) throw new VFSException("Node cannot be null");

            var toCopy = folderToCopy;
            var toReplace = nodeToReplace;
            var replacement = nodeReplacement;

            Folder copyOfFolderToCopy = null;
            Folder previous = null;

            while (toCopy != null)
            {
                var newFolder = GetBlockList(toCopy).CopyReplacingReference(toCopy, toReplace, replacement, NextVersion);
                if (copyOfFolderToCopy == null) copyOfFolderToCopy = newFolder;

                if (previous != null)
                {
                    previous.Parent = newFolder;
                    _persistence.Persist(previous);
                }

                toReplace = toCopy;
                toCopy = toCopy.Parent;
                replacement = newFolder;

                previous = newFolder;
            }

            Debug.Assert(previous != null, "previous != null");
            Debug.Assert(string.IsNullOrEmpty(previous.Name), "previous.Name == \"\"");

            // previous is now the new root node!
            previous.IsRoot = true;
            _persistence.Persist(previous);
            ResetRoot(previous);

            return copyOfFolderToCopy;
        }
Exemple #8
0
        private void ResetRoot(Folder newRoot)
        {
            newRoot.Version = NextVersion;

            Root = newRoot;
            Root.IsRoot = true;
            LatestRoot = Root;
            Root.BlocksUsed = _blockAllocation.CurrentMax;
            _persistence.Persist(Root);
            _options.RootBlockNr = Root.BlockNumber;
            WriteConfig();
        }
Exemple #9
0
        public void Import(string source, Folder destination, string name, CallbacksBase importCallbacks)
        {
            CheckDisposed();
            CheckName(name);
            CheckVersion();

            destination = ArchiveAndReplaceRoot(destination, null, null);

            if (Directory.Exists(source)) CollectImportDirectoryTotals(source, importCallbacks);
            else if (File.Exists(source)) importCallbacks.TotalToProcess++;
            else throw new NotFoundException();

            if (Directory.Exists(source)) ImportDirectory(source, destination, name, importCallbacks, false);
            else if (File.Exists(source)) ImportFile(source, destination, name, importCallbacks);
            else throw new NotFoundException();

            SetBlocksUsed();

            importCallbacks.OperationCompleted(!importCallbacks.ShouldAbort());
        }
Exemple #10
0
 public IEnumerable<IIndexNode> List(Folder folder)
 {
     CheckDisposed();
     return GetBlockList(folder).AsEnumerable().ToList();
 }
Exemple #11
0
 public IEnumerable<Folder> Folders(Folder folder)
 {
     return List(folder).OfType<Folder>().ToList();
 }
Exemple #12
0
        public IIndexNode Find(Folder folder, string name)
        {
            CheckDisposed();

            return GetBlockList(folder).Find(name);
        }
Exemple #13
0
 public IEnumerable<VFSFile> Files(Folder folder)
 {
     return List(folder).OfType<VFSFile>().ToList();
 }
Exemple #14
0
        private void ImportFile(string source, Folder destination, string name, CallbacksBase importCallbacks)
        {
            if (importCallbacks.ShouldAbort()) return;

            var f = new FileInfo(source);
            if (f.Length > _options.MaximumFileSize)
                throw new VFSException(
                    string.Format("File is too big. Maximum file size is {0}. You can adjust the BlockSize in the Options to allow bigger files.",
                    _options.MaximumFileSize));

            var file = CreateFile(source, destination, name);
            AppendBlockReference(destination, file.BlockNumber);

            importCallbacks.CurrentlyProcessed++;
        }
Exemple #15
0
        private void CollectExportDirectoryTotals(Folder source, CallbacksBase exportCallbacks)
        {
            // Direcotry
            exportCallbacks.TotalToProcess++;

            // File
            exportCallbacks.TotalToProcess += Files(source).Count();

            foreach (var folder in Folders(source))
            {
                CollectExportDirectoryTotals(folder, exportCallbacks);
            }
        }
Exemple #16
0
        private Folder ImportRootFolder()
        {
            var folder = _blockParser.ParseFolder(_blockManipulator.ReadBlock(_options.RootBlockNr));
            if (folder != null)
            {
                folder.BlockNumber = _options.RootBlockNr;
                return folder;
            }

            var root = new Folder { IsRoot = true };
            _blockManipulator.WriteBlock(root.BlockNumber, _blockParser.NodeToBytes(root));

            return root;
        }
Exemple #17
0
        private void CopyFile(VFSFile fileToCopy, Folder destination, string name, CallbacksBase copyCallbacks)
        {
            if (copyCallbacks.ShouldAbort()) return;

            CheckName(name);

            var newFile = new VFSFile(name)
                           {
                               Parent = destination,
                               BlockNumber = _blockAllocation.Allocate(),
                               LastBlockSize = fileToCopy.LastBlockSize,
                               IndirectNodeNumber = fileToCopy.IndirectNodeNumber,
                               BlocksCount = fileToCopy.BlocksCount,
                               PredecessorBlockNr = fileToCopy.BlockNumber,
                               Version = NextVersion
                           };

            _persistence.Persist(newFile);
            copyCallbacks.CurrentlyProcessed++;

            ArchiveAndReplaceRoot(destination, null, newFile);
        }
Exemple #18
0
        public void Copy(IIndexNode nodeToCopy, Folder destination, string name, CallbacksBase copyCallbacks)
        {
            CheckDisposed();
            CheckName(name);
            CheckVersion();

            // Gather totals (copy in ~O(1) :D)
            copyCallbacks.TotalToProcess++;

            // Do the real copy
            if (nodeToCopy is Folder) CopyFolder(nodeToCopy as Folder, destination, name, copyCallbacks);
            else if (nodeToCopy is VFSFile) CopyFile(nodeToCopy as VFSFile, destination, name, copyCallbacks);
            else throw new ArgumentException("nodeToCopy must be of type Folder or VFSFile", "nodeToCopy");

            SetBlocksUsed();

            copyCallbacks.OperationCompleted(!copyCallbacks.ShouldAbort());
        }
Exemple #19
0
        private void CopyFolder(Folder folderToCopy, Folder destination, string name, CallbacksBase copyCallbacks)
        {
            if (copyCallbacks.ShouldAbort()) return;

            CheckName(name);

            var copiedFolder = CreateFolder(destination, name, true);
            copiedFolder.IndirectNodeNumber = folderToCopy.IndirectNodeNumber;
            copiedFolder.BlocksCount = folderToCopy.BlocksCount;
            copiedFolder.PredecessorBlockNr = folderToCopy.BlockNumber;
            //copiedFolder.Version = NextVersion;
            _persistence.Persist(copiedFolder);

            //ArchiveAndReplaceRoot(copiedFolder.Parent, null, copiedFolder);
            copyCallbacks.CurrentlyProcessed++;
        }
 public void Copy(IIndexNode nodeToCopy, Folder destination, string nameOfCopiedElement, CallbacksBase copyCallbacks)
 {
     _lock.EnterWriteLock();
     try
     {
         _fileSystem.Copy(nodeToCopy, destination, nameOfCopiedElement, copyCallbacks);
     }
     finally
     {
         _lock.ExitWriteLock();
     }
 }
Exemple #21
0
        private VFSFile CreateFile(string source, Folder destination, string name)
        {
            var file = new VFSFile(name) { Parent = destination, BlockNumber = _blockAllocation.Allocate() };

            using (var b = new BinaryReader(File.OpenRead(source)))
            using (var w = DecorateToVFSStream(new VFSFileStream(file, _blockParser, _options, _blockAllocation, _blockManipulator, _persistence)))
            {
                byte[] block;
                while ((block = b.ReadBytes(_options.BlockSize)).Length > 0)
                {
                    w.Write(block, 0, block.Length);
                }
            }

            //Note: we could save some metadata too..
            return file;
        }
 public bool Exists(Folder folder, string name)
 {
     _lock.EnterUpgradeableReadLock();
     try
     {
         return _fileSystem.Exists(folder, name);
     }
     finally
     {
         _lock.ExitUpgradeableReadLock();
     }
 }
Exemple #23
0
        private Folder CreateFolder(Folder parentFolder, string name, bool createVersion)
        {
            CheckDisposed();
            CheckName(name);
            CheckVersion();

            if (Exists(parentFolder, name)) throw new AlreadyExistsException();

            var newParentFolder = createVersion ? ArchiveAndReplaceRoot(parentFolder, null, null) : parentFolder;

            var folder = new Folder(name)
                             {
                                 Parent = newParentFolder,
                                 BlockNumber = _blockAllocation.Allocate(),
                                 Version = newParentFolder.Version
                             };
            _persistence.Persist(folder);

            AppendBlockReference(newParentFolder, folder.BlockNumber);

            return folder;
        }
 public IEnumerable<Folder> Folders(Folder folder)
 {
     _lock.EnterUpgradeableReadLock();
     try
     {
         return _fileSystem.Folders(folder).ToList();
     }
     finally
     {
         _lock.ExitUpgradeableReadLock();
     }
 }
Exemple #25
0
        private void ExportFolder(Folder folder, string destination, CallbacksBase exportCallbacks)
        {
            if (exportCallbacks.ShouldAbort()) return;

            Directory.CreateDirectory(destination);
            exportCallbacks.CurrentlyProcessed++;

            foreach (var vfsFile in Files(folder))
            {
                ExportFile(vfsFile, Path.Combine(destination, vfsFile.Name), exportCallbacks);
            }
            foreach (var f in Folders(folder))
            {
                ExportFolder(f, Path.Combine(destination, f.Name), exportCallbacks);
            }
        }
 public IEnumerable<IIndexNode> List(Folder folder)
 {
     _lock.EnterUpgradeableReadLock();
     try
     {
         return _fileSystem.List(folder).ToList();
     }
     finally
     {
         _lock.ExitUpgradeableReadLock();
     }
 }
Exemple #27
0
        private void ImportDirectory(string source, Folder destination, string name, CallbacksBase importCallbacks, bool createVersion)
        {
            if (importCallbacks.ShouldAbort()) return;

            var info = new DirectoryInfo(source);

            var newFolder = CreateFolder(destination, name, createVersion);

            importCallbacks.CurrentlyProcessed++;

            foreach (var directoryInfo in info.GetDirectories())
                ImportDirectory(directoryInfo.FullName, newFolder, directoryInfo.Name, importCallbacks, false);

            foreach (var fileInfo in info.GetFiles())
                ImportFile(fileInfo.FullName, newFolder, fileInfo.Name, importCallbacks);
        }
        public void TestWriteFolderBlock()
        {
            var options = TestHelper.CreateFileSystemOptions("");
            var b = new BlockParser(options);

            var f = new Folder("blubα");

            var bb = b.NodeToBytes(f);

            Assert.AreEqual(0x1, bb[0]);
        }
Exemple #29
0
 public Folder CreateFolder(Folder parentFolder, string name)
 {
     var ret = CreateFolder(parentFolder, name, true);
     SetBlocksUsed();
     return ret;
 }