Explorar el Código

UPD: Improve error handling for wfx plugins

Alexander Koblov hace 16 años
padre
commit
3449d35d40

+ 63 - 55
plugins/wfx/Network/src/netfunc.pas

@@ -172,51 +172,39 @@ begin
   ListRec^.Index := 0;
   ListRec^.Handle := INVALID_HANDLE_VALUE;
   FillChar(FindData, SizeOf(FindData), #0);
-  // root directory
-  if Path = PathDelim then
-  begin
-    ListRec^.List := GetContainerList(nil); // get list of networks
-    FindData.dwFileAttributes := FILE_ATTRIBUTE_DIRECTORY;
-    StrPCopy(FindData.cFileName, PNetRes(ListRec^.List[0])^.RemoteName);
-  end
-  else
-  begin
-    bFound := False;
-    sDirName := StrPas(Path);
-
-    iCount := GetDirs(sDirName, Dirs);
-    if iCount = 0 then
-      Exit;
-    // open shared folder
-    if iCount > 3 then
+  try
+    // root directory
+    if Path = PathDelim then
     begin
-      sDirName := ExtractNetworkFileName(Path) + PathDelim + '*';
-      ListRec^.Handle := FindFirstFile(PAnsiChar(sDirName), FindData);
-      Result := THandle(ListRec);
-      Exit;
-    end;
-
-    sDirName := Dirs[0];
-    ListRec^.List := GetContainerList(nil); // get list of networks
-    for I := 0 to ListRec^.List.Count - 1 do
+      ListRec^.List := GetContainerList(nil); // get list of networks
+      FindData.dwFileAttributes := FILE_ATTRIBUTE_DIRECTORY;
+      StrPCopy(FindData.cFileName, PNetRes(ListRec^.List[0])^.RemoteName);
+    end
+    else
     begin
-      // open subnet
-      if PNetRes(ListRec^.List[i])^.RemoteName = sDirName then
+      bFound := False;
+      sDirName := StrPas(Path);
+
+      iCount := GetDirs(sDirName, Dirs);
+      if iCount = 0 then
+        begin
+          Result:= wfxInvalidHandle;
+          Exit;
+        end;
+      // open shared folder
+      if iCount > 3 then
       begin
-        NetRes := PNetRes(ListRec^.List[I])^;
-        FreeNetResList(ListRec^.List);
-        ListRec^.List := GetContainerList(@NetRes);
-        bFound := True;
-        Break;
+        sDirName := ExtractNetworkFileName(Path) + PathDelim + '*';
+        ListRec^.Handle := FindFirstFile(PAnsiChar(sDirName), FindData);
+        Result := THandle(ListRec);
+        Exit;
       end;
-    end;
-    // open domen
-    if bFound and Assigned(ListRec^.List) and (iCount > 1) then
-    begin
-      bFound := False;
-      sDirName := Dirs[1];
+
+      sDirName := Dirs[0];
+      ListRec^.List := GetContainerList(nil); // get list of networks
       for I := 0 to ListRec^.List.Count - 1 do
       begin
+        // open subnet
         if PNetRes(ListRec^.List[i])^.RemoteName = sDirName then
         begin
           NetRes := PNetRes(ListRec^.List[I])^;
@@ -226,14 +214,14 @@ begin
           Break;
         end;
       end;
-      // open computer
-      if bFound and Assigned(ListRec^.List) and (iCount = 3) then
+      // open domen
+      if bFound and Assigned(ListRec^.List) and (iCount > 1) then
       begin
         bFound := False;
-        sDirName := Dirs[2];
+        sDirName := Dirs[1];
         for I := 0 to ListRec^.List.Count - 1 do
         begin
-          if PNetRes(ListRec^.List[i])^.RemoteName = '\\' + sDirName then
+          if PNetRes(ListRec^.List[i])^.RemoteName = sDirName then
           begin
             NetRes := PNetRes(ListRec^.List[I])^;
             FreeNetResList(ListRec^.List);
@@ -241,20 +229,40 @@ begin
             bFound := True;
             Break;
           end;
-        end; // for
-
-        if not Assigned(ListRec^.List) then
-        begin
-          Result := INVALID_HANDLE_VALUE;
-          Exit;
         end;
-      end; // openPC
-    end;   // open Domen
+        // open computer
+        if bFound and Assigned(ListRec^.List) and (iCount = 3) then
+        begin
+          bFound := False;
+          sDirName := Dirs[2];
+          for I := 0 to ListRec^.List.Count - 1 do
+          begin
+            if PNetRes(ListRec^.List[i])^.RemoteName = '\\' + sDirName then
+            begin
+              NetRes := PNetRes(ListRec^.List[I])^;
+              FreeNetResList(ListRec^.List);
+              ListRec^.List := GetContainerList(@NetRes);
+              bFound := True;
+              Break;
+            end;
+          end; // for
+
+          if not Assigned(ListRec^.List) then
+          begin
+            Result := wfxInvalidHandle;
+            Exit;
+          end;
+        end; // openPC
+      end;   // open Domen
 
-    FindData.dwFileAttributes := FILE_ATTRIBUTE_DIRECTORY;
-    StrPCopy(FindData.cFileName, ExtractFileName(ExcludeTrailingPathDelimiters(PNetRes(ListRec^.List[0])^.RemoteName)));
-  end; // PathDelim
-  Result := THandle(ListRec);
+      FindData.dwFileAttributes := FILE_ATTRIBUTE_DIRECTORY;
+      StrPCopy(FindData.cFileName, ExtractFileName(ExcludeTrailingPathDelimiters(PNetRes(ListRec^.List[0])^.RemoteName)));
+    end; // PathDelim
+
+    Result:= THandle(ListRec);
+  except
+    Result:= wfxInvalidHandle;
+  end;
 end;
 
 function FsFindNext(Hdl: THandle; var FindData: TWin32FindData): bool; stdcall;

+ 1 - 1
sdk/wfxplugin.pas

@@ -172,7 +172,7 @@ type
   THandle = {$IFDEF CPU64}QWord{$ELSE}LongWord{$ENDIF};
 
 const
-  hInvalidHandle: THandle = THandle(-1);
+  wfxInvalidHandle: THandle = THandle(-1);
 
 { Some Windows specific stuff }
 

+ 1 - 1
src/newdesign/uwfxpluginfilesource.pas

@@ -447,7 +447,7 @@ procedure TWfxPluginFileSource.FillAndCount(Files: TFiles; out NewFiles: TFiles;
     with FWfxModule do
     begin
       Handle := WfxFindFirst(srcPath, FindData);
-      if Handle = hInvalidHandle then Exit;
+      if Handle = wfxInvalidHandle then Exit;
 
       repeat
         if (FindData.FileName = '.') or (FindData.FileName = '..') then Continue;

+ 1 - 1
src/newdesign/uwfxpluginlistoperation.pas

@@ -85,7 +85,7 @@ begin
     end;
 
     Handle := WfxFindFirst(FCurrentPath, FindData);
-    if Handle = feInvalidHandle then Exit;
+    if Handle = wfxInvalidHandle then Exit;
     repeat
       if (FindData.FileName = '.') or (FindData.FileName = '..') then Continue;
 

+ 16 - 12
src/uwfxmodule.pas

@@ -245,18 +245,22 @@ var
   FindDataA: TWin32FindData;
   FindDataW: TWin32FindDataW;
 begin
-  if Assigned(FsFindFirstW) then
-    begin
-      Result:= FsFindFirstW(PWideChar(UTF8Decode(Path)), FindDataW);
-      if Result <> hInvalidHandle then
-        FindData:= LoadWfxFindData(FindDataW);
-    end
-  else if Assigned(FsFindFirst) then
-    begin
-      Result:= FsFindFirst(PAnsiChar(UTF8ToSys(Path)), FindDataA);
-      if Result <> hInvalidHandle then
-        FindData:= LoadWfxFindData(FindDataA);
-    end;
+  try
+    if Assigned(FsFindFirstW) then
+      begin
+        Result:= FsFindFirstW(PWideChar(UTF8Decode(Path)), FindDataW);
+        if Result <> wfxInvalidHandle then
+          FindData:= LoadWfxFindData(FindDataW);
+      end
+    else if Assigned(FsFindFirst) then
+      begin
+        Result:= FsFindFirst(PAnsiChar(UTF8ToSys(Path)), FindDataA);
+        if Result <> wfxInvalidHandle then
+          FindData:= LoadWfxFindData(FindDataA);
+      end;
+  except
+    Result:= wfxInvalidHandle;
+  end;
 end;
 
 function TWFXModule.WfxFindNext(Hdl: THandle; var FindData: TWfxFindData): Boolean;