99
1010	"github.com/microsoft/wmi/pkg/base/query" 
1111	"github.com/microsoft/wmi/pkg/errors" 
12- 	cim "github.com/microsoft/wmi/pkg/wmiinstance" 
1312	"github.com/microsoft/wmi/server2019/root/microsoft/windows/storage" 
1413)
1514
@@ -129,131 +128,84 @@ func ListPartitionsWithFilters(selectorList []string, filters ...*query.WmiQuery
129128	return  partitions , nil 
130129}
131130
132- // ListPartitionToVolumeMappings builds a mapping between partition and volume with partition Object ID as the key. 
133- // 
134- // The equivalent WMI query is: 
135- // 
136- //		SELECT [selectors] FROM MSFT_PartitionToVolume 
137- // 
138- //	 Partition                                                               | Volume 
139- //	 ---------                                                               | ------ 
140- //	 MSFT_Partition (ObjectId = "{1}\\WIN-8E2EVAQ9QSB\ROOT/Microsoft/Win...) | MSFT_Volume (ObjectId = "{1}\\WIN-8E2EVAQ9QS... 
141- // 
142- // Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-partitiontovolume 
143- // for the WMI class definition. 
144- func  ListPartitionToVolumeMappings () (map [string ]string , error ) {
145- 	return  ListWMIInstanceMappings (WMINamespaceStorage , "MSFT_PartitionToVolume" , nil ,
146- 		mappingObjectRefIndexer ("Partition" , "MSFT_Partition" , "ObjectId" ),
147- 		mappingObjectRefIndexer ("Volume" , "MSFT_Volume" , "ObjectId" ),
148- 	)
149- }
150- 
151- // ListVolumeToPartitionMappings builds a mapping between volume and partition with volume Object ID as the key. 
152- // 
153- // The equivalent WMI query is: 
131+ // FindPartitionsByVolume finds all partitions associated with the given volumes 
132+ // using MSFT_PartitionToVolume association. 
154133// 
155- //		SELECT [selectors] FROM  MSFT_PartitionToVolume 
134+ // WMI association  MSFT_PartitionToVolume:  
156135// 
157- //	  Partition                                                               | Volume 
158- //	  ---------                                                               | ------ 
159- //	  MSFT_Partition (ObjectId = "{1}\\WIN-8E2EVAQ9QSB\ROOT/Microsoft/Win...) | MSFT_Volume (ObjectId = "{1}\\WIN-8E2EVAQ9QS... 
136+ //	Partition                                                               | Volume 
137+ //	---------                                                               | ------ 
138+ //	MSFT_Partition (ObjectId = "{1}\\WIN-8E2EVAQ9QSB\ROOT/Microsoft/Win...) | MSFT_Volume (ObjectId = "{1}\\WIN-8E2EVAQ9QS... 
160139// 
161140// Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-partitiontovolume 
162141// for the WMI class definition. 
163- func  ListVolumeToPartitionMappings () (map [string ]string , error ) {
164- 	return  ListWMIInstanceMappings (WMINamespaceStorage , "MSFT_PartitionToVolume" , nil ,
165- 		mappingObjectRefIndexer ("Volume" , "MSFT_Volume" , "ObjectId" ),
166- 		mappingObjectRefIndexer ("Partition" , "MSFT_Partition" , "ObjectId" ),
167- 	)
168- }
169- 
170- // FindPartitionsByVolume finds all partitions associated with the given volumes 
171- // using partition-to-volume mapping. 
172- func  FindPartitionsByVolume (partitions  []* storage.MSFT_Partition , volumes  []* storage.MSFT_Volume ) ([]* storage.MSFT_Partition , error ) {
173- 	var  partitionInstances  []* cim.WmiInstance 
174- 	for  _ , part  :=  range  partitions  {
175- 		partitionInstances  =  append (partitionInstances , part .WmiInstance )
176- 	}
177- 
178- 	var  volumeInstances  []* cim.WmiInstance 
179- 	for  _ , volume  :=  range  volumes  {
180- 		volumeInstances  =  append (volumeInstances , volume .WmiInstance )
181- 	}
182- 
183- 	partitionToVolumeMappings , err  :=  ListPartitionToVolumeMappings ()
184- 	if  err  !=  nil  {
185- 		return  nil , err 
186- 	}
187- 
188- 	filtered , err  :=  FindInstancesByObjectIDMapping (partitionInstances , volumeInstances , partitionToVolumeMappings )
189- 	if  err  !=  nil  {
190- 		return  nil , err 
191- 	}
192- 
142+ func  FindPartitionsByVolume (volumes  []* storage.MSFT_Volume ) ([]* storage.MSFT_Partition , error ) {
193143	var  result  []* storage.MSFT_Partition 
194- 	for  _ , instance  :=  range  filtered  {
195- 		part , err  :=  storage . NewMSFT_PartitionEx1 ( instance )
144+ 	for  _ , vol  :=  range  volumes  {
145+ 		collection , err  :=  vol . GetAssociated ( "MSFT_PartitionToVolume" ,  "MSFT_Partition" ,  "Partition" ,  "Volume" )
196146		if  err  !=  nil  {
197- 			return  nil , fmt .Errorf ("failed to query partition %v. error: %v" , instance , err )
147+ 			return  nil , fmt .Errorf ("failed to query associated  partition for  %v. error: %v" , vol , err )
198148		}
199149
200- 		result  =  append (result , part )
150+ 		for  _ , instance  :=  range  collection  {
151+ 			part , err  :=  storage .NewMSFT_PartitionEx1 (instance )
152+ 			if  err  !=  nil  {
153+ 				return  nil , fmt .Errorf ("failed to query partition %v. error: %v" , instance , err )
154+ 			}
155+ 
156+ 			result  =  append (result , part )
157+ 		}
201158	}
202159
203160	return  result , nil 
204161}
205162
206163// FindVolumesByPartition finds all volumes associated with the given partitions 
207- // using volume-to-partition mapping. 
208- func  FindVolumesByPartition (volumes  []* storage.MSFT_Volume , partitions  []* storage.MSFT_Partition ) ([]* storage.MSFT_Volume , error ) {
209- 	var  volumeInstances  []* cim.WmiInstance 
210- 	for  _ , volume  :=  range  volumes  {
211- 		volumeInstances  =  append (volumeInstances , volume .WmiInstance )
212- 	}
213- 
214- 	var  partitionInstances  []* cim.WmiInstance 
215- 	for  _ , part  :=  range  partitions  {
216- 		partitionInstances  =  append (partitionInstances , part .WmiInstance )
217- 	}
218- 
219- 	volumeToPartitionMappings , err  :=  ListVolumeToPartitionMappings ()
220- 	if  err  !=  nil  {
221- 		return  nil , err 
222- 	}
223- 
224- 	filtered , err  :=  FindInstancesByObjectIDMapping (volumeInstances , partitionInstances , volumeToPartitionMappings )
225- 	if  err  !=  nil  {
226- 		return  nil , err 
227- 	}
228- 
164+ // using MSFT_PartitionToVolume association. 
165+ // 
166+ // WMI association MSFT_PartitionToVolume: 
167+ // 
168+ //	Partition                                                               | Volume 
169+ //	---------                                                               | ------ 
170+ //	MSFT_Partition (ObjectId = "{1}\\WIN-8E2EVAQ9QSB\ROOT/Microsoft/Win...) | MSFT_Volume (ObjectId = "{1}\\WIN-8E2EVAQ9QS... 
171+ // 
172+ // Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-partitiontovolume 
173+ // for the WMI class definition. 
174+ func  FindVolumesByPartition (partitions  []* storage.MSFT_Partition ) ([]* storage.MSFT_Volume , error ) {
229175	var  result  []* storage.MSFT_Volume 
230- 	for  _ , instance  :=  range  filtered  {
231- 		volume , err  :=  storage . NewMSFT_VolumeEx1 ( instance )
176+ 	for  _ , part  :=  range  partitions  {
177+ 		collection , err  :=  part . GetAssociated ( "MSFT_PartitionToVolume" ,  "MSFT_Volume" ,  "Volume" ,  "Partition" )
232178		if  err  !=  nil  {
233- 			return  nil , fmt .Errorf ("failed to query volume  %v. error: %v" , instance , err )
179+ 			return  nil , fmt .Errorf ("failed to query associated volumes for  %v. error: %v" , part , err )
234180		}
235181
236- 		result  =  append (result , volume )
182+ 		for  _ , instance  :=  range  collection  {
183+ 			volume , err  :=  storage .NewMSFT_VolumeEx1 (instance )
184+ 			if  err  !=  nil  {
185+ 				return  nil , fmt .Errorf ("failed to query volume %v. error: %v" , instance , err )
186+ 			}
187+ 
188+ 			result  =  append (result , volume )
189+ 		}
237190	}
238191
239192	return  result , nil 
240193}
241194
242195// GetPartitionByVolumeUniqueID retrieves a specific partition from a volume identified by its unique ID. 
243- func  GetPartitionByVolumeUniqueID (volumeID  string ,  partitionSelectorList  [] string ) (* storage.MSFT_Partition , error ) {
196+ func  GetPartitionByVolumeUniqueID (volumeID  string ) (* storage.MSFT_Partition , error ) {
244197	volume , err  :=  QueryVolumeByUniqueID (volumeID , []string {"ObjectId" })
245198	if  err  !=  nil  {
246199		return  nil , err 
247200	}
248201
249- 	partitions , err  :=  ListPartitionsWithFilters ( partitionSelectorList )
202+ 	result , err  :=  FindPartitionsByVolume ([] * storage. MSFT_Volume { volume } )
250203	if  err  !=  nil  {
251204		return  nil , err 
252205	}
253206
254- 	result , err  :=  FindPartitionsByVolume (partitions , []* storage.MSFT_Volume {volume })
255- 	if  err  !=  nil  {
256- 		return  nil , err 
207+ 	if  len (result ) ==  0  {
208+ 		return  nil , errors .NotFound 
257209	}
258210
259211	return  result [0 ], nil 
@@ -269,12 +221,7 @@ func GetVolumeByDriveLetter(driveLetter string, partitionSelectorList []string)
269221		return  nil , err 
270222	}
271223
272- 	volumes , err  :=  ListVolumes (partitionSelectorList )
273- 	if  err  !=  nil  {
274- 		return  nil , err 
275- 	}
276- 
277- 	result , err  :=  FindVolumesByPartition (volumes , partitions )
224+ 	result , err  :=  FindVolumesByPartition (partitions )
278225	if  err  !=  nil  {
279226		return  nil , err 
280227	}
0 commit comments