您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

405 行
15 KiB

  1. %% Vendored from hex_core v0.5.1, do not edit manually
  2. % Copied from https://github.com/erlang/otp/blob/OTP-20.0.1/lib/stdlib/src/erl_tar.hrl
  3. %%
  4. %% %CopyrightBegin%
  5. %%
  6. %% Copyright Ericsson AB 2017. All Rights Reserved.
  7. %%
  8. %% Licensed under the Apache License, Version 2.0 (the "License");
  9. %% you may not use this file except in compliance with the License.
  10. %% You may obtain a copy of the License at
  11. %%
  12. %% http://www.apache.org/licenses/LICENSE-2.0
  13. %%
  14. %% Unless required by applicable law or agreed to in writing, software
  15. %% distributed under the License is distributed on an "AS IS" BASIS,
  16. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. %% See the License for the specific language governing permissions and
  18. %% limitations under the License.
  19. %%
  20. %% %CopyrightEnd%
  21. %% Options used when adding files to a tar archive.
  22. -record(add_opts, {
  23. read_info, %% Fun to use for read file/link info.
  24. chunk_size = 0, %% For file reading when sending to sftp. 0=do not chunk
  25. verbose = false, %% Verbose on/off.
  26. atime = undefined,
  27. mtime = undefined,
  28. ctime = undefined,
  29. uid = 0,
  30. gid = 0}).
  31. -type add_opts() :: #add_opts{}.
  32. %% Options used when reading a tar archive.
  33. -record(read_opts, {
  34. cwd :: string(), %% Current working directory.
  35. keep_old_files = false :: boolean(), %% Owerwrite or not.
  36. files = all, %% Set of files to extract (or all)
  37. output = file :: 'file' | 'memory',
  38. open_mode = [], %% Open mode options.
  39. verbose = false :: boolean()}). %% Verbose on/off.
  40. -type read_opts() :: #read_opts{}.
  41. -type add_opt() :: dereference |
  42. verbose |
  43. {chunks, pos_integer()}.
  44. -type extract_opt() :: {cwd, string()} |
  45. {files, [string()]} |
  46. compressed |
  47. cooked |
  48. memory |
  49. keep_old_files |
  50. verbose.
  51. -type create_opt() :: compressed |
  52. cooked |
  53. dereference |
  54. verbose.
  55. -type filelist() :: [file:filename() |
  56. {string(), binary()} |
  57. {string(), file:filename()}].
  58. -type tar_time() :: non_neg_integer().
  59. %% The tar header, once fully parsed.
  60. -record(tar_header, {
  61. name = "" :: string(), %% name of header file entry
  62. mode = 8#100644 :: non_neg_integer(), %% permission and mode bits
  63. uid = 0 :: non_neg_integer(), %% user id of owner
  64. gid = 0 :: non_neg_integer(), %% group id of owner
  65. size = 0 :: non_neg_integer(), %% length in bytes
  66. mtime :: tar_time(), %% modified time
  67. typeflag :: char(), %% type of header entry
  68. linkname = "" :: string(), %% target name of link
  69. uname = "" :: string(), %% user name of owner
  70. gname = "" :: string(), %% group name of owner
  71. devmajor = 0 :: non_neg_integer(), %% major number of character or block device
  72. devminor = 0 :: non_neg_integer(), %% minor number of character or block device
  73. atime :: tar_time(), %% access time
  74. ctime :: tar_time() %% status change time
  75. }).
  76. -type tar_header() :: #tar_header{}.
  77. %% Metadata for a sparse file fragment
  78. -record(sparse_entry, {
  79. offset = 0 :: non_neg_integer(),
  80. num_bytes = 0 :: non_neg_integer()}).
  81. -type sparse_entry() :: #sparse_entry{}.
  82. %% Contains metadata about fragments of a sparse file
  83. -record(sparse_array, {
  84. entries = [] :: [sparse_entry()],
  85. is_extended = false :: boolean(),
  86. max_entries = 0 :: non_neg_integer()}).
  87. -type sparse_array() :: #sparse_array{}.
  88. %% A subset of tar header fields common to all tar implementations
  89. -record(header_v7, {
  90. name :: binary(),
  91. mode :: binary(), %% octal
  92. uid :: binary(), %% integer
  93. gid :: binary(), %% integer
  94. size :: binary(), %% integer
  95. mtime :: binary(), %% integer
  96. checksum :: binary(), %% integer
  97. typeflag :: byte(), %% char
  98. linkname :: binary()}).
  99. -type header_v7() :: #header_v7{}.
  100. %% The set of fields specific to GNU tar formatted archives
  101. -record(header_gnu, {
  102. header_v7 :: header_v7(),
  103. magic :: binary(),
  104. version :: binary(),
  105. uname :: binary(),
  106. gname :: binary(),
  107. devmajor :: binary(), %% integer
  108. devminor :: binary(), %% integer
  109. atime :: binary(), %% integer
  110. ctime :: binary(), %% integer
  111. sparse :: sparse_array(),
  112. real_size :: binary()}). %% integer
  113. -type header_gnu() :: #header_gnu{}.
  114. %% The set of fields specific to STAR-formatted archives
  115. -record(header_star, {
  116. header_v7 :: header_v7(),
  117. magic :: binary(),
  118. version :: binary(),
  119. uname :: binary(),
  120. gname :: binary(),
  121. devmajor :: binary(), %% integer
  122. devminor :: binary(), %% integer
  123. prefix :: binary(),
  124. atime :: binary(), %% integer
  125. ctime :: binary(), %% integer
  126. trailer :: binary()}).
  127. -type header_star() :: #header_star{}.
  128. %% The set of fields specific to USTAR-formatted archives
  129. -record(header_ustar, {
  130. header_v7 :: header_v7(),
  131. magic :: binary(),
  132. version :: binary(),
  133. uname :: binary(),
  134. gname :: binary(),
  135. devmajor :: binary(), %% integer
  136. devminor :: binary(), %% integer
  137. prefix :: binary()}).
  138. -type header_ustar() :: #header_ustar{}.
  139. -type header_fields() :: header_v7() |
  140. header_gnu() |
  141. header_star() |
  142. header_ustar().
  143. %% The overall tar reader, it holds the low-level file handle,
  144. %% its access, position, and the I/O primitives wrapper.
  145. -record(reader, {
  146. handle :: file:io_device() | term(),
  147. access :: read | write | ram,
  148. pos = 0 :: non_neg_integer(),
  149. func :: file_op()
  150. }).
  151. -type reader() :: #reader{}.
  152. %% A reader for a regular file within the tar archive,
  153. %% It tracks its current state relative to that file.
  154. -record(reg_file_reader, {
  155. handle :: reader(),
  156. num_bytes = 0,
  157. pos = 0,
  158. size = 0
  159. }).
  160. -type reg_file_reader() :: #reg_file_reader{}.
  161. %% A reader for a sparse file within the tar archive,
  162. %% It tracks its current state relative to that file.
  163. -record(sparse_file_reader, {
  164. handle :: reader(),
  165. num_bytes = 0, %% bytes remaining
  166. pos = 0, %% pos
  167. size = 0, %% total size of file
  168. sparse_map = #sparse_array{}
  169. }).
  170. -type sparse_file_reader() :: #sparse_file_reader{}.
  171. %% Types for the readers
  172. -type reader_type() :: reader() | reg_file_reader() | sparse_file_reader().
  173. -type handle() :: file:io_device() | term().
  174. %% Type for the I/O primitive wrapper function
  175. -type file_op() :: fun((write | close | read2 | position,
  176. {handle(), iodata()} | handle() | {handle(), non_neg_integer()}
  177. | {handle(), non_neg_integer()}) ->
  178. ok | eof | {ok, string() | binary()} | {ok, non_neg_integer()}
  179. | {error, term()}).
  180. %% These constants (except S_IFMT) are
  181. %% used to determine what type of device
  182. %% a file is. Namely, `S_IFMT band file_info.mode`
  183. %% will equal one of these contants, and tells us
  184. %% which type it is. The stdlib file_info record
  185. %% does not differentiate between device types, and
  186. %% will not allow us to differentiate between sockets
  187. %% and named pipes. These constants are pulled from libc.
  188. -define(S_IFMT, 61440).
  189. -define(S_IFSOCK, 49152). %% socket
  190. -define(S_FIFO, 4096). %% fifo/named pipe
  191. -define(S_IFBLK, 24576). %% block device
  192. -define(S_IFCHR, 8192). %% character device
  193. %% Typeflag constants for the tar header
  194. -define(TYPE_REGULAR, $0). %% regular file
  195. -define(TYPE_REGULAR_A, 0). %% regular file
  196. -define(TYPE_LINK, $1). %% hard link
  197. -define(TYPE_SYMLINK, $2). %% symbolic link
  198. -define(TYPE_CHAR, $3). %% character device node
  199. -define(TYPE_BLOCK, $4). %% block device node
  200. -define(TYPE_DIR, $5). %% directory
  201. -define(TYPE_FIFO, $6). %% fifo node
  202. -define(TYPE_CONT, $7). %% reserved
  203. -define(TYPE_X_HEADER, $x). %% extended header
  204. -define(TYPE_X_GLOBAL_HEADER, $g). %% global extended header
  205. -define(TYPE_GNU_LONGNAME, $L). %% next file has a long name
  206. -define(TYPE_GNU_LONGLINK, $K). %% next file symlinks to a file with a long name
  207. -define(TYPE_GNU_SPARSE, $S). %% sparse file
  208. %% Mode constants from tar spec
  209. -define(MODE_ISUID, 4000). %% set uid
  210. -define(MODE_ISGID, 2000). %% set gid
  211. -define(MODE_ISVTX, 1000). %% save text (sticky bit)
  212. -define(MODE_ISDIR, 40000). %% directory
  213. -define(MODE_ISFIFO, 10000). %% fifo
  214. -define(MODE_ISREG, 100000). %% regular file
  215. -define(MODE_ISLNK, 120000). %% symbolic link
  216. -define(MODE_ISBLK, 60000). %% block special file
  217. -define(MODE_ISCHR, 20000). %% character special file
  218. -define(MODE_ISSOCK, 140000). %% socket
  219. %% Keywords for PAX extended header
  220. -define(PAX_ATIME, <<"atime">>).
  221. -define(PAX_CHARSET, <<"charset">>).
  222. -define(PAX_COMMENT, <<"comment">>).
  223. -define(PAX_CTIME, <<"ctime">>). %% ctime is not a valid pax header
  224. -define(PAX_GID, <<"gid">>).
  225. -define(PAX_GNAME, <<"gname">>).
  226. -define(PAX_LINKPATH, <<"linkpath">>).
  227. -define(PAX_MTIME, <<"mtime">>).
  228. -define(PAX_PATH, <<"path">>).
  229. -define(PAX_SIZE, <<"size">>).
  230. -define(PAX_UID, <<"uid">>).
  231. -define(PAX_UNAME, <<"uname">>).
  232. -define(PAX_XATTR, <<"SCHILY.xattr.">>).
  233. -define(PAX_XATTR_STR, "SCHILY.xattr.").
  234. -define(PAX_NONE, <<"">>).
  235. %% Tar format constants
  236. %% Unknown format
  237. -define(FORMAT_UNKNOWN, 0).
  238. %% The format of the original Unix V7 tar tool prior to standardization
  239. -define(FORMAT_V7, 1).
  240. %% The old and new GNU formats, incompatible with USTAR.
  241. %% This covers the old GNU sparse extension, but it does
  242. %% not cover the GNU sparse extensions using PAX headers,
  243. %% versions 0.0, 0.1, and 1.0; these fall under the PAX format.
  244. -define(FORMAT_GNU, 2).
  245. %% Schily's tar format, which is incompatible with USTAR.
  246. %% This does not cover STAR extensions to the PAX format; these
  247. %% fall under the PAX format.
  248. -define(FORMAT_STAR, 3).
  249. %% USTAR is the former standardization of tar defined in POSIX.1-1988,
  250. %% it is incompatible with the GNU and STAR formats.
  251. -define(FORMAT_USTAR, 4).
  252. %% PAX is the latest standardization of tar defined in POSIX.1-2001.
  253. %% This is an extension of USTAR and is "backwards compatible" with it.
  254. %%
  255. %% Some newer formats add their own extensions to PAX, such as GNU sparse
  256. %% files and SCHILY extended attributes. Since they are backwards compatible
  257. %% with PAX, they will be labelled as "PAX".
  258. -define(FORMAT_PAX, 5).
  259. %% Magic constants
  260. -define(MAGIC_GNU, <<"ustar ">>).
  261. -define(VERSION_GNU, <<" \x00">>).
  262. -define(MAGIC_USTAR, <<"ustar\x00">>).
  263. -define(VERSION_USTAR, <<"00">>).
  264. -define(TRAILER_STAR, <<"tar\x00">>).
  265. %% Size constants
  266. -define(BLOCK_SIZE, 512). %% size of each block in a tar stream
  267. -define(NAME_SIZE, 100). %% max length of the name field in USTAR format
  268. -define(PREFIX_SIZE, 155). %% max length of the prefix field in USTAR format
  269. %% Maximum size of a nanosecond value as an integer
  270. -define(MAX_NANO_INT_SIZE, 9).
  271. %% Maximum size of a 64-bit signed integer
  272. -define(MAX_INT64, (1 bsl 63 - 1)).
  273. -define(PAX_GNU_SPARSE_NUMBLOCKS, <<"GNU.sparse.numblocks">>).
  274. -define(PAX_GNU_SPARSE_OFFSET, <<"GNU.sparse.offset">>).
  275. -define(PAX_GNU_SPARSE_NUMBYTES, <<"GNU.sparse.numbytes">>).
  276. -define(PAX_GNU_SPARSE_MAP, <<"GNU.sparse.map">>).
  277. -define(PAX_GNU_SPARSE_NAME, <<"GNU.sparse.name">>).
  278. -define(PAX_GNU_SPARSE_MAJOR, <<"GNU.sparse.major">>).
  279. -define(PAX_GNU_SPARSE_MINOR, <<"GNU.sparse.minor">>).
  280. -define(PAX_GNU_SPARSE_SIZE, <<"GNU.sparse.size">>).
  281. -define(PAX_GNU_SPARSE_REALSIZE, <<"GNU.sparse.realsize">>).
  282. -define(V7_NAME, 0).
  283. -define(V7_NAME_LEN, 100).
  284. -define(V7_MODE, 100).
  285. -define(V7_MODE_LEN, 8).
  286. -define(V7_UID, 108).
  287. -define(V7_UID_LEN, 8).
  288. -define(V7_GID, 116).
  289. -define(V7_GID_LEN, 8).
  290. -define(V7_SIZE, 124).
  291. -define(V7_SIZE_LEN, 12).
  292. -define(V7_MTIME, 136).
  293. -define(V7_MTIME_LEN, 12).
  294. -define(V7_CHKSUM, 148).
  295. -define(V7_CHKSUM_LEN, 8).
  296. -define(V7_TYPE, 156).
  297. -define(V7_TYPE_LEN, 1).
  298. -define(V7_LINKNAME, 157).
  299. -define(V7_LINKNAME_LEN, 100).
  300. -define(STAR_TRAILER, 508).
  301. -define(STAR_TRAILER_LEN, 4).
  302. -define(USTAR_MAGIC, 257).
  303. -define(USTAR_MAGIC_LEN, 6).
  304. -define(USTAR_VERSION, 263).
  305. -define(USTAR_VERSION_LEN, 2).
  306. -define(USTAR_UNAME, 265).
  307. -define(USTAR_UNAME_LEN, 32).
  308. -define(USTAR_GNAME, 297).
  309. -define(USTAR_GNAME_LEN, 32).
  310. -define(USTAR_DEVMAJ, 329).
  311. -define(USTAR_DEVMAJ_LEN, 8).
  312. -define(USTAR_DEVMIN, 337).
  313. -define(USTAR_DEVMIN_LEN, 8).
  314. -define(USTAR_PREFIX, 345).
  315. -define(USTAR_PREFIX_LEN, 155).
  316. -define(GNU_MAGIC, 257).
  317. -define(GNU_MAGIC_LEN, 6).
  318. -define(GNU_VERSION, 263).
  319. -define(GNU_VERSION_LEN, 2).
  320. %% ?BLOCK_SIZE of zero-bytes.
  321. %% Two of these in a row mark the end of an archive.
  322. -define(ZERO_BLOCK, <<0,0,0,0,0,0,0,0,0,0,
  323. 0,0,0,0,0,0,0,0,0,0,
  324. 0,0,0,0,0,0,0,0,0,0,
  325. 0,0,0,0,0,0,0,0,0,0,
  326. 0,0,0,0,0,0,0,0,0,0,
  327. 0,0,0,0,0,0,0,0,0,0,
  328. 0,0,0,0,0,0,0,0,0,0,
  329. 0,0,0,0,0,0,0,0,0,0,
  330. 0,0,0,0,0,0,0,0,0,0,
  331. 0,0,0,0,0,0,0,0,0,0,
  332. 0,0,0,0,0,0,0,0,0,0,
  333. 0,0,0,0,0,0,0,0,0,0,
  334. 0,0,0,0,0,0,0,0,0,0,
  335. 0,0,0,0,0,0,0,0,0,0,
  336. 0,0,0,0,0,0,0,0,0,0,
  337. 0,0,0,0,0,0,0,0,0,0,
  338. 0,0,0,0,0,0,0,0,0,0,
  339. 0,0,0,0,0,0,0,0,0,0,
  340. 0,0,0,0,0,0,0,0,0,0,
  341. 0,0,0,0,0,0,0,0,0,0,
  342. 0,0,0,0,0,0,0,0,0,0,
  343. 0,0,0,0,0,0,0,0,0,0,
  344. 0,0,0,0,0,0,0,0,0,0,
  345. 0,0,0,0,0,0,0,0,0,0,
  346. 0,0,0,0,0,0,0,0,0,0,
  347. 0,0,0,0,0,0,0,0,0,0,
  348. 0,0,0,0,0,0,0,0,0,0,
  349. 0,0,0,0,0,0,0,0,0,0,
  350. 0,0,0,0,0,0,0,0,0,0,
  351. 0,0,0,0,0,0,0,0,0,0,
  352. 0,0,0,0,0,0,0,0,0,0,
  353. 0,0,0,0,0,0,0,0,0,0,
  354. 0,0,0,0,0,0,0,0,0,0,
  355. 0,0,0,0,0,0,0,0,0,0,
  356. 0,0,0,0,0,0,0,0,0,0,
  357. 0,0,0,0,0,0,0,0,0,0,
  358. 0,0,0,0,0,0,0,0,0,0,
  359. 0,0,0,0,0,0,0,0,0,0,
  360. 0,0,0,0,0,0,0,0,0,0,
  361. 0,0,0,0,0,0,0,0,0,0,
  362. 0,0,0,0,0,0,0,0,0,0,
  363. 0,0,0,0,0,0,0,0,0,0,
  364. 0,0,0,0,0,0,0,0,0,0,
  365. 0,0,0,0,0,0,0,0,0,0,
  366. 0,0,0,0,0,0,0,0,0,0,
  367. 0,0,0,0,0,0,0,0,0,0,
  368. 0,0,0,0,0,0,0,0,0,0,
  369. 0,0,0,0,0,0,0,0,0,0,
  370. 0,0,0,0,0,0,0,0,0,0,
  371. 0,0,0,0,0,0,0,0,0,0,
  372. 0,0,0,0,0,0,0,0,0,0,0,0>>).
  373. -define(BILLION, 1000000000).
  374. -define(EPOCH, {{1970,1,1}, {0,0,0}}).