cramfs-01-devtable.patch 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. --- cramfs-1.1.orig/cramfsck.c 2002-02-22 17:00:42.000000000 -0700
  2. +++ cramfs-1.1/cramfsck.c 2002-12-21 01:25:17.000000000 -0700
  3. @@ -51,10 +51,11 @@
  4. #include <utime.h>
  5. #include <sys/ioctl.h>
  6. #define _LINUX_STRING_H_
  7. -#include <linux/fs.h>
  8. -#include <linux/cramfs_fs.h>
  9. +#include "linux/cramfs_fs.h"
  10. #include <zlib.h>
  11. +#define BLKGETSIZE _IO(0x12,96) /* return device size /512 (long *arg) */
  12. +
  13. /* Exit codes used by fsck-type programs */
  14. #define FSCK_OK 0 /* No errors */
  15. #define FSCK_NONDESTRUCT 1 /* File system errors corrected */
  16. @@ -75,7 +76,7 @@
  17. static int opt_verbose = 0; /* 1 = verbose (-v), 2+ = very verbose (-vv) */
  18. #ifdef INCLUDE_FS_TESTS
  19. static int opt_extract = 0; /* extract cramfs (-x) */
  20. -static char *extract_dir = "root"; /* extraction directory (-x) */
  21. +static char *extract_dir = "/"; /* extraction directory (-x) */
  22. static uid_t euid; /* effective UID */
  23. /* (cramfs_super + start) <= start_dir < end_dir <= start_data <= end_data */
  24. @@ -155,7 +156,7 @@
  25. }
  26. if (*length < sizeof(struct cramfs_super)) {
  27. - die(FSCK_UNCORRECTED, 0, "file length too short");
  28. + die(FSCK_UNCORRECTED, 0, "filesystem smaller than a cramfs superblock!");
  29. }
  30. /* find superblock */
  31. @@ -190,7 +191,8 @@
  32. die(FSCK_UNCORRECTED, 0, "zero file count");
  33. }
  34. if (*length < super.size) {
  35. - die(FSCK_UNCORRECTED, 0, "file length too short");
  36. + die(FSCK_UNCORRECTED, 0, "file length too short, %lu is smaller than %lu",
  37. + *length, super.size);
  38. }
  39. else if (*length > super.size) {
  40. fprintf(stderr, "warning: file extends past end of filesystem\n");
  41. @@ -267,11 +269,11 @@
  42. #ifdef INCLUDE_FS_TESTS
  43. static void print_node(char type, struct cramfs_inode *i, char *name)
  44. {
  45. - char info[10];
  46. + char info[11];
  47. if (S_ISCHR(i->mode) || (S_ISBLK(i->mode))) {
  48. /* major/minor numbers can be as high as 2^12 or 4096 */
  49. - snprintf(info, 10, "%4d,%4d", major(i->size), minor(i->size));
  50. + snprintf(info, 11, "%4d,%4d", major(i->size), minor(i->size));
  51. }
  52. else {
  53. /* size be as high as 2^24 or 16777216 */
  54. @@ -445,8 +447,10 @@
  55. }
  56. /* TODO: Do we need to check end_dir for empty case? */
  57. memcpy(newpath, path, pathlen);
  58. - newpath[pathlen] = '/';
  59. - pathlen++;
  60. + if (pathlen > 1) {
  61. + newpath[pathlen] = '/';
  62. + pathlen++;
  63. + }
  64. if (opt_verbose) {
  65. print_node('d', i, path);
  66. }
  67. --- cramfs-1.1.orig/device_table.txt 1969-12-31 17:00:00.000000000 -0700
  68. +++ cramfs-1.1/device_table.txt 2003-01-01 05:13:44.000000000 -0700
  69. @@ -0,0 +1,129 @@
  70. +# When building a target filesystem, it is desirable to not have to
  71. +# become root and then run 'mknod' a thousand times. Using a device
  72. +# table you can create device nodes and directories "on the fly".
  73. +#
  74. +# This is a sample device table file for use with mkcramfs. You can
  75. +# do all sorts of interesting things with a device table file. For
  76. +# example, if you want to adjust the permissions on a particular file
  77. +# you can just add an entry like:
  78. +# /sbin/foobar f 2755 0 0 - - - - -
  79. +# and (assuming the file /sbin/foobar exists) it will be made setuid
  80. +# root (regardless of what its permissions are on the host filesystem.
  81. +# Furthermore, you can use a single table entry to create a many device
  82. +# minors. For example, if I wanted to create /dev/hda and /dev/hda[0-15]
  83. +# I could just use the following two table entries:
  84. +# /dev/hda b 640 0 0 3 0 0 0 -
  85. +# /dev/hda b 640 0 0 3 1 1 1 15
  86. +#
  87. +# Device table entries take the form of:
  88. +# <name> <type> <mode> <uid> <gid> <major> <minor> <start> <inc> <count>
  89. +# where name is the file name, type can be one of:
  90. +# f A regular file
  91. +# d Directory
  92. +# c Character special device file
  93. +# b Block special device file
  94. +# p Fifo (named pipe)
  95. +# uid is the user id for the target file, gid is the group id for the
  96. +# target file. The rest of the entries (major, minor, etc) apply only
  97. +# to device special files.
  98. +
  99. +# Have fun
  100. +# -Erik Andersen <andersen@codepoet.org>
  101. +#
  102. +
  103. +#<name> <type> <mode> <uid> <gid> <major> <minor> <start> <inc> <count>
  104. +/dev d 755 0 0 - - - - -
  105. +/dev/mem c 640 0 0 1 1 0 0 -
  106. +/dev/kmem c 640 0 0 1 2 0 0 -
  107. +/dev/null c 640 0 0 1 3 0 0 -
  108. +/dev/zero c 640 0 0 1 5 0 0 -
  109. +/dev/random c 640 0 0 1 8 0 0 -
  110. +/dev/urandom c 640 0 0 1 9 0 0 -
  111. +/dev/tty c 666 0 0 5 0 0 0 -
  112. +/dev/tty c 666 0 0 4 0 0 1 6
  113. +/dev/console c 640 0 0 5 1 0 0 -
  114. +/dev/ram b 640 0 0 1 1 0 0 -
  115. +/dev/ram b 640 0 0 1 0 0 1 4
  116. +/dev/loop b 640 0 0 7 0 0 1 2
  117. +/dev/ptmx c 666 0 0 5 2 0 0 -
  118. +#/dev/ttyS c 640 0 0 4 64 0 1 4
  119. +#/dev/psaux c 640 0 0 10 1 0 0 -
  120. +#/dev/rtc c 640 0 0 10 135 0 0 -
  121. +
  122. +# Adjust permissions on some normal files
  123. +#/etc/shadow f 600 0 0 - - - - -
  124. +#/bin/tinylogin f 4755 0 0 - - - - -
  125. +
  126. +# User-mode Linux stuff
  127. +/dev/ubda b 640 0 0 98 0 0 0 -
  128. +/dev/ubda b 640 0 0 98 1 1 1 15
  129. +
  130. +# IDE Devices
  131. +/dev/hda b 640 0 0 3 0 0 0 -
  132. +/dev/hda b 640 0 0 3 1 1 1 15
  133. +/dev/hdb b 640 0 0 3 64 0 0 -
  134. +/dev/hdb b 640 0 0 3 65 1 1 15
  135. +#/dev/hdc b 640 0 0 22 0 0 0 -
  136. +#/dev/hdc b 640 0 0 22 1 1 1 15
  137. +#/dev/hdd b 640 0 0 22 64 0 0 -
  138. +#/dev/hdd b 640 0 0 22 65 1 1 15
  139. +#/dev/hde b 640 0 0 33 0 0 0 -
  140. +#/dev/hde b 640 0 0 33 1 1 1 15
  141. +#/dev/hdf b 640 0 0 33 64 0 0 -
  142. +#/dev/hdf b 640 0 0 33 65 1 1 15
  143. +#/dev/hdg b 640 0 0 34 0 0 0 -
  144. +#/dev/hdg b 640 0 0 34 1 1 1 15
  145. +#/dev/hdh b 640 0 0 34 64 0 0 -
  146. +#/dev/hdh b 640 0 0 34 65 1 1 15
  147. +
  148. +# SCSI Devices
  149. +#/dev/sda b 640 0 0 8 0 0 0 -
  150. +#/dev/sda b 640 0 0 8 1 1 1 15
  151. +#/dev/sdb b 640 0 0 8 16 0 0 -
  152. +#/dev/sdb b 640 0 0 8 17 1 1 15
  153. +#/dev/sdc b 640 0 0 8 32 0 0 -
  154. +#/dev/sdc b 640 0 0 8 33 1 1 15
  155. +#/dev/sdd b 640 0 0 8 48 0 0 -
  156. +#/dev/sdd b 640 0 0 8 49 1 1 15
  157. +#/dev/sde b 640 0 0 8 64 0 0 -
  158. +#/dev/sde b 640 0 0 8 65 1 1 15
  159. +#/dev/sdf b 640 0 0 8 80 0 0 -
  160. +#/dev/sdf b 640 0 0 8 81 1 1 15
  161. +#/dev/sdg b 640 0 0 8 96 0 0 -
  162. +#/dev/sdg b 640 0 0 8 97 1 1 15
  163. +#/dev/sdh b 640 0 0 8 112 0 0 -
  164. +#/dev/sdh b 640 0 0 8 113 1 1 15
  165. +#/dev/sg c 640 0 0 21 0 0 1 15
  166. +#/dev/scd b 640 0 0 11 0 0 1 15
  167. +#/dev/st c 640 0 0 9 0 0 1 8
  168. +#/dev/nst c 640 0 0 9 128 0 1 8
  169. +#/dev/st c 640 0 0 9 32 1 1 4
  170. +#/dev/st c 640 0 0 9 64 1 1 4
  171. +#/dev/st c 640 0 0 9 96 1 1 4
  172. +
  173. +# Floppy disk devices
  174. +#/dev/fd b 640 0 0 2 0 0 1 2
  175. +#/dev/fd0d360 b 640 0 0 2 4 0 0 -
  176. +#/dev/fd1d360 b 640 0 0 2 5 0 0 -
  177. +#/dev/fd0h1200 b 640 0 0 2 8 0 0 -
  178. +#/dev/fd1h1200 b 640 0 0 2 9 0 0 -
  179. +#/dev/fd0u1440 b 640 0 0 2 28 0 0 -
  180. +#/dev/fd1u1440 b 640 0 0 2 29 0 0 -
  181. +#/dev/fd0u2880 b 640 0 0 2 32 0 0 -
  182. +#/dev/fd1u2880 b 640 0 0 2 33 0 0 -
  183. +
  184. +# All the proprietary cdrom devices in the world
  185. +#/dev/aztcd b 640 0 0 29 0 0 0 -
  186. +#/dev/bpcd b 640 0 0 41 0 0 0 -
  187. +#/dev/capi20 c 640 0 0 68 0 0 1 2
  188. +#/dev/cdu31a b 640 0 0 15 0 0 0 -
  189. +#/dev/cdu535 b 640 0 0 24 0 0 0 -
  190. +#/dev/cm206cd b 640 0 0 32 0 0 0 -
  191. +#/dev/sjcd b 640 0 0 18 0 0 0 -
  192. +#/dev/sonycd b 640 0 0 15 0 0 0 -
  193. +#/dev/gscd b 640 0 0 16 0 0 0 -
  194. +#/dev/sbpcd b 640 0 0 25 0 0 0 -
  195. +#/dev/sbpcd b 640 0 0 25 0 0 1 4
  196. +#/dev/mcd b 640 0 0 23 0 0 0 -
  197. +#/dev/optcd b 640 0 0 17 0 0 0 -
  198. +
  199. --- cramfs-1.1.orig/mkcramfs.c 2002-02-20 01:03:32.000000000 -0700
  200. +++ cramfs-1.1/mkcramfs.c 2002-12-21 01:25:17.000000000 -0700
  201. @@ -1,3 +1,4 @@
  202. +/* vi: set sw=8 ts=8: */
  203. /*
  204. * mkcramfs - make a cramfs file system
  205. *
  206. @@ -16,12 +17,21 @@
  207. * You should have received a copy of the GNU General Public License
  208. * along with this program; if not, write to the Free Software
  209. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  210. + *
  211. + * Added device table support (code taken from mkfs.jffs2.c, credit to
  212. + * Erik Andersen <andersen@codepoet.org>) as well as an option to squash
  213. + * permissions. - Russ Dill <Russ.Dill@asu.edu> September 2002
  214. + *
  215. + * Reworked, cleaned up, and updated for cramfs-1.1, December 2002
  216. + * - Erik Andersen <andersen@codepoet.org>
  217. + *
  218. */
  219. /*
  220. * If you change the disk format of cramfs, please update fs/cramfs/README.
  221. */
  222. +#define _GNU_SOURCE
  223. #include <sys/types.h>
  224. #include <stdio.h>
  225. #include <sys/stat.h>
  226. @@ -33,8 +43,15 @@
  227. #include <errno.h>
  228. #include <string.h>
  229. #include <stdarg.h>
  230. +#include <libgen.h>
  231. +#include <ctype.h>
  232. +#include <assert.h>
  233. +#include <getopt.h>
  234. #include <linux/cramfs_fs.h>
  235. #include <zlib.h>
  236. +#ifdef DMALLOC
  237. +#include <dmalloc.h>
  238. +#endif
  239. /* Exit codes used by mkfs-type programs */
  240. #define MKFS_OK 0 /* No errors */
  241. @@ -71,11 +88,17 @@
  242. + (1 << CRAMFS_SIZE_WIDTH) - 1 /* filesize */ \
  243. + (1 << CRAMFS_SIZE_WIDTH) * 4 / PAGE_CACHE_SIZE /* block pointers */ )
  244. +
  245. +/* The kernel assumes PAGE_CACHE_SIZE as block size. */
  246. +#define PAGE_CACHE_SIZE (4096)
  247. +
  248. +
  249. static const char *progname = "mkcramfs";
  250. static unsigned int blksize = PAGE_CACHE_SIZE;
  251. static long total_blocks = 0, total_nodes = 1; /* pre-count the root node */
  252. static int image_length = 0;
  253. +
  254. /*
  255. * If opt_holes is set, then mkcramfs can create explicit holes in the
  256. * data, which saves 26 bytes per hole (which is a lot smaller a
  257. @@ -91,10 +114,12 @@
  258. static int opt_holes = 0;
  259. static int opt_pad = 0;
  260. static int opt_verbose = 0;
  261. +static int opt_squash = 0;
  262. static char *opt_image = NULL;
  263. static char *opt_name = NULL;
  264. static int warn_dev, warn_gid, warn_namelen, warn_skip, warn_size, warn_uid;
  265. +static const char *const memory_exhausted = "memory exhausted";
  266. /* In-core version of inode / directory entry. */
  267. struct entry {
  268. @@ -123,7 +148,7 @@
  269. {
  270. FILE *stream = status ? stderr : stdout;
  271. - fprintf(stream, "usage: %s [-h] [-e edition] [-i file] [-n name] dirname outfile\n"
  272. + fprintf(stream, "usage: %s [-h] [-e edition] [-i file] [-n name] [-D file] dirname outfile\n"
  273. " -h print this help\n"
  274. " -E make all warnings errors (non-zero exit status)\n"
  275. " -e edition set edition number (part of fsid)\n"
  276. @@ -133,39 +158,157 @@
  277. " -s sort directory entries (old option, ignored)\n"
  278. " -v be more verbose\n"
  279. " -z make explicit holes (requires >= 2.3.39)\n"
  280. - " dirname root of the directory tree to be compressed\n"
  281. + " -D Use the named FILE as a device table file\n"
  282. + " -q squash permissions (make everything owned by root)\n"
  283. + " dirname root of the filesystem to be compressed\n"
  284. " outfile output file\n", progname, PAD_SIZE);
  285. exit(status);
  286. }
  287. -static void die(int status, int syserr, const char *fmt, ...)
  288. +static void verror_msg(const char *s, va_list p)
  289. +{
  290. + fflush(stdout);
  291. + fprintf(stderr, "mkcramfs: ");
  292. + vfprintf(stderr, s, p);
  293. +}
  294. +
  295. +static void vperror_msg(const char *s, va_list p)
  296. +{
  297. + int err = errno;
  298. +
  299. + if (s == 0)
  300. + s = "";
  301. + verror_msg(s, p);
  302. + if (*s)
  303. + s = ": ";
  304. + fprintf(stderr, "%s%s\n", s, strerror(err));
  305. +}
  306. +
  307. +static void perror_msg(const char *s, ...)
  308. +{
  309. + va_list p;
  310. +
  311. + va_start(p, s);
  312. + vperror_msg(s, p);
  313. + va_end(p);
  314. +}
  315. +
  316. +static void error_msg_and_die(const char *s, ...)
  317. +{
  318. + va_list p;
  319. +
  320. + va_start(p, s);
  321. + verror_msg(s, p);
  322. + va_end(p);
  323. + putc('\n', stderr);
  324. + exit(MKFS_ERROR);
  325. +}
  326. +
  327. +static void perror_msg_and_die(const char *s, ...)
  328. +{
  329. + va_list p;
  330. +
  331. + va_start(p, s);
  332. + vperror_msg(s, p);
  333. + va_end(p);
  334. + exit(MKFS_ERROR);
  335. +}
  336. +#ifndef DMALLOC
  337. +extern char *xstrdup(const char *s)
  338. +{
  339. + char *t;
  340. +
  341. + if (s == NULL)
  342. + return NULL;
  343. + t = strdup(s);
  344. + if (t == NULL)
  345. + error_msg_and_die(memory_exhausted);
  346. + return t;
  347. +}
  348. +
  349. +extern void *xmalloc(size_t size)
  350. +{
  351. + void *ptr = malloc(size);
  352. +
  353. + if (ptr == NULL && size != 0)
  354. + error_msg_and_die(memory_exhausted);
  355. + return ptr;
  356. +}
  357. +
  358. +extern void *xcalloc(size_t nmemb, size_t size)
  359. +{
  360. + void *ptr = calloc(nmemb, size);
  361. +
  362. + if (ptr == NULL && nmemb != 0 && size != 0)
  363. + error_msg_and_die(memory_exhausted);
  364. + return ptr;
  365. +}
  366. +
  367. +extern void *xrealloc(void *ptr, size_t size)
  368. +{
  369. + ptr = realloc(ptr, size);
  370. + if (ptr == NULL && size != 0)
  371. + error_msg_and_die(memory_exhausted);
  372. + return ptr;
  373. +}
  374. +#endif
  375. +
  376. +static FILE *xfopen(const char *path, const char *mode)
  377. {
  378. - va_list arg_ptr;
  379. - int save = errno;
  380. + FILE *fp;
  381. +
  382. + if ((fp = fopen(path, mode)) == NULL)
  383. + perror_msg_and_die("%s", path);
  384. + return fp;
  385. +}
  386. - fflush(0);
  387. - va_start(arg_ptr, fmt);
  388. - fprintf(stderr, "%s: ", progname);
  389. - vfprintf(stderr, fmt, arg_ptr);
  390. - if (syserr) {
  391. - fprintf(stderr, ": %s", strerror(save));
  392. +extern int xopen(const char *pathname, int flags, mode_t mode)
  393. +{
  394. + int ret;
  395. +
  396. + if (flags & O_CREAT)
  397. + ret = open(pathname, flags, mode);
  398. + else
  399. + ret = open(pathname, flags);
  400. + if (ret == -1) {
  401. + perror_msg_and_die("%s", pathname);
  402. }
  403. - fprintf(stderr, "\n");
  404. - va_end(arg_ptr);
  405. - exit(status);
  406. + return ret;
  407. }
  408. +extern char *xreadlink(const char *path)
  409. +{
  410. + static const int GROWBY = 80; /* how large we will grow strings by */
  411. +
  412. + char *buf = NULL;
  413. + int bufsize = 0, readsize = 0;
  414. +
  415. + do {
  416. + buf = xrealloc(buf, bufsize += GROWBY);
  417. + readsize = readlink(path, buf, bufsize); /* 1st try */
  418. + if (readsize == -1) {
  419. + perror_msg("%s:%s", progname, path);
  420. + return NULL;
  421. + }
  422. + }
  423. + while (bufsize < readsize + 1);
  424. +
  425. + buf[readsize] = '\0';
  426. +
  427. + return buf;
  428. +}
  429. +
  430. static void map_entry(struct entry *entry)
  431. {
  432. if (entry->path) {
  433. entry->fd = open(entry->path, O_RDONLY);
  434. if (entry->fd < 0) {
  435. - die(MKFS_ERROR, 1, "open failed: %s", entry->path);
  436. + error_msg_and_die("open failed: %s", entry->path);
  437. }
  438. entry->uncompressed = mmap(NULL, entry->size, PROT_READ, MAP_PRIVATE, entry->fd, 0);
  439. if (entry->uncompressed == MAP_FAILED) {
  440. - die(MKFS_ERROR, 1, "mmap failed: %s", entry->path);
  441. + error_msg_and_die("mmap failed: %s", entry->path);
  442. }
  443. }
  444. }
  445. @@ -174,8 +317,9 @@
  446. {
  447. if (entry->path) {
  448. if (munmap(entry->uncompressed, entry->size) < 0) {
  449. - die(MKFS_ERROR, 1, "munmap failed: %s", entry->path);
  450. + error_msg_and_die("munmap failed: %s", entry->path);
  451. }
  452. + entry->uncompressed=NULL;
  453. close(entry->fd);
  454. }
  455. }
  456. @@ -204,7 +348,8 @@
  457. find_identical_file(orig->next, newfile));
  458. }
  459. -static void eliminate_doubles(struct entry *root, struct entry *orig) {
  460. +static void eliminate_doubles(struct entry *root, struct entry *orig)
  461. +{
  462. if (orig) {
  463. if (orig->size && (orig->path || orig->uncompressed))
  464. find_identical_file(root, orig);
  465. @@ -232,10 +377,7 @@
  466. /* Set up the path. */
  467. /* TODO: Reuse the parent's buffer to save memcpy'ing and duplication. */
  468. - path = malloc(len + 1 + MAX_INPUT_NAMELEN + 1);
  469. - if (!path) {
  470. - die(MKFS_ERROR, 1, "malloc failed");
  471. - }
  472. + path = xmalloc(len + 1 + MAX_INPUT_NAMELEN + 1);
  473. memcpy(path, name, len);
  474. endpath = path + len;
  475. *endpath = '/';
  476. @@ -245,7 +387,7 @@
  477. dircount = scandir(name, &dirlist, 0, cramsort);
  478. if (dircount < 0) {
  479. - die(MKFS_ERROR, 1, "scandir failed: %s", name);
  480. + error_msg_and_die("scandir failed: %s", name);
  481. }
  482. /* process directory */
  483. @@ -269,25 +411,20 @@
  484. }
  485. namelen = strlen(dirent->d_name);
  486. if (namelen > MAX_INPUT_NAMELEN) {
  487. - die(MKFS_ERROR, 0,
  488. - "very long (%u bytes) filename found: %s\n"
  489. - "please increase MAX_INPUT_NAMELEN in mkcramfs.c and recompile",
  490. + error_msg_and_die(
  491. + "Very long (%u bytes) filename `%s' found.\n"
  492. + " Please increase MAX_INPUT_NAMELEN in mkcramfs.c and recompile. Exiting.\n",
  493. namelen, dirent->d_name);
  494. }
  495. memcpy(endpath, dirent->d_name, namelen + 1);
  496. if (lstat(path, &st) < 0) {
  497. + perror(endpath);
  498. warn_skip = 1;
  499. continue;
  500. }
  501. - entry = calloc(1, sizeof(struct entry));
  502. - if (!entry) {
  503. - die(MKFS_ERROR, 1, "calloc failed");
  504. - }
  505. - entry->name = strdup(dirent->d_name);
  506. - if (!entry->name) {
  507. - die(MKFS_ERROR, 1, "strdup failed");
  508. - }
  509. + entry = xcalloc(1, sizeof(struct entry));
  510. + entry->name = xstrdup(dirent->d_name);
  511. /* truncate multi-byte UTF-8 filenames on character boundary */
  512. if (namelen > CRAMFS_MAXPATHLEN) {
  513. namelen = CRAMFS_MAXPATHLEN;
  514. @@ -297,24 +434,25 @@
  515. namelen--;
  516. /* are we reasonably certain it was UTF-8 ? */
  517. if (entry->name[namelen] < 0x80 || !namelen) {
  518. - die(MKFS_ERROR, 0, "cannot truncate filenames not encoded in UTF-8");
  519. + error_msg_and_die("cannot truncate filenames not encoded in UTF-8");
  520. }
  521. }
  522. entry->name[namelen] = '\0';
  523. }
  524. entry->mode = st.st_mode;
  525. entry->size = st.st_size;
  526. - entry->uid = st.st_uid;
  527. + entry->uid = opt_squash ? 0 : st.st_uid;
  528. if (entry->uid >= 1 << CRAMFS_UID_WIDTH)
  529. warn_uid = 1;
  530. - entry->gid = st.st_gid;
  531. - if (entry->gid >= 1 << CRAMFS_GID_WIDTH)
  532. + entry->gid = opt_squash ? 0 : st.st_gid;
  533. + if (entry->gid >= 1 << CRAMFS_GID_WIDTH) {
  534. /* TODO: We ought to replace with a default
  535. gid instead of truncating; otherwise there
  536. are security problems. Maybe mode should
  537. be &= ~070. Same goes for uid once Linux
  538. supports >16-bit uids. */
  539. warn_gid = 1;
  540. + }
  541. size = sizeof(struct cramfs_inode) + ((namelen + 3) & ~3);
  542. *fslen_ub += size;
  543. if (S_ISDIR(st.st_mode)) {
  544. @@ -325,21 +463,15 @@
  545. warn_skip = 1;
  546. continue;
  547. }
  548. - entry->path = strdup(path);
  549. - if (!entry->path) {
  550. - die(MKFS_ERROR, 1, "strdup failed");
  551. - }
  552. + entry->path = xstrdup(path);
  553. if ((entry->size >= 1 << CRAMFS_SIZE_WIDTH)) {
  554. warn_size = 1;
  555. entry->size = (1 << CRAMFS_SIZE_WIDTH) - 1;
  556. }
  557. }
  558. } else if (S_ISLNK(st.st_mode)) {
  559. - entry->uncompressed = malloc(entry->size);
  560. + entry->uncompressed = xreadlink(path);
  561. if (!entry->uncompressed) {
  562. - die(MKFS_ERROR, 1, "malloc failed");
  563. - }
  564. - if (readlink(path, entry->uncompressed, entry->size) < 0) {
  565. warn_skip = 1;
  566. continue;
  567. }
  568. @@ -351,7 +483,7 @@
  569. if (entry->size & -(1<<CRAMFS_SIZE_WIDTH))
  570. warn_dev = 1;
  571. } else {
  572. - die(MKFS_ERROR, 0, "bogus file type: %s", entry->name);
  573. + error_msg_and_die("bogus file type: %s", entry->name);
  574. }
  575. if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
  576. @@ -378,7 +510,9 @@
  577. struct cramfs_super *super = (struct cramfs_super *) base;
  578. unsigned int offset = sizeof(struct cramfs_super) + image_length;
  579. - offset += opt_pad; /* 0 if no padding */
  580. + if (opt_pad) {
  581. + offset += opt_pad; /* 0 if no padding */
  582. + }
  583. super->magic = CRAMFS_MAGIC;
  584. super->flags = CRAMFS_FLAG_FSID_VERSION_2 | CRAMFS_FLAG_SORTED_DIRS;
  585. @@ -414,10 +548,10 @@
  586. struct cramfs_inode *inode = (struct cramfs_inode *) (base + entry->dir_offset);
  587. if ((offset & 3) != 0) {
  588. - die(MKFS_ERROR, 0, "illegal offset of %lu bytes", offset);
  589. + error_msg_and_die("illegal offset of %lu bytes", offset);
  590. }
  591. if (offset >= (1 << (2 + CRAMFS_OFFSET_WIDTH))) {
  592. - die(MKFS_ERROR, 0, "filesystem too big");
  593. + error_msg_and_die("filesystem too big");
  594. }
  595. inode->offset = (offset >> 2);
  596. }
  597. @@ -429,7 +563,7 @@
  598. */
  599. static void print_node(struct entry *e)
  600. {
  601. - char info[10];
  602. + char info[12];
  603. char type = '?';
  604. if (S_ISREG(e->mode)) type = 'f';
  605. @@ -442,11 +576,11 @@
  606. if (S_ISCHR(e->mode) || (S_ISBLK(e->mode))) {
  607. /* major/minor numbers can be as high as 2^12 or 4096 */
  608. - snprintf(info, 10, "%4d,%4d", major(e->size), minor(e->size));
  609. + snprintf(info, 11, "%4d,%4d", major(e->size), minor(e->size));
  610. }
  611. else {
  612. /* size be as high as 2^24 or 16777216 */
  613. - snprintf(info, 10, "%9d", e->size);
  614. + snprintf(info, 11, "%9d", e->size);
  615. }
  616. printf("%c %04o %s %5d:%-3d %s\n",
  617. @@ -462,17 +596,9 @@
  618. {
  619. int stack_entries = 0;
  620. int stack_size = 64;
  621. - struct entry **entry_stack;
  622. -
  623. - entry_stack = malloc(stack_size * sizeof(struct entry *));
  624. - if (!entry_stack) {
  625. - die(MKFS_ERROR, 1, "malloc failed");
  626. - }
  627. -
  628. - if (opt_verbose) {
  629. - printf("root:\n");
  630. - }
  631. + struct entry **entry_stack = NULL;
  632. + entry_stack = xmalloc(stack_size * sizeof(struct entry *));
  633. for (;;) {
  634. int dir_start = stack_entries;
  635. while (entry) {
  636. @@ -506,10 +632,7 @@
  637. if (entry->child) {
  638. if (stack_entries >= stack_size) {
  639. stack_size *= 2;
  640. - entry_stack = realloc(entry_stack, stack_size * sizeof(struct entry *));
  641. - if (!entry_stack) {
  642. - die(MKFS_ERROR, 1, "realloc failed");
  643. - }
  644. + entry_stack = xrealloc(entry_stack, stack_size * sizeof(struct entry *));
  645. }
  646. entry_stack[stack_entries] = entry;
  647. stack_entries++;
  648. @@ -543,7 +666,7 @@
  649. set_data_offset(entry, base, offset);
  650. if (opt_verbose) {
  651. - printf("%s:\n", entry->name);
  652. + printf("'%s':\n", entry->name);
  653. }
  654. entry = entry->child;
  655. }
  656. @@ -553,16 +676,21 @@
  657. static int is_zero(char const *begin, unsigned len)
  658. {
  659. - /* Returns non-zero iff the first LEN bytes from BEGIN are all NULs. */
  660. - return (len-- == 0 ||
  661. - (begin[0] == '\0' &&
  662. - (len-- == 0 ||
  663. - (begin[1] == '\0' &&
  664. - (len-- == 0 ||
  665. - (begin[2] == '\0' &&
  666. - (len-- == 0 ||
  667. - (begin[3] == '\0' &&
  668. - memcmp(begin, begin + 4, len) == 0))))))));
  669. + if (opt_holes)
  670. + /* Returns non-zero iff the first LEN bytes from BEGIN are
  671. + all NULs. */
  672. + return (len-- == 0 ||
  673. + (begin[0] == '\0' &&
  674. + (len-- == 0 ||
  675. + (begin[1] == '\0' &&
  676. + (len-- == 0 ||
  677. + (begin[2] == '\0' &&
  678. + (len-- == 0 ||
  679. + (begin[3] == '\0' &&
  680. + memcmp(begin, begin + 4, len) == 0))))))));
  681. + else
  682. + /* Never create holes. */
  683. + return 0;
  684. }
  685. /*
  686. @@ -575,37 +703,34 @@
  687. * Note that size > 0, as a zero-sized file wouldn't ever
  688. * have gotten here in the first place.
  689. */
  690. -static unsigned int do_compress(char *base, unsigned int offset, char const *name, char *uncompressed, unsigned int size)
  691. +static unsigned int do_compress(char *base, unsigned int offset, struct entry *entry)
  692. {
  693. + unsigned int size = entry->size;
  694. unsigned long original_size = size;
  695. unsigned long original_offset = offset;
  696. unsigned long new_size;
  697. unsigned long blocks = (size - 1) / blksize + 1;
  698. unsigned long curr = offset + 4 * blocks;
  699. int change;
  700. + char *uncompressed = entry->uncompressed;
  701. - total_blocks += blocks;
  702. + total_blocks += blocks;
  703. do {
  704. unsigned long len = 2 * blksize;
  705. unsigned int input = size;
  706. - int err;
  707. -
  708. if (input > blksize)
  709. input = blksize;
  710. size -= input;
  711. - if (!(opt_holes && is_zero (uncompressed, input))) {
  712. - err = compress2(base + curr, &len, uncompressed, input, Z_BEST_COMPRESSION);
  713. - if (err != Z_OK) {
  714. - die(MKFS_ERROR, 0, "compression error: %s", zError(err));
  715. - }
  716. + if (!is_zero (uncompressed, input)) {
  717. + compress(base + curr, &len, uncompressed, input);
  718. curr += len;
  719. }
  720. uncompressed += input;
  721. if (len > blksize*2) {
  722. /* (I don't think this can happen with zlib.) */
  723. - die(MKFS_ERROR, 0, "AIEEE: block \"compressed\" to > 2*blocklength (%ld)", len);
  724. + error_msg_and_die("AIEEE: block \"compressed\" to > 2*blocklength (%ld)\n", len);
  725. }
  726. *(u32 *) (base + offset) = curr;
  727. @@ -618,10 +743,12 @@
  728. st_blocks * 512. But if you say that then perhaps
  729. administrative data should also be included in both. */
  730. change = new_size - original_size;
  731. - if (opt_verbose > 1) {
  732. - printf("%6.2f%% (%+d bytes)\t%s\n",
  733. - (change * 100) / (double) original_size, change, name);
  734. +#if 0
  735. + if (opt_verbose) {
  736. + printf("%6.2f%% (%+d bytes)\t%s\n",
  737. + (change * 100) / (double) original_size, change, entry->name);
  738. }
  739. +#endif
  740. return curr;
  741. }
  742. @@ -644,7 +771,7 @@
  743. set_data_offset(entry, base, offset);
  744. entry->offset = offset;
  745. map_entry(entry);
  746. - offset = do_compress(base, offset, entry->name, entry->uncompressed, entry->size);
  747. + offset = do_compress(base, offset, entry);
  748. unmap_entry(entry);
  749. }
  750. }
  751. @@ -660,13 +787,10 @@
  752. int fd;
  753. char *buf;
  754. - fd = open(file, O_RDONLY);
  755. - if (fd < 0) {
  756. - die(MKFS_ERROR, 1, "open failed: %s", file);
  757. - }
  758. + fd = xopen(file, O_RDONLY, 0);
  759. buf = mmap(NULL, image_length, PROT_READ, MAP_PRIVATE, fd, 0);
  760. if (buf == MAP_FAILED) {
  761. - die(MKFS_ERROR, 1, "mmap failed");
  762. + error_msg_and_die("mmap failed");
  763. }
  764. memcpy(base + offset, buf, image_length);
  765. munmap(buf, image_length);
  766. @@ -679,6 +803,328 @@
  767. return (offset + image_length);
  768. }
  769. +static struct entry *find_filesystem_entry(struct entry *dir, char *name, mode_t type)
  770. +{
  771. + struct entry *e = dir;
  772. +
  773. + if (S_ISDIR(dir->mode)) {
  774. + e = dir->child;
  775. + }
  776. + while (e) {
  777. + /* Only bother to do the expensive strcmp on matching file types */
  778. + if (type == (e->mode & S_IFMT) && e->name) {
  779. + if (S_ISDIR(e->mode)) {
  780. + int len = strlen(e->name);
  781. +
  782. + /* Check if we are a parent of the correct path */
  783. + if (strncmp(e->name, name, len) == 0) {
  784. + /* Is this an _exact_ match? */
  785. + if (strcmp(name, e->name) == 0) {
  786. + return (e);
  787. + }
  788. + /* Looks like we found a parent of the correct path */
  789. + if (name[len] == '/') {
  790. + if (e->child) {
  791. + return (find_filesystem_entry (e, name + len + 1, type));
  792. + } else {
  793. + return NULL;
  794. + }
  795. + }
  796. + }
  797. + } else {
  798. + if (strcmp(name, e->name) == 0) {
  799. + return (e);
  800. + }
  801. + }
  802. + }
  803. + e = e->next;
  804. + }
  805. + return (NULL);
  806. +}
  807. +
  808. +void modify_entry(char *full_path, unsigned long uid, unsigned long gid,
  809. + unsigned long mode, unsigned long rdev, struct entry *root, loff_t *fslen_ub)
  810. +{
  811. + char *name, *path, *full;
  812. + struct entry *curr, *parent, *entry, *prev;
  813. +
  814. + full = xstrdup(full_path);
  815. + path = xstrdup(dirname(full));
  816. + name = full_path + strlen(path) + 1;
  817. + free(full);
  818. + if (strcmp(path, "/") == 0) {
  819. + parent = root;
  820. + name = full_path + 1;
  821. + } else {
  822. + if (!(parent = find_filesystem_entry(root, path+1, S_IFDIR)))
  823. + error_msg_and_die("%s/%s: could not find parent\n", path, name);
  824. + }
  825. + if ((entry = find_filesystem_entry(parent, name, (mode & S_IFMT)))) {
  826. + /* its there, just modify permissions */
  827. + entry->mode = mode;
  828. + entry->uid = uid;
  829. + entry->gid = gid;
  830. + } else { /* make a new entry */
  831. +
  832. + /* code partially replicated from parse_directory() */
  833. + size_t namelen;
  834. + if (S_ISREG(mode)) {
  835. + error_msg_and_die("%s: regular file from device_table file must exist on disk!", full_path);
  836. + }
  837. +
  838. + namelen = strlen(name);
  839. + if (namelen > MAX_INPUT_NAMELEN) {
  840. + error_msg_and_die(
  841. + "Very long (%u bytes) filename `%s' found.\n"
  842. + " Please increase MAX_INPUT_NAMELEN in mkcramfs.c and recompile. Exiting.\n",
  843. + namelen, name);
  844. + }
  845. + entry = xcalloc(1, sizeof(struct entry));
  846. + entry->name = xstrdup(name);
  847. + /* truncate multi-byte UTF-8 filenames on character boundary */
  848. + if (namelen > CRAMFS_MAXPATHLEN) {
  849. + namelen = CRAMFS_MAXPATHLEN;
  850. + warn_namelen = 1;
  851. + /* the first lost byte must not be a trail byte */
  852. + while ((entry->name[namelen] & 0xc0) == 0x80) {
  853. + namelen--;
  854. + /* are we reasonably certain it was UTF-8 ? */
  855. + if (entry->name[namelen] < 0x80 || !namelen) {
  856. + error_msg_and_die("cannot truncate filenames not encoded in UTF-8");
  857. + }
  858. + }
  859. + entry->name[namelen] = '\0';
  860. + }
  861. + entry->mode = mode;
  862. + entry->uid = uid;
  863. + entry->gid = gid;
  864. + entry->size = 0;
  865. + if (S_ISBLK(mode) || S_ISCHR(mode)) {
  866. + entry->size = rdev;
  867. + if (entry->size & -(1<<CRAMFS_SIZE_WIDTH))
  868. + warn_dev = 1;
  869. + }
  870. +
  871. + /* ok, now we have to backup and correct the size of all the entries above us */
  872. + *fslen_ub += sizeof(struct cramfs_inode) + ((namelen + 3) & ~3);
  873. + parent->size += sizeof(struct cramfs_inode) + ((namelen + 3) & ~3);
  874. +
  875. + /* alright, time to link us in */
  876. + curr = parent->child;
  877. + prev = NULL;
  878. + while (curr && strcmp(name, curr->name) > 0) {
  879. + prev = curr;
  880. + curr = curr->next;
  881. + }
  882. + if (!prev) parent->child = entry;
  883. + else prev->next = entry;
  884. + entry->next = curr;
  885. + entry->child = NULL;
  886. + }
  887. + if (entry->uid >= 1 << CRAMFS_UID_WIDTH)
  888. + warn_uid = 1;
  889. + if (entry->gid >= 1 << CRAMFS_GID_WIDTH) {
  890. + /* TODO: We ought to replace with a default
  891. + gid instead of truncating; otherwise there
  892. + are security problems. Maybe mode should
  893. + be &= ~070. Same goes for uid once Linux
  894. + supports >16-bit uids. */
  895. + warn_gid = 1;
  896. + }
  897. + free(path);
  898. +}
  899. +
  900. +/* the GNU C library has a wonderful scanf("%as", string) which will
  901. + allocate the string with the right size, good to avoid buffer overruns.
  902. + the following macros use it if available or use a hacky workaround...
  903. + */
  904. +
  905. +#ifdef __GNUC__
  906. +#define SCANF_PREFIX "a"
  907. +#define SCANF_STRING(s) (&s)
  908. +#define GETCWD_SIZE 0
  909. +#else
  910. +#define SCANF_PREFIX "511"
  911. +#define SCANF_STRING(s) (s = xmalloc(512))
  912. +#define GETCWD_SIZE -1
  913. +inline int snprintf(char *str, size_t n, const char *fmt, ...)
  914. +{
  915. + int ret;
  916. + va_list ap;
  917. +
  918. + va_start(ap, fmt);
  919. + ret = vsprintf(str, fmt, ap);
  920. + va_end(ap);
  921. + return ret;
  922. +}
  923. +#endif
  924. +
  925. +/* device table entries take the form of:
  926. + <path> <type> <mode> <uid> <gid> <major> <minor> <start> <inc> <count>
  927. + /dev/mem c 640 0 0 1 1 0 0 -
  928. +
  929. + type can be one of:
  930. + f A regular file
  931. + d Directory
  932. + c Character special device file
  933. + b Block special device file
  934. + p Fifo (named pipe)
  935. +
  936. + I don't bother with symlinks (permissions are irrelevant), hard
  937. + links (special cases of regular files), or sockets (why bother).
  938. +
  939. + Regular files must exist in the target root directory. If a char,
  940. + block, fifo, or directory does not exist, it will be created.
  941. +*/
  942. +
  943. +static int interpret_table_entry(char *line, struct entry *root, loff_t *fslen_ub)
  944. +{
  945. + char type, *name = NULL;
  946. + unsigned long mode = 0755, uid = 0, gid = 0, major = 0, minor = 0;
  947. + unsigned long start = 0, increment = 1, count = 0;
  948. +
  949. + if (sscanf (line, "%" SCANF_PREFIX "s %c %lo %lu %lu %lu %lu %lu %lu %lu",
  950. + SCANF_STRING(name), &type, &mode, &uid, &gid, &major, &minor,
  951. + &start, &increment, &count) < 0)
  952. + {
  953. + return 1;
  954. + }
  955. +
  956. + if (!strcmp(name, "/")) {
  957. + error_msg_and_die("Device table entries require absolute paths");
  958. + }
  959. +
  960. + switch (type) {
  961. + case 'd':
  962. + mode |= S_IFDIR;
  963. + modify_entry(name, uid, gid, mode, 0, root, fslen_ub);
  964. + break;
  965. + case 'f':
  966. + mode |= S_IFREG;
  967. + modify_entry(name, uid, gid, mode, 0, root, fslen_ub);
  968. + break;
  969. + case 'p':
  970. + mode |= S_IFIFO;
  971. + modify_entry(name, uid, gid, mode, 0, root, fslen_ub);
  972. + break;
  973. + case 'c':
  974. + case 'b':
  975. + mode |= (type == 'c') ? S_IFCHR : S_IFBLK;
  976. + if (count > 0) {
  977. + char *buf;
  978. + unsigned long i;
  979. + dev_t rdev;
  980. +
  981. + for (i = start; i < count; i++) {
  982. + asprintf(&buf, "%s%lu", name, i);
  983. + rdev = makedev(major, minor + (i * increment - start));
  984. + modify_entry(buf, uid, gid, mode, rdev, root, fslen_ub);
  985. + free(buf);
  986. + }
  987. + } else {
  988. + dev_t rdev = makedev(major, minor);
  989. + modify_entry(name, uid, gid, mode, rdev, root, fslen_ub);
  990. + }
  991. + break;
  992. + default:
  993. + error_msg_and_die("Unsupported file type");
  994. + }
  995. + free(name);
  996. + return 0;
  997. +}
  998. +
  999. +static int parse_device_table(FILE *file, struct entry *root, loff_t *fslen_ub)
  1000. +{
  1001. + char *line;
  1002. + int status = 0;
  1003. + size_t length = 0;
  1004. +
  1005. + /* Turn off squash, since we must ensure that values
  1006. + * entered via the device table are not squashed */
  1007. + opt_squash = 0;
  1008. +
  1009. + /* Looks ok so far. The general plan now is to read in one
  1010. + * line at a time, check for leading comment delimiters ('#'),
  1011. + * then try and parse the line as a device table. If we fail
  1012. + * to parse things, try and help the poor fool to fix their
  1013. + * device table with a useful error msg... */
  1014. + line = NULL;
  1015. + while (getline(&line, &length, file) != -1) {
  1016. + /* First trim off any whitespace */
  1017. + int len = strlen(line);
  1018. +
  1019. + /* trim trailing whitespace */
  1020. + while (len > 0 && isspace(line[len - 1]))
  1021. + line[--len] = '\0';
  1022. + /* trim leading whitespace */
  1023. + memmove(line, &line[strspn(line, " \n\r\t\v")], len);
  1024. +
  1025. + /* How long are we after trimming? */
  1026. + len = strlen(line);
  1027. +
  1028. + /* If this is NOT a comment line, try to interpret it */
  1029. + if (len && *line != '#') {
  1030. + if (interpret_table_entry(line, root, fslen_ub))
  1031. + status = 1;
  1032. + }
  1033. +
  1034. + free(line);
  1035. + line = NULL;
  1036. + }
  1037. + free(line);
  1038. + fclose(file);
  1039. +
  1040. + return status;
  1041. +}
  1042. +
  1043. +void traverse(struct entry *entry, int depth)
  1044. +{
  1045. + struct entry *curr = entry;
  1046. + int i;
  1047. +
  1048. + while (curr) {
  1049. + for (i = 0; i < depth; i++) putchar(' ');
  1050. + printf("%s: size=%d mode=%d same=%p\n",
  1051. + (curr->name)? (char*)curr->name : "/",
  1052. + curr->size, curr->mode, curr->same);
  1053. + if (curr->child) traverse(curr->child, depth + 4);
  1054. + curr = curr->next;
  1055. + }
  1056. +}
  1057. +
  1058. +static void free_filesystem_entry(struct entry *dir)
  1059. +{
  1060. + struct entry *e = dir, *last;
  1061. +
  1062. + if (S_ISDIR(dir->mode)) {
  1063. + e = dir->child;
  1064. + }
  1065. + while (e) {
  1066. + if (e->name)
  1067. + free(e->name);
  1068. + if (e->path)
  1069. + free(e->path);
  1070. + if (e->uncompressed)
  1071. + free(e->uncompressed);
  1072. + last = e;
  1073. + if (e->child) {
  1074. + free_filesystem_entry(e);
  1075. + }
  1076. + e = e->next;
  1077. + free(last);
  1078. + }
  1079. +}
  1080. +
  1081. +
  1082. +/*
  1083. + * Usage:
  1084. + *
  1085. + * mkcramfs directory-name outfile
  1086. + *
  1087. + * where "directory-name" is simply the root of the directory
  1088. + * tree that we want to generate a compressed filesystem out
  1089. + * of.
  1090. + */
  1091. int main(int argc, char **argv)
  1092. {
  1093. struct stat st; /* used twice... */
  1094. @@ -692,6 +1138,7 @@
  1095. u32 crc;
  1096. int c; /* for getopt */
  1097. char *ep; /* for strtoul */
  1098. + FILE *devtable = NULL;
  1099. total_blocks = 0;
  1100. @@ -699,7 +1146,7 @@
  1101. progname = argv[0];
  1102. /* command line options */
  1103. - while ((c = getopt(argc, argv, "hEe:i:n:psvz")) != EOF) {
  1104. + while ((c = getopt(argc, argv, "hEe:i:n:psvzD:q")) != EOF) {
  1105. switch (c) {
  1106. case 'h':
  1107. usage(MKFS_OK);
  1108. @@ -715,7 +1162,7 @@
  1109. case 'i':
  1110. opt_image = optarg;
  1111. if (lstat(opt_image, &st) < 0) {
  1112. - die(MKFS_ERROR, 1, "lstat failed: %s", opt_image);
  1113. + error_msg_and_die("lstat failed: %s", opt_image);
  1114. }
  1115. image_length = st.st_size; /* may be padded later */
  1116. fslen_ub += (image_length + 3); /* 3 is for padding */
  1117. @@ -736,6 +1183,16 @@
  1118. case 'z':
  1119. opt_holes = 1;
  1120. break;
  1121. + case 'q':
  1122. + opt_squash = 1;
  1123. + break;
  1124. + case 'D':
  1125. + devtable = xfopen(optarg, "r");
  1126. + if (fstat(fileno(devtable), &st) < 0)
  1127. + perror_msg_and_die(optarg);
  1128. + if (st.st_size < 10)
  1129. + error_msg_and_die("%s: not a proper device table file\n", optarg);
  1130. + break;
  1131. }
  1132. }
  1133. @@ -745,25 +1202,23 @@
  1134. outfile = argv[optind + 1];
  1135. if (stat(dirname, &st) < 0) {
  1136. - die(MKFS_USAGE, 1, "stat failed: %s", dirname);
  1137. - }
  1138. - fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  1139. - if (fd < 0) {
  1140. - die(MKFS_USAGE, 1, "open failed: %s", outfile);
  1141. + error_msg_and_die("stat failed: %s", dirname);
  1142. }
  1143. + fd = xopen(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  1144. - root_entry = calloc(1, sizeof(struct entry));
  1145. - if (!root_entry) {
  1146. - die(MKFS_ERROR, 1, "calloc failed");
  1147. - }
  1148. + root_entry = xcalloc(1, sizeof(struct entry));
  1149. root_entry->mode = st.st_mode;
  1150. root_entry->uid = st.st_uid;
  1151. root_entry->gid = st.st_gid;
  1152. root_entry->size = parse_directory(root_entry, dirname, &root_entry->child, &fslen_ub);
  1153. + if (devtable) {
  1154. + parse_device_table(devtable, root_entry, &fslen_ub);
  1155. + }
  1156. +
  1157. /* always allocate a multiple of blksize bytes because that's
  1158. - what we're going to write later on */
  1159. + what we're going to write later on */
  1160. fslen_ub = ((fslen_ub - 1) | (blksize - 1)) + 1;
  1161. if (fslen_ub > MAXFSLEN) {
  1162. @@ -790,7 +1245,7 @@
  1163. rom_image = mmap(NULL, fslen_ub?fslen_ub:1, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  1164. if (rom_image == MAP_FAILED) {
  1165. - die(MKFS_ERROR, 1, "mmap failed");
  1166. + error_msg_and_die("mmap failed");
  1167. }
  1168. /* Skip the first opt_pad bytes for boot loader code */
  1169. @@ -807,6 +1262,7 @@
  1170. }
  1171. offset = write_directory_structure(root_entry->child, rom_image, offset);
  1172. + if (opt_verbose)
  1173. printf("Directory data: %d bytes\n", offset);
  1174. offset = write_data(root_entry, rom_image, offset);
  1175. @@ -814,30 +1270,38 @@
  1176. /* We always write a multiple of blksize bytes, so that
  1177. losetup works. */
  1178. offset = ((offset - 1) | (blksize - 1)) + 1;
  1179. + if (opt_verbose)
  1180. printf("Everything: %d kilobytes\n", offset >> 10);
  1181. /* Write the superblock now that we can fill in all of the fields. */
  1182. write_superblock(root_entry, rom_image+opt_pad, offset);
  1183. + if (opt_verbose)
  1184. printf("Super block: %d bytes\n", sizeof(struct cramfs_super));
  1185. /* Put the checksum in. */
  1186. crc = crc32(0L, Z_NULL, 0);
  1187. crc = crc32(crc, (rom_image+opt_pad), (offset-opt_pad));
  1188. ((struct cramfs_super *) (rom_image+opt_pad))->fsid.crc = crc;
  1189. + if (opt_verbose)
  1190. printf("CRC: %x\n", crc);
  1191. /* Check to make sure we allocated enough space. */
  1192. if (fslen_ub < offset) {
  1193. - die(MKFS_ERROR, 0, "not enough space allocated for ROM image (%Ld allocated, %d used)", fslen_ub, offset);
  1194. + error_msg_and_die("not enough space allocated for ROM "
  1195. + "image (%Ld allocated, %d used)", fslen_ub, offset);
  1196. }
  1197. written = write(fd, rom_image, offset);
  1198. if (written < 0) {
  1199. - die(MKFS_ERROR, 1, "write failed");
  1200. + error_msg_and_die("write failed");
  1201. }
  1202. if (offset != written) {
  1203. - die(MKFS_ERROR, 0, "ROM image write failed (wrote %d of %d bytes)", written, offset);
  1204. + error_msg_and_die("ROM image write failed (wrote %d of %d bytes)", written, offset);
  1205. }
  1206. +
  1207. + /* Free up memory */
  1208. + free_filesystem_entry(root_entry);
  1209. + free(root_entry);
  1210. /* (These warnings used to come at the start, but they scroll off the
  1211. screen too quickly.) */