archive_write_set_format_mtree.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228
  1. /*-
  2. * Copyright (c) 2008 Joerg Sonnenberger
  3. * Copyright (c) 2009-2012 Michihiro NAKAJIMA
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "archive_platform.h"
  27. __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_mtree.c 201171 2009-12-29 06:39:07Z kientzle $");
  28. #ifdef HAVE_SYS_TYPES_H
  29. #include <sys/types.h>
  30. #endif
  31. #include <errno.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include "archive.h"
  35. #include "archive_crypto_private.h"
  36. #include "archive_entry.h"
  37. #include "archive_private.h"
  38. #include "archive_rb.h"
  39. #include "archive_string.h"
  40. #include "archive_write_private.h"
  41. #define INDENTNAMELEN 15
  42. #define MAXLINELEN 80
  43. #define SET_KEYS \
  44. (F_FLAGS | F_GID | F_GNAME | F_MODE | F_TYPE | F_UID | F_UNAME)
  45. struct attr_counter {
  46. struct attr_counter *prev;
  47. struct attr_counter *next;
  48. struct mtree_entry *m_entry;
  49. int count;
  50. };
  51. struct att_counter_set {
  52. struct attr_counter *uid_list;
  53. struct attr_counter *gid_list;
  54. struct attr_counter *mode_list;
  55. struct attr_counter *flags_list;
  56. };
  57. struct mtree_chain {
  58. struct mtree_entry *first;
  59. struct mtree_entry **last;
  60. };
  61. /*
  62. * The Data only for a directory file.
  63. */
  64. struct dir_info {
  65. struct archive_rb_tree rbtree;
  66. struct mtree_chain children;
  67. struct mtree_entry *chnext;
  68. int virtual;
  69. };
  70. /*
  71. * The Data only for a regular file.
  72. */
  73. struct reg_info {
  74. int compute_sum;
  75. uint32_t crc;
  76. #ifdef ARCHIVE_HAS_MD5
  77. unsigned char buf_md5[16];
  78. #endif
  79. #ifdef ARCHIVE_HAS_RMD160
  80. unsigned char buf_rmd160[20];
  81. #endif
  82. #ifdef ARCHIVE_HAS_SHA1
  83. unsigned char buf_sha1[20];
  84. #endif
  85. #ifdef ARCHIVE_HAS_SHA256
  86. unsigned char buf_sha256[32];
  87. #endif
  88. #ifdef ARCHIVE_HAS_SHA384
  89. unsigned char buf_sha384[48];
  90. #endif
  91. #ifdef ARCHIVE_HAS_SHA512
  92. unsigned char buf_sha512[64];
  93. #endif
  94. };
  95. struct mtree_entry {
  96. struct archive_rb_node rbnode;
  97. struct mtree_entry *next;
  98. struct mtree_entry *parent;
  99. struct dir_info *dir_info;
  100. struct reg_info *reg_info;
  101. struct archive_string parentdir;
  102. struct archive_string basename;
  103. struct archive_string pathname;
  104. struct archive_string symlink;
  105. struct archive_string uname;
  106. struct archive_string gname;
  107. struct archive_string fflags_text;
  108. unsigned int nlink;
  109. mode_t filetype;
  110. mode_t mode;
  111. int64_t size;
  112. int64_t uid;
  113. int64_t gid;
  114. time_t mtime;
  115. long mtime_nsec;
  116. unsigned long fflags_set;
  117. unsigned long fflags_clear;
  118. dev_t rdevmajor;
  119. dev_t rdevminor;
  120. dev_t devmajor;
  121. dev_t devminor;
  122. int64_t ino;
  123. };
  124. struct mtree_writer {
  125. struct mtree_entry *mtree_entry;
  126. struct mtree_entry *root;
  127. struct mtree_entry *cur_dirent;
  128. struct archive_string cur_dirstr;
  129. struct mtree_chain file_list;
  130. struct archive_string ebuf;
  131. struct archive_string buf;
  132. int first;
  133. uint64_t entry_bytes_remaining;
  134. /*
  135. * Set global value.
  136. */
  137. struct {
  138. int processing;
  139. mode_t type;
  140. int keys;
  141. int64_t uid;
  142. int64_t gid;
  143. mode_t mode;
  144. unsigned long fflags_set;
  145. unsigned long fflags_clear;
  146. } set;
  147. struct att_counter_set acs;
  148. int classic;
  149. int depth;
  150. /* check sum */
  151. int compute_sum;
  152. uint32_t crc;
  153. uint64_t crc_len;
  154. #ifdef ARCHIVE_HAS_MD5
  155. archive_md5_ctx md5ctx;
  156. #endif
  157. #ifdef ARCHIVE_HAS_RMD160
  158. archive_rmd160_ctx rmd160ctx;
  159. #endif
  160. #ifdef ARCHIVE_HAS_SHA1
  161. archive_sha1_ctx sha1ctx;
  162. #endif
  163. #ifdef ARCHIVE_HAS_SHA256
  164. archive_sha256_ctx sha256ctx;
  165. #endif
  166. #ifdef ARCHIVE_HAS_SHA384
  167. archive_sha384_ctx sha384ctx;
  168. #endif
  169. #ifdef ARCHIVE_HAS_SHA512
  170. archive_sha512_ctx sha512ctx;
  171. #endif
  172. /* Keyword options */
  173. int keys;
  174. #define F_CKSUM 0x00000001 /* check sum */
  175. #define F_DEV 0x00000002 /* device type */
  176. #define F_DONE 0x00000004 /* directory done */
  177. #define F_FLAGS 0x00000008 /* file flags */
  178. #define F_GID 0x00000010 /* gid */
  179. #define F_GNAME 0x00000020 /* group name */
  180. #define F_IGN 0x00000040 /* ignore */
  181. #define F_MAGIC 0x00000080 /* name has magic chars */
  182. #define F_MD5 0x00000100 /* MD5 digest */
  183. #define F_MODE 0x00000200 /* mode */
  184. #define F_NLINK 0x00000400 /* number of links */
  185. #define F_NOCHANGE 0x00000800 /* If owner/mode "wrong", do
  186. * not change */
  187. #define F_OPT 0x00001000 /* existence optional */
  188. #define F_RMD160 0x00002000 /* RIPEMD160 digest */
  189. #define F_SHA1 0x00004000 /* SHA-1 digest */
  190. #define F_SIZE 0x00008000 /* size */
  191. #define F_SLINK 0x00010000 /* symbolic link */
  192. #define F_TAGS 0x00020000 /* tags */
  193. #define F_TIME 0x00040000 /* modification time */
  194. #define F_TYPE 0x00080000 /* file type */
  195. #define F_UID 0x00100000 /* uid */
  196. #define F_UNAME 0x00200000 /* user name */
  197. #define F_VISIT 0x00400000 /* file visited */
  198. #define F_SHA256 0x00800000 /* SHA-256 digest */
  199. #define F_SHA384 0x01000000 /* SHA-384 digest */
  200. #define F_SHA512 0x02000000 /* SHA-512 digest */
  201. #define F_INO 0x04000000 /* inode number */
  202. #define F_RESDEV 0x08000000 /* device ID on which the
  203. * entry resides */
  204. /* Options */
  205. int dironly; /* If it is set, ignore all files except
  206. * directory files, like mtree(8) -d option. */
  207. int indent; /* If it is set, indent output data. */
  208. int output_global_set; /* If it is set, use /set keyword to set
  209. * global values. When generating mtree
  210. * classic format, it is set by default. */
  211. };
  212. #define DEFAULT_KEYS (F_DEV | F_FLAGS | F_GID | F_GNAME | F_SLINK | F_MODE\
  213. | F_NLINK | F_SIZE | F_TIME | F_TYPE | F_UID\
  214. | F_UNAME)
  215. #define attr_counter_set_reset attr_counter_set_free
  216. static void attr_counter_free(struct attr_counter **);
  217. static int attr_counter_inc(struct attr_counter **, struct attr_counter *,
  218. struct attr_counter *, struct mtree_entry *);
  219. static struct attr_counter * attr_counter_new(struct mtree_entry *,
  220. struct attr_counter *);
  221. static int attr_counter_set_collect(struct mtree_writer *,
  222. struct mtree_entry *);
  223. static void attr_counter_set_free(struct mtree_writer *);
  224. static int get_global_set_keys(struct mtree_writer *, struct mtree_entry *);
  225. static int mtree_entry_add_child_tail(struct mtree_entry *,
  226. struct mtree_entry *);
  227. static int mtree_entry_create_virtual_dir(struct archive_write *, const char *,
  228. struct mtree_entry **);
  229. static int mtree_entry_cmp_node(const struct archive_rb_node *,
  230. const struct archive_rb_node *);
  231. static int mtree_entry_cmp_key(const struct archive_rb_node *, const void *);
  232. static int mtree_entry_exchange_same_entry(struct archive_write *,
  233. struct mtree_entry *, struct mtree_entry *);
  234. static void mtree_entry_free(struct mtree_entry *);
  235. static int mtree_entry_new(struct archive_write *, struct archive_entry *,
  236. struct mtree_entry **);
  237. static void mtree_entry_register_free(struct mtree_writer *);
  238. static void mtree_entry_register_init(struct mtree_writer *);
  239. static int mtree_entry_setup_filenames(struct archive_write *,
  240. struct mtree_entry *, struct archive_entry *);
  241. static int mtree_entry_tree_add(struct archive_write *, struct mtree_entry **);
  242. static void sum_init(struct mtree_writer *);
  243. static void sum_update(struct mtree_writer *, const void *, size_t);
  244. static void sum_final(struct mtree_writer *, struct reg_info *);
  245. static void sum_write(struct archive_string *, struct reg_info *);
  246. static int write_mtree_entry(struct archive_write *, struct mtree_entry *);
  247. static int write_dot_dot_entry(struct archive_write *, struct mtree_entry *);
  248. #define COMPUTE_CRC(var, ch) (var) = (var) << 8 ^ crctab[(var) >> 24 ^ (ch)]
  249. static const uint32_t crctab[] = {
  250. 0x0,
  251. 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b,
  252. 0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6,
  253. 0x2b4bcb61, 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd,
  254. 0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9, 0x5f15adac,
  255. 0x5bd4b01b, 0x569796c2, 0x52568b75, 0x6a1936c8, 0x6ed82b7f,
  256. 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3, 0x709f7b7a,
  257. 0x745e66cd, 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039,
  258. 0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5, 0xbe2b5b58,
  259. 0xbaea46ef, 0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033,
  260. 0xa4ad16ea, 0xa06c0b5d, 0xd4326d90, 0xd0f37027, 0xddb056fe,
  261. 0xd9714b49, 0xc7361b4c, 0xc3f706fb, 0xceb42022, 0xca753d95,
  262. 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1, 0xe13ef6f4,
  263. 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d, 0x34867077, 0x30476dc0,
  264. 0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5,
  265. 0x2ac12072, 0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16,
  266. 0x018aeb13, 0x054bf6a4, 0x0808d07d, 0x0cc9cdca, 0x7897ab07,
  267. 0x7c56b6b0, 0x71159069, 0x75d48dde, 0x6b93dddb, 0x6f52c06c,
  268. 0x6211e6b5, 0x66d0fb02, 0x5e9f46bf, 0x5a5e5b08, 0x571d7dd1,
  269. 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
  270. 0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b,
  271. 0xbb60adfc, 0xb6238b25, 0xb2e29692, 0x8aad2b2f, 0x8e6c3698,
  272. 0x832f1041, 0x87ee0df6, 0x99a95df3, 0x9d684044, 0x902b669d,
  273. 0x94ea7b2a, 0xe0b41de7, 0xe4750050, 0xe9362689, 0xedf73b3e,
  274. 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2, 0xc6bcf05f,
  275. 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34,
  276. 0xdc3abded, 0xd8fba05a, 0x690ce0ee, 0x6dcdfd59, 0x608edb80,
  277. 0x644fc637, 0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb,
  278. 0x4f040d56, 0x4bc510e1, 0x46863638, 0x42472b8f, 0x5c007b8a,
  279. 0x58c1663d, 0x558240e4, 0x51435d53, 0x251d3b9e, 0x21dc2629,
  280. 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5, 0x3f9b762c,
  281. 0x3b5a6b9b, 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff,
  282. 0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623, 0xf12f560e,
  283. 0xf5ee4bb9, 0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65,
  284. 0xeba91bbc, 0xef68060b, 0xd727bbb6, 0xd3e6a601, 0xdea580d8,
  285. 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd, 0xcda1f604, 0xc960ebb3,
  286. 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7, 0xae3afba2,
  287. 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b, 0x9b3660c6, 0x9ff77d71,
  288. 0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74,
  289. 0x857130c3, 0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640,
  290. 0x4e8ee645, 0x4a4ffbf2, 0x470cdd2b, 0x43cdc09c, 0x7b827d21,
  291. 0x7f436096, 0x7200464f, 0x76c15bf8, 0x68860bfd, 0x6c47164a,
  292. 0x61043093, 0x65c52d24, 0x119b4be9, 0x155a565e, 0x18197087,
  293. 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
  294. 0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d,
  295. 0x2056cd3a, 0x2d15ebe3, 0x29d4f654, 0xc5a92679, 0xc1683bce,
  296. 0xcc2b1d17, 0xc8ea00a0, 0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb,
  297. 0xdbee767c, 0xe3a1cbc1, 0xe760d676, 0xea23f0af, 0xeee2ed18,
  298. 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4, 0x89b8fd09,
  299. 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662,
  300. 0x933eb0bb, 0x97ffad0c, 0xafb010b1, 0xab710d06, 0xa6322bdf,
  301. 0xa2f33668, 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
  302. };
  303. static const unsigned char safe_char[256] = {
  304. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 00 - 0F */
  305. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 1F */
  306. /* !"$%&'()*+,-./ EXCLUSION:0x20( ) 0x23(#) */
  307. 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 - 2F */
  308. /* 0123456789:;<>? EXCLUSION:0x3d(=) */
  309. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, /* 30 - 3F */
  310. /* @ABCDEFGHIJKLMNO */
  311. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 40 - 4F */
  312. /* PQRSTUVWXYZ[]^_ EXCLUSION:0x5c(\) */
  313. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, /* 50 - 5F */
  314. /* `abcdefghijklmno */
  315. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 60 - 6F */
  316. /* pqrstuvwxyz{|}~ */
  317. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, /* 70 - 7F */
  318. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
  319. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
  320. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
  321. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
  322. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
  323. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
  324. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
  325. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
  326. };
  327. static void
  328. mtree_quote(struct archive_string *s, const char *str)
  329. {
  330. const char *start;
  331. char buf[4];
  332. unsigned char c;
  333. for (start = str; *str != '\0'; ++str) {
  334. if (safe_char[*(const unsigned char *)str])
  335. continue;
  336. if (start != str)
  337. archive_strncat(s, start, str - start);
  338. c = (unsigned char)*str;
  339. buf[0] = '\\';
  340. buf[1] = (c / 64) + '0';
  341. buf[2] = (c / 8 % 8) + '0';
  342. buf[3] = (c % 8) + '0';
  343. archive_strncat(s, buf, 4);
  344. start = str + 1;
  345. }
  346. if (start != str)
  347. archive_strncat(s, start, str - start);
  348. }
  349. /*
  350. * Indent a line as mtree utility to be readable for people.
  351. */
  352. static void
  353. mtree_indent(struct mtree_writer *mtree)
  354. {
  355. int i, fn, nd, pd;
  356. const char *r, *s, *x;
  357. if (mtree->classic) {
  358. if (mtree->indent) {
  359. nd = 0;
  360. pd = mtree->depth * 4;
  361. } else {
  362. nd = mtree->depth?4:0;
  363. pd = 0;
  364. }
  365. } else
  366. nd = pd = 0;
  367. fn = 1;
  368. s = r = mtree->ebuf.s;
  369. x = NULL;
  370. while (*r == ' ')
  371. r++;
  372. while ((r = strchr(r, ' ')) != NULL) {
  373. if (fn) {
  374. fn = 0;
  375. for (i = 0; i < nd + pd; i++)
  376. archive_strappend_char(&mtree->buf, ' ');
  377. archive_strncat(&mtree->buf, s, r - s);
  378. if (nd + (r -s) > INDENTNAMELEN) {
  379. archive_strncat(&mtree->buf, " \\\n", 3);
  380. for (i = 0; i < (INDENTNAMELEN + 1 + pd); i++)
  381. archive_strappend_char(&mtree->buf, ' ');
  382. } else {
  383. for (i = (int)(r -s + nd);
  384. i < (INDENTNAMELEN + 1); i++)
  385. archive_strappend_char(&mtree->buf, ' ');
  386. }
  387. s = ++r;
  388. x = NULL;
  389. continue;
  390. }
  391. if (pd + (r - s) <= MAXLINELEN - 3 - INDENTNAMELEN)
  392. x = r++;
  393. else {
  394. if (x == NULL)
  395. x = r;
  396. archive_strncat(&mtree->buf, s, x - s);
  397. archive_strncat(&mtree->buf, " \\\n", 3);
  398. for (i = 0; i < (INDENTNAMELEN + 1 + pd); i++)
  399. archive_strappend_char(&mtree->buf, ' ');
  400. s = r = ++x;
  401. x = NULL;
  402. }
  403. }
  404. if (fn) {
  405. for (i = 0; i < nd + pd; i++)
  406. archive_strappend_char(&mtree->buf, ' ');
  407. archive_strcat(&mtree->buf, s);
  408. s += strlen(s);
  409. }
  410. if (x != NULL && pd + strlen(s) > MAXLINELEN - 3 - INDENTNAMELEN) {
  411. /* Last keyword is longer. */
  412. archive_strncat(&mtree->buf, s, x - s);
  413. archive_strncat(&mtree->buf, " \\\n", 3);
  414. for (i = 0; i < (INDENTNAMELEN + 1 + pd); i++)
  415. archive_strappend_char(&mtree->buf, ' ');
  416. s = ++x;
  417. }
  418. archive_strcat(&mtree->buf, s);
  419. archive_string_empty(&mtree->ebuf);
  420. }
  421. /*
  422. * Write /set keyword.
  423. * Set most used value of uid,gid,mode and fflags, which are
  424. * collected by attr_counter_set_collect() function.
  425. */
  426. static void
  427. write_global(struct mtree_writer *mtree)
  428. {
  429. struct archive_string setstr;
  430. struct archive_string unsetstr;
  431. struct att_counter_set *acs;
  432. int keys, oldkeys, effkeys;
  433. archive_string_init(&setstr);
  434. archive_string_init(&unsetstr);
  435. keys = mtree->keys & SET_KEYS;
  436. oldkeys = mtree->set.keys;
  437. effkeys = keys;
  438. acs = &mtree->acs;
  439. if (mtree->set.processing) {
  440. /*
  441. * Check if the global data needs updating.
  442. */
  443. effkeys &= ~F_TYPE;
  444. if (acs->uid_list == NULL)
  445. effkeys &= ~(F_UNAME | F_UID);
  446. else if (oldkeys & (F_UNAME | F_UID)) {
  447. if (acs->uid_list->count < 2 ||
  448. mtree->set.uid == acs->uid_list->m_entry->uid)
  449. effkeys &= ~(F_UNAME | F_UID);
  450. }
  451. if (acs->gid_list == NULL)
  452. effkeys &= ~(F_GNAME | F_GID);
  453. else if (oldkeys & (F_GNAME | F_GID)) {
  454. if (acs->gid_list->count < 2 ||
  455. mtree->set.gid == acs->gid_list->m_entry->gid)
  456. effkeys &= ~(F_GNAME | F_GID);
  457. }
  458. if (acs->mode_list == NULL)
  459. effkeys &= ~F_MODE;
  460. else if (oldkeys & F_MODE) {
  461. if (acs->mode_list->count < 2 ||
  462. mtree->set.mode == acs->mode_list->m_entry->mode)
  463. effkeys &= ~F_MODE;
  464. }
  465. if (acs->flags_list == NULL)
  466. effkeys &= ~F_FLAGS;
  467. else if ((oldkeys & F_FLAGS) != 0) {
  468. if (acs->flags_list->count < 2 ||
  469. (acs->flags_list->m_entry->fflags_set ==
  470. mtree->set.fflags_set &&
  471. acs->flags_list->m_entry->fflags_clear ==
  472. mtree->set.fflags_clear))
  473. effkeys &= ~F_FLAGS;
  474. }
  475. } else {
  476. if (acs->uid_list == NULL)
  477. keys &= ~(F_UNAME | F_UID);
  478. if (acs->gid_list == NULL)
  479. keys &= ~(F_GNAME | F_GID);
  480. if (acs->mode_list == NULL)
  481. keys &= ~F_MODE;
  482. if (acs->flags_list == NULL)
  483. keys &= ~F_FLAGS;
  484. }
  485. if ((keys & effkeys & F_TYPE) != 0) {
  486. if (mtree->dironly) {
  487. archive_strcat(&setstr, " type=dir");
  488. mtree->set.type = AE_IFDIR;
  489. } else {
  490. archive_strcat(&setstr, " type=file");
  491. mtree->set.type = AE_IFREG;
  492. }
  493. }
  494. if ((keys & effkeys & F_UNAME) != 0) {
  495. if (archive_strlen(&(acs->uid_list->m_entry->uname)) > 0) {
  496. archive_strcat(&setstr, " uname=");
  497. mtree_quote(&setstr, acs->uid_list->m_entry->uname.s);
  498. } else {
  499. keys &= ~F_UNAME;
  500. if ((oldkeys & F_UNAME) != 0)
  501. archive_strcat(&unsetstr, " uname");
  502. }
  503. }
  504. if ((keys & effkeys & F_UID) != 0) {
  505. mtree->set.uid = acs->uid_list->m_entry->uid;
  506. archive_string_sprintf(&setstr, " uid=%jd",
  507. (intmax_t)mtree->set.uid);
  508. }
  509. if ((keys & effkeys & F_GNAME) != 0) {
  510. if (archive_strlen(&(acs->gid_list->m_entry->gname)) > 0) {
  511. archive_strcat(&setstr, " gname=");
  512. mtree_quote(&setstr, acs->gid_list->m_entry->gname.s);
  513. } else {
  514. keys &= ~F_GNAME;
  515. if ((oldkeys & F_GNAME) != 0)
  516. archive_strcat(&unsetstr, " gname");
  517. }
  518. }
  519. if ((keys & effkeys & F_GID) != 0) {
  520. mtree->set.gid = acs->gid_list->m_entry->gid;
  521. archive_string_sprintf(&setstr, " gid=%jd",
  522. (intmax_t)mtree->set.gid);
  523. }
  524. if ((keys & effkeys & F_MODE) != 0) {
  525. mtree->set.mode = acs->mode_list->m_entry->mode;
  526. archive_string_sprintf(&setstr, " mode=%o",
  527. (unsigned int)mtree->set.mode);
  528. }
  529. if ((keys & effkeys & F_FLAGS) != 0) {
  530. if (archive_strlen(
  531. &(acs->flags_list->m_entry->fflags_text)) > 0) {
  532. archive_strcat(&setstr, " flags=");
  533. mtree_quote(&setstr,
  534. acs->flags_list->m_entry->fflags_text.s);
  535. mtree->set.fflags_set =
  536. acs->flags_list->m_entry->fflags_set;
  537. mtree->set.fflags_clear =
  538. acs->flags_list->m_entry->fflags_clear;
  539. } else {
  540. keys &= ~F_FLAGS;
  541. if ((oldkeys & F_FLAGS) != 0)
  542. archive_strcat(&unsetstr, " flags");
  543. }
  544. }
  545. if (unsetstr.length > 0)
  546. archive_string_sprintf(&mtree->buf, "/unset%s\n", unsetstr.s);
  547. archive_string_free(&unsetstr);
  548. if (setstr.length > 0)
  549. archive_string_sprintf(&mtree->buf, "/set%s\n", setstr.s);
  550. archive_string_free(&setstr);
  551. mtree->set.keys = keys;
  552. mtree->set.processing = 1;
  553. }
  554. static struct attr_counter *
  555. attr_counter_new(struct mtree_entry *me, struct attr_counter *prev)
  556. {
  557. struct attr_counter *ac;
  558. ac = malloc(sizeof(*ac));
  559. if (ac != NULL) {
  560. ac->prev = prev;
  561. ac->next = NULL;
  562. ac->count = 1;
  563. ac->m_entry = me;
  564. }
  565. return (ac);
  566. }
  567. static void
  568. attr_counter_free(struct attr_counter **top)
  569. {
  570. struct attr_counter *ac, *tac;
  571. if (*top == NULL)
  572. return;
  573. ac = *top;
  574. while (ac != NULL) {
  575. tac = ac->next;
  576. free(ac);
  577. ac = tac;
  578. }
  579. *top = NULL;
  580. }
  581. static int
  582. attr_counter_inc(struct attr_counter **top, struct attr_counter *ac,
  583. struct attr_counter *last, struct mtree_entry *me)
  584. {
  585. struct attr_counter *pac;
  586. if (ac != NULL) {
  587. ac->count++;
  588. if (*top == ac || ac->prev->count >= ac->count)
  589. return (0);
  590. for (pac = ac->prev; pac; pac = pac->prev) {
  591. if (pac->count >= ac->count)
  592. break;
  593. }
  594. ac->prev->next = ac->next;
  595. if (ac->next != NULL)
  596. ac->next->prev = ac->prev;
  597. if (pac != NULL) {
  598. ac->prev = pac;
  599. ac->next = pac->next;
  600. pac->next = ac;
  601. if (ac->next != NULL)
  602. ac->next->prev = ac;
  603. } else {
  604. ac->prev = NULL;
  605. ac->next = *top;
  606. *top = ac;
  607. ac->next->prev = ac;
  608. }
  609. } else {
  610. ac = attr_counter_new(me, last);
  611. if (ac == NULL)
  612. return (-1);
  613. last->next = ac;
  614. }
  615. return (0);
  616. }
  617. /*
  618. * Tabulate uid,gid,mode and fflags of a entry in order to be used for /set.
  619. */
  620. static int
  621. attr_counter_set_collect(struct mtree_writer *mtree, struct mtree_entry *me)
  622. {
  623. struct attr_counter *ac, *last;
  624. struct att_counter_set *acs = &mtree->acs;
  625. int keys = mtree->keys;
  626. if (keys & (F_UNAME | F_UID)) {
  627. if (acs->uid_list == NULL) {
  628. acs->uid_list = attr_counter_new(me, NULL);
  629. if (acs->uid_list == NULL)
  630. return (-1);
  631. } else {
  632. last = NULL;
  633. for (ac = acs->uid_list; ac; ac = ac->next) {
  634. if (ac->m_entry->uid == me->uid)
  635. break;
  636. last = ac;
  637. }
  638. if (attr_counter_inc(&acs->uid_list, ac, last, me) < 0)
  639. return (-1);
  640. }
  641. }
  642. if (keys & (F_GNAME | F_GID)) {
  643. if (acs->gid_list == NULL) {
  644. acs->gid_list = attr_counter_new(me, NULL);
  645. if (acs->gid_list == NULL)
  646. return (-1);
  647. } else {
  648. last = NULL;
  649. for (ac = acs->gid_list; ac; ac = ac->next) {
  650. if (ac->m_entry->gid == me->gid)
  651. break;
  652. last = ac;
  653. }
  654. if (attr_counter_inc(&acs->gid_list, ac, last, me) < 0)
  655. return (-1);
  656. }
  657. }
  658. if (keys & F_MODE) {
  659. if (acs->mode_list == NULL) {
  660. acs->mode_list = attr_counter_new(me, NULL);
  661. if (acs->mode_list == NULL)
  662. return (-1);
  663. } else {
  664. last = NULL;
  665. for (ac = acs->mode_list; ac; ac = ac->next) {
  666. if (ac->m_entry->mode == me->mode)
  667. break;
  668. last = ac;
  669. }
  670. if (attr_counter_inc(&acs->mode_list, ac, last, me) < 0)
  671. return (-1);
  672. }
  673. }
  674. if (keys & F_FLAGS) {
  675. if (acs->flags_list == NULL) {
  676. acs->flags_list = attr_counter_new(me, NULL);
  677. if (acs->flags_list == NULL)
  678. return (-1);
  679. } else {
  680. last = NULL;
  681. for (ac = acs->flags_list; ac; ac = ac->next) {
  682. if (ac->m_entry->fflags_set == me->fflags_set &&
  683. ac->m_entry->fflags_clear ==
  684. me->fflags_clear)
  685. break;
  686. last = ac;
  687. }
  688. if (attr_counter_inc(&acs->flags_list, ac, last, me) < 0)
  689. return (-1);
  690. }
  691. }
  692. return (0);
  693. }
  694. static void
  695. attr_counter_set_free(struct mtree_writer *mtree)
  696. {
  697. struct att_counter_set *acs = &mtree->acs;
  698. attr_counter_free(&acs->uid_list);
  699. attr_counter_free(&acs->gid_list);
  700. attr_counter_free(&acs->mode_list);
  701. attr_counter_free(&acs->flags_list);
  702. }
  703. static int
  704. get_global_set_keys(struct mtree_writer *mtree, struct mtree_entry *me)
  705. {
  706. int keys;
  707. keys = mtree->keys;
  708. /*
  709. * If a keyword has been set by /set, we do not need to
  710. * output it.
  711. */
  712. if (mtree->set.keys == 0)
  713. return (keys);/* /set is not used. */
  714. if ((mtree->set.keys & (F_GNAME | F_GID)) != 0 &&
  715. mtree->set.gid == me->gid)
  716. keys &= ~(F_GNAME | F_GID);
  717. if ((mtree->set.keys & (F_UNAME | F_UID)) != 0 &&
  718. mtree->set.uid == me->uid)
  719. keys &= ~(F_UNAME | F_UID);
  720. if (mtree->set.keys & F_FLAGS) {
  721. if (mtree->set.fflags_set == me->fflags_set &&
  722. mtree->set.fflags_clear == me->fflags_clear)
  723. keys &= ~F_FLAGS;
  724. }
  725. if ((mtree->set.keys & F_MODE) != 0 && mtree->set.mode == me->mode)
  726. keys &= ~F_MODE;
  727. switch (me->filetype) {
  728. case AE_IFLNK: case AE_IFSOCK: case AE_IFCHR:
  729. case AE_IFBLK: case AE_IFIFO:
  730. break;
  731. case AE_IFDIR:
  732. if ((mtree->set.keys & F_TYPE) != 0 &&
  733. mtree->set.type == AE_IFDIR)
  734. keys &= ~F_TYPE;
  735. break;
  736. case AE_IFREG:
  737. default: /* Handle unknown file types as regular files. */
  738. if ((mtree->set.keys & F_TYPE) != 0 &&
  739. mtree->set.type == AE_IFREG)
  740. keys &= ~F_TYPE;
  741. break;
  742. }
  743. return (keys);
  744. }
  745. static int
  746. mtree_entry_new(struct archive_write *a, struct archive_entry *entry,
  747. struct mtree_entry **m_entry)
  748. {
  749. struct mtree_entry *me;
  750. const char *s;
  751. int r;
  752. static const struct archive_rb_tree_ops rb_ops = {
  753. mtree_entry_cmp_node, mtree_entry_cmp_key
  754. };
  755. me = calloc(1, sizeof(*me));
  756. if (me == NULL) {
  757. archive_set_error(&a->archive, ENOMEM,
  758. "Can't allocate memory for a mtree entry");
  759. *m_entry = NULL;
  760. return (ARCHIVE_FATAL);
  761. }
  762. r = mtree_entry_setup_filenames(a, me, entry);
  763. if (r < ARCHIVE_WARN) {
  764. mtree_entry_free(me);
  765. *m_entry = NULL;
  766. return (r);
  767. }
  768. if ((s = archive_entry_symlink(entry)) != NULL)
  769. archive_strcpy(&me->symlink, s);
  770. me->nlink = archive_entry_nlink(entry);
  771. me->filetype = archive_entry_filetype(entry);
  772. me->mode = archive_entry_mode(entry) & 07777;
  773. me->uid = archive_entry_uid(entry);
  774. me->gid = archive_entry_gid(entry);
  775. if ((s = archive_entry_uname(entry)) != NULL)
  776. archive_strcpy(&me->uname, s);
  777. if ((s = archive_entry_gname(entry)) != NULL)
  778. archive_strcpy(&me->gname, s);
  779. if ((s = archive_entry_fflags_text(entry)) != NULL)
  780. archive_strcpy(&me->fflags_text, s);
  781. archive_entry_fflags(entry, &me->fflags_set, &me->fflags_clear);
  782. me->mtime = archive_entry_mtime(entry);
  783. me->mtime_nsec = archive_entry_mtime_nsec(entry);
  784. me->rdevmajor = archive_entry_rdevmajor(entry);
  785. me->rdevminor = archive_entry_rdevminor(entry);
  786. me->devmajor = archive_entry_devmajor(entry);
  787. me->devminor = archive_entry_devminor(entry);
  788. me->ino = archive_entry_ino(entry);
  789. me->size = archive_entry_size(entry);
  790. if (me->filetype == AE_IFDIR) {
  791. me->dir_info = calloc(1, sizeof(*me->dir_info));
  792. if (me->dir_info == NULL) {
  793. mtree_entry_free(me);
  794. archive_set_error(&a->archive, ENOMEM,
  795. "Can't allocate memory for a mtree entry");
  796. *m_entry = NULL;
  797. return (ARCHIVE_FATAL);
  798. }
  799. __archive_rb_tree_init(&me->dir_info->rbtree, &rb_ops);
  800. me->dir_info->children.first = NULL;
  801. me->dir_info->children.last = &(me->dir_info->children.first);
  802. me->dir_info->chnext = NULL;
  803. } else if (me->filetype == AE_IFREG) {
  804. me->reg_info = calloc(1, sizeof(*me->reg_info));
  805. if (me->reg_info == NULL) {
  806. mtree_entry_free(me);
  807. archive_set_error(&a->archive, ENOMEM,
  808. "Can't allocate memory for a mtree entry");
  809. *m_entry = NULL;
  810. return (ARCHIVE_FATAL);
  811. }
  812. me->reg_info->compute_sum = 0;
  813. }
  814. *m_entry = me;
  815. return (ARCHIVE_OK);
  816. }
  817. static void
  818. mtree_entry_free(struct mtree_entry *me)
  819. {
  820. archive_string_free(&me->parentdir);
  821. archive_string_free(&me->basename);
  822. archive_string_free(&me->pathname);
  823. archive_string_free(&me->symlink);
  824. archive_string_free(&me->uname);
  825. archive_string_free(&me->gname);
  826. archive_string_free(&me->fflags_text);
  827. free(me->dir_info);
  828. free(me->reg_info);
  829. free(me);
  830. }
  831. static int
  832. archive_write_mtree_header(struct archive_write *a,
  833. struct archive_entry *entry)
  834. {
  835. struct mtree_writer *mtree= a->format_data;
  836. struct mtree_entry *mtree_entry;
  837. int r, r2;
  838. if (mtree->first) {
  839. mtree->first = 0;
  840. archive_strcat(&mtree->buf, "#mtree\n");
  841. if ((mtree->keys & SET_KEYS) == 0)
  842. mtree->output_global_set = 0;/* Disabled. */
  843. }
  844. mtree->entry_bytes_remaining = archive_entry_size(entry);
  845. /* While directory only mode, we do not handle non directory files. */
  846. if (mtree->dironly && archive_entry_filetype(entry) != AE_IFDIR)
  847. return (ARCHIVE_OK);
  848. r2 = mtree_entry_new(a, entry, &mtree_entry);
  849. if (r2 < ARCHIVE_WARN)
  850. return (r2);
  851. r = mtree_entry_tree_add(a, &mtree_entry);
  852. if (r < ARCHIVE_WARN) {
  853. mtree_entry_free(mtree_entry);
  854. return (r);
  855. }
  856. mtree->mtree_entry = mtree_entry;
  857. /* If the current file is a regular file, we have to
  858. * compute the sum of its content.
  859. * Initialize a bunch of sum check context. */
  860. if (mtree_entry->reg_info)
  861. sum_init(mtree);
  862. return (r2);
  863. }
  864. static int
  865. write_mtree_entry(struct archive_write *a, struct mtree_entry *me)
  866. {
  867. struct mtree_writer *mtree = a->format_data;
  868. struct archive_string *str;
  869. int keys, ret;
  870. if (me->dir_info) {
  871. if (mtree->classic) {
  872. /*
  873. * Output a comment line to describe the full
  874. * pathname of the entry as mtree utility does
  875. * while generating classic format.
  876. */
  877. if (!mtree->dironly)
  878. archive_strappend_char(&mtree->buf, '\n');
  879. if (me->parentdir.s)
  880. archive_string_sprintf(&mtree->buf,
  881. "# %s/%s\n",
  882. me->parentdir.s, me->basename.s);
  883. else
  884. archive_string_sprintf(&mtree->buf,
  885. "# %s\n",
  886. me->basename.s);
  887. }
  888. if (mtree->output_global_set)
  889. write_global(mtree);
  890. }
  891. archive_string_empty(&mtree->ebuf);
  892. str = (mtree->indent || mtree->classic)? &mtree->ebuf : &mtree->buf;
  893. if (!mtree->classic && me->parentdir.s) {
  894. /*
  895. * If generating format is not classic one(v1), output
  896. * a full pathname.
  897. */
  898. mtree_quote(str, me->parentdir.s);
  899. archive_strappend_char(str, '/');
  900. }
  901. mtree_quote(str, me->basename.s);
  902. keys = get_global_set_keys(mtree, me);
  903. if ((keys & F_NLINK) != 0 &&
  904. me->nlink != 1 && me->filetype != AE_IFDIR)
  905. archive_string_sprintf(str, " nlink=%u", me->nlink);
  906. if ((keys & F_GNAME) != 0 && archive_strlen(&me->gname) > 0) {
  907. archive_strcat(str, " gname=");
  908. mtree_quote(str, me->gname.s);
  909. }
  910. if ((keys & F_UNAME) != 0 && archive_strlen(&me->uname) > 0) {
  911. archive_strcat(str, " uname=");
  912. mtree_quote(str, me->uname.s);
  913. }
  914. if ((keys & F_FLAGS) != 0) {
  915. if (archive_strlen(&me->fflags_text) > 0) {
  916. archive_strcat(str, " flags=");
  917. mtree_quote(str, me->fflags_text.s);
  918. } else if (mtree->set.processing &&
  919. (mtree->set.keys & F_FLAGS) != 0)
  920. /* Overwrite the global parameter. */
  921. archive_strcat(str, " flags=none");
  922. }
  923. if ((keys & F_TIME) != 0)
  924. archive_string_sprintf(str, " time=%jd.%jd",
  925. (intmax_t)me->mtime, (intmax_t)me->mtime_nsec);
  926. if ((keys & F_MODE) != 0)
  927. archive_string_sprintf(str, " mode=%o", (unsigned int)me->mode);
  928. if ((keys & F_GID) != 0)
  929. archive_string_sprintf(str, " gid=%jd", (intmax_t)me->gid);
  930. if ((keys & F_UID) != 0)
  931. archive_string_sprintf(str, " uid=%jd", (intmax_t)me->uid);
  932. if ((keys & F_INO) != 0)
  933. archive_string_sprintf(str, " inode=%jd", (intmax_t)me->ino);
  934. if ((keys & F_RESDEV) != 0) {
  935. archive_string_sprintf(str,
  936. " resdevice=native,%ju,%ju",
  937. (uintmax_t)me->devmajor,
  938. (uintmax_t)me->devminor);
  939. }
  940. switch (me->filetype) {
  941. case AE_IFLNK:
  942. if ((keys & F_TYPE) != 0)
  943. archive_strcat(str, " type=link");
  944. if ((keys & F_SLINK) != 0) {
  945. archive_strcat(str, " link=");
  946. mtree_quote(str, me->symlink.s);
  947. }
  948. break;
  949. case AE_IFSOCK:
  950. if ((keys & F_TYPE) != 0)
  951. archive_strcat(str, " type=socket");
  952. break;
  953. case AE_IFCHR:
  954. if ((keys & F_TYPE) != 0)
  955. archive_strcat(str, " type=char");
  956. if ((keys & F_DEV) != 0) {
  957. archive_string_sprintf(str,
  958. " device=native,%ju,%ju",
  959. (uintmax_t)me->rdevmajor,
  960. (uintmax_t)me->rdevminor);
  961. }
  962. break;
  963. case AE_IFBLK:
  964. if ((keys & F_TYPE) != 0)
  965. archive_strcat(str, " type=block");
  966. if ((keys & F_DEV) != 0) {
  967. archive_string_sprintf(str,
  968. " device=native,%ju,%ju",
  969. (uintmax_t)me->rdevmajor,
  970. (uintmax_t)me->rdevminor);
  971. }
  972. break;
  973. case AE_IFDIR:
  974. if ((keys & F_TYPE) != 0)
  975. archive_strcat(str, " type=dir");
  976. break;
  977. case AE_IFIFO:
  978. if ((keys & F_TYPE) != 0)
  979. archive_strcat(str, " type=fifo");
  980. break;
  981. case AE_IFREG:
  982. default: /* Handle unknown file types as regular files. */
  983. if ((keys & F_TYPE) != 0)
  984. archive_strcat(str, " type=file");
  985. if ((keys & F_SIZE) != 0)
  986. archive_string_sprintf(str, " size=%jd",
  987. (intmax_t)me->size);
  988. break;
  989. }
  990. /* Write a bunch of sum. */
  991. if (me->reg_info)
  992. sum_write(str, me->reg_info);
  993. archive_strappend_char(str, '\n');
  994. if (mtree->indent || mtree->classic)
  995. mtree_indent(mtree);
  996. if (mtree->buf.length > 32768) {
  997. ret = __archive_write_output(
  998. a, mtree->buf.s, mtree->buf.length);
  999. archive_string_empty(&mtree->buf);
  1000. } else
  1001. ret = ARCHIVE_OK;
  1002. return (ret);
  1003. }
  1004. static int
  1005. write_dot_dot_entry(struct archive_write *a, struct mtree_entry *n)
  1006. {
  1007. struct mtree_writer *mtree = a->format_data;
  1008. int ret;
  1009. if (n->parentdir.s) {
  1010. if (mtree->indent) {
  1011. int i, pd = mtree->depth * 4;
  1012. for (i = 0; i < pd; i++)
  1013. archive_strappend_char(&mtree->buf, ' ');
  1014. }
  1015. archive_string_sprintf(&mtree->buf, "# %s/%s\n",
  1016. n->parentdir.s, n->basename.s);
  1017. }
  1018. if (mtree->indent) {
  1019. archive_string_empty(&mtree->ebuf);
  1020. archive_strncat(&mtree->ebuf, "..\n\n", (mtree->dironly)?3:4);
  1021. mtree_indent(mtree);
  1022. } else
  1023. archive_strncat(&mtree->buf, "..\n\n", (mtree->dironly)?3:4);
  1024. if (mtree->buf.length > 32768) {
  1025. ret = __archive_write_output(
  1026. a, mtree->buf.s, mtree->buf.length);
  1027. archive_string_empty(&mtree->buf);
  1028. } else
  1029. ret = ARCHIVE_OK;
  1030. return (ret);
  1031. }
  1032. /*
  1033. * Write mtree entries saved at attr_counter_set_collect() function.
  1034. */
  1035. static int
  1036. write_mtree_entry_tree(struct archive_write *a)
  1037. {
  1038. struct mtree_writer *mtree = a->format_data;
  1039. struct mtree_entry *np = mtree->root;
  1040. struct archive_rb_node *n;
  1041. int ret;
  1042. do {
  1043. if (mtree->output_global_set) {
  1044. /*
  1045. * Collect attribute infomation to know which value
  1046. * is frequently used among the children.
  1047. */
  1048. attr_counter_set_reset(mtree);
  1049. ARCHIVE_RB_TREE_FOREACH(n, &(np->dir_info->rbtree)) {
  1050. struct mtree_entry *e = (struct mtree_entry *)n;
  1051. if (attr_counter_set_collect(mtree, e) < 0) {
  1052. archive_set_error(&a->archive, ENOMEM,
  1053. "Can't allocate memory");
  1054. return (ARCHIVE_FATAL);
  1055. }
  1056. }
  1057. }
  1058. if (!np->dir_info->virtual || mtree->classic) {
  1059. ret = write_mtree_entry(a, np);
  1060. if (ret != ARCHIVE_OK)
  1061. return (ARCHIVE_FATAL);
  1062. } else {
  1063. /* Whenever output_global_set is enabled
  1064. * output global value(/set keywords)
  1065. * even if the directory entry is not allowed
  1066. * to be written because the global values
  1067. * can be used for the children. */
  1068. if (mtree->output_global_set)
  1069. write_global(mtree);
  1070. }
  1071. /*
  1072. * Output the attribute of all files except directory files.
  1073. */
  1074. mtree->depth++;
  1075. ARCHIVE_RB_TREE_FOREACH(n, &(np->dir_info->rbtree)) {
  1076. struct mtree_entry *e = (struct mtree_entry *)n;
  1077. if (e->dir_info)
  1078. mtree_entry_add_child_tail(np, e);
  1079. else {
  1080. ret = write_mtree_entry(a, e);
  1081. if (ret != ARCHIVE_OK)
  1082. return (ARCHIVE_FATAL);
  1083. }
  1084. }
  1085. mtree->depth--;
  1086. if (np->dir_info->children.first != NULL) {
  1087. /*
  1088. * Descend the tree.
  1089. */
  1090. np = np->dir_info->children.first;
  1091. if (mtree->indent)
  1092. mtree->depth++;
  1093. continue;
  1094. } else if (mtree->classic) {
  1095. /*
  1096. * While printing mtree classic, if there are not
  1097. * any directory files(except "." and "..") in the
  1098. * directory, output two dots ".." as returning
  1099. * the parent directory.
  1100. */
  1101. ret = write_dot_dot_entry(a, np);
  1102. if (ret != ARCHIVE_OK)
  1103. return (ARCHIVE_FATAL);
  1104. }
  1105. while (np != np->parent) {
  1106. if (np->dir_info->chnext == NULL) {
  1107. /*
  1108. * Ascend the tree; go back to the parent.
  1109. */
  1110. if (mtree->indent)
  1111. mtree->depth--;
  1112. if (mtree->classic) {
  1113. ret = write_dot_dot_entry(a,
  1114. np->parent);
  1115. if (ret != ARCHIVE_OK)
  1116. return (ARCHIVE_FATAL);
  1117. }
  1118. np = np->parent;
  1119. } else {
  1120. /*
  1121. * Switch to next mtree entry in the directory.
  1122. */
  1123. np = np->dir_info->chnext;
  1124. break;
  1125. }
  1126. }
  1127. } while (np != np->parent);
  1128. return (ARCHIVE_OK);
  1129. }
  1130. static int
  1131. archive_write_mtree_finish_entry(struct archive_write *a)
  1132. {
  1133. struct mtree_writer *mtree = a->format_data;
  1134. struct mtree_entry *me;
  1135. if ((me = mtree->mtree_entry) == NULL)
  1136. return (ARCHIVE_OK);
  1137. mtree->mtree_entry = NULL;
  1138. if (me->reg_info)
  1139. sum_final(mtree, me->reg_info);
  1140. return (ARCHIVE_OK);
  1141. }
  1142. static int
  1143. archive_write_mtree_close(struct archive_write *a)
  1144. {
  1145. struct mtree_writer *mtree= a->format_data;
  1146. int ret;
  1147. if (mtree->root != NULL) {
  1148. ret = write_mtree_entry_tree(a);
  1149. if (ret != ARCHIVE_OK)
  1150. return (ARCHIVE_FATAL);
  1151. }
  1152. archive_write_set_bytes_in_last_block(&a->archive, 1);
  1153. return __archive_write_output(a, mtree->buf.s, mtree->buf.length);
  1154. }
  1155. static ssize_t
  1156. archive_write_mtree_data(struct archive_write *a, const void *buff, size_t n)
  1157. {
  1158. struct mtree_writer *mtree= a->format_data;
  1159. if (n > mtree->entry_bytes_remaining)
  1160. n = (size_t)mtree->entry_bytes_remaining;
  1161. mtree->entry_bytes_remaining -= n;
  1162. /* We don't need to compute a regular file sum */
  1163. if (mtree->mtree_entry == NULL)
  1164. return (n);
  1165. if (mtree->mtree_entry->filetype == AE_IFREG)
  1166. sum_update(mtree, buff, n);
  1167. return (n);
  1168. }
  1169. static int
  1170. archive_write_mtree_free(struct archive_write *a)
  1171. {
  1172. struct mtree_writer *mtree= a->format_data;
  1173. if (mtree == NULL)
  1174. return (ARCHIVE_OK);
  1175. /* Make sure we dot not leave any entries. */
  1176. mtree_entry_register_free(mtree);
  1177. archive_string_free(&mtree->cur_dirstr);
  1178. archive_string_free(&mtree->ebuf);
  1179. archive_string_free(&mtree->buf);
  1180. attr_counter_set_free(mtree);
  1181. free(mtree);
  1182. a->format_data = NULL;
  1183. return (ARCHIVE_OK);
  1184. }
  1185. static int
  1186. archive_write_mtree_options(struct archive_write *a, const char *key,
  1187. const char *value)
  1188. {
  1189. struct mtree_writer *mtree= a->format_data;
  1190. int keybit = 0;
  1191. switch (key[0]) {
  1192. case 'a':
  1193. if (strcmp(key, "all") == 0)
  1194. keybit = ~0;
  1195. break;
  1196. case 'c':
  1197. if (strcmp(key, "cksum") == 0)
  1198. keybit = F_CKSUM;
  1199. break;
  1200. case 'd':
  1201. if (strcmp(key, "device") == 0)
  1202. keybit = F_DEV;
  1203. else if (strcmp(key, "dironly") == 0) {
  1204. mtree->dironly = (value != NULL)? 1: 0;
  1205. return (ARCHIVE_OK);
  1206. }
  1207. break;
  1208. case 'f':
  1209. if (strcmp(key, "flags") == 0)
  1210. keybit = F_FLAGS;
  1211. break;
  1212. case 'g':
  1213. if (strcmp(key, "gid") == 0)
  1214. keybit = F_GID;
  1215. else if (strcmp(key, "gname") == 0)
  1216. keybit = F_GNAME;
  1217. break;
  1218. case 'i':
  1219. if (strcmp(key, "indent") == 0) {
  1220. mtree->indent = (value != NULL)? 1: 0;
  1221. return (ARCHIVE_OK);
  1222. } else if (strcmp(key, "inode") == 0) {
  1223. keybit = F_INO;
  1224. }
  1225. break;
  1226. case 'l':
  1227. if (strcmp(key, "link") == 0)
  1228. keybit = F_SLINK;
  1229. break;
  1230. case 'm':
  1231. if (strcmp(key, "md5") == 0 ||
  1232. strcmp(key, "md5digest") == 0)
  1233. keybit = F_MD5;
  1234. if (strcmp(key, "mode") == 0)
  1235. keybit = F_MODE;
  1236. break;
  1237. case 'n':
  1238. if (strcmp(key, "nlink") == 0)
  1239. keybit = F_NLINK;
  1240. break;
  1241. case 'r':
  1242. if (strcmp(key, "resdevice") == 0) {
  1243. keybit = F_RESDEV;
  1244. } else if (strcmp(key, "ripemd160digest") == 0 ||
  1245. strcmp(key, "rmd160") == 0 ||
  1246. strcmp(key, "rmd160digest") == 0)
  1247. keybit = F_RMD160;
  1248. break;
  1249. case 's':
  1250. if (strcmp(key, "sha1") == 0 ||
  1251. strcmp(key, "sha1digest") == 0)
  1252. keybit = F_SHA1;
  1253. if (strcmp(key, "sha256") == 0 ||
  1254. strcmp(key, "sha256digest") == 0)
  1255. keybit = F_SHA256;
  1256. if (strcmp(key, "sha384") == 0 ||
  1257. strcmp(key, "sha384digest") == 0)
  1258. keybit = F_SHA384;
  1259. if (strcmp(key, "sha512") == 0 ||
  1260. strcmp(key, "sha512digest") == 0)
  1261. keybit = F_SHA512;
  1262. if (strcmp(key, "size") == 0)
  1263. keybit = F_SIZE;
  1264. break;
  1265. case 't':
  1266. if (strcmp(key, "time") == 0)
  1267. keybit = F_TIME;
  1268. else if (strcmp(key, "type") == 0)
  1269. keybit = F_TYPE;
  1270. break;
  1271. case 'u':
  1272. if (strcmp(key, "uid") == 0)
  1273. keybit = F_UID;
  1274. else if (strcmp(key, "uname") == 0)
  1275. keybit = F_UNAME;
  1276. else if (strcmp(key, "use-set") == 0) {
  1277. mtree->output_global_set = (value != NULL)? 1: 0;
  1278. return (ARCHIVE_OK);
  1279. }
  1280. break;
  1281. }
  1282. if (keybit != 0) {
  1283. if (value != NULL)
  1284. mtree->keys |= keybit;
  1285. else
  1286. mtree->keys &= ~keybit;
  1287. return (ARCHIVE_OK);
  1288. }
  1289. /* Note: The "warn" return is just to inform the options
  1290. * supervisor that we didn't handle it. It will generate
  1291. * a suitable error if no one used this option. */
  1292. return (ARCHIVE_WARN);
  1293. }
  1294. static int
  1295. archive_write_set_format_mtree_default(struct archive *_a, const char *fn)
  1296. {
  1297. struct archive_write *a = (struct archive_write *)_a;
  1298. struct mtree_writer *mtree;
  1299. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW, fn);
  1300. if (a->format_free != NULL)
  1301. (a->format_free)(a);
  1302. if ((mtree = calloc(1, sizeof(*mtree))) == NULL) {
  1303. archive_set_error(&a->archive, ENOMEM,
  1304. "Can't allocate mtree data");
  1305. return (ARCHIVE_FATAL);
  1306. }
  1307. mtree->mtree_entry = NULL;
  1308. mtree->first = 1;
  1309. memset(&(mtree->set), 0, sizeof(mtree->set));
  1310. mtree->keys = DEFAULT_KEYS;
  1311. mtree->dironly = 0;
  1312. mtree->indent = 0;
  1313. archive_string_init(&mtree->ebuf);
  1314. archive_string_init(&mtree->buf);
  1315. mtree_entry_register_init(mtree);
  1316. a->format_data = mtree;
  1317. a->format_free = archive_write_mtree_free;
  1318. a->format_name = "mtree";
  1319. a->format_options = archive_write_mtree_options;
  1320. a->format_write_header = archive_write_mtree_header;
  1321. a->format_close = archive_write_mtree_close;
  1322. a->format_write_data = archive_write_mtree_data;
  1323. a->format_finish_entry = archive_write_mtree_finish_entry;
  1324. a->archive.archive_format = ARCHIVE_FORMAT_MTREE;
  1325. a->archive.archive_format_name = "mtree";
  1326. return (ARCHIVE_OK);
  1327. }
  1328. int
  1329. archive_write_set_format_mtree(struct archive *_a)
  1330. {
  1331. return archive_write_set_format_mtree_default(_a,
  1332. "archive_write_set_format_mtree");
  1333. }
  1334. int
  1335. archive_write_set_format_mtree_classic(struct archive *_a)
  1336. {
  1337. int r;
  1338. r = archive_write_set_format_mtree_default(_a,
  1339. "archive_write_set_format_mtree_classic");
  1340. if (r == ARCHIVE_OK) {
  1341. struct archive_write *a = (struct archive_write *)_a;
  1342. struct mtree_writer *mtree;
  1343. mtree = (struct mtree_writer *)a->format_data;
  1344. /* Set to output a mtree archive in classic format. */
  1345. mtree->classic = 1;
  1346. /* Basically, mtree classic format uses '/set' global
  1347. * value. */
  1348. mtree->output_global_set = 1;
  1349. }
  1350. return (r);
  1351. }
  1352. static void
  1353. sum_init(struct mtree_writer *mtree)
  1354. {
  1355. mtree->compute_sum = 0;
  1356. if (mtree->keys & F_CKSUM) {
  1357. mtree->compute_sum |= F_CKSUM;
  1358. mtree->crc = 0;
  1359. mtree->crc_len = 0;
  1360. }
  1361. #ifdef ARCHIVE_HAS_MD5
  1362. if (mtree->keys & F_MD5) {
  1363. if (archive_md5_init(&mtree->md5ctx) == ARCHIVE_OK)
  1364. mtree->compute_sum |= F_MD5;
  1365. else
  1366. mtree->keys &= ~F_MD5;/* Not supported. */
  1367. }
  1368. #endif
  1369. #ifdef ARCHIVE_HAS_RMD160
  1370. if (mtree->keys & F_RMD160) {
  1371. if (archive_rmd160_init(&mtree->rmd160ctx) == ARCHIVE_OK)
  1372. mtree->compute_sum |= F_RMD160;
  1373. else
  1374. mtree->keys &= ~F_RMD160;/* Not supported. */
  1375. }
  1376. #endif
  1377. #ifdef ARCHIVE_HAS_SHA1
  1378. if (mtree->keys & F_SHA1) {
  1379. if (archive_sha1_init(&mtree->sha1ctx) == ARCHIVE_OK)
  1380. mtree->compute_sum |= F_SHA1;
  1381. else
  1382. mtree->keys &= ~F_SHA1;/* Not supported. */
  1383. }
  1384. #endif
  1385. #ifdef ARCHIVE_HAS_SHA256
  1386. if (mtree->keys & F_SHA256) {
  1387. if (archive_sha256_init(&mtree->sha256ctx) == ARCHIVE_OK)
  1388. mtree->compute_sum |= F_SHA256;
  1389. else
  1390. mtree->keys &= ~F_SHA256;/* Not supported. */
  1391. }
  1392. #endif
  1393. #ifdef ARCHIVE_HAS_SHA384
  1394. if (mtree->keys & F_SHA384) {
  1395. if (archive_sha384_init(&mtree->sha384ctx) == ARCHIVE_OK)
  1396. mtree->compute_sum |= F_SHA384;
  1397. else
  1398. mtree->keys &= ~F_SHA384;/* Not supported. */
  1399. }
  1400. #endif
  1401. #ifdef ARCHIVE_HAS_SHA512
  1402. if (mtree->keys & F_SHA512) {
  1403. if (archive_sha512_init(&mtree->sha512ctx) == ARCHIVE_OK)
  1404. mtree->compute_sum |= F_SHA512;
  1405. else
  1406. mtree->keys &= ~F_SHA512;/* Not supported. */
  1407. }
  1408. #endif
  1409. }
  1410. static void
  1411. sum_update(struct mtree_writer *mtree, const void *buff, size_t n)
  1412. {
  1413. if (mtree->compute_sum & F_CKSUM) {
  1414. /*
  1415. * Compute a POSIX 1003.2 checksum
  1416. */
  1417. const unsigned char *p;
  1418. size_t nn;
  1419. for (nn = n, p = buff; nn--; ++p)
  1420. COMPUTE_CRC(mtree->crc, *p);
  1421. mtree->crc_len += n;
  1422. }
  1423. #ifdef ARCHIVE_HAS_MD5
  1424. if (mtree->compute_sum & F_MD5)
  1425. archive_md5_update(&mtree->md5ctx, buff, n);
  1426. #endif
  1427. #ifdef ARCHIVE_HAS_RMD160
  1428. if (mtree->compute_sum & F_RMD160)
  1429. archive_rmd160_update(&mtree->rmd160ctx, buff, n);
  1430. #endif
  1431. #ifdef ARCHIVE_HAS_SHA1
  1432. if (mtree->compute_sum & F_SHA1)
  1433. archive_sha1_update(&mtree->sha1ctx, buff, n);
  1434. #endif
  1435. #ifdef ARCHIVE_HAS_SHA256
  1436. if (mtree->compute_sum & F_SHA256)
  1437. archive_sha256_update(&mtree->sha256ctx, buff, n);
  1438. #endif
  1439. #ifdef ARCHIVE_HAS_SHA384
  1440. if (mtree->compute_sum & F_SHA384)
  1441. archive_sha384_update(&mtree->sha384ctx, buff, n);
  1442. #endif
  1443. #ifdef ARCHIVE_HAS_SHA512
  1444. if (mtree->compute_sum & F_SHA512)
  1445. archive_sha512_update(&mtree->sha512ctx, buff, n);
  1446. #endif
  1447. }
  1448. static void
  1449. sum_final(struct mtree_writer *mtree, struct reg_info *reg)
  1450. {
  1451. if (mtree->compute_sum & F_CKSUM) {
  1452. uint64_t len;
  1453. /* Include the length of the file. */
  1454. for (len = mtree->crc_len; len != 0; len >>= 8)
  1455. COMPUTE_CRC(mtree->crc, len & 0xff);
  1456. reg->crc = ~mtree->crc;
  1457. }
  1458. #ifdef ARCHIVE_HAS_MD5
  1459. if (mtree->compute_sum & F_MD5)
  1460. archive_md5_final(&mtree->md5ctx, reg->buf_md5);
  1461. #endif
  1462. #ifdef ARCHIVE_HAS_RMD160
  1463. if (mtree->compute_sum & F_RMD160)
  1464. archive_rmd160_final(&mtree->rmd160ctx, reg->buf_rmd160);
  1465. #endif
  1466. #ifdef ARCHIVE_HAS_SHA1
  1467. if (mtree->compute_sum & F_SHA1)
  1468. archive_sha1_final(&mtree->sha1ctx, reg->buf_sha1);
  1469. #endif
  1470. #ifdef ARCHIVE_HAS_SHA256
  1471. if (mtree->compute_sum & F_SHA256)
  1472. archive_sha256_final(&mtree->sha256ctx, reg->buf_sha256);
  1473. #endif
  1474. #ifdef ARCHIVE_HAS_SHA384
  1475. if (mtree->compute_sum & F_SHA384)
  1476. archive_sha384_final(&mtree->sha384ctx, reg->buf_sha384);
  1477. #endif
  1478. #ifdef ARCHIVE_HAS_SHA512
  1479. if (mtree->compute_sum & F_SHA512)
  1480. archive_sha512_final(&mtree->sha512ctx, reg->buf_sha512);
  1481. #endif
  1482. /* Save what types of sum are computed. */
  1483. reg->compute_sum = mtree->compute_sum;
  1484. }
  1485. #if defined(ARCHIVE_HAS_MD5) || defined(ARCHIVE_HAS_RMD160) || \
  1486. defined(ARCHIVE_HAS_SHA1) || defined(ARCHIVE_HAS_SHA256) || \
  1487. defined(ARCHIVE_HAS_SHA384) || defined(ARCHIVE_HAS_SHA512)
  1488. static void
  1489. strappend_bin(struct archive_string *s, const unsigned char *bin, int n)
  1490. {
  1491. static const char hex[] = "0123456789abcdef";
  1492. int i;
  1493. for (i = 0; i < n; i++) {
  1494. archive_strappend_char(s, hex[bin[i] >> 4]);
  1495. archive_strappend_char(s, hex[bin[i] & 0x0f]);
  1496. }
  1497. }
  1498. #endif
  1499. static void
  1500. sum_write(struct archive_string *str, struct reg_info *reg)
  1501. {
  1502. if (reg->compute_sum & F_CKSUM) {
  1503. archive_string_sprintf(str, " cksum=%ju",
  1504. (uintmax_t)reg->crc);
  1505. }
  1506. #ifdef ARCHIVE_HAS_MD5
  1507. if (reg->compute_sum & F_MD5) {
  1508. archive_strcat(str, " md5digest=");
  1509. strappend_bin(str, reg->buf_md5, sizeof(reg->buf_md5));
  1510. }
  1511. #endif
  1512. #ifdef ARCHIVE_HAS_RMD160
  1513. if (reg->compute_sum & F_RMD160) {
  1514. archive_strcat(str, " rmd160digest=");
  1515. strappend_bin(str, reg->buf_rmd160, sizeof(reg->buf_rmd160));
  1516. }
  1517. #endif
  1518. #ifdef ARCHIVE_HAS_SHA1
  1519. if (reg->compute_sum & F_SHA1) {
  1520. archive_strcat(str, " sha1digest=");
  1521. strappend_bin(str, reg->buf_sha1, sizeof(reg->buf_sha1));
  1522. }
  1523. #endif
  1524. #ifdef ARCHIVE_HAS_SHA256
  1525. if (reg->compute_sum & F_SHA256) {
  1526. archive_strcat(str, " sha256digest=");
  1527. strappend_bin(str, reg->buf_sha256, sizeof(reg->buf_sha256));
  1528. }
  1529. #endif
  1530. #ifdef ARCHIVE_HAS_SHA384
  1531. if (reg->compute_sum & F_SHA384) {
  1532. archive_strcat(str, " sha384digest=");
  1533. strappend_bin(str, reg->buf_sha384, sizeof(reg->buf_sha384));
  1534. }
  1535. #endif
  1536. #ifdef ARCHIVE_HAS_SHA512
  1537. if (reg->compute_sum & F_SHA512) {
  1538. archive_strcat(str, " sha512digest=");
  1539. strappend_bin(str, reg->buf_sha512, sizeof(reg->buf_sha512));
  1540. }
  1541. #endif
  1542. }
  1543. static int
  1544. mtree_entry_cmp_node(const struct archive_rb_node *n1,
  1545. const struct archive_rb_node *n2)
  1546. {
  1547. const struct mtree_entry *e1 = (const struct mtree_entry *)n1;
  1548. const struct mtree_entry *e2 = (const struct mtree_entry *)n2;
  1549. return (strcmp(e2->basename.s, e1->basename.s));
  1550. }
  1551. static int
  1552. mtree_entry_cmp_key(const struct archive_rb_node *n, const void *key)
  1553. {
  1554. const struct mtree_entry *e = (const struct mtree_entry *)n;
  1555. return (strcmp((const char *)key, e->basename.s));
  1556. }
  1557. #if defined(_WIN32) || defined(__CYGWIN__)
  1558. static int
  1559. cleanup_backslash_1(char *p)
  1560. {
  1561. int mb, dos;
  1562. mb = dos = 0;
  1563. while (*p) {
  1564. if (*(unsigned char *)p > 127)
  1565. mb = 1;
  1566. if (*p == '\\') {
  1567. /* If we have not met any multi-byte characters,
  1568. * we can replace '\' with '/'. */
  1569. if (!mb)
  1570. *p = '/';
  1571. dos = 1;
  1572. }
  1573. p++;
  1574. }
  1575. if (!mb || !dos)
  1576. return (0);
  1577. return (-1);
  1578. }
  1579. static void
  1580. cleanup_backslash_2(wchar_t *p)
  1581. {
  1582. /* Convert a path-separator from '\' to '/' */
  1583. while (*p != L'\0') {
  1584. if (*p == L'\\')
  1585. *p = L'/';
  1586. p++;
  1587. }
  1588. }
  1589. #endif
  1590. /*
  1591. * Generate a parent directory name and a base name from a pathname.
  1592. */
  1593. static int
  1594. mtree_entry_setup_filenames(struct archive_write *a, struct mtree_entry *file,
  1595. struct archive_entry *entry)
  1596. {
  1597. const char *pathname;
  1598. char *p, *dirname, *slash;
  1599. size_t len;
  1600. int ret = ARCHIVE_OK;
  1601. archive_strcpy(&file->pathname, archive_entry_pathname(entry));
  1602. #if defined(_WIN32) || defined(__CYGWIN__)
  1603. /*
  1604. * Convert a path-separator from '\' to '/'
  1605. */
  1606. if (cleanup_backslash_1(file->pathname.s) != 0) {
  1607. const wchar_t *wp = archive_entry_pathname_w(entry);
  1608. struct archive_wstring ws;
  1609. if (wp != NULL) {
  1610. int r;
  1611. archive_string_init(&ws);
  1612. archive_wstrcpy(&ws, wp);
  1613. cleanup_backslash_2(ws.s);
  1614. archive_string_empty(&(file->pathname));
  1615. r = archive_string_append_from_wcs(&(file->pathname),
  1616. ws.s, ws.length);
  1617. archive_wstring_free(&ws);
  1618. if (r < 0 && errno == ENOMEM) {
  1619. archive_set_error(&a->archive, ENOMEM,
  1620. "Can't allocate memory");
  1621. return (ARCHIVE_FATAL);
  1622. }
  1623. }
  1624. }
  1625. #else
  1626. (void)a; /* UNUSED */
  1627. #endif
  1628. pathname = file->pathname.s;
  1629. if (strcmp(pathname, ".") == 0) {
  1630. archive_strcpy(&file->basename, ".");
  1631. return (ARCHIVE_OK);
  1632. }
  1633. archive_strcpy(&(file->parentdir), pathname);
  1634. len = file->parentdir.length;
  1635. p = dirname = file->parentdir.s;
  1636. /*
  1637. * Remove leading '/' and '../' elements
  1638. */
  1639. while (*p) {
  1640. if (p[0] == '/') {
  1641. p++;
  1642. len--;
  1643. } else if (p[0] != '.')
  1644. break;
  1645. else if (p[1] == '.' && p[2] == '/') {
  1646. p += 3;
  1647. len -= 3;
  1648. } else
  1649. break;
  1650. }
  1651. if (p != dirname) {
  1652. memmove(dirname, p, len+1);
  1653. p = dirname;
  1654. }
  1655. /*
  1656. * Remove "/","/." and "/.." elements from tail.
  1657. */
  1658. while (len > 0) {
  1659. size_t ll = len;
  1660. if (len > 0 && p[len-1] == '/') {
  1661. p[len-1] = '\0';
  1662. len--;
  1663. }
  1664. if (len > 1 && p[len-2] == '/' && p[len-1] == '.') {
  1665. p[len-2] = '\0';
  1666. len -= 2;
  1667. }
  1668. if (len > 2 && p[len-3] == '/' && p[len-2] == '.' &&
  1669. p[len-1] == '.') {
  1670. p[len-3] = '\0';
  1671. len -= 3;
  1672. }
  1673. if (ll == len)
  1674. break;
  1675. }
  1676. while (*p) {
  1677. if (p[0] == '/') {
  1678. if (p[1] == '/')
  1679. /* Convert '//' --> '/' */
  1680. strcpy(p, p+1);
  1681. else if (p[1] == '.' && p[2] == '/')
  1682. /* Convert '/./' --> '/' */
  1683. strcpy(p, p+2);
  1684. else if (p[1] == '.' && p[2] == '.' && p[3] == '/') {
  1685. /* Convert 'dir/dir1/../dir2/'
  1686. * --> 'dir/dir2/'
  1687. */
  1688. char *rp = p -1;
  1689. while (rp >= dirname) {
  1690. if (*rp == '/')
  1691. break;
  1692. --rp;
  1693. }
  1694. if (rp > dirname) {
  1695. strcpy(rp, p+3);
  1696. p = rp;
  1697. } else {
  1698. strcpy(dirname, p+4);
  1699. p = dirname;
  1700. }
  1701. } else
  1702. p++;
  1703. } else
  1704. p++;
  1705. }
  1706. p = dirname;
  1707. len = strlen(p);
  1708. /*
  1709. * Add "./" prefiex.
  1710. * NOTE: If the pathname does not have a path separator, we have
  1711. * to add "./" to the head of the pathename because mtree reader
  1712. * will suppose that it is v1(a.k.a classic) mtree format and
  1713. * change the directory unexpectedly and so it will make a wrong
  1714. * path.
  1715. */
  1716. if (strcmp(p, ".") != 0 && strncmp(p, "./", 2) != 0) {
  1717. struct archive_string as;
  1718. archive_string_init(&as);
  1719. archive_strcpy(&as, "./");
  1720. archive_strncat(&as, p, len);
  1721. archive_string_empty(&file->parentdir);
  1722. archive_string_concat(&file->parentdir, &as);
  1723. archive_string_free(&as);
  1724. p = file->parentdir.s;
  1725. len = archive_strlen(&file->parentdir);
  1726. }
  1727. /*
  1728. * Find out the position which points the last position of
  1729. * path separator('/').
  1730. */
  1731. slash = NULL;
  1732. for (; *p != '\0'; p++) {
  1733. if (*p == '/')
  1734. slash = p;
  1735. }
  1736. if (slash == NULL) {
  1737. /* The pathname doesn't have a parent directory. */
  1738. file->parentdir.length = len;
  1739. archive_string_copy(&(file->basename), &(file->parentdir));
  1740. archive_string_empty(&(file->parentdir));
  1741. *file->parentdir.s = '\0';
  1742. return (ret);
  1743. }
  1744. /* Make a basename from file->parentdir.s and slash */
  1745. *slash = '\0';
  1746. file->parentdir.length = slash - file->parentdir.s;
  1747. archive_strcpy(&(file->basename), slash + 1);
  1748. return (ret);
  1749. }
  1750. static int
  1751. mtree_entry_create_virtual_dir(struct archive_write *a, const char *pathname,
  1752. struct mtree_entry **m_entry)
  1753. {
  1754. struct archive_entry *entry;
  1755. struct mtree_entry *file;
  1756. int r;
  1757. entry = archive_entry_new();
  1758. if (entry == NULL) {
  1759. *m_entry = NULL;
  1760. archive_set_error(&a->archive, ENOMEM,
  1761. "Can't allocate memory");
  1762. return (ARCHIVE_FATAL);
  1763. }
  1764. archive_entry_copy_pathname(entry, pathname);
  1765. archive_entry_set_mode(entry, AE_IFDIR | 0755);
  1766. archive_entry_set_mtime(entry, time(NULL), 0);
  1767. r = mtree_entry_new(a, entry, &file);
  1768. archive_entry_free(entry);
  1769. if (r < ARCHIVE_WARN) {
  1770. *m_entry = NULL;
  1771. archive_set_error(&a->archive, ENOMEM,
  1772. "Can't allocate memory");
  1773. return (ARCHIVE_FATAL);
  1774. }
  1775. file->dir_info->virtual = 1;
  1776. *m_entry = file;
  1777. return (ARCHIVE_OK);
  1778. }
  1779. static void
  1780. mtree_entry_register_add(struct mtree_writer *mtree, struct mtree_entry *file)
  1781. {
  1782. file->next = NULL;
  1783. *mtree->file_list.last = file;
  1784. mtree->file_list.last = &(file->next);
  1785. }
  1786. static void
  1787. mtree_entry_register_init(struct mtree_writer *mtree)
  1788. {
  1789. mtree->file_list.first = NULL;
  1790. mtree->file_list.last = &(mtree->file_list.first);
  1791. }
  1792. static void
  1793. mtree_entry_register_free(struct mtree_writer *mtree)
  1794. {
  1795. struct mtree_entry *file, *file_next;
  1796. file = mtree->file_list.first;
  1797. while (file != NULL) {
  1798. file_next = file->next;
  1799. mtree_entry_free(file);
  1800. file = file_next;
  1801. }
  1802. }
  1803. static int
  1804. mtree_entry_add_child_tail(struct mtree_entry *parent,
  1805. struct mtree_entry *child)
  1806. {
  1807. child->dir_info->chnext = NULL;
  1808. *parent->dir_info->children.last = child;
  1809. parent->dir_info->children.last = &(child->dir_info->chnext);
  1810. return (1);
  1811. }
  1812. /*
  1813. * Find a entry from a parent entry with the name.
  1814. */
  1815. static struct mtree_entry *
  1816. mtree_entry_find_child(struct mtree_entry *parent, const char *child_name)
  1817. {
  1818. struct mtree_entry *np;
  1819. if (parent == NULL)
  1820. return (NULL);
  1821. np = (struct mtree_entry *)__archive_rb_tree_find_node(
  1822. &(parent->dir_info->rbtree), child_name);
  1823. return (np);
  1824. }
  1825. static int
  1826. get_path_component(char *name, size_t n, const char *fn)
  1827. {
  1828. char *p;
  1829. size_t l;
  1830. p = strchr(fn, '/');
  1831. if (p == NULL) {
  1832. if ((l = strlen(fn)) == 0)
  1833. return (0);
  1834. } else
  1835. l = p - fn;
  1836. if (l > n -1)
  1837. return (-1);
  1838. memcpy(name, fn, l);
  1839. name[l] = '\0';
  1840. return ((int)l);
  1841. }
  1842. /*
  1843. * Add a new entry into the tree.
  1844. */
  1845. static int
  1846. mtree_entry_tree_add(struct archive_write *a, struct mtree_entry **filep)
  1847. {
  1848. #if defined(_WIN32) && !defined(__CYGWIN__)
  1849. char name[_MAX_FNAME];/* Included null terminator size. */
  1850. #elif defined(NAME_MAX) && NAME_MAX >= 255
  1851. char name[NAME_MAX+1];
  1852. #else
  1853. char name[256];
  1854. #endif
  1855. struct mtree_writer *mtree = (struct mtree_writer *)a->format_data;
  1856. struct mtree_entry *dent, *file, *np;
  1857. const char *fn, *p;
  1858. int l, r;
  1859. file = *filep;
  1860. if (file->parentdir.length == 0 && file->basename.length == 1 &&
  1861. file->basename.s[0] == '.') {
  1862. file->parent = file;
  1863. if (mtree->root != NULL) {
  1864. np = mtree->root;
  1865. goto same_entry;
  1866. }
  1867. mtree->root = file;
  1868. mtree_entry_register_add(mtree, file);
  1869. return (ARCHIVE_OK);
  1870. }
  1871. if (file->parentdir.length == 0) {
  1872. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1873. "Internal programing error "
  1874. "in generating canonical name for %s",
  1875. file->pathname.s);
  1876. return (ARCHIVE_FAILED);
  1877. }
  1878. fn = p = file->parentdir.s;
  1879. /*
  1880. * If the path of the parent directory of `file' entry is
  1881. * the same as the path of `cur_dirent', add `file' entry to
  1882. * `cur_dirent'.
  1883. */
  1884. if (archive_strlen(&(mtree->cur_dirstr))
  1885. == archive_strlen(&(file->parentdir)) &&
  1886. strcmp(mtree->cur_dirstr.s, fn) == 0) {
  1887. if (!__archive_rb_tree_insert_node(
  1888. &(mtree->cur_dirent->dir_info->rbtree),
  1889. (struct archive_rb_node *)file)) {
  1890. /* There is the same name in the tree. */
  1891. np = (struct mtree_entry *)__archive_rb_tree_find_node(
  1892. &(mtree->cur_dirent->dir_info->rbtree),
  1893. file->basename.s);
  1894. goto same_entry;
  1895. }
  1896. file->parent = mtree->cur_dirent;
  1897. mtree_entry_register_add(mtree, file);
  1898. return (ARCHIVE_OK);
  1899. }
  1900. dent = mtree->root;
  1901. for (;;) {
  1902. l = get_path_component(name, sizeof(name), fn);
  1903. if (l == 0) {
  1904. np = NULL;
  1905. break;
  1906. }
  1907. if (l < 0) {
  1908. archive_set_error(&a->archive,
  1909. ARCHIVE_ERRNO_MISC,
  1910. "A name buffer is too small");
  1911. return (ARCHIVE_FATAL);
  1912. }
  1913. if (l == 1 && name[0] == '.' && dent != NULL &&
  1914. dent == mtree->root) {
  1915. fn += l;
  1916. if (fn[0] == '/')
  1917. fn++;
  1918. continue;
  1919. }
  1920. np = mtree_entry_find_child(dent, name);
  1921. if (np == NULL || fn[0] == '\0')
  1922. break;
  1923. /* Find next sub directory. */
  1924. if (!np->dir_info) {
  1925. /* NOT Directory! */
  1926. archive_set_error(&a->archive,
  1927. ARCHIVE_ERRNO_MISC,
  1928. "`%s' is not directory, we cannot insert `%s' ",
  1929. np->pathname.s, file->pathname.s);
  1930. return (ARCHIVE_FAILED);
  1931. }
  1932. fn += l;
  1933. if (fn[0] == '/')
  1934. fn++;
  1935. dent = np;
  1936. }
  1937. if (np == NULL) {
  1938. /*
  1939. * Create virtual parent directories.
  1940. */
  1941. while (fn[0] != '\0') {
  1942. struct mtree_entry *vp;
  1943. struct archive_string as;
  1944. archive_string_init(&as);
  1945. archive_strncat(&as, p, fn - p + l);
  1946. if (as.s[as.length-1] == '/') {
  1947. as.s[as.length-1] = '\0';
  1948. as.length--;
  1949. }
  1950. r = mtree_entry_create_virtual_dir(a, as.s, &vp);
  1951. archive_string_free(&as);
  1952. if (r < ARCHIVE_WARN)
  1953. return (r);
  1954. if (strcmp(vp->pathname.s, ".") == 0) {
  1955. vp->parent = vp;
  1956. mtree->root = vp;
  1957. } else {
  1958. __archive_rb_tree_insert_node(
  1959. &(dent->dir_info->rbtree),
  1960. (struct archive_rb_node *)vp);
  1961. vp->parent = dent;
  1962. }
  1963. mtree_entry_register_add(mtree, vp);
  1964. np = vp;
  1965. fn += l;
  1966. if (fn[0] == '/')
  1967. fn++;
  1968. l = get_path_component(name, sizeof(name), fn);
  1969. if (l < 0) {
  1970. archive_string_free(&as);
  1971. archive_set_error(&a->archive,
  1972. ARCHIVE_ERRNO_MISC,
  1973. "A name buffer is too small");
  1974. return (ARCHIVE_FATAL);
  1975. }
  1976. dent = np;
  1977. }
  1978. /* Found out the parent directory where `file' can be
  1979. * inserted. */
  1980. mtree->cur_dirent = dent;
  1981. archive_string_empty(&(mtree->cur_dirstr));
  1982. archive_string_ensure(&(mtree->cur_dirstr),
  1983. archive_strlen(&(dent->parentdir)) +
  1984. archive_strlen(&(dent->basename)) + 2);
  1985. if (archive_strlen(&(dent->parentdir)) +
  1986. archive_strlen(&(dent->basename)) == 0)
  1987. mtree->cur_dirstr.s[0] = 0;
  1988. else {
  1989. if (archive_strlen(&(dent->parentdir)) > 0) {
  1990. archive_string_copy(&(mtree->cur_dirstr),
  1991. &(dent->parentdir));
  1992. archive_strappend_char(
  1993. &(mtree->cur_dirstr), '/');
  1994. }
  1995. archive_string_concat(&(mtree->cur_dirstr),
  1996. &(dent->basename));
  1997. }
  1998. if (!__archive_rb_tree_insert_node(
  1999. &(dent->dir_info->rbtree),
  2000. (struct archive_rb_node *)file)) {
  2001. np = (struct mtree_entry *)__archive_rb_tree_find_node(
  2002. &(dent->dir_info->rbtree), file->basename.s);
  2003. goto same_entry;
  2004. }
  2005. file->parent = dent;
  2006. mtree_entry_register_add(mtree, file);
  2007. return (ARCHIVE_OK);
  2008. }
  2009. same_entry:
  2010. /*
  2011. * We have already has the entry the filename of which is
  2012. * the same.
  2013. */
  2014. r = mtree_entry_exchange_same_entry(a, np, file);
  2015. if (r < ARCHIVE_WARN)
  2016. return (r);
  2017. if (np->dir_info)
  2018. np->dir_info->virtual = 0;
  2019. *filep = np;
  2020. mtree_entry_free(file);
  2021. return (ARCHIVE_WARN);
  2022. }
  2023. static int
  2024. mtree_entry_exchange_same_entry(struct archive_write *a, struct mtree_entry *np,
  2025. struct mtree_entry *file)
  2026. {
  2027. if ((np->mode & AE_IFMT) != (file->mode & AE_IFMT)) {
  2028. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2029. "Found duplicate entries `%s' and its file type is "
  2030. "different",
  2031. np->pathname.s);
  2032. return (ARCHIVE_FAILED);
  2033. }
  2034. /* Update the existent mtree entry's attributes by the new one's. */
  2035. archive_string_empty(&np->symlink);
  2036. archive_string_concat(&np->symlink, &file->symlink);
  2037. archive_string_empty(&np->uname);
  2038. archive_string_concat(&np->uname, &file->uname);
  2039. archive_string_empty(&np->gname);
  2040. archive_string_concat(&np->gname, &file->gname);
  2041. archive_string_empty(&np->fflags_text);
  2042. archive_string_concat(&np->fflags_text, &file->fflags_text);
  2043. np->nlink = file->nlink;
  2044. np->filetype = file->filetype;
  2045. np->mode = file->mode;
  2046. np->size = file->size;
  2047. np->uid = file->uid;
  2048. np->gid = file->gid;
  2049. np->fflags_set = file->fflags_set;
  2050. np->fflags_clear = file->fflags_clear;
  2051. np->mtime = file->mtime;
  2052. np->mtime_nsec = file->mtime_nsec;
  2053. np->rdevmajor = file->rdevmajor;
  2054. np->rdevminor = file->rdevminor;
  2055. np->devmajor = file->devmajor;
  2056. np->devminor = file->devminor;
  2057. np->ino = file->ino;
  2058. return (ARCHIVE_WARN);
  2059. }