public async Task <string[]> GetFiles(string path, bool recursive) { await Connect(); List <string> list = new List <string>(); DatastorePath dsPath = new DatastorePath(path); string oldRoot = ""; string pattern = dsPath.File ?? "*"; RetrievePropertiesResponse response = await _vim.RetrievePropertiesAsync( _props, FilterFactory.DatastoreFilter(_res) ); ObjectContent[] oc = response.returnval; foreach (ObjectContent obj in oc) { ManagedObjectReference dsBrowser = (ManagedObjectReference)obj.propSet[0].val; var capability = obj.propSet[1].val as DatastoreCapability; var summary = obj.propSet[2].val as DatastoreSummary; // if topLevelDirectory not supported (vsan), map from directory name to guid) if ( capability.topLevelDirectoryCreateSupportedSpecified && !capability.topLevelDirectoryCreateSupported && dsPath.TopLevelFolder.HasValue() ) { oldRoot = dsPath.TopLevelFolder; string target = summary.url + oldRoot; if (!_dsnsMap.ContainsKey(target)) { var result = await _vim.ConvertNamespacePathToUuidPathAsync( _dsns, _datacenter, target ); _dsnsMap.Add(target, result.Replace(summary.url, "")); } dsPath.TopLevelFolder = _dsnsMap[target]; // vmcloud sddc errors on Search_Datastore() // so force SearchDatastoreSubFolders() recursive = true; pattern = "*" + Path.GetExtension(dsPath.File); _logger.LogDebug("mapped datastore namespace: " + dsPath.ToString()); } if (summary.name == dsPath.Datastore) { ManagedObjectReference task = null; TaskInfo info = null; var spec = new HostDatastoreBrowserSearchSpec { matchPattern = new string[] { pattern }, }; var results = new List <HostDatastoreBrowserSearchResults>(); if (recursive) { try { task = await _vim.SearchDatastoreSubFolders_TaskAsync( dsBrowser, dsPath.FolderPath, spec ); info = await WaitForVimTask(task); if (info.result != null) { results.AddRange((HostDatastoreBrowserSearchResults[])info.result); } } catch (Exception ex) { _logger.LogError(ex, "error searching datastore."); } } else { task = await _vim.SearchDatastore_TaskAsync( dsBrowser, dsPath.FolderPath, spec ); info = await WaitForVimTask(task); if (info.result != null) { results.Add((HostDatastoreBrowserSearchResults)info.result); } } foreach (HostDatastoreBrowserSearchResults result in results) { if (result != null && result.file != null && result.file.Length > 0) { string fp = result.folderPath; if (oldRoot.HasValue()) { fp = fp.Replace(dsPath.TopLevelFolder, oldRoot); } if (!fp.EndsWith("/")) { fp += "/"; } list.AddRange(result.file.Select(o => fp + o.path)); } } } } return(list.ToArray()); }
public async Task <string[]> GetFiles(string path, bool recursive) { await Connect(); List <string> list = new List <string>(); DatastorePath dsPath = new DatastorePath(path); RetrievePropertiesResponse response = await _vim.RetrievePropertiesAsync( _props, FilterFactory.DatastoreFilter(_res)); ObjectContent[] oc = response.returnval; foreach (ObjectContent obj in oc) { ManagedObjectReference dsBrowser = (ManagedObjectReference)obj.propSet[0].val; string dsName = ((DatastoreSummary)obj.propSet[1].val).name; if (dsName == dsPath.Datastore) { ManagedObjectReference task = null; TaskInfo info = null; HostDatastoreBrowserSearchSpec spec = new HostDatastoreBrowserSearchSpec { matchPattern = new string[] { dsPath.File } }; List <HostDatastoreBrowserSearchResults> results = new List <HostDatastoreBrowserSearchResults>(); if (recursive) { task = await _vim.SearchDatastoreSubFolders_TaskAsync( dsBrowser, dsPath.FolderPath, spec); info = await WaitForVimTask(task); if (info.result != null) { results.AddRange((HostDatastoreBrowserSearchResults[])info.result); } } else { task = await _vim.SearchDatastore_TaskAsync( dsBrowser, dsPath.FolderPath, spec); info = await WaitForVimTask(task); if (info.result != null) { results.Add((HostDatastoreBrowserSearchResults)info.result); } } foreach (HostDatastoreBrowserSearchResults result in results) { if (result != null && result.file != null && result.file.Length > 0) { string fp = result.folderPath; if (!fp.EndsWith("/")) { fp += "/"; } list.AddRange(result.file.Select(o => fp + o.path)); } } } } return(list.ToArray()); }