📄 mkfs.cramfs.c
字号:
/* * mkcramfs - make a cramfs file system * * Copyright (C) 1999-2001 Transmeta Corporation * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//* * Old version would die on largish filesystems. Change to mmap the * files one by one instaed of all simultaneously. - aeb, 2002-11-01 */#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>#include <getopt.h>#include <zlib.h>#include "cramfs.h"#include "md5.h"#include "nls.h"#define PAD_SIZE 512 /* only 0 and 512 supported by kernel */static const char *progname = "mkcramfs";static int verbose = 0;#ifdef __ia64__#define PAGE_CACHE_SIZE (16384)#elif defined __alpha__#define PAGE_CACHE_SIZE (8192)#else#define PAGE_CACHE_SIZE (4096)#endif/* The kernel assumes PAGE_CACHE_SIZE as block size. */static unsigned int blksize = PAGE_CACHE_SIZE; /* settable via -b option */static long total_blocks = 0, total_nodes = 1; /* pre-count the root node */static int image_length = 0;/* * If opt_holes is set, then mkcramfs can create explicit holes in the * data, which saves 26 bytes per hole (which is a lot smaller a * saving than for most filesystems). * * Note that kernels up to at least 2.3.39 don't support cramfs holes, * which is why this is turned off by default. */static int opt_edition = 0;static int opt_errors = 0;static int opt_holes = 0;static int opt_pad = 0;static char *opt_image = NULL;static char *opt_name = NULL;static int warn_dev = 0;static int warn_gid = 0;static int warn_namelen = 0;static int warn_skip = 0;static int warn_size = 0;static int warn_uid = 0;#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; unsigned char md5sum[16]; unsigned char flags;#define HAVE_MD5 1#define INVALID 2 /* FS data */ char *path; struct entry *same; /* points to other identical file */ unsigned int offset; /* pointer to compressed data in archive */ unsigned int dir_offset; /* offset of directory entry in archive */ /* organization */ struct entry *child; /* NULL for non-directory and empty dir */ struct entry *next;};/* * Width of various bitfields in struct cramfs_inode. * Used only to generate warnings. */#define CRAMFS_SIZE_WIDTH 24#define CRAMFS_UID_WIDTH 16#define CRAMFS_GID_WIDTH 8#define CRAMFS_OFFSET_WIDTH 26/* Input status of 0 to print help and exit without an error. */static voidusage(int status) { FILE *stream = status ? stderr : stdout; fprintf(stream, _("usage: %s [-v] [-b blksz] [-e edition] [-i file] [-n name] " "dirname outfile\n" " -h print this help\n" " -v be verbose\n" " -E make all warnings errors " "(non-zero exit status)\n" " -b blksz use this blocksize, must equal page size\n" " -e edition set edition number (part of fsid)\n" " -i file insert a file image into the filesystem " "(requires >= 2.4.0)\n" " -n name set name of cramfs filesystem\n" " -p pad by %d bytes for boot code\n" " -s sort directory entries (old option, ignored)\n" " -z make explicit holes (requires >= 2.3.39)\n" " dirname root of the filesystem to be compressed\n" " outfile output file\n"), progname, PAD_SIZE); exit(status);}/* malloc or die */static void *xmalloc (size_t size) { void *t = malloc(size); if (t == NULL) { perror(NULL); exit(8); /* out of memory */ } return t;}static char *do_mmap(char *path, unsigned int size, unsigned int mode){ int fd; char *start; if (!size) return NULL; if (S_ISLNK(mode)) { start = xmalloc(size); if (readlink(path, start, size) < 0) { perror(path); warn_skip = 1; start = NULL; } return start; } fd = open(path, O_RDONLY); if (fd < 0) { perror(path); warn_skip = 1; return NULL; } start = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); if (-1 == (int) (long) start) { perror("mmap"); exit(8); } close(fd); return start;}static voiddo_munmap(char *start, unsigned int size, unsigned int mode){ if (S_ISLNK(mode)) free(start); else munmap(start, size);}/* compute md5sums, so that we do not have to compare every pair of files */static voidmdfile(struct entry *e) { MD5_CTX ctx; char *start; start = do_mmap(e->path, e->size, e->mode); if (start == NULL) { e->flags |= INVALID; } else { MD5Init(&ctx); MD5Update(&ctx, start, e->size); MD5Final(e->md5sum, &ctx); do_munmap(start, e->size, e->mode); e->flags |= HAVE_MD5; }}/* md5 digests are equal; files are almost certainly the same, but just to be sure, do the comparison */static intidentical_file(struct entry *e1, struct entry *e2){ char *start1, *start2; int equal; start1 = do_mmap(e1->path, e1->size, e1->mode); if (!start1) return 0; start2 = do_mmap(e2->path, e2->size, e2->mode); if (!start2) return 0; equal = !memcmp(start1, start2, e1->size); do_munmap(start1, e1->size, e1->mode); do_munmap(start2, e2->size, e2->mode); return equal;}/* * 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 *new){ if (orig == new) return 1; if (!orig) return 0; if (orig->size == new->size && orig->path) { if (!orig->flags) mdfile(orig); if (!new->flags) mdfile(new); if ((orig->flags & HAVE_MD5) && (new->flags & HAVE_MD5) && !memcmp(orig->md5sum, new->md5sum, 16) && identical_file(orig, new)) { new->same = orig; return 1; } } return find_identical_file(orig->child, new) || find_identical_file(orig->next, new);}static void eliminate_doubles(struct entry *root, struct entry *orig) { if (orig) { if (orig->size && orig->path) find_identical_file(root,orig); eliminate_doubles(root,orig->child); eliminate_doubles(root,orig->next); }}/* * We define our own sorting function instead of using alphasort which * uses strcoll and changes ordering based on locale information. */static int cramsort (const void *a, const void *b){ return strcmp ((*(const struct dirent **) a)->d_name, (*(const struct dirent **) b)->d_name);}static unsigned int parse_directory(struct entry *root_entry, const char *name, struct entry **prev, loff_t *fslen_ub){ struct dirent **dirlist; int totalsize = 0, dircount, dirindex; char *path, *endpath; size_t len = strlen(name); /* Set up the path. */ /* TODO: Reuse the parent's buffer to save memcpy'ing and duplication. */ path = xmalloc(len + 1 + MAX_INPUT_NAMELEN + 1); memcpy(path, name, len); endpath = path + len; *endpath = '/'; endpath++; /* read in the directory and sort */ dircount = scandir(name, &dirlist, 0, cramsort); if (dircount < 0) { perror(name); exit(8); } /* process directory */ for (dirindex = 0; dirindex < dircount; dirindex++) { struct dirent *dirent; struct entry *entry; struct stat st; int size; size_t namelen; dirent = dirlist[dirindex]; /* 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(8); } memcpy(endpath, dirent->d_name, namelen + 1); if (lstat(path, &st) < 0) { perror(endpath); warn_skip = 1; continue; } entry = calloc(1, sizeof(struct entry)); if (!entry) { perror(NULL); exit(8); } entry->name = strdup(dirent->d_name); if (!entry->name) { perror(NULL); exit(8); } 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 << CRAMFS_UID_WIDTH) warn_uid = 1; entry->gid = st.st_gid; if (entry->gid >= 1 << CRAMFS_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)) { entry->path = strdup(path); if (entry->size) { if (entry->size >= (1 << CRAMFS_SIZE_WIDTH)) { warn_size = 1; entry->size = (1 << CRAMFS_SIZE_WIDTH) - 1; } } } else if (S_ISLNK(st.st_mode)) { entry->path = strdup(path); } else if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) { /* maybe we should skip sockets */ entry->size = 0; } else { entry->size = st.st_rdev; if (entry->size & -(1<<CRAMFS_SIZE_WIDTH)) warn_dev = 1; } if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) { int blocks = ((entry->size - 1) / blksize + 1); /* block pointers & data expansion allowance + data */ if (entry->size) *fslen_ub += (4+26)*blocks + entry->size + 3; } /* Link it into the list */ *prev = entry; prev = &entry->next; totalsize += size; } free(path); free(dirlist); /* allocated by scandir() with malloc() */ return totalsize;}/* Returns sizeof(struct cramfs_super), which includes the root inode. */static unsigned int write_superblock(struct entry *root, char *base, int size){ struct cramfs_super *super = (struct cramfs_super *) base; unsigned int offset = sizeof(struct cramfs_super) + image_length; if (opt_pad) { offset += opt_pad; } super->magic = CRAMFS_MAGIC; super->flags = CRAMFS_FLAG_FSID_VERSION_2 | CRAMFS_FLAG_SORTED_DIRS; if (opt_holes) super->flags |= CRAMFS_FLAG_HOLES; if (image_length > 0) super->flags |= CRAMFS_FLAG_SHIFTED_ROOT_OFFSET; super->size = size; memcpy(super->signature, CRAMFS_SIGNATURE, sizeof(super->signature)); super->fsid.crc = crc32(0L, Z_NULL, 0); super->fsid.edition = opt_edition; super->fsid.blocks = total_blocks; super->fsid.files = total_nodes; memset(super->name, 0x00, sizeof(super->name)); if (opt_name) strncpy(super->name, opt_name, sizeof(super->name)); else 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); if (offset >= (1 << (2 + CRAMFS_OFFSET_WIDTH))) { fprintf(stderr, _("filesystem too big. Exiting.\n")); exit(8); } inode->offset = (offset >> 2);}/* * We do a width-first printout of the directory
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -