⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mkfs.cramfs.c

📁 Util-linux 软件包包含许多工具。其中比较重要的是加载、卸载、格式化、分区和管理硬盘驱动器
💻 C
📖 第 1 页 / 共 2 页
字号:
 * entries, using a stack to remember the directories * we've seen. */#define MAXENTRIES (100)static unsigned int write_directory_structure(struct entry *entry, char *base, unsigned int offset){	int stack_entries = 0;	struct entry *entry_stack[MAXENTRIES];	for (;;) {		int dir_start = stack_entries;		while (entry) {			struct cramfs_inode *inode =				(struct cramfs_inode *) (base + offset);			size_t len = strlen(entry->name);			entry->dir_offset = offset;			inode->mode = entry->mode;			inode->uid = entry->uid;			inode->gid = entry->gid;			inode->size = entry->size;			inode->offset = 0;			/* Non-empty directories, regfiles and symlinks will			   write over inode->offset later. */			offset += sizeof(struct cramfs_inode);			total_nodes++;	/* another node */			memcpy(base + offset, entry->name, len);			/* Pad up the name to a 4-byte boundary */			while (len & 3) {				*(base + offset + len) = '\0';				len++;			}			inode->namelen = len >> 2;			offset += len;			if (verbose)				printf("  %s\n", entry->name);			if (entry->child) {				if (stack_entries >= MAXENTRIES) {					fprintf(stderr,						_("Exceeded MAXENTRIES.  Raise"						  " this value in mkcramfs.c "						  "and recompile.  Exiting.\n")						);					exit(8);				}				entry_stack[stack_entries] = entry;				stack_entries++;			}			entry = entry->next;		}		/*		 * Reverse the order the stack entries pushed during                 * this directory, for a small optimization of disk                 * access in the created fs.  This change makes things                 * `ls -UR' order.		 */		{			struct entry **lo = entry_stack + dir_start;			struct entry **hi = entry_stack + stack_entries;			struct entry *tmp;			while (lo < --hi) {				tmp = *lo;				*lo++ = *hi;				*hi = tmp;			}		}		/* Pop a subdirectory entry from the stack, and recurse. */		if (!stack_entries)			break;		stack_entries--;		entry = entry_stack[stack_entries];		set_data_offset(entry, base, offset);		if (verbose)			printf("'%s':\n", entry->name);		entry = entry->child;	}	return offset;}static int is_zero(char const *begin, unsigned len){	if (opt_holes)		/* Returns non-zero iff the first LEN bytes from BEGIN are		   all NULs. */		return (len-- == 0 ||			(begin[0] == '\0' &&			 (len-- == 0 ||			  (begin[1] == '\0' &&			   (len-- == 0 ||			    (begin[2] == '\0' &&			     (len-- == 0 ||			      (begin[3] == '\0' &&			       memcmp(begin, begin + 4, len) == 0))))))));	else		/* Never create holes. */		return 0;}/* * One 4-byte pointer per block and then the actual blocked * output. The first block does not need an offset pointer, * as it will start immediately after the pointer block; * so the i'th pointer points to the end of the i'th block * (i.e. the start of the (i+1)'th block or past EOF). * * Note that size > 0, as a zero-sized file wouldn't ever * have gotten here in the first place. */static unsigned intdo_compress(char *base, unsigned int offset, char const *name,	    char *path, unsigned int size, unsigned int mode){	unsigned long original_size, original_offset, new_size, blocks, curr;	int change;	char *p, *start;	/* get uncompressed data */	start = do_mmap(path, size, mode);	if (start == NULL)		return offset;	p = start;	original_size = size;	original_offset = offset;	blocks = (size - 1) / blksize + 1;	curr = offset + 4 * blocks;	total_blocks += blocks;	do {		unsigned long len = 2 * blksize;		unsigned int input = size;		if (input > blksize)			input = blksize;		size -= input;		if (!is_zero (p, input)) {			compress(base + curr, &len, p, input);			curr += len;		}		p += input;		if (len > blksize*2) {			/* (I don't think this can happen with zlib.) */			printf(_("AIEEE: block \"compressed\" to > "				 "2*blocklength (%ld)\n"),			       len);			exit(8);		}		*(u32 *) (base + offset) = curr;		offset += 4;	} while (size);	do_munmap(start, original_size, mode);	curr = (curr + 3) & ~3;	new_size = curr - original_offset;	/* TODO: Arguably, original_size in these 2 lines should be	   st_blocks * 512.  But if you say that, then perhaps	   administrative data should also be included in both. */	change = new_size - original_size;	if (verbose)		printf(_("%6.2f%% (%+d bytes)\t%s\n"),		       (change * 100) / (double) original_size, change, name);	return curr;}/* * Traverse the entry tree, writing data for every item that has * non-null entry->path (i.e. every symlink and non-empty * regfile). */static unsigned intwrite_data(struct entry *entry, char *base, unsigned int offset) {	struct entry *e;	for (e = entry; e; e = e->next) {		if (e->path) {                        if (e->same) {                                set_data_offset(e, base, e->same->offset);                                e->offset = e->same->offset;                        } else {                                set_data_offset(e, base, offset);                                e->offset = offset;                                offset = do_compress(base, offset, e->name,						     e->path, e->size,e->mode);                        }		} else if (e->child)			offset = write_data(e->child, base, offset);	}	return offset;}static unsigned int write_file(char *file, char *base, unsigned int offset){	int fd;	char *buf;	fd = open(file, O_RDONLY);	if (fd < 0) {		perror(file);		exit(8);	}	buf = mmap(NULL, image_length, PROT_READ, MAP_PRIVATE, fd, 0);	memcpy(base + offset, buf, image_length);	munmap(buf, image_length);	close (fd);	/* Pad up the image_length to a 4-byte boundary */	while (image_length & 3) {		*(base + offset + image_length) = '\0';		image_length++;	}	return (offset + image_length);}/* * Maximum size fs you can create is roughly 256MB.  (The last file's * data must begin within 256MB boundary but can extend beyond that.) * * Note that if you want it to fit in a ROM then you're limited to what the * hardware and kernel can support (64MB?). */static unsigned intmaxfslen(void) {	return (((1 << CRAMFS_OFFSET_WIDTH) - 1) << 2)    /* offset */		+ (1 << CRAMFS_SIZE_WIDTH) - 1            /* filesize */		+ (1 << CRAMFS_SIZE_WIDTH) * 4 / blksize; /* block pointers */}/* * Usage: * *      mkcramfs directory-name outfile * * where "directory-name" is simply the root of the directory * tree that we want to generate a compressed filesystem out * of. */int main(int argc, char **argv){	struct stat st;		/* used twice... */	struct entry *root_entry;	char *rom_image;	ssize_t offset, written;	int fd;	/* initial guess (upper-bound) of required filesystem size */	loff_t fslen_ub = sizeof(struct cramfs_super);	unsigned int fslen_max;	char const *dirname, *outfile;	u32 crc = crc32(0L, Z_NULL, 0);	int c;	total_blocks = 0;	if (argc) {		char *p;		progname = argv[0];		if ((p = strrchr(progname, '/')) != NULL)			progname = p+1;	}	/* command line options */	while ((c = getopt(argc, argv, "hb:Ee:i:n:psVvz")) != EOF) {		switch (c) {		case 'h':			usage(0);		case 'b':			blksize = atoi(optarg);			if (blksize <= 0)				usage(1);			break;		case 'E':			opt_errors = 1;			break;		case 'e':			opt_edition = atoi(optarg);			break;		case 'i':			opt_image = optarg;			if (lstat(opt_image, &st) < 0) {				perror(opt_image);				exit(16);			}			image_length = st.st_size; /* may be padded later */			fslen_ub += (image_length + 3); /* 3 is for padding */			break;		case 'n':			opt_name = optarg;			break;		case 'p':			opt_pad = PAD_SIZE;			fslen_ub += PAD_SIZE;			break;		case 's':			/* old option, ignored */			break;		case 'V':			printf(_("%s from %s\n"),			       progname, util_linux_version);			exit(0);		case 'v':			verbose = 1;			break;		case 'z':			opt_holes = 1;			break;		}	}	if ((argc - optind) != 2)		usage(16);	dirname = argv[optind];	outfile = argv[optind + 1];	if (stat(dirname, &st) < 0) {		perror(dirname);		exit(16);	}	fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);	root_entry = calloc(1, sizeof(struct entry));	if (!root_entry) {		perror(NULL);		exit(8);	}	root_entry->mode = st.st_mode;	root_entry->uid = st.st_uid;	root_entry->gid = st.st_gid;	root_entry->size = parse_directory(root_entry, dirname, &root_entry->child, &fslen_ub);	/* always allocate a multiple of blksize bytes because that's           what we're going to write later on */	fslen_ub = ((fslen_ub - 1) | (blksize - 1)) + 1;	fslen_max = maxfslen();	if (fslen_ub > fslen_max) {		fprintf(stderr,			_("warning: guestimate of required size (upper bound) "			  "is %LdMB, but maximum image size is %uMB.  "			  "We might die prematurely.\n"),			fslen_ub >> 20,			fslen_max >> 20);		fslen_ub = fslen_max;	}        /* find duplicate files */        eliminate_doubles(root_entry,root_entry);	/* TODO: Why do we use a private/anonymous mapping here           followed by a write below, instead of just a shared mapping           and a couple of ftruncate calls?  Is it just to save us           having to deal with removing the file afterwards?  If we           really need this huge anonymous mapping, we ought to mmap           in smaller chunks, so that the user doesn't need nn MB of           RAM free.  If the reason is to be able to write to           un-mmappable block devices, then we could try shared mmap           and revert to anonymous mmap if the shared mmap fails. */	rom_image = mmap(NULL,			 fslen_ub?fslen_ub:1,			 PROT_READ | PROT_WRITE,			 MAP_PRIVATE | MAP_ANONYMOUS,			 -1, 0);	if (-1 == (int) (long) rom_image) {		perror("ROM image map");		exit(8);	}	/* Skip the first opt_pad bytes for boot loader code */	offset = opt_pad;	memset(rom_image, 0x00, opt_pad);	/* Skip the superblock and come back to write it later. */	offset += sizeof(struct cramfs_super);	/* Insert a file image. */	if (opt_image) {		if (verbose)			printf(_("Including: %s\n"), opt_image);		offset = write_file(opt_image, rom_image, offset);	}	offset = write_directory_structure(root_entry->child, rom_image, offset);	if (verbose)		printf(_("Directory data: %d bytes\n"), offset);	offset = write_data(root_entry, rom_image, offset);	/* We always write a multiple of blksize bytes, so that           losetup works. */	offset = ((offset - 1) | (blksize - 1)) + 1;	if (verbose)		printf(_("Everything: %d kilobytes\n"), offset >> 10);	/* Write the superblock now that we can fill in all of the fields. */	write_superblock(root_entry, rom_image+opt_pad, offset);	if (verbose)		printf(_("Super block: %d bytes\n"),		       sizeof(struct cramfs_super));	/* Put the checksum in. */	crc = crc32(crc, (rom_image+opt_pad), (offset-opt_pad));	((struct cramfs_super *) (rom_image+opt_pad))->fsid.crc = crc;	if (verbose)		printf(_("CRC: %x\n"), crc);	/* Check to make sure we allocated enough space. */	if (fslen_ub < offset) {		fprintf(stderr,			_("not enough space allocated for ROM image "			  "(%Ld allocated, %d used)\n"),			fslen_ub, offset);		exit(8);	}	written = write(fd, rom_image, offset);	if (written < 0) {		perror("ROM image");		exit(8);	}	if (offset != written) {		fprintf(stderr, _("ROM image write failed (%d %d)\n"),			written, offset);		exit(8);	}	/* (These warnings used to come at the start, but they scroll off the           screen too quickly.) */	if (warn_namelen) /* (can't happen when reading from ext2fs) */		fprintf(stderr, /* bytes, not chars: think UTF8. */			_("warning: filenames truncated to 255 bytes.\n"));	if (warn_skip)		fprintf(stderr,			_("warning: files were skipped due to errors.\n"));	if (warn_size)		fprintf(stderr,			_("warning: file sizes truncated to %luMB "			  "(minus 1 byte).\n"),			1L << (CRAMFS_SIZE_WIDTH - 20));	if (warn_uid) /* (not possible with current Linux versions) */		fprintf(stderr,			_("warning: uids truncated to %u bits.  "			  "(This may be a security concern.)\n"),			CRAMFS_UID_WIDTH);	if (warn_gid)		fprintf(stderr,			_("warning: gids truncated to %u bits.  "			  "(This may be a security concern.)\n"),			CRAMFS_GID_WIDTH);	if (warn_dev)		fprintf(stderr,			_("WARNING: device numbers truncated to %u bits.  "			  "This almost certainly means\n"			  "that some device files will be wrong.\n"),			CRAMFS_OFFSET_WIDTH);	if (opt_errors &&	    (warn_namelen|warn_skip|warn_size|warn_uid|warn_gid|warn_dev))		exit(8);	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -