usr/gen_init_cpio.c

Source file repositories/reference/linux-study-clean/usr/gen_init_cpio.c

File Facts

System
Linux kernel
Corpus path
usr/gen_init_cpio.c
Extension
.c
Size
17489 bytes
Lines
782
Domain
Support Tooling And Documentation
Bucket
usr
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

struct file_handler {
	const char *type;
	int (*handler)(const char *line);
};

static int push_buf(const char *name, size_t name_len)
{
	ssize_t len;

	len = write(outfd, name, name_len);
	if (len != name_len)
		return -1;

	offset += name_len;
	return 0;
}

static int push_pad(size_t padlen)
{
	ssize_t len = 0;

	if (!padlen)
		return 0;

	if (padlen < sizeof(padding))
		len = write(outfd, padding, padlen);
	if (len != padlen)
		return -1;

	offset += padlen;
	return 0;
}

static int push_rest(const char *name, size_t name_len)
{
	ssize_t len;

	len = write(outfd, name, name_len);
	if (len != name_len)
		return -1;

	offset += name_len;

	return push_pad(padlen(name_len + CPIO_HDR_LEN, 4));
}

static int cpio_trailer(void)
{
	int len;
	unsigned int namesize = sizeof(CPIO_TRAILER);

	len = dprintf(outfd, "%s%08X%08X%08lX%08lX%08X%08lX"
	       "%08X%08X%08X%08X%08X%08X%08X",
		do_csum ? "070702" : "070701", /* magic */
		0,			/* ino */
		0,			/* mode */
		(long) 0,		/* uid */
		(long) 0,		/* gid */
		1,			/* nlink */
		(long) 0,		/* mtime */
		0,			/* filesize */
		0,			/* major */
		0,			/* minor */
		0,			/* rmajor */
		0,			/* rminor */
		namesize,		/* namesize */
		0);			/* chksum */
	offset += len;

	if (len != CPIO_HDR_LEN ||
	    push_rest(CPIO_TRAILER, namesize) < 0 ||
	    push_pad(padlen(offset, 512)) < 0)
		return -1;

	if (fsync(outfd) < 0 && errno != EINVAL)
		return -1;

	return 0;
}

static int cpio_mkslink(const char *name, const char *target,
			 unsigned int mode, uid_t uid, gid_t gid)
{
	int len;
	unsigned int namesize, targetsize = strlen(target) + 1;

	if (name[0] == '/')
		name++;
	namesize = strlen(name) + 1;

Annotation

Implementation Notes