Commit 24fa5096 authored by Luciano Rocha's avatar Luciano Rocha Committed by Linus Torvalds

[PATCH] usr/gen_init_cpio.c: support for hard links

Extend usr/gen_init_cpio.c "file" entry, adding support for hard links.

Previous format:
file <name> <location> <mode> <uid> <gid>

New format:
file <name> <location> <mode> <uid> <gid> [<hard links>]

The hard links specification is optional, keeping the previous
behaviour.

All hard links are defined sequentially in the resulting cpio and the
file data is present only in the last link. This is the behaviour of
GNU's cpio and is supported by the kernel initramfs extractor.
Signed-off-by: default avatarLuciano Rocha <strange@nsk.no-ip.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 67d38229
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
* Original work by Jeff Garzik * Original work by Jeff Garzik
* *
* External file lists, symlink, pipe and fifo support by Thayne Harbaugh * External file lists, symlink, pipe and fifo support by Thayne Harbaugh
* Hard link support by Luciano Rocha
*/ */
#define xstr(s) #s #define xstr(s) #s
...@@ -286,16 +287,19 @@ static int cpio_mknod_line(const char *line) ...@@ -286,16 +287,19 @@ static int cpio_mknod_line(const char *line)
return rc; return rc;
} }
/* Not marked static to keep the compiler quiet, as no one uses this yet... */
static int cpio_mkfile(const char *name, const char *location, static int cpio_mkfile(const char *name, const char *location,
unsigned int mode, uid_t uid, gid_t gid) unsigned int mode, uid_t uid, gid_t gid,
unsigned int nlinks)
{ {
char s[256]; char s[256];
char *filebuf = NULL; char *filebuf = NULL;
struct stat buf; struct stat buf;
long size;
int file = -1; int file = -1;
int retval; int retval;
int rc = -1; int rc = -1;
int namesize;
int i;
mode |= S_IFREG; mode |= S_IFREG;
...@@ -323,29 +327,41 @@ static int cpio_mkfile(const char *name, const char *location, ...@@ -323,29 +327,41 @@ static int cpio_mkfile(const char *name, const char *location,
goto error; goto error;
} }
sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX" size = 0;
"%08X%08X%08X%08X%08X%08X%08X", for (i = 1; i <= nlinks; i++) {
"070701", /* magic */ /* data goes on last link */
ino++, /* ino */ if (i == nlinks) size = buf.st_size;
mode, /* mode */
(long) uid, /* uid */ namesize = strlen(name) + 1;
(long) gid, /* gid */ sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
1, /* nlink */ "%08lX%08X%08X%08X%08X%08X%08X",
(long) buf.st_mtime, /* mtime */ "070701", /* magic */
(int) buf.st_size, /* filesize */ ino, /* ino */
3, /* major */ mode, /* mode */
1, /* minor */ (long) uid, /* uid */
0, /* rmajor */ (long) gid, /* gid */
0, /* rminor */ nlinks, /* nlink */
(unsigned)strlen(name) + 1,/* namesize */ (long) buf.st_mtime, /* mtime */
0); /* chksum */ size, /* filesize */
push_hdr(s); 3, /* major */
push_string(name); 1, /* minor */
push_pad(); 0, /* rmajor */
0, /* rminor */
namesize, /* namesize */
0); /* chksum */
push_hdr(s);
push_string(name);
push_pad();
if (size) {
fwrite(filebuf, size, 1, stdout);
offset += size;
push_pad();
}
fwrite(filebuf, buf.st_size, 1, stdout); name += namesize;
offset += buf.st_size; }
push_pad(); ino++;
rc = 0; rc = 0;
error: error:
...@@ -357,18 +373,51 @@ static int cpio_mkfile(const char *name, const char *location, ...@@ -357,18 +373,51 @@ static int cpio_mkfile(const char *name, const char *location,
static int cpio_mkfile_line(const char *line) static int cpio_mkfile_line(const char *line)
{ {
char name[PATH_MAX + 1]; char name[PATH_MAX + 1];
char *dname = NULL; /* malloc'ed buffer for hard links */
char location[PATH_MAX + 1]; char location[PATH_MAX + 1];
unsigned int mode; unsigned int mode;
int uid; int uid;
int gid; int gid;
int nlinks = 1;
int end = 0, dname_len = 0;
int rc = -1; int rc = -1;
if (5 != sscanf(line, "%" str(PATH_MAX) "s %" str(PATH_MAX) "s %o %d %d", name, location, &mode, &uid, &gid)) { if (5 > sscanf(line, "%" str(PATH_MAX) "s %" str(PATH_MAX)
"s %o %d %d %n",
name, location, &mode, &uid, &gid, &end)) {
fprintf(stderr, "Unrecognized file format '%s'", line); fprintf(stderr, "Unrecognized file format '%s'", line);
goto fail; goto fail;
} }
rc = cpio_mkfile(name, location, mode, uid, gid); if (end && isgraph(line[end])) {
int len;
int nend;
dname = malloc(strlen(line));
if (!dname) {
fprintf (stderr, "out of memory (%d)\n", dname_len);
goto fail;
}
dname_len = strlen(name) + 1;
memcpy(dname, name, dname_len);
do {
nend = 0;
if (sscanf(line + end, "%" str(PATH_MAX) "s %n",
name, &nend) < 1)
break;
len = strlen(name) + 1;
memcpy(dname + dname_len, name, len);
dname_len += len;
nlinks++;
end += nend;
} while (isgraph(line[end]));
} else {
dname = name;
}
rc = cpio_mkfile(dname, location, mode, uid, gid, nlinks);
fail: fail:
if (dname_len) free(dname);
return rc; return rc;
} }
...@@ -381,22 +430,23 @@ void usage(const char *prog) ...@@ -381,22 +430,23 @@ void usage(const char *prog)
"describe the files to be included in the initramfs archive:\n" "describe the files to be included in the initramfs archive:\n"
"\n" "\n"
"# a comment\n" "# a comment\n"
"file <name> <location> <mode> <uid> <gid>\n" "file <name> <location> <mode> <uid> <gid> [<hard links>]\n"
"dir <name> <mode> <uid> <gid>\n" "dir <name> <mode> <uid> <gid>\n"
"nod <name> <mode> <uid> <gid> <dev_type> <maj> <min>\n" "nod <name> <mode> <uid> <gid> <dev_type> <maj> <min>\n"
"slink <name> <target> <mode> <uid> <gid>\n" "slink <name> <target> <mode> <uid> <gid>\n"
"pipe <name> <mode> <uid> <gid>\n" "pipe <name> <mode> <uid> <gid>\n"
"sock <name> <mode> <uid> <gid>\n" "sock <name> <mode> <uid> <gid>\n"
"\n" "\n"
"<name> name of the file/dir/nod/etc in the archive\n" "<name> name of the file/dir/nod/etc in the archive\n"
"<location> location of the file in the current filesystem\n" "<location> location of the file in the current filesystem\n"
"<target> link target\n" "<target> link target\n"
"<mode> mode/permissions of the file\n" "<mode> mode/permissions of the file\n"
"<uid> user id (0=root)\n" "<uid> user id (0=root)\n"
"<gid> group id (0=root)\n" "<gid> group id (0=root)\n"
"<dev_type> device type (b=block, c=character)\n" "<dev_type> device type (b=block, c=character)\n"
"<maj> major number of nod\n" "<maj> major number of nod\n"
"<min> minor number of nod\n" "<min> minor number of nod\n"
"<hard links> space separated list of other links to file\n"
"\n" "\n"
"example:\n" "example:\n"
"# A simple initramfs\n" "# A simple initramfs\n"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment