// Copyright (C) 2025, The Duplicati Team // https://duplicati.com, hello@duplicati.com // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. using System; using Duplicati.Proprietary.DiskImage; #nullable enable namespace Duplicati.UnitTest.DiskImage { /// /// Interface for disk image helper operations that work across different operating systems. /// Provides methods for creating, attaching, formatting, and managing virtual disk images /// for testing disk image backup and restore operations. /// public interface IDiskImageHelper : IDisposable { /// /// Creates a virtual disk file, attaches it to the system, and returns the physical drive path. /// /// The path where the disk image file will be created. /// The size of the disk image in megabytes. /// The physical drive path (e.g., \\.\PhysicalDriveN on Windows, /dev/loopN on Linux). string CreateAndAttachDisk(string imagePath, long sizeMB); /// /// Gets the disk number or identifier for an attached disk image. /// /// The path to the disk image file. /// The disk number/index, or -1 if not found. int GetDiskNumber(string imagePath); /// /// Initializes a disk with the specified partition table type (GPT or MBR). /// /// The disk number. /// The partition table type (GPT or MBR). void InitializeDisk(int diskNumber, PartitionTableType tableType); /// /// Creates a partition on the specified disk, formats it with the specified filesystem, /// and assigns a drive letter or mount point. /// /// The disk number. /// The filesystem type (NTFS, FAT32, EXT4, etc.). /// The size of the partition in MB (0 for all available space). /// The assigned drive letter (Windows) or mount point identifier. char CreateAndFormatPartition(int diskNumber, FileSystemType fsType, long sizeMB = 0); /// /// Flushes the volume cache to ensure all data is written to disk. /// /// The drive letter or mount point identifier. void FlushVolume(char driveLetter); /// /// Populates a drive with test data including files of various sizes and directory structures. /// /// The drive letter to populate. /// The number of files to create. /// The size of each file in KB. void PopulateTestData(char driveLetter, int fileCount = 10, int fileSizeKB = 10); /// /// Detaches a disk image file from the system. /// /// The path to the disk image file. void DetachDisk(string imagePath); /// /// Unmounts a disk image for writing operations by removing drive letters/mount points /// and setting the disk offline. /// /// The path to the disk image file. /// Optional drive letter to unmount. If null, will attempt to detect. void UnmountForWriting(string imagePath, char? driveLetter = null); /// /// Brings a disk online for read operations. /// /// The path to the disk image file. void BringOnline(string imagePath); /// /// Mounts a disk image for reading and returns the assigned drive letter. /// /// The path to the disk image file. /// Optional preferred drive letter. If null, one will be assigned. /// The assigned drive letter. char MountForReading(string imagePath, char? driveLetter = null); /// /// Cleans up and deletes a disk image file, ensuring it is detached first. /// /// The path to the disk image file. void CleanupDisk(string imagePath); /// /// Gets detailed information about a disk. /// /// The disk number. /// Detailed information about the disk. string GetDiskDetails(int diskNumber); /// /// Gets the volume information for a drive letter. /// /// The drive letter. /// The volume information. string GetVolumeInfo(char driveLetter); /// /// Checks if the current process has the necessary privileges to perform disk operations. /// /// true if running with sufficient privileges; otherwise, false. bool HasRequiredPrivileges(); } }