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

📄 mkcramfs.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <sys/types.h>#include <stdio.h>#include <sys/stat.h>#include <unistd.h>#include <sys/mman.h>#include <sys/fcntl.h>#include <dirent.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <assert.h>/* zlib required.. */#include <zlib.h>typedef unsigned char u8;typedef unsigned short u16;typedef unsigned int u32;#include "cramfs.h"static const char *progname = "mkcramfs";/* N.B. If you change the disk format of cramfs, please update fs/cramfs/README. */static void usage(void){	fprintf(stderr, "Usage: '%s dirname outfile'\n"		" where <dirname> is the root of the\n"		" filesystem to be compressed.\n", progname);	exit(1);}/* * If DO_HOLES is defined, then mkcramfs can create explicit holes in the * data, which saves 26 bytes per hole (which is a lot smaller a saving than * most filesystems). * * Note that kernels up to at least 2.3.39 don't support cramfs holes, which * is why this defaults to undefined at the moment. *//* #define DO_HOLES 1 */#define PAGE_CACHE_SIZE (4096)/* The kernel assumes PAGE_CACHE_SIZE as block size. */static unsigned int blksize = PAGE_CACHE_SIZE;static int warn_dev, warn_gid, warn_namelen, warn_size, warn_uid;#ifndef MIN# define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))#endif/* In-core version of inode / directory entry. */struct entry {	/* stats */	char *name;	unsigned int mode, size, uid, gid;	/* FS data */	void *uncompressed;        /* points to other identical file */        struct entry *same;        unsigned int offset;            /* pointer to compressed data in archive */	unsigned int dir_offset;	/* Where in the archive is the directory entry? */	/* organization */	struct entry *child; /* null for non-directories and empty directories */	struct entry *next;};/* * Width of various bitfields in struct cramfs_inode. * Used only to generate warnings. */#define SIZE_WIDTH 24#define UID_WIDTH 16#define GID_WIDTH 8#define OFFSET_WIDTH 26/* * The longest file name component to allow for in the input directory tree. * Ext2fs (and many others) allow up to 255 bytes.  A couple of filesystems * allow longer (e.g. smbfs 1024), but there isn't much use in supporting * >255-byte names in the input directory tree given that such names get * truncated to 255 bytes when written to cramfs. */#define MAX_INPUT_NAMELEN 255static int find_identical_file(struct entry *orig,struct entry *newfile){        if(orig==newfile) return 1;        if(!orig) return 0;        if(orig->size==newfile->size && orig->uncompressed && !memcmp(orig->uncompressed,newfile->uncompressed,orig->size)) {                newfile->same=orig;                return 1;        }        return find_identical_file(orig->child,newfile) ||                   find_identical_file(orig->next,newfile);}static void eliminate_doubles(struct entry *root,struct entry *orig) {        if(orig) {                if(orig->size && orig->uncompressed) 			find_identical_file(root,orig);                eliminate_doubles(root,orig->child);                eliminate_doubles(root,orig->next);        }}static unsigned int parse_directory(struct entry *root_entry, const char *name, struct entry **prev, loff_t *fslen_ub){	DIR *dir;	int count = 0, totalsize = 0;	struct dirent *dirent;	char *path, *endpath;	size_t len = strlen(name);	dir = opendir(name);	if (!dir) {		perror(name);		exit(2);	}	/* Set up the path. */	/* TODO: Reuse the parent's buffer to save memcpy'ing and duplication. */	path = malloc(len + 1 + MAX_INPUT_NAMELEN + 1);	if (!path) {		perror(NULL);		exit(1);	}	memcpy(path, name, len);	endpath = path + len;	*endpath = '/';	endpath++;	while ((dirent = readdir(dir)) != NULL) {		struct entry *entry;		struct stat st;		int size;		size_t namelen;		/* Ignore "." and ".." - we won't be adding them to the archive */		if (dirent->d_name[0] == '.') {			if (dirent->d_name[1] == '\0')				continue;			if (dirent->d_name[1] == '.') {				if (dirent->d_name[2] == '\0')					continue;			}		}		namelen = strlen(dirent->d_name);		if (namelen > MAX_INPUT_NAMELEN) {			fprintf(stderr,				"Very long (%u bytes) filename `%s' found.\n"				" Please increase MAX_INPUT_NAMELEN in mkcramfs.c and recompile.  Exiting.\n",				namelen, dirent->d_name);			exit(1);		}		memcpy(endpath, dirent->d_name, namelen + 1);		if (lstat(path, &st) < 0) {			perror(endpath);			continue;		}		entry = calloc(1, sizeof(struct entry));		if (!entry) {			perror(NULL);			exit(5);		}		entry->name = strdup(dirent->d_name);		if (!entry->name) {			perror(NULL);			exit(1);		}		if (namelen > 255) {			/* Can't happen when reading from ext2fs. */			/* TODO: we ought to avoid chopping in half			   multi-byte UTF8 characters. */			entry->name[namelen = 255] = '\0';			warn_namelen = 1;		}		entry->mode = st.st_mode;		entry->size = st.st_size;		entry->uid = st.st_uid;		if (entry->uid >= 1 << UID_WIDTH)			warn_uid = 1;		entry->gid = st.st_gid;		if (entry->gid >= 1 << GID_WIDTH)			/* TODO: We ought to replace with a default                           gid instead of truncating; otherwise there                           are security problems.  Maybe mode should                           be &= ~070.  Same goes for uid once Linux                           supports >16-bit uids. */			warn_gid = 1;		size = sizeof(struct cramfs_inode) + ((namelen + 3) & ~3);		*fslen_ub += size;		if (S_ISDIR(st.st_mode)) {			entry->size = parse_directory(root_entry, path, &entry->child, fslen_ub);		} else if (S_ISREG(st.st_mode)) {			/* TODO: We ought to open files in do_compress, one			   at a time, instead of amassing all these memory			   maps during parse_directory (which don't get used			   until do_compress anyway).  As it is, we tend to			   get EMFILE errors (especially if mkcramfs is run			   by non-root).			   While we're at it, do analagously for symlinks			   (which would just save a little memory). */			int fd = open(path, O_RDONLY);			if (fd < 0) {				perror(path);				continue;			}			if (entry->size) {				if ((entry->size >= 1 << SIZE_WIDTH)) {					warn_size = 1;					entry->size = (1 << SIZE_WIDTH) - 1;				}				entry->uncompressed = mmap(NULL, entry->size, PROT_READ, MAP_PRIVATE, fd, 0);				if (-1 == (int) (long) entry->uncompressed) {					perror("mmap");					exit(5);				}			}			close(fd);		} else if (S_ISLNK(st.st_mode)) {			entry->uncompressed = malloc(entry->size);			if (!entry->uncompressed) {				perror(NULL);				exit(5);			}			if (readlink(path, entry->uncompressed, entry->size) < 0) {				perror(path);				continue;			}		} else {			entry->size = st.st_rdev;			if (entry->size & -(1<<SIZE_WIDTH))				warn_dev = 1;		}		if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {			/* block pointers & data expansion allowance + data */                        if(entry->size)                                 *fslen_ub += ((4+26)*((entry->size - 1) / blksize + 1)                                              + MIN(entry->size + 3, st.st_blocks << 9));                        else                                 *fslen_ub += MIN(entry->size + 3, st.st_blocks << 9);                }		/* Link it into the list */		*prev = entry;		prev = &entry->next;		count++;		totalsize += size;	}	closedir(dir);	free(path);	return totalsize;}static void set_random(void *area, size_t size){	int fd = open("/dev/random", O_RDONLY);	if (fd >= 0) {		if (read(fd, area, size) == size)			return;	}	memset(area, 0x00, size);}/* Returns sizeof(struct cramfs_super), which includes the root inode. */static unsigned int write_superblock(struct entry *root, char *base){	struct cramfs_super *super = (struct cramfs_super *) base;	unsigned int offset = sizeof(struct cramfs_super);	super->magic = CRAMFS_MAGIC;	super->flags = 0;	/* Note: 0x10000 is meaningless, which is a bug; but	   super->size is never used anyway. */	super->size = 0x10000;	memcpy(super->signature, CRAMFS_SIGNATURE, sizeof(super->signature));	set_random(super->fsid, sizeof(super->fsid));	strncpy(super->name, "Compressed", sizeof(super->name));	super->root.mode = root->mode;	super->root.uid = root->uid;	super->root.gid = root->gid;	super->root.size = root->size;	super->root.offset = offset >> 2;	return offset;}static void set_data_offset(struct entry *entry, char *base, unsigned long offset){	struct cramfs_inode *inode = (struct cramfs_inode *) (base + entry->dir_offset);	assert ((offset & 3) == 0);	if (offset >= (1 << (2 + OFFSET_WIDTH))) {		fprintf(stderr, "filesystem too big.  Exiting.\n");		exit(1);	}	inode->offset = (offset >> 2);}/* * We do a width-first printout of the directory

⌨️ 快捷键说明

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