master
cpp 125 lines 4.04 KB
Raw
1 /*++
2
3 Copyright (c) Microsoft. All rights reserved.
4
5 Module Name:
6
7 VolumeService.cpp
8
9 Abstract:
10
11 This file contains the VolumeService implementation
12
13 --*/
14 #include "VolumeService.h"
15 #include "WarningCallback.h"
16 #include <wslutil.h>
17 #include <wslc.h>
18
19 using namespace wsl::shared;
20 using namespace wsl::shared::string;
21 using namespace wsl::windows::common::wslutil;
22
23 namespace wsl::windows::wslc::services {
24
25 WSLCVolumeInformation VolumeService::Create(models::Session& session, const models::CreateVolumeOptions& createOptions)
26 {
27 WSLCVolumeOptions options{};
28 options.Name = createOptions.Name.c_str();
29 if (createOptions.Driver.has_value())
30 {
31 options.Driver = createOptions.Driver->c_str();
32 }
33
34 // Set driver options
35 std::vector<KeyValuePair> driverOpts;
36 for (const auto& option : createOptions.DriverOpts)
37 {
38 driverOpts.push_back({.Key = option.first.c_str(), .Value = option.second.c_str()});
39 }
40
41 // Set labels
42 std::vector<KeyValuePair> labels;
43 for (const auto& label : createOptions.Labels)
44 {
45 labels.push_back({.Key = label.first.c_str(), .Value = label.second.c_str()});
46 }
47
48 options.DriverOpts = driverOpts.data();
49 options.DriverOptsCount = static_cast<ULONG>(driverOpts.size());
50 options.Labels = labels.data();
51 options.LabelsCount = static_cast<ULONG>(labels.size());
52
53 WSLCVolumeInformation info{};
54 THROW_IF_FAILED(session.Get()->CreateVolume(&options, &info));
55 return info;
56 }
57
58 void VolumeService::Delete(models::Session& session, const std::string& name)
59 {
60 THROW_IF_FAILED(session.Get()->DeleteVolume(name.c_str()));
61 }
62
63 std::vector<wsl::windows::common::wslc_schema::VolumeListEntry> VolumeService::List(
64 models::Session& session, const std::vector<std::pair<std::string, std::string>>& filters)
65 {
66 std::vector<WSLCFilter> filterEntries;
67 filterEntries.reserve(filters.size());
68 for (const auto& [key, value] : filters)
69 {
70 filterEntries.push_back({.Key = key.c_str(), .Value = value.c_str()});
71 }
72
73 wil::unique_cotaskmem_ansistring output;
74 THROW_IF_FAILED(session.Get()->ListVolumes(
75 filterEntries.empty() ? nullptr : filterEntries.data(), static_cast<ULONG>(filterEntries.size()), &output));
76
77 return FromJson<std::vector<wsl::windows::common::wslc_schema::VolumeListEntry>>(output.get());
78 }
79
80 wsl::windows::common::wslc_schema::InspectVolume VolumeService::Inspect(models::Session& session, const std::string& name)
81 {
82 wil::unique_cotaskmem_ansistring output;
83 THROW_IF_FAILED(session.Get()->InspectVolume(name.c_str(), &output));
84 return FromJson<wsl::windows::common::wslc_schema::InspectVolume>(output.get());
85 }
86
87 models::PruneVolumesResult VolumeService::Prune(
88 Terminal& terminal, models::Session& session, bool all, const std::vector<std::pair<std::string, std::string>>& filters)
89 {
90 WarningCallback warningCallback(terminal);
91 const bool hasExplicitAll = std::any_of(filters.begin(), filters.end(), [](const auto& f) { return f.first == "all"; });
92
93 std::vector<WSLCFilter> filterEntries;
94 filterEntries.reserve(filters.size() + ((all && !hasExplicitAll) ? 1 : 0));
95 if (all && !hasExplicitAll)
96 {
97 filterEntries.push_back({.Key = "all", .Value = "true"});
98 }
99
100 for (const auto& [key, value] : filters)
101 {
102 filterEntries.push_back({.Key = key.c_str(), .Value = value.c_str()});
103 }
104
105 wil::unique_cotaskmem_array_ptr<WSLCVolumeName> volumes;
106 ULONGLONG spaceReclaimed = 0;
107 THROW_IF_FAILED(session.Get()->PruneVolumes(
108 filterEntries.empty() ? nullptr : filterEntries.data(),
109 static_cast<ULONG>(filterEntries.size()),
110 &warningCallback,
111 &volumes,
112 volumes.size_address<ULONG>(),
113 &spaceReclaimed));
114
115 models::PruneVolumesResult result;
116 result.SpaceReclaimed = spaceReclaimed;
117 result.PrunedVolumes.reserve(volumes.size());
118 for (auto ptr = volumes.get(), end = volumes.get() + volumes.size(); ptr != end; ++ptr)
119 {
120 result.PrunedVolumes.emplace_back(*ptr);
121 }
122
123 return result;
124 }
125 } // namespace wsl::windows::wslc::services