Usage
plash nodepath CONTAINER [--allow-root-container]
Description
Prints the path of a given image.
The --allow-root-container option
allows the root image ("0") to be specified as image.
Example
$ plash nodepath 19
home/ihucos/.plashdata/layer/0/2/19
$ plash nodepath 19 | xargs tree
Tested Behaviour
#!/bin/bash
set -exu
(! plash nodepath mynoexistentimage)
newcont=$(plash build -f 1 --invalidate-layer)
nodepath=$(plash nodepath $newcont)
echo $nodepath | grep $newcont
layerup=$(plash build -f $newcont --layer --invalidate-layer)
nodepath=$(plash nodepath $layerup)
echo $nodepath | grep $newcont
echo $nodepath | grep $layerup
• test positive number validation
[[ "$(plash nodepath xx 2>&1)" = *"positive number"* ]]
[[ "$(plash nodepath 222x 2>&1)" = *"positive number"* ]]
[[ "$(plash nodepath x222 2>&1)" = *"positive number"* ]]
[[ "$(plash nodepath -1 2>&1)" = *"positive number"* ]]
[[ "$(plash nodepath -- 2>&1)" = *"positive number"* ]]
[[ "$(plash nodepath "" 2>&1)" = *"positive number"* ]]
• test querying for the special root image
[[ "$(plash nodepath 0 2>&1)" = *"special root image"* ]]
[[ "$(plash nodepath 0 --allow-root-container 2>&1)" = *"/layer/0" ]]
plash nodepath 0 --allow-root-container
• check bad PLASH_DATA error
[[ "$(PLASH_DATA=/xxx plash nodepath 1 2>&1)" = *"/xxx: No such file or directory" ]]
• no argument gets help message
[[ "$(plash nodepath 2>&1)" = *"usage: "* ]]
Source Code
#define USAGE "usage: plash nodepath CONTAINER [--allow-root-container]\n"
#define _GNU_SOURCE
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <plash.h>
int nodepath_main(int argc, char *argv[]) {
int i = 0;
char *nodepath, *plash_data;
if (argc < 2) {
fputs(USAGE, stderr);
return EXIT_FAILURE;
}
// validate/normalize input
if (!argv[1][0] || strspn(argv[1], "0123456789") != strlen(argv[1]))
pl_fatal("image arg must be a positive number, got: %s", argv[1]);
if (0 == strcmp(argv[1], "0") &&
(argc <= 2 || 0 != strcmp(argv[2], "--allow-root-container"))) {
pl_fatal("image must not be the special root image ('0')");
}
plash_data = pl_call("data");
if (chdir(plash_data) == -1 || chdir("index") == -1)
pl_fatal("run `plash init`: chdir: %s", plash_data);
if (!(nodepath = realpath(argv[1], NULL))) {
errno = 0;
pl_fatal("no image: %s", argv[1]);
}
puts(nodepath);
return EXIT_SUCCESS;
}