archive_write_set_format_mtree.c 58 KB

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