Exemplo n.º 1
0
        public async Task <IVolume> GetVolume()
        {
            Log.Debug("Getting volume of {Partition}", this);

            var results = await PowerShellMixin.ExecuteCommand("Get-Volume",
                                                               ("Partition", await this.GetPsPartition()));

            var result = results.FirstOrDefault()?.ImmediateBaseObject;

            if (result == null)
            {
                return(null);
            }

            var driveLetter = (char?)result.GetPropertyValue("DriveLetter");
            var vol         = new Volume(this)
            {
                Size  = new ByteSize(Convert.ToUInt64(result.GetPropertyValue("Size"))),
                Label = (string)result.GetPropertyValue("FileSystemLabel"),
                Root  = driveLetter != null?PathExtensions.GetRootPath(driveLetter.Value) : null,
                            FileSystemFormat = FileSystemFormat.FromString((string)result.GetPropertyValue("FileSystem")),
            };

            Log.Debug("Obtained {Volume}", vol);

            return(vol);
        }
Exemplo n.º 2
0
        private async Task <Volume> GetVolumeCore(Partition partition)
        {
            Log.Debug("Getting volume of {Partition}", partition);

            var results = await ps.ExecuteCommand("Get-Volume",
                                                  ("Partition", await GetPsPartition(partition)));

            var result = results.FirstOrDefault()?.ImmediateBaseObject;

            if (result == null)
            {
                throw new ApplicationException($"Cannot get volume for {partition}");
            }

            var vol = new Volume(partition)
            {
                Partition       = partition,
                Size            = new ByteSize(Convert.ToUInt64(result.GetPropertyValue("Size"))),
                Label           = (string)result.GetPropertyValue("FileSystemLabel"),
                Letter          = (char?)result.GetPropertyValue("DriveLetter"),
                FileSytemFormat = FileSystemFormat.FromString((string)result.GetPropertyValue("FileSystem")),
            };

            Log.Debug("Obtained {Volume}", vol);

            return(vol);
        }
Exemplo n.º 3
0
        private static async Task EnsureFormatted(Partition partition, string label, FileSystemFormat fileSystemFormat)
        {
            var volume = await partition.GetVolume();

            if (volume == null)
            {
                throw new ApplicationException($"Couldn't get volume for {partition}");
            }

            await volume.Mount();

            if (Directory.EnumerateFileSystemEntries(volume.Root).Count() > 1)
            {
                throw new ApplicationException("The format operation failed. The drive shouldn't contain any file after the format");
            }

            var sameLabel            = string.Equals(volume.Label, label);
            var sameFileSystemFormat = Equals(volume.FileSytemFormat, fileSystemFormat);

            if (!sameLabel || !sameFileSystemFormat)
            {
                Log.Verbose("Same label? {Value}. Same file system format? {Value}", sameLabel, sameFileSystemFormat);
                throw new ApplicationException("The format operation failed");
            }
        }
Exemplo n.º 4
0
        public Task Format(Volume volume, FileSystemFormat fileSystemFormat, string fileSystemLabel)
        {
            ps.Commands.Clear();
            var cmd = $@"Get-Partition -UniqueId ""{volume.Partition.Id}"" | Get-Volume | Format-Volume -FileSystem {fileSystemFormat.Moniker} -NewFileSystemLabel ""{fileSystemLabel}"" -Force -Confirm:$false";

            ps.AddScript(cmd);

            return(Task.Factory.FromAsync(ps.BeginInvoke(), x => ps.EndInvoke(x)));
        }
        private async Task Format(string partitionName, FileSystemFormat fileSystemFormat, string label)
        {
            var part = await disk.GetPartitionByName(partitionName);

            await part.EnsureWritable();

            var vol = await part.GetVolume();

            await vol.Format(fileSystemFormat, label);
        }
Exemplo n.º 6
0
        public async Task Execute(string partitionDescriptor, string fileSystemFormat, string label = null)
        {
            var part = await fileSystem.GetPartitionFromDescriptor(partitionDescriptor);

            await part.EnsureWritable();

            var vol = await part.GetVolume();

            var systemFormat = FileSystemFormat.FromString(fileSystemFormat);
            await vol.Format(systemFormat, label);
        }
Exemplo n.º 7
0
        public async Task Format(FileSystemFormat fileSystemFormat, string label)
        {
            var part = await this.Partition.GetPsPartition();

            await PowerShellMixin.ExecuteCommand("Format-Volume",
                                                 ("Partition", part),
                                                 ("Force", null),
                                                 ("Confirm", false),
                                                 ("FileSystem", fileSystemFormat.Moniker),
                                                 ("NewFileSystemLabel", label)
                                                 );
        }
Exemplo n.º 8
0
        private async Task FormatCore(Partition partition, FileSystemFormat fileSystemFormat, string label = null)
        {
            label = label ?? partition.Name ?? "";

            Log.Verbose(@"Formatting {Partition} as {Format} labeled as ""{Label}""", partition, fileSystemFormat, label);

            var part = await GetPsPartition(partition);

            await ps.ExecuteCommand("Format-Volume",
                                    ("Partition", part),
                                    ("Force", null),
                                    ("Confirm", false),
                                    ("FileSystem", fileSystemFormat.Moniker),
                                    ("NewFileSystemLabel", label)
                                    );

            await EnsureFormatted(partition, label, fileSystemFormat);
        }
Exemplo n.º 9
0
 public static void FromString(this FileSystemFormat format, string str)
 {
     EnumFromString(format, str);
 }
Exemplo n.º 10
0
 public static string ToString(this FileSystemFormat format)
 {
     return(EnumToString(format));
 }
Exemplo n.º 11
0
 public Task Format(FileSystemFormat fileSystemFormat, string label)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 12
0
 public async Task Format(Partition partition, FileSystemFormat fileSystemFormat, string label = null)
 {
     await Observable.FromAsync(() => FormatCore(partition, fileSystemFormat, label)).RetryWithBackoffStrategy(4);
 }
Exemplo n.º 13
0
 public Task Format(Volume volume, FileSystemFormat ntfs, string fileSystemLabel)
 {
     throw new NotImplementedException();
 }