dcfileattributes.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 UnixToWinFileAttr(const FileName: String; Attr: TFileAttrs): TFileAttrs;
  91. function SingleStrToFileAttr(sAttr: String): TFileAttrs;
  92. function WinSingleStrToFileAttr(sAttr: String): TFileAttrs;
  93. function UnixSingleStrToFileAttr(sAttr: String): TFileAttrs;
  94. {en
  95. Convert file attributes from string to number
  96. @param(Attributes File attributes as string)
  97. @returns(File attributes as number)
  98. }
  99. function StrToFileAttr(sAttr: String): TFileAttrs;
  100. {en
  101. Convert file attributes to string in the format of "attr1+attr2+attr3+".
  102. @param(Attributes File attributes)
  103. @returns(File attributes as string)
  104. }
  105. function FileAttrToStr(Attr: TFileAttrs): String;
  106. {en
  107. Convert Windows file attributes from string to number
  108. @param(Attributes File attributes as string)
  109. @returns(File attributes as number)
  110. }
  111. function WinStrToFileAttr(sAttr: String): TFileAttrs;
  112. {en
  113. Convert Unix file attributes from string to number
  114. @param(Attributes File attributes as string)
  115. @returns(File attributes as number)
  116. }
  117. function UnixStrToFileAttr(sAttr: String): TFileAttrs;
  118. function FormatNtfsAttributes(iAttr: TFileAttrs): String;
  119. function FormatUnixAttributes(iAttr: TFileAttrs): String;
  120. function FormatUnixModeOctal(iAttr: TFileAttrs): String;
  121. implementation
  122. uses
  123. DCStrUtils;
  124. type
  125. TAttrStrToFileAttr = record
  126. Str: String;
  127. Attr: TFileAttrs;
  128. end;
  129. const
  130. WinAttrStrToFileAttr: array[0..9] of TAttrStrToFileAttr = (
  131. (Str: 'r'; Attr: FILE_ATTRIBUTE_READONLY),
  132. (Str: 'h'; Attr: FILE_ATTRIBUTE_HIDDEN),
  133. (Str: 's'; Attr: FILE_ATTRIBUTE_SYSTEM),
  134. (Str: 'd'; Attr: FILE_ATTRIBUTE_DIRECTORY),
  135. (Str: 'a'; Attr: FILE_ATTRIBUTE_ARCHIVE),
  136. (Str: 't'; Attr: FILE_ATTRIBUTE_TEMPORARY),
  137. (Str: 'p'; Attr: FILE_ATTRIBUTE_SPARSE_FILE),
  138. (Str: 'l'; Attr: FILE_ATTRIBUTE_REPARSE_POINT),
  139. (Str: 'c'; Attr: FILE_ATTRIBUTE_COMPRESSED),
  140. (Str: 'e'; Attr: FILE_ATTRIBUTE_ENCRYPTED));
  141. UnixAttrStrToFileAttr: array[0..18] of TAttrStrToFileAttr = (
  142. // Permissions
  143. (Str: 'ur'; Attr: S_IRUSR),
  144. (Str: 'uw'; Attr: S_IWUSR),
  145. (Str: 'ux'; Attr: S_IXUSR),
  146. (Str: 'gr'; Attr: S_IRGRP),
  147. (Str: 'gw'; Attr: S_IWGRP),
  148. (Str: 'gx'; Attr: S_IXGRP),
  149. (Str: 'or'; Attr: S_IROTH),
  150. (Str: 'ow'; Attr: S_IWOTH),
  151. (Str: 'ox'; Attr: S_IXOTH),
  152. (Str: 'us'; Attr: S_ISUID),
  153. (Str: 'gs'; Attr: S_ISGID),
  154. (Str: 'sb'; Attr: S_ISVTX),
  155. // File types
  156. (Str: 'f'; Attr: S_IFIFO),
  157. (Str: 'c'; Attr: S_IFCHR),
  158. (Str: 'd'; Attr: S_IFDIR),
  159. (Str: 'b'; Attr: S_IFBLK),
  160. (Str: 'r'; Attr: S_IFREG),
  161. (Str: 'l'; Attr: S_IFLNK),
  162. (Str: 's'; Attr: S_IFSOCK));
  163. function WinToUnixFileAttr(Attr: TFileAttrs): TFileAttrs;
  164. begin
  165. Result := S_IRUSR or S_IRGRP or S_IROTH;
  166. if (Attr and faDirectory) <> 0 then
  167. Result := Result or S_IFDIR or S_IXUGO or S_IWUSR
  168. else begin
  169. Result := Result or S_IFREG;
  170. if (Attr and faReadOnly) = 0 then
  171. Result := Result or S_IWUSR;
  172. end;
  173. end;
  174. function UnixToWinFileAttr(Attr: TFileAttrs): TFileAttrs;
  175. begin
  176. Result := 0;
  177. case (Attr and S_IFMT) of
  178. 0, S_IFREG:
  179. Result := faArchive;
  180. S_IFLNK:
  181. Result := Result or faSymLink;
  182. S_IFDIR:
  183. Result := Result or faDirectory;
  184. S_IFIFO, S_IFCHR, S_IFBLK, S_IFSOCK:
  185. Result := Result or faSysFile;
  186. end;
  187. if (Attr and S_IWUSR) = 0 then
  188. Result := Result or faReadOnly;
  189. end;
  190. function UnixToWcxFileAttr(Attr: TFileAttrs): TFileAttrs;
  191. begin
  192. {$IF DEFINED(MSWINDOWS)}
  193. Result := UnixToWinFileAttr(Attr);
  194. {$ELSEIF DEFINED(UNIX)}
  195. Result := Attr;
  196. {$ELSE}
  197. Result := 0;
  198. {$ENDIF}
  199. end;
  200. function UnixToWinFileAttr(const FileName: String; Attr: TFileAttrs): TFileAttrs;
  201. begin
  202. Result := UnixToWinFileAttr(Attr);
  203. if (Length(FileName) > 1) and (FileName[1] = '.') and (FileName[2] <> '.') then
  204. Result := Result or faHidden;
  205. end;
  206. function SingleStrToFileAttr(sAttr: String): TFileAttrs;
  207. begin
  208. {$IF DEFINED(MSWINDOWS)}
  209. Result := WinSingleStrToFileAttr(sAttr);
  210. {$ELSEIF DEFINED(UNIX)}
  211. Result := UnixSingleStrToFileAttr(sAttr);
  212. {$ENDIF}
  213. end;
  214. function WinSingleStrToFileAttr(sAttr: String): TFileAttrs;
  215. var
  216. i: Integer;
  217. begin
  218. for i := Low(WinAttrStrToFileAttr) to High(WinAttrStrToFileAttr) do
  219. begin
  220. if sAttr = WinAttrStrToFileAttr[i].Str then
  221. Exit(WinAttrStrToFileAttr[i].Attr);
  222. end;
  223. Result := 0;
  224. end;
  225. function UnixSingleStrToFileAttr(sAttr: String): TFileAttrs;
  226. var
  227. i: Integer;
  228. begin
  229. if Length(sAttr) > 0 then
  230. begin
  231. if sAttr[1] in ['0'..'7'] then
  232. begin
  233. // Octal representation.
  234. Exit(TFileAttrs(OctToDec(sAttr)));
  235. end
  236. else
  237. begin
  238. for i := Low(UnixAttrStrToFileAttr) to High(UnixAttrStrToFileAttr) do
  239. begin
  240. if sAttr = UnixAttrStrToFileAttr[i].Str then
  241. Exit(UnixAttrStrToFileAttr[i].Attr);
  242. end;
  243. end;
  244. end;
  245. Result := 0;
  246. end;
  247. function StrToFileAttr(sAttr: String): TFileAttrs; inline;
  248. begin
  249. {$IF DEFINED(MSWINDOWS)}
  250. Result := WinStrToFileAttr(sAttr);
  251. {$ELSEIF DEFINED(UNIX)}
  252. Result := UnixStrToFileAttr(sAttr);
  253. {$ENDIF}
  254. end;
  255. function FileAttrToStr(Attr: TFileAttrs): String;
  256. var
  257. i: Integer;
  258. begin
  259. Result := '';
  260. {$IF DEFINED(MSWINDOWS)}
  261. for i := Low(WinAttrStrToFileAttr) to High(WinAttrStrToFileAttr) do
  262. begin
  263. if Attr and WinAttrStrToFileAttr[i].Attr <> 0 then
  264. Result := Result + WinAttrStrToFileAttr[i].Str + '+';
  265. end;
  266. {$ELSEIF DEFINED(UNIX)}
  267. for i := Low(UnixAttrStrToFileAttr) to High(UnixAttrStrToFileAttr) do
  268. begin
  269. if Attr and UnixAttrStrToFileAttr[i].Attr <> 0 then
  270. Result := Result + UnixAttrStrToFileAttr[i].Str + '+';
  271. end;
  272. {$ENDIF}
  273. end;
  274. function WinStrToFileAttr(sAttr: String): TFileAttrs;
  275. var
  276. I: LongInt;
  277. begin
  278. Result:= 0;
  279. sAttr:= LowerCase(sAttr);
  280. for I:= 1 to Length(sAttr) do
  281. case sAttr[I] of
  282. 'd': Result := Result or FILE_ATTRIBUTE_DIRECTORY;
  283. 'l': Result := Result or FILE_ATTRIBUTE_REPARSE_POINT;
  284. 'r': Result := Result or FILE_ATTRIBUTE_READONLY;
  285. 'a': Result := Result or FILE_ATTRIBUTE_ARCHIVE;
  286. 'h': Result := Result or FILE_ATTRIBUTE_HIDDEN;
  287. 's': Result := Result or FILE_ATTRIBUTE_SYSTEM;
  288. end;
  289. end;
  290. function UnixStrToFileAttr(sAttr: String): TFileAttrs;
  291. begin
  292. Result:= 0;
  293. if Length(sAttr) < 10 then Exit;
  294. if sAttr[1] = 'd' then Result:= Result or S_IFDIR;
  295. if sAttr[1] = 'l' then Result:= Result or S_IFLNK;
  296. if sAttr[1] = 's' then Result:= Result or S_IFSOCK;
  297. if sAttr[1] = 'f' then Result:= Result or S_IFIFO;
  298. if sAttr[1] = 'b' then Result:= Result or S_IFBLK;
  299. if sAttr[1] = 'c' then Result:= Result or S_IFCHR;
  300. if sAttr[2] = 'r' then Result:= Result or S_IRUSR;
  301. if sAttr[3] = 'w' then Result:= Result or S_IWUSR;
  302. if sAttr[4] = 'x' then Result:= Result or S_IXUSR;
  303. if sAttr[5] = 'r' then Result:= Result or S_IRGRP;
  304. if sAttr[6] = 'w' then Result:= Result or S_IWGRP;
  305. if sAttr[7] = 'x' then Result:= Result or S_IXGRP;
  306. if sAttr[8] = 'r' then Result:= Result or S_IROTH;
  307. if sAttr[9] = 'w' then Result:= Result or S_IWOTH;
  308. if sAttr[10] = 'x' then Result:= Result or S_IXOTH;
  309. if sAttr[4] = 'S' then Result:= Result or S_ISUID;
  310. if sAttr[7] = 'S' then Result:= Result or S_ISGID;
  311. if sAttr[10] = 'T' then Result:= Result or S_ISVTX;
  312. if sAttr[4] = 's' then Result:= Result or S_IXUSR or S_ISUID;
  313. if sAttr[7] = 's' then Result:= Result or S_IXGRP or S_ISGID;
  314. if sAttr[10] = 't' then Result:= Result or S_IXOTH or S_ISVTX;
  315. end;
  316. function FormatNtfsAttributes(iAttr: TFileAttrs): String;
  317. begin
  318. Result:= '--------';
  319. if (iAttr and FILE_ATTRIBUTE_DIRECTORY ) <> 0 then Result[1] := 'd';
  320. if (iAttr and FILE_ATTRIBUTE_REPARSE_POINT) <> 0 then Result[1] := 'l';
  321. if (iAttr and FILE_ATTRIBUTE_READONLY ) <> 0 then Result[2] := 'r';
  322. if (iAttr and FILE_ATTRIBUTE_ARCHIVE ) <> 0 then Result[3] := 'a';
  323. if (iAttr and FILE_ATTRIBUTE_HIDDEN ) <> 0 then Result[4] := 'h';
  324. if (iAttr and FILE_ATTRIBUTE_SYSTEM ) <> 0 then Result[5] := 's';
  325. // These two are exclusive on NTFS.
  326. if (iAttr and FILE_ATTRIBUTE_COMPRESSED ) <> 0 then Result[6] := 'c';
  327. if (iAttr and FILE_ATTRIBUTE_ENCRYPTED ) <> 0 then Result[6] := 'e';
  328. if (iAttr and FILE_ATTRIBUTE_TEMPORARY ) <> 0 then Result[7] := 't';
  329. if (iAttr and FILE_ATTRIBUTE_SPARSE_FILE ) <> 0 then Result[8] := 'p';
  330. end;
  331. function FormatUnixAttributes(iAttr: TFileAttrs): String;
  332. begin
  333. Result:= '----------';
  334. if ((iAttr and S_IFMT) = S_IFDIR) then Result[1] := 'd';
  335. if ((iAttr and S_IFMT) = S_IFLNK) then Result[1] := 'l';
  336. if ((iAttr and S_IFMT) = S_IFSOCK) then Result[1] := 's';
  337. if ((iAttr and S_IFMT) = S_IFIFO) then Result[1] := 'f';
  338. if ((iAttr and S_IFMT) = S_IFBLK) then Result[1] := 'b';
  339. if ((iAttr and S_IFMT) = S_IFCHR) then Result[1] := 'c';
  340. if ((iAttr and S_IRUSR) = S_IRUSR) then Result[2] := 'r';
  341. if ((iAttr and S_IWUSR) = S_IWUSR) then Result[3] := 'w';
  342. if ((iAttr and S_IXUSR) = S_IXUSR) then Result[4] := 'x';
  343. if ((iAttr and S_IRGRP) = S_IRGRP) then Result[5] := 'r';
  344. if ((iAttr and S_IWGRP) = S_IWGRP) then Result[6] := 'w';
  345. if ((iAttr and S_IXGRP) = S_IXGRP) then Result[7] := 'x';
  346. if ((iAttr and S_IROTH) = S_IROTH) then Result[8] := 'r';
  347. if ((iAttr and S_IWOTH) = S_IWOTH) then Result[9] := 'w';
  348. if ((iAttr and S_IXOTH) = S_IXOTH) then Result[10] := 'x';
  349. if ((iAttr and S_ISUID) = S_ISUID) then
  350. begin
  351. if Result[4] = 'x' then
  352. Result[4] := 's'
  353. else
  354. Result[4] := 'S';
  355. end;
  356. if ((iAttr and S_ISGID) = S_ISGID) then
  357. begin
  358. if Result[7] = 'x' then
  359. Result[7] := 's'
  360. else
  361. Result[7] := 'S';
  362. end;
  363. if ((iAttr and S_ISVTX) = S_ISVTX) then
  364. begin
  365. if Result[10] = 'x' then
  366. Result[10] := 't'
  367. else
  368. Result[10] := 'T';
  369. end;
  370. end;
  371. function FormatUnixModeOctal(iAttr: TFileAttrs): String;
  372. begin
  373. Result:= DecToOct(iAttr and $0FFF);
  374. Result:= StringOfChar('0', 4 - Length(Result)) + Result;
  375. end;
  376. end.