dcdarwin.pas 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. unit DCDarwin;
  2. {$mode delphi}
  3. {$packrecords c}
  4. {$pointermath on}
  5. {$modeswitch objectivec1}
  6. interface
  7. uses
  8. Classes, SysUtils, DCBasicTypes, CocoaAll, BaseUnix;
  9. const
  10. CLOSE_RANGE_CLOEXEC = (1 << 2);
  11. function CloseRange(first: cuint; last: cuint; flags: cint): cint; cdecl;
  12. // MacOS File Utils
  13. function MacosFileSetCreationTime( const path:String; const birthtime:TFileTimeEx ): Boolean;
  14. implementation
  15. uses
  16. DCUnix;
  17. type
  18. proc_fdinfo = record
  19. proc_fd: Int32;
  20. proc_fdtype: UInt32;
  21. end;
  22. Pproc_fdinfo = ^proc_fdinfo;
  23. const
  24. PROC_PIDLISTFDS = 1;
  25. PROC_PIDLISTFD_SIZE = SizeOf(proc_fdinfo);
  26. function proc_pidinfo(pid: cint; flavor: cint; arg: cuint64; buffer: pointer; buffersize: cint): cint; cdecl; external 'proc';
  27. function CloseRange(first: cuint; last: cuint; flags: cint): cint; cdecl;
  28. var
  29. I: cint;
  30. Handle: cint;
  31. ProcessId: TPid;
  32. bufferSize: cint;
  33. pidInfo: Pproc_fdinfo;
  34. begin
  35. Result:= -1;
  36. ProcessId:= FpGetpid;
  37. bufferSize:= proc_pidinfo(ProcessId, PROC_PIDLISTFDS, 0, nil, 0);
  38. pidInfo:= GetMem(bufferSize);
  39. if Assigned(pidInfo) then
  40. begin
  41. bufferSize:= proc_pidinfo(ProcessId, PROC_PIDLISTFDS, 0, pidInfo, bufferSize);
  42. for I:= 0 to (bufferSize div PROC_PIDLISTFD_SIZE) - 1 do
  43. begin
  44. Handle:= pidInfo[I].proc_fd;
  45. if (Handle >= first) and (Handle <= last) then
  46. begin
  47. if (flags and CLOSE_RANGE_CLOEXEC <> 0) then
  48. FileCloseOnExec(Handle)
  49. else begin
  50. FileClose(Handle);
  51. end;
  52. end;
  53. end;
  54. Result:= 0;
  55. FreeMem(pidInfo);
  56. end;
  57. end;
  58. function StringToNSString(const S: String): NSString;
  59. begin
  60. Result:= NSString(NSString.stringWithUTF8String(PAnsiChar(S)));
  61. end;
  62. function MacosFileSetCreationTime( const path:String; const birthtime:TFileTimeEx ): Boolean;
  63. var
  64. seconds: Double;
  65. attrs: NSMutableDictionary;
  66. nsPath: NSString;
  67. begin
  68. Result:= true;
  69. if birthtime = TFileTimeExNull then exit;
  70. seconds:= birthtime.sec.ToDouble + birthtime.nanosec.ToDouble / (1000.0*1000.0*1000.0);
  71. attrs:= NSMutableDictionary.dictionaryWithCapacity( 1 );
  72. attrs.setValue_forKey( NSDate.dateWithTimeIntervalSince1970(seconds), NSFileCreationDate );
  73. nsPath:= StringToNSString( path );
  74. Result:= NSFileManager.defaultManager.setAttributes_ofItemAtPath_error( attrs, nsPath, nil );
  75. end;
  76. end.