@@ -70,9 +70,9 @@ private static string NTPathToPS(string path)
7070 /// <returns>The list of default drives.</returns>
7171 protected override Collection < PSDriveInfo > InitializeDefaultDrives ( )
7272 {
73- PSDriveInfo drive = new PSDriveInfo ( "NtObject" , this . ProviderInfo , GLOBAL_ROOT , "NT Object Manager Root Directory" , null ) ;
73+ PSDriveInfo drive = new PSDriveInfo ( "NtObject" , ProviderInfo , GLOBAL_ROOT , "NT Object Manager Root Directory" , null ) ;
7474 int session_id = Process . GetCurrentProcess ( ) . SessionId ;
75- string base_dir = null ;
75+ string base_dir ;
7676 if ( session_id == 0 )
7777 {
7878 base_dir = GLOBAL_ROOT + "BaseNamedObjects" ;
@@ -223,23 +223,23 @@ private string GetRelativePath(string path)
223223
224224 private ObjectManagerPSDriveInfo GetDrive ( )
225225 {
226- return ( ObjectManagerPSDriveInfo ) this . PSDriveInfo ;
226+ return ( ObjectManagerPSDriveInfo ) PSDriveInfo ;
227227 }
228228
229- private NtDirectory GetPathDirectory ( string path )
229+ private NtResult < NtDirectory > GetPathDirectory ( string path , bool throw_on_error )
230230 {
231231 int last_slash = path . LastIndexOf ( '\\ ' ) ;
232232 if ( last_slash == - 1 )
233233 {
234- return GetDrive ( ) . DirectoryRoot . Duplicate ( ) ;
234+ return GetDrive ( ) . DirectoryRoot . Duplicate ( throw_on_error ) ;
235235 }
236236 else
237237 {
238238 NtDirectory dir = GetDrive ( ) . DirectoryRoot ;
239239 string base_path = path . Substring ( 0 , last_slash ) ;
240240
241241 return NtDirectory . Open ( base_path ,
242- dir , DirectoryAccessRights . MaximumAllowed ) ;
242+ dir , DirectoryAccessRights . MaximumAllowed , throw_on_error ) ;
243243 }
244244 }
245245
@@ -286,17 +286,14 @@ protected override bool ItemExists(string path)
286286 {
287287 return true ;
288288 }
289-
290- try
289+
290+ using ( var dir = GetPathDirectory ( path , false ) )
291291 {
292- using ( NtDirectory dir = GetPathDirectory ( path ) )
292+ if ( dir . IsSuccess )
293293 {
294- exists = GetEntry ( dir , path ) != null ;
294+ exists = GetEntry ( dir . Result , path ) != null ;
295295 }
296296 }
297- catch ( NtException )
298- {
299- }
300297
301298 // If we can't find it indirectly, at least see if there's a directory with this name.
302299 return exists || GetDrive ( ) . DirectoryRoot . DirectoryExists ( path ) ;
@@ -323,18 +320,15 @@ protected override bool IsItemContainer(string path)
323320 return true ;
324321 }
325322
326- try
323+ using ( var dir = GetPathDirectory ( path , false ) )
327324 {
328- using ( NtDirectory dir = GetPathDirectory ( path ) )
325+ if ( dir . IsSuccess )
329326 {
330- ObjectDirectoryInformation dir_info = GetEntry ( dir , path ) ;
327+ ObjectDirectoryInformation dir_info = GetEntry ( dir . Result , path ) ;
331328 is_container = dir_info != null
332329 && dir_info . NtTypeName . Equals ( "directory" , StringComparison . OrdinalIgnoreCase ) ;
333330 }
334331 }
335- catch ( NtException )
336- {
337- }
338332
339333 return is_container || GetDrive ( ) . DirectoryRoot . DirectoryExists ( path ) ;
340334 }
@@ -452,16 +446,18 @@ protected override void GetItem(string path)
452446 }
453447
454448 string relative_path = GetRelativePath ( PSPathToNT ( path ) ) ;
455- using ( NtDirectory dir = GetPathDirectory ( relative_path ) )
449+ using ( var dir = GetPathDirectory ( relative_path , false ) )
456450 {
451+ if ( ! dir . IsSuccess )
452+ return ;
457453 if ( relative_path . Length == 0 )
458454 {
459455 WriteItemObject ( new NtDirectoryEntry ( GetDrive ( ) . DirectoryRoot , relative_path , string . Empty , "Directory" ) ,
460456 NTPathToPS ( BuildDrivePath ( relative_path ) ) , true ) ;
461457 }
462458 else
463459 {
464- ObjectDirectoryInformation dir_info = GetEntry ( dir , relative_path ) ;
460+ ObjectDirectoryInformation dir_info = GetEntry ( dir . Result , relative_path ) ;
465461 if ( dir_info != null )
466462 {
467463 WriteItemObject ( new NtDirectoryEntry ( GetDrive ( ) . DirectoryRoot , relative_path , dir_info . Name , dir_info . NtTypeName ) ,
@@ -535,15 +531,11 @@ private void AddMatches(NtDirectory root, string base_path, IEnumerable<string>
535531 {
536532 foreach ( ObjectDirectoryInformation entry in matching_entries )
537533 {
538- try
539- {
540- using ( NtDirectory dir = NtDirectory . Open ( entry . Name , root , DirectoryAccessRights . Query ) )
541- {
542- AddMatches ( dir , base_path + entry . Name + @"\" , remaining . Skip ( 1 ) , matches ) ;
543- }
544- }
545- catch ( NtException )
534+ using ( var dir = NtDirectory . Open ( entry . Name , root , DirectoryAccessRights . Query , false ) )
546535 {
536+ if ( ! dir . IsSuccess )
537+ continue ;
538+ AddMatches ( dir . Result , base_path + entry . Name + @"\" , remaining . Skip ( 1 ) , matches ) ;
547539 }
548540 }
549541 }
@@ -650,15 +642,15 @@ protected override void NewItem(string path, string itemTypeName, object newItem
650642 void ISecurityDescriptorCmdletProvider . GetSecurityDescriptor ( string path , AccessControlSections includeSections )
651643 {
652644 string relative_path = GetRelativePath ( PSPathToNT ( path ) ) ;
653- using ( NtDirectory dir = GetPathDirectory ( relative_path ) )
645+ using ( var dir = GetPathDirectory ( relative_path , true ) )
654646 {
655647 if ( relative_path . Length == 0 )
656648 {
657- WriteItemObject ( new GenericObjectSecurity ( dir , includeSections ) , path , true ) ;
649+ WriteItemObject ( new GenericObjectSecurity ( dir . Result , includeSections ) , path , true ) ;
658650 }
659651 else
660652 {
661- ObjectDirectoryInformation dir_info = GetEntry ( dir , relative_path ) ;
653+ ObjectDirectoryInformation dir_info = GetEntry ( dir . Result , relative_path ) ;
662654 if ( dir_info == null )
663655 {
664656 throw new NtException ( NtStatus . STATUS_OBJECT_NAME_NOT_FOUND ) ;
@@ -677,9 +669,9 @@ void ISecurityDescriptorCmdletProvider.SetSecurityDescriptor(string path, Object
677669 if ( securityDescriptor is GenericObjectSecurity obj_security )
678670 {
679671 string relative_path = GetRelativePath ( PSPathToNT ( path ) ) ;
680- using ( NtDirectory dir = GetPathDirectory ( relative_path ) )
672+ using ( var dir = GetPathDirectory ( relative_path , true ) )
681673 {
682- ObjectDirectoryInformation dir_info = GetEntry ( dir , relative_path ) ;
674+ ObjectDirectoryInformation dir_info = GetEntry ( dir . Result , relative_path ) ;
683675 if ( dir_info == null )
684676 {
685677 throw new NtException ( NtStatus . STATUS_OBJECT_NAME_NOT_FOUND ) ;
0 commit comments