dcfileattributes.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. {
  2. Double Commander
  3. -------------------------------------------------------------------------
  4. Functions handling file attributes.
  5. Copyright (C) 2012 Przemysław Nagay ([email protected])
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. }
  18. unit DCFileAttributes;
  19. {$mode objfpc}{$H+}
  20. interface
  21. uses
  22. Classes, SysUtils, DCBasicTypes;
  23. const
  24. // Windows attributes
  25. FILE_ATTRIBUTE_READONLY = $0001;
  26. FILE_ATTRIBUTE_HIDDEN = $0002;
  27. FILE_ATTRIBUTE_SYSTEM = $0004;
  28. FILE_ATTRIBUTE_VOLUME = $0008;
  29. FILE_ATTRIBUTE_DIRECTORY = $0010;
  30. FILE_ATTRIBUTE_ARCHIVE = $0020;
  31. FILE_ATTRIBUTE_DEVICE = $0040;
  32. FILE_ATTRIBUTE_NORMAL = $0080;
  33. FILE_ATTRIBUTE_TEMPORARY = $0100;
  34. FILE_ATTRIBUTE_SPARSE_FILE = $0200;
  35. FILE_ATTRIBUTE_REPARSE_POINT = $0400;
  36. FILE_ATTRIBUTE_COMPRESSED = $0800;
  37. FILE_ATTRIBUTE_OFFLINE = $1000;
  38. FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = $2000;
  39. FILE_ATTRIBUTE_ENCRYPTED = $4000;
  40. FILE_ATTRIBUTE_VIRTUAL = $10000;
  41. FILE_ATTRIBUTE_PINNED = $80000;
  42. FILE_ATTRIBUTE_UNPINNED = $100000;
  43. FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS = $400000;
  44. // Unix attributes
  45. { attributes mask }
  46. S_IFMT = $F000;
  47. { first-in/first-out (FIFO/pipe) }
  48. S_IFIFO = $1000;
  49. { character-special file (tty/console) }
  50. S_IFCHR = $2000;
  51. { directory }
  52. S_IFDIR = $4000;
  53. { blocking device (unused) }
  54. S_IFBLK = $6000;
  55. { regular }
  56. S_IFREG = $8000;
  57. { symbolic link (unused) }
  58. S_IFLNK = $A000;
  59. { Berkeley socket }
  60. S_IFSOCK = $C000;
  61. { mode_t possible values }
  62. S_IRUSR = %0100000000; { Read permission for owner }
  63. S_IWUSR = %0010000000; { Write permission for owner }
  64. S_IXUSR = %0001000000; { Exec permission for owner }
  65. S_IRGRP = %0000100000; { Read permission for group }
  66. S_IWGRP = %0000010000; { Write permission for group }
  67. S_IXGRP = %0000001000; { Exec permission for group }
  68. S_IROTH = %0000000100; { Read permission for world }
  69. S_IWOTH = %0000000010; { Write permission for world }
  70. S_IXOTH = %0000000001; { Exec permission for world }
  71. S_IRWXU = S_IRUSR or S_IWUSR or S_IXUSR;
  72. S_IRWXG = S_IRGRP or S_IWGRP or S_IXGRP;
  73. S_IRWXO = S_IROTH or S_IWOTH or S_IXOTH;
  74. S_IXUGO = S_IXUSR or S_IXGRP or S_IXOTH;
  75. { POSIX setuid(), setgid(), and sticky bit }
  76. S_ISUID = $0800;
  77. S_ISGID = $0400;
  78. S_ISVTX = $0200;
  79. // Generic attributes
  80. {$IF DEFINED(MSWINDOWS)}
  81. GENERIC_ATTRIBUTE_FILE = FILE_ATTRIBUTE_ARCHIVE;
  82. GENERIC_ATTRIBUTE_FOLDER = GENERIC_ATTRIBUTE_FILE or FILE_ATTRIBUTE_DIRECTORY;
  83. {$ELSEIF DEFINED(UNIX)}
  84. GENERIC_ATTRIBUTE_FILE = S_IRUSR or S_IWUSR or S_IRGRP or S_IROTH;
  85. GENERIC_ATTRIBUTE_FOLDER = GENERIC_ATTRIBUTE_FILE or S_IFDIR or S_IXUGO;
  86. {$ENDIF}
  87. function WinToUnixFileAttr(Attr: TFileAttrs): TFileAttrs;
  88. function UnixToWinFileAttr(Attr: TFileAttrs): TFileAttrs;
  89. function UnixToWcxFileAttr(Attr: TFileAttrs): TFileAttrs;
  90. function WinToWcxFileAttr(Attr: TFileAttrs): TFileAttrs;
  91. function UnixToWinFileAttr(const FileName: String; Attr: TFileAttrs): TFileAttrs;
  92. function SingleStrToFileAttr(sAttr: String): TFileAttrs;
  93. function WinSingleStrToFileAttr(sAttr: String): TFileAttrs;
  94. function UnixSingleStrToFileAttr(sAttr: String): TFileAttrs;
  95. {en
  96. Convert file attributes from string to number
  97. @param(Attributes File attributes as string)
  98. @returns(File attributes as number)
  99. }
  100. function StrToFileAttr(sAttr: String): TFileAttrs;
  101. {en
  102. Convert file attributes to string in the format of "attr1+attr2+attr3+".
  103. @param(Attributes File attributes)
  104. @returns(File attributes as string)
  105. }
  106. function FileAttrToStr(Attr: TFileAttrs): String;
  107. {en
  108. Convert Windows file attributes from string to number
  109. @param(Attributes File attributes as string)
  110. @returns(File attributes as number)
  111. }
  112. function WinStrToFileAttr(sAttr: String): TFileAttrs;
  113. {en
  114. Convert Unix file attributes from string to number
  115. @param(Attributes File attributes as string)
  116. @returns(File attributes as number)
  117. }
  118. function UnixStrToFileAttr(sAttr: String): TFileAttrs;
  119. function FormatNtfsAttributes(iAttr: TFileAttrs): String;
  120. function FormatUnixAttributes(iAttr: TFileAttrs): String;
  121. function FormatUnixModeOctal(iAttr: TFileAttrs): String;
  122. implementation
  123. uses
  124. DCStrUtils;
  125. type
  126. TAttrStrToFileAttr = record
  127. Str: String;
  128. Attr: TFileAttrs;
  129. end;
  130. const
  131. WinAttrStrToFileAttr: array[0..9] of TAttrStrToFileAttr = (
  132. (Str: 'r'; Attr: FILE_ATTRIBUTE_READONLY),
  133. (Str: 'h'; Attr: FILE_ATTRIBUTE_HIDDEN),
  134. (Str: 's'; Attr: FILE_ATTRIBUTE_SYSTEM),
  135. (Str: 'd'; Attr: FILE_ATTRIBUTE_DIRECTORY),
  136. (Str: 'a'; Attr: FILE_ATTRIBUTE_ARCHIVE),
  137. (Str: 't'; Attr: FILE_ATTRIBUTE_TEMPORARY),
  138. (Str: 'p'; Attr: FILE_ATTRIBUTE_SPARSE_FILE),
  139. (Str: 'l'; Attr: FILE_ATTRIBUTE_REPARSE_POINT),
  140. (Str: 'c'; Attr: FILE_ATTRIBUTE_COMPRESSED),
  141. (Str: 'e'; Attr: FILE_ATTRIBUTE_ENCRYPTED));
  142. UnixAttrStrToFileAttr: array[0..18] of TAttrStrToFileAttr = (
  143. // Permissions
  144. (Str: 'ur'; Attr: S_IRUSR),
  145. (Str: 'uw'; Attr: S_IWUSR),
  146. (Str: 'ux'; Attr: S_IXUSR),
  147. (Str: 'gr'; Attr: S_IRGRP),
  148. (Str: 'gw'; Attr: S_IWGRP),
  149. (Str: 'gx'; Attr: S_IXGRP),
  150. (Str: 'or'; Attr: S_IROTH),
  151. (Str: 'ow'; Attr: S_IWOTH),
  152. (Str: 'ox'; Attr: S_IXOTH),
  153. (Str: 'us'; Attr: S_ISUID),
  154. (Str: 'gs'; Attr: S_ISGID),
  155. (Str: 'sb'; Attr: S_ISVTX),
  156. // File types
  157. (Str: 'f'; Attr: S_IFIFO),
  158. (Str: 'c'; Attr: S_IFCHR),
  159. (Str: 'd'; Attr: S_IFDIR),
  160. (Str: 'b'; Attr: S_IFBLK),
  161. (Str: 'r'; Attr: S_IFREG),
  162. (Str: 'l'; Attr: S_IFLNK),
  163. (Str: 's'; Attr: S_IFSOCK));
  164. function WinToUnixFileAttr(Attr: TFileAttrs): TFileAttrs;
  165. begin
  166. Result := S_IRUSR or S_IRGRP or S_IROTH;
  167. if (Attr and faDirectory) <> 0 then
  168. Result := Result or S_IFDIR or S_IXUGO or S_IWUSR
  169. else begin
  170. Result := Result or S_IFREG;
  171. if (Attr and faReadOnly) = 0 then
  172. Result := Result or S_IWUSR;
  173. end;
  174. end;
  175. function UnixToWinFileAttr(Attr: TFileAttrs): TFileAttrs;
  176. begin
  177. Result := 0;
  178. case (Attr and S_IFMT) of
  179. 0, S_IFREG:
  180. Result := faArchive;
  181. S_IFLNK:
  182. Result := Result or faSymLink;
  183. S_IFDIR:
  184. Result := Result or faDirectory;
  185. S_IFIFO, S_IFCHR, S_IFBLK, S_IFSOCK:
  186. Result := Result or faSysFile;
  187. end;
  188. if (Attr and S_IWUSR) = 0 then
  189. Result := Result or faReadOnly;
  190. end;
  191. function UnixToWcxFileAttr(Attr: TFileAttrs): TFileAttrs;
  192. begin
  193. {$IF DEFINED(MSWINDOWS)}
  194. Result := UnixToWinFileAttr(Attr);
  195. {$ELSEIF DEFINED(UNIX)}
  196. Result := Attr;
  197. {$ELSE}
  198. Result := 0;
  199. {$ENDIF}
  200. end;
  201. function WinToWcxFileAttr(Attr: TFileAttrs): TFileAttrs;
  202. begin
  203. {$IF DEFINED(UNIX)}
  204. Result := WinToUnixFileAttr(Attr);
  205. {$ELSEIF DEFINED(MSWINDOWS)}
  206. Result := Attr;
  207. {$ELSE}
  208. Result := 0;
  209. {$ENDIF}
  210. end;
  211. function UnixToWinFileAttr(const FileName: String; Attr: TFileAttrs): TFileAttrs;
  212. begin
  213. Result := UnixToWinFileAttr(Attr);
  214. if (Length(FileName) > 1) and (FileName[1] = '.') and (FileName[2] <> '.') then
  215. Result := Result or faHidden;
  216. end;
  217. function SingleStrToFileAttr(sAttr: String): TFileAttrs;
  218. begin
  219. {$IF DEFINED(MSWINDOWS)}
  220. Result := WinSingleStrToFileAttr(sAttr);
  221. {$ELSEIF DEFINED(UNIX)}
  222. Result := UnixSingleStrToFileAttr(sAttr);
  223. {$ENDIF}
  224. end;
  225. function WinSingleStrToFileAttr(sAttr: String): TFileAttrs;
  226. var
  227. i: Integer;
  228. begin
  229. for i := Low(WinAttrStrToFileAttr) to High(WinAttrStrToFileAttr) do
  230. begin
  231. if sAttr = WinAttrStrToFileAttr[i].Str then
  232. Exit(WinAttrStrToFileAttr[i].Attr);
  233. end;
  234. Result := 0;
  235. end;
  236. function UnixSingleStrToFileAttr(sAttr: String): TFileAttrs;
  237. var
  238. i: Integer;
  239. begin
  240. if Length(sAttr) > 0 then
  241. begin
  242. if sAttr[1] in ['0'..'7'] then
  243. begin
  244. // Octal representation.
  245. Exit(TFileAttrs(OctToDec(sAttr)));
  246. end
  247. else
  248. begin
  249. for i := Low(UnixAttrStrToFileAttr) to High(UnixAttrStrToFileAttr) do
  250. begin
  251. if sAttr = UnixAttrStrToFileAttr[i].Str then
  252. Exit(UnixAttrStrToFileAttr[i].Attr);
  253. end;
  254. end;
  255. end;
  256. Result := 0;
  257. end;
  258. function StrToFileAttr(sAttr: String): TFileAttrs; inline;
  259. begin
  260. {$IF DEFINED(MSWINDOWS)}
  261. Result := WinStrToFileAttr(sAttr);
  262. {$ELSEIF DEFINED(UNIX)}
  263. Result := UnixStrToFileAttr(sAttr);
  264. {$ENDIF}
  265. end;
  266. function FileAttrToStr(Attr: TFileAttrs): String;
  267. var
  268. i: Integer;
  269. begin
  270. Result := '';
  271. {$IF DEFINED(MSWINDOWS)}
  272. for i := Low(WinAttrStrToFileAttr) to High(WinAttrStrToFileAttr) do
  273. begin
  274. if Attr and WinAttrStrToFileAttr[i].Attr <> 0 then
  275. Result := Result + WinAttrStrToFileAttr[i].Str + '+';
  276. end;
  277. {$ELSEIF DEFINED(UNIX)}
  278. for i := Low(UnixAttrStrToFileAttr) to High(UnixAttrStrToFileAttr) do
  279. begin
  280. if Attr and UnixAttrStrToFileAttr[i].Attr <> 0 then
  281. Result := Result + UnixAttrStrToFileAttr[i].Str + '+';
  282. end;
  283. {$ENDIF}
  284. end;
  285. function WinStrToFileAttr(sAttr: String): TFileAttrs;
  286. var
  287. I: LongInt;
  288. begin
  289. Result:= 0;
  290. sAttr:= LowerCase(sAttr);
  291. for I:= 1 to Length(sAttr) do
  292. case sAttr[I] of
  293. 'd': Result := Result or FILE_ATTRIBUTE_DIRECTORY;
  294. 'l': Result := Result or FILE_ATTRIBUTE_REPARSE_POINT;
  295. 'r': Result := Result or FILE_ATTRIBUTE_READONLY;
  296. 'a': Result := Result or FILE_ATTRIBUTE_ARCHIVE;
  297. 'h': Result := Result or FILE_ATTRIBUTE_HIDDEN;
  298. 's': Result := Result or FILE_ATTRIBUTE_SYSTEM;
  299. end;
  300. end;
  301. function UnixStrToFileAttr(sAttr: String): TFileAttrs;
  302. begin
  303. Result:= 0;
  304. if Length(sAttr) < 10 then Exit;
  305. if sAttr[1] = 'd' then Result:= Result or S_IFDIR;
  306. if sAttr[1] = 'l' then Result:= Result or S_IFLNK;
  307. if sAttr[1] = 's' then Result:= Result or S_IFSOCK;
  308. if sAttr[1] = 'f' then Result:= Result or S_IFIFO;
  309. if sAttr[1] = 'b' then Result:= Result or S_IFBLK;
  310. if sAttr[1] = 'c' then Result:= Result or S_IFCHR;
  311. if sAttr[2] = 'r' then Result:= Result or S_IRUSR;
  312. if sAttr[3] = 'w' then Result:= Result or S_IWUSR;
  313. if sAttr[4] = 'x' then Result:= Result or S_IXUSR;
  314. if sAttr[5] = 'r' then Result:= Result or S_IRGRP;
  315. if sAttr[6] = 'w' then Result:= Result or S_IWGRP;
  316. if sAttr[7] = 'x' then Result:= Result or S_IXGRP;
  317. if sAttr[8] = 'r' then Result:= Result or S_IROTH;
  318. if sAttr[9] = 'w' then Result:= Result or S_IWOTH;
  319. if sAttr[10] = 'x' then Result:= Result or S_IXOTH;
  320. if sAttr[4] = 'S' then Result:= Result or S_ISUID;
  321. if sAttr[7] = 'S' then Result:= Result or S_ISGID;
  322. if sAttr[10] = 'T' then Result:= Result or S_ISVTX;
  323. if sAttr[4] = 's' then Result:= Result or S_IXUSR or S_ISUID;
  324. if sAttr[7] = 's' then Result:= Result or S_IXGRP or S_ISGID;
  325. if sAttr[10] = 't' then Result:= Result or S_IXOTH or S_ISVTX;
  326. end;
  327. function FormatNtfsAttributes(iAttr: TFileAttrs): String;
  328. begin
  329. Result:= '--------';
  330. if (iAttr and FILE_ATTRIBUTE_DIRECTORY ) <> 0 then Result[1] := 'd';
  331. if (iAttr and FILE_ATTRIBUTE_REPARSE_POINT) <> 0 then Result[1] := 'l';
  332. if (iAttr and FILE_ATTRIBUTE_READONLY ) <> 0 then Result[2] := 'r';
  333. if (iAttr and FILE_ATTRIBUTE_ARCHIVE ) <> 0 then Result[3] := 'a';
  334. if (iAttr and FILE_ATTRIBUTE_HIDDEN ) <> 0 then Result[4] := 'h';
  335. if (iAttr and FILE_ATTRIBUTE_SYSTEM ) <> 0 then Result[5] := 's';
  336. // These two are exclusive on NTFS.
  337. if (iAttr and FILE_ATTRIBUTE_COMPRESSED ) <> 0 then Result[6] := 'c';
  338. if (iAttr and FILE_ATTRIBUTE_ENCRYPTED ) <> 0 then Result[6] := 'e';
  339. if (iAttr and FILE_ATTRIBUTE_TEMPORARY ) <> 0 then Result[7] := 't';
  340. if (iAttr and FILE_ATTRIBUTE_SPARSE_FILE ) <> 0 then Result[8] := 'p';
  341. end;
  342. function FormatUnixAttributes(iAttr: TFileAttrs): String;
  343. begin
  344. Result:= '----------';
  345. if ((iAttr and S_IFMT) = S_IFDIR) then Result[1] := 'd';
  346. if ((iAttr and S_IFMT) = S_IFLNK) then Result[1] := 'l';
  347. if ((iAttr and S_IFMT) = S_IFSOCK) then Result[1] := 's';
  348. if ((iAttr and S_IFMT) = S_IFIFO) then Result[1] := 'f';
  349. if ((iAttr and S_IFMT) = S_IFBLK) then Result[1] := 'b';
  350. if ((iAttr and S_IFMT) = S_IFCHR) then Result[1] := 'c';
  351. if ((iAttr and S_IRUSR) = S_IRUSR) then Result[2] := 'r';
  352. if ((iAttr and S_IWUSR) = S_IWUSR) then Result[3] := 'w';
  353. if ((iAttr and S_IXUSR) = S_IXUSR) then Result[4] := 'x';
  354. if ((iAttr and S_IRGRP) = S_IRGRP) then Result[5] := 'r';
  355. if ((iAttr and S_IWGRP) = S_IWGRP) then Result[6] := 'w';
  356. if ((iAttr and S_IXGRP) = S_IXGRP) then Result[7] := 'x';
  357. if ((iAttr and S_IROTH) = S_IROTH) then Result[8] := 'r';
  358. if ((iAttr and S_IWOTH) = S_IWOTH) then Result[9] := 'w';
  359. if ((iAttr and S_IXOTH) = S_IXOTH) then Result[10] := 'x';
  360. if ((iAttr and S_ISUID) = S_ISUID) then
  361. begin
  362. if Result[4] = 'x' then
  363. Result[4] := 's'
  364. else
  365. Result[4] := 'S';
  366. end;
  367. if ((iAttr and S_ISGID) = S_ISGID) then
  368. begin
  369. if Result[7] = 'x' then
  370. Result[7] := 's'
  371. else
  372. Result[7] := 'S';
  373. end;
  374. if ((iAttr and S_ISVTX) = S_ISVTX) then
  375. begin
  376. if Result[10] = 'x' then
  377. Result[10] := 't'
  378. else
  379. Result[10] := 'T';
  380. end;
  381. end;
  382. function FormatUnixModeOctal(iAttr: TFileAttrs): String;
  383. begin
  384. Result:= DecToOct(iAttr and $0FFF);
  385. Result:= StringOfChar('0', 4 - Length(Result)) + Result;
  386. end;
  387. end.