UPD: refactor uDarwinFileUtil

(cherry picked from commit 62bff9e3f8)
This commit is contained in:
rich2014 2025-12-08 22:13:53 +08:00 committed by Alexander Koblov
commit f210dfd570
3 changed files with 30 additions and 30 deletions

View file

@ -11,25 +11,25 @@ uses
uDebug, uLog,
MacOSAll, CocoaAll, Cocoa_Extra, CocoaUtils;
function MountNetworkDrive(const serverAddress: String): Boolean;
function unmountAndEject(const path: String): Boolean;
var
HasMountURL: Boolean = False;
type
TDarwinFileUtil = class
private class var
NetFS: TLibHandle;
CoreServices: TLibHandle;
FSMountServerVolumeSync: function(url: CFURLRef; mountDir: CFURLRef; user: CFStringRef; password: CFStringRef;
mountedVolumeRefNum: FSVolumeRefNumPtr; flags: OptionBits): OSStatus; stdcall;
NetFSMountURLSync: function(_url: CFURLRef; _mountpath: CFURLRef; _user: CFStringRef; _passwd: CFStringRef;
_open_options: CFMutableDictionaryRef; _mount_options: CFMutableDictionaryRef; _mountpoints: CFArrayRefPtr): Int32; cdecl;
public class var
isMountSupported: Boolean;
public
class function mount(const serverAddress: String): Boolean;
class function unmountAndEject(const path: String): Boolean;
end;
implementation
var
NetFS: TLibHandle = NilHandle;
CoreServices: TLibHandle = NilHandle;
var
FSMountServerVolumeSync: function(url: CFURLRef; mountDir: CFURLRef; user: CFStringRef; password: CFStringRef;
mountedVolumeRefNum: FSVolumeRefNumPtr; flags: OptionBits): OSStatus; stdcall;
NetFSMountURLSync: function(_url: CFURLRef; _mountpath: CFURLRef; _user: CFStringRef; _passwd: CFStringRef;
_open_options: CFMutableDictionaryRef; _mount_options: CFMutableDictionaryRef; _mountpoints: CFArrayRefPtr): Int32; cdecl;
function MountNetworkDrive(const serverAddress: String): Boolean;
class function TDarwinFileUtil.mount(const serverAddress: String): Boolean;
var
sharePath: NSURL;
mountPoints: CFArrayRef = nil;
@ -85,30 +85,30 @@ begin
self.Free;
end;
function unmountAndEject(const path: String): Boolean;
class function TDarwinFileUtil.unmountAndEject(const path: String): Boolean;
begin
Result:= TUnmountManager.unmount( path, True );
end;
procedure Initialize;
begin
NetFS:= LoadLibrary('/System/Library/Frameworks/NetFS.framework/NetFS');
if (NetFS <> NilHandle) then
TDarwinFileUtil.NetFS:= LoadLibrary('/System/Library/Frameworks/NetFS.framework/NetFS');
if (TDarwinFileUtil.NetFS <> NilHandle) then
begin
@NetFSMountURLSync:= GetProcAddress(NetFS, 'NetFSMountURLSync');
@TDarwinFileUtil.NetFSMountURLSync:= GetProcAddress(TDarwinFileUtil.NetFS, 'NetFSMountURLSync');
end;
CoreServices:= LoadLibrary('/System/Library/Frameworks/CoreServices.framework/CoreServices');
if (CoreServices <> NilHandle) then
TDarwinFileUtil.CoreServices:= LoadLibrary('/System/Library/Frameworks/CoreServices.framework/CoreServices');
if (TDarwinFileUtil.CoreServices <> NilHandle) then
begin
@FSMountServerVolumeSync:= GetProcAddress(CoreServices, 'FSMountServerVolumeSync');
@TDarwinFileUtil.FSMountServerVolumeSync:= GetProcAddress(TDarwinFileUtil.CoreServices, 'FSMountServerVolumeSync');
end;
HasMountURL:= Assigned(NetFSMountURLSync) or Assigned(FSMountServerVolumeSync);
TDarwinFileUtil.isMountSupported:= Assigned(TDarwinFileUtil.NetFSMountURLSync) or Assigned(TDarwinFileUtil.FSMountServerVolumeSync);
end;
procedure Finalize;
begin
if (NetFS <> NilHandle) then FreeLibrary(NetFS);
if (CoreServices <> NilHandle) then FreeLibrary(CoreServices);
if (TDarwinFileUtil.NetFS <> NilHandle) then FreeLibrary(TDarwinFileUtil.NetFS);
if (TDarwinFileUtil.CoreServices <> NilHandle) then FreeLibrary(TDarwinFileUtil.CoreServices);
end;
initialization

View file

@ -503,7 +503,7 @@ begin
Result := fpSystemStatus('pumount ' + Drive^.DeviceId) = 0;
if not Result then
{$ELSEIF DEFINED(DARWIN)}
Result := unmountAndEject( Drive^.Path );
Result := TDarwinFileUtil.unmountAndEject( Drive^.Path );
if not Result then
{$ENDIF}
Result := fpSystemStatus('umount ' + Drive^.Path) = 0;
@ -525,7 +525,7 @@ begin
Result := uUDisks2.Eject(Drive^.DeviceId);
if not Result then
{$ELSEIF DEFINED(DARWIN)}
Result := unmountAndEject( Drive^.Path );
Result := TDarwinFileUtil.unmountAndEject( Drive^.Path );
if not Result then
{$ENDIF}
Result := fpSystemStatus('eject ' + Drive^.DeviceId) = 0;

View file

@ -720,7 +720,7 @@ begin
{$ENDIF}
{$IF DEFINED(DARWIN)}
if HasMountURL then
if TDarwinFileUtil.isMountSupported then
begin
with frmMain do
begin
@ -1071,7 +1071,7 @@ begin
if ShowInputQuery(Application.Title, rsMsgURL, False, Address) then
begin
{$IF DEFINED(DARWIN)}
MountNetworkDrive(Address);
TDarwinFileUtil.mount(Address);
{$ELSE}
ChooseFileSource(frmMain.ActiveFrame, Address);
{$ENDIF}