Commit 3b1ec9fb8197197d5e3bcca3a05e82d4f50f11bc

Authored by Sally, Gene
Committed by Sam Ravnborg
1 parent efddd79512

kbuild: gen_init_cpio expands shell variables in file names

Modify gen_init_cpio so that lines that specify files can contain
what looks like a shell variable that's expanded during processing.

For example:

   file /sbin/kinit ${RFS_BASE}/usr/src/klibc/kinit/kinit 0755 0 0

given RFS_BASE is "/some/directory" in the environment

would be expanded to

   file /sbin/kinit /some/directory/usr/src/klibc/kinit/kinit 0755 0 0

If several environment variables appear in a line, they are all expanded
with processing happening from left to right.
Undefined variables expand to a null string.
Syntax errors stop processing, letting the existing error handling
show the user offending line.

This patch helps embedded folks who frequently create several
RFS directories and then switch between them as they're tuning
an initramfs.

Signed-off-by: gene.sally@timesys.com
Signed-off-by: Sam Ravnborg <sam@ravnborg.org>

Showing 1 changed file with 27 additions and 1 deletions Side-by-side Diff

... ... @@ -370,6 +370,30 @@
370 370 return rc;
371 371 }
372 372  
  373 +static char *cpio_replace_env(char *new_location)
  374 +{
  375 + char expanded[PATH_MAX + 1];
  376 + char env_var[PATH_MAX + 1];
  377 + char *start;
  378 + char *end;
  379 +
  380 + for (start = NULL; (start = strstr(new_location, "${")); ) {
  381 + end = strchr(start, '}');
  382 + if (start < end) {
  383 + *env_var = *expanded = '\0';
  384 + strncat(env_var, start + 2, end - start - 2);
  385 + strncat(expanded, new_location, start - new_location);
  386 + strncat(expanded, getenv(env_var), PATH_MAX);
  387 + strncat(expanded, end + 1, PATH_MAX);
  388 + strncpy(new_location, expanded, PATH_MAX);
  389 + } else
  390 + break;
  391 + }
  392 +
  393 + return new_location;
  394 +}
  395 +
  396 +
373 397 static int cpio_mkfile_line(const char *line)
374 398 {
375 399 char name[PATH_MAX + 1];
... ... @@ -415,7 +439,8 @@
415 439 } else {
416 440 dname = name;
417 441 }
418   - rc = cpio_mkfile(dname, location, mode, uid, gid, nlinks);
  442 + rc = cpio_mkfile(dname, cpio_replace_env(location),
  443 + mode, uid, gid, nlinks);
419 444 fail:
420 445 if (dname_len) free(dname);
421 446 return rc;
... ... @@ -439,6 +464,7 @@
439 464 "\n"
440 465 "<name> name of the file/dir/nod/etc in the archive\n"
441 466 "<location> location of the file in the current filesystem\n"
  467 + " expands shell variables quoted with ${}\n"
442 468 "<target> link target\n"
443 469 "<mode> mode/permissions of the file\n"
444 470 "<uid> user id (0=root)\n"