private Cursor WriteFileDescriptor(IFileEntry file, bool isNewFile) { _logger.Information($"Start writing file descriptor for filename {file.EntryName}"); var allocatedCursor = _allocator.AllocateFile(file.DataLength, isNewFile); var filesystemDescriptor = _filesystemDescriptorAccessor.Value; var createdOn = DateTime.UtcNow.ToUnixTime(); var updatedOn = createdOn; var id = Guid.NewGuid(); var fileDescriptor = new EntryDescriptor(id, file.ParentEntryId, file.EntryName, EntryType.File, createdOn, updatedOn, allocatedCursor.Offset, file.DataLength); var fileDescriptorOffset = -FilesystemDescriptor.BytesTotal - (filesystemDescriptor.EntryDescriptorsCount * filesystemDescriptor.EntryDescriptorLength) - filesystemDescriptor.EntryDescriptorLength; var origin = SeekOrigin.End; var cursor = new Cursor(fileDescriptorOffset, origin); var storageItem = new StorageItem <EntryDescriptor>(in fileDescriptor, in cursor); _entryDescriptorRepository.Write(storageItem); _logger.Information($"Done writing file descriptor for filename {file.EntryName}"); return(allocatedCursor); }
/// <inheritdoc/> public void Create(DirectoryEntry directoryEntry) { _logger.Information($"Start writing entry descriptor for directory {directoryEntry.EntryName}"); var filesystemDescriptor = _filesystemDescriptorAccessor.Value; var createdOn = DateTime.UtcNow.ToUnixTime(); var updatedOn = createdOn; var entryDescriptor = new EntryDescriptor( directoryEntry.Id, directoryEntry.ParentEntryId, directoryEntry.EntryName, EntryType.Directory, createdOn, updatedOn, 0, 0); var entryDescriptorOffset = -FilesystemDescriptor.BytesTotal - (filesystemDescriptor.EntryDescriptorsCount * filesystemDescriptor.EntryDescriptorLength) - filesystemDescriptor.EntryDescriptorLength; var origin = SeekOrigin.End; var cursor = new Cursor(entryDescriptorOffset, origin); var storageItem = new StorageItem <EntryDescriptor>(in entryDescriptor, in cursor); _entryDescriptorRepository.Write(storageItem); _logger.Information($"Done writing entry descriptor for directory {directoryEntry.EntryName}"); IncreaseEntriesCount(1); }
/// <inheritdoc /> public void Delete(string entryName) { // 1. Find last descriptor var filesystemDescriptor = _filesystemDescriptorAccessor.Value; var lastDescriptorCursor = new Cursor( -FilesystemDescriptor.BytesTotal - (filesystemDescriptor.EntryDescriptorsCount * filesystemDescriptor.EntryDescriptorLength), SeekOrigin.End); var lastDescriptor = _entryDescriptorRepository.Read(lastDescriptorCursor).Value; // 2. Find current descriptor var descriptorItem = _entryDescriptorRepository.Find(entryName); var cursor = descriptorItem.Cursor; // 3. Save last descriptor in new empty space (perform swap with last) _entryDescriptorRepository.Write(new StorageItem <EntryDescriptor>(lastDescriptor, cursor)); // 4. Decrease count of descriptors IncreaseEntriesCount(-1); }
private void CreateRootDirectory() { var rootDirectoryId = Guid.NewGuid(); var createdOn = DateTime.UtcNow.ToUnixTime(); var updatedOn = createdOn; var rootDirectory = new EntryDescriptor( rootDirectoryId, rootDirectoryId, "/", EntryType.Directory, createdOn, updatedOn, 0, 0); // Write root directory descriptor var filesystemDescriptor = _filesystemDescriptorAccessor.Value; var cursor = new Cursor(-FilesystemDescriptor.BytesTotal - filesystemDescriptor.EntryDescriptorLength, SeekOrigin.End); _entryDescriptorRepository.Write(new StorageItem <EntryDescriptor>(rootDirectory, cursor)); }
private StorageItem <EntryDescriptor> CopyFile(EntryDescriptor entryDescriptor, Cursor cursor, int destinationOffset) { var newDescriptor = new EntryDescriptor( entryDescriptor.Id, entryDescriptor.ParentId, entryDescriptor.Name, entryDescriptor.Type, entryDescriptor.CreatedOn, entryDescriptor.UpdatedOn, destinationOffset, entryDescriptor.DataLength); var newStorageItem = new StorageItem <EntryDescriptor>(in newDescriptor, in cursor); _logger.Information($"Moving {entryDescriptor.DataLength} bytes of data from {entryDescriptor.DataOffset} to {destinationOffset}"); PerformCopy(entryDescriptor.DataOffset, destinationOffset, entryDescriptor.DataLength); _entryDescriptorRepository.Write(newStorageItem); _logger.Information($"{entryDescriptor.DataLength} bytes of data moved to offset {destinationOffset}"); return(newStorageItem); }