pas2jsfileutilsnodejs.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. {%MainUnit pas2jsfileutils.pas}
  2. {
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 2018 Mattias Gaertner [email protected]
  5. NodeJS backend of pas2jsfileutils
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************
  12. }
  13. function FilenameIsAbsolute(const aFilename: string): boolean;
  14. begin
  15. Result:=NJS_Path.isAbsolute(aFilename);
  16. end;
  17. function ExpandFileNamePJ(const FileName: string; BaseDir: string): string;
  18. var
  19. IsAbs: Boolean;
  20. HomeDir, Fn: String;
  21. begin
  22. Fn := GetForcedPathDelims(Filename);
  23. IsAbs := FileNameIsUnixAbsolute(Fn);
  24. if (not IsAbs) then
  25. begin
  26. if ((Length(Fn) > 1) and (Fn[1] = '~') and (Fn[2] = '/')) or (Fn = '~') then
  27. begin
  28. HomeDir := GetEnvironmentVariablePJ('HOME');
  29. if not FileNameIsUnixAbsolute(HomeDir) then
  30. HomeDir := ExpandFileNamePJ(HomeDir,'');
  31. Fn := HomeDir + Copy(Fn,2,length(Fn));
  32. IsAbs := True;
  33. end;
  34. end;
  35. if IsAbs then
  36. begin
  37. Result := ResolveDots(Fn);
  38. end
  39. else
  40. begin
  41. if (BaseDir = '') then
  42. Fn := IncludeTrailingPathDelimiter(GetCurrentDirPJ) + Fn
  43. else
  44. Fn := IncludeTrailingPathDelimiter(BaseDir) + Fn;
  45. Fn := ResolveDots(Fn);
  46. //if BaseDir is not absolute then this needs to be expanded as well
  47. if not FileNameIsUnixAbsolute(Fn) then
  48. Fn := ExpandFileNamePJ(Fn, '');
  49. Result := Fn;
  50. end;
  51. end;
  52. function GetCurrentDirPJ: String;
  53. begin
  54. Result:=GetCurrentDir;
  55. end;
  56. function GetPhysicalFilename(const Filename: string; ExceptionOnError: boolean
  57. ): string;
  58. var
  59. OldPath, NewPath: String;
  60. p, l: integer;
  61. begin
  62. Result:=Filename;
  63. p:=1;
  64. l:=length(Result);
  65. while p<=l do
  66. begin
  67. while (p<=l) and (Result[p]='/') do
  68. inc(p);
  69. if p>l then exit;
  70. if Result[p]<>'/' then
  71. begin
  72. repeat
  73. inc(p);
  74. until (p>l) or (Result[p]='/');
  75. OldPath:=LeftStr(Result,p-1);
  76. NewPath:=ResolveSymLinks(OldPath,ExceptionOnError);
  77. if NewPath='' then exit('');
  78. if OldPath<>NewPath then
  79. begin
  80. Result:=NewPath+copy(Result,length(OldPath)+1,length(Result));
  81. p:=length(NewPath)+1;
  82. end;
  83. end;
  84. end;
  85. end;
  86. function ResolveSymLinks(const Filename: string; ExceptionOnError: boolean
  87. ): string;
  88. var
  89. LinkFilename: string;
  90. AText: string;
  91. Depth: Integer;
  92. begin
  93. Result:=Filename;
  94. Depth:=0;
  95. while Depth<12 do
  96. begin
  97. inc(Depth);
  98. try
  99. LinkFilename:=NJS_FS.readlinkSync(Result);
  100. except
  101. if not ExceptionOnError then
  102. exit;
  103. if isString(JSExceptValue) then
  104. AText:=String(JSExceptValue)
  105. else if isObject(JSExceptValue) and isString(TJSObject(JSExceptValue)['message']) then
  106. begin
  107. if TJSObject(JSExceptValue)['code']='EINVAL' then
  108. begin
  109. // not a symbolic link
  110. exit;
  111. end;
  112. AText:=String(TJSObject(JSExceptValue)['message']);
  113. end else
  114. AText:='uknown error ('+jsTypeOf(JSExceptValue)+')';
  115. if Pos(Filename,AText)<1 then
  116. AText+=' "'+Filename+'"';
  117. raise EFOpenError.Create(AText);
  118. end;
  119. if LinkFilename='' then
  120. begin
  121. // not a symbolic link, just a regular file
  122. exit;
  123. end;
  124. if not FilenameIsAbsolute(LinkFilename) then
  125. Result:=ExtractFilePath(Result)+LinkFilename
  126. else
  127. Result:=LinkFilename;
  128. end;
  129. // probably an endless loop
  130. if ExceptionOnError then
  131. raise EFOpenError.Create('too many links, maybe an endless loop.')
  132. else
  133. Result:='';
  134. end;
  135. function IsUNCPath(const Path: String): Boolean;
  136. begin
  137. Result := false;
  138. end;
  139. function ExtractUNCVolume(const Path: String): String;
  140. begin
  141. Result := '';
  142. end;
  143. function FileIsWritable(const AFilename: string): boolean;
  144. begin
  145. try
  146. NJS_FS.accessSync(AFilename,W_OK);
  147. except
  148. exit(false);
  149. end;
  150. Result:=true;
  151. end;
  152. function FileIsExecutable(const AFilename: string): boolean;
  153. begin
  154. try
  155. NJS_FS.accessSync(AFilename,X_OK);
  156. except
  157. exit(false);
  158. end;
  159. Result:=true;
  160. end;
  161. function GetEnvironmentVariableCountPJ: Integer;
  162. begin
  163. Result:=GetEnvironmentVariableCount;
  164. end;
  165. function GetEnvironmentStringPJ(Index: Integer): string;
  166. begin
  167. Result:=GetEnvironmentString(Index);
  168. end;
  169. function GetEnvironmentVariablePJ(const EnvVar: string): String;
  170. begin
  171. Result:=GetEnvironmentVariable(EnvVar);
  172. end;
  173. function GetConsoleTextEncoding: string;
  174. begin
  175. Result:=GetDefaultTextEncoding;
  176. end;
  177. procedure InitPlatform;
  178. begin
  179. end;