Usage
plash purge [ --yes ]
Description
Deletes all build data unatomically. Running containers that rely on the
build data will enter an undefined state. Plashs behaviour while this command
did not finish running returning an non zero exit code is undefined. The
data directory itself is not deleted as it could be a mountpoint.
  Tested Behaviour
  
    
      #!/bin/sh
    
  
    
      set -eux
    
  
    
      
    
  
    
      • Purge deletes plash data when confirmed
    
  
    
      export PLASH_DATA=$(mktemp -d)
    
  
    
      plash init
    
  
    
      echo 'y' | plash purge
    
  
    
      out=$(ls -A $PLASH_DATA)
    
  
    
      ! test -n "$out"
    
  
    
      
    
  
    
      
    
  
    
      • Purge does not deletes plash data when confirmed badly
    
  
    
      export PLASH_DATA=$(mktemp -d)
    
  
    
      plash init
    
  
    
      set +ex
    
  
    
      echo 'jap' | plash purge
    
  
    
      set -ex
    
  
    
      out=$(ls -A $PLASH_DATA)
    
  
    
      test -n "$out"
    
  
    
      
    
  
    
      • purge two times
    
  
    
      export PLASH_DATA=$(mktemp -d)
    
  
    
      plash init
    
  
    
      plash purge --yes
    
  
    
      plash purge --yes
    
  
    
      
    
  
    
      • call plash purge on empty plash data
    
  
    
      export PLASH_DATA=$(mktemp -d)
    
  
    
      plash purge --yes
    
  
    
      
    
  
    
      • check that purge removes all data in the PLASH_DATA directory
    
  
    
      export PLASH_DATA=$(mktemp -d)
    
  
    
      plash init
    
  
    
      out=$(ls -A $PLASH_DATA)
    
  
    
      test -n "$out"
    
  
    
      plash purge --yes
    
  
    
      out=$(ls -A $PLASH_DATA)
    
  
    
      test -z "$out"
    
  
    
      
    
  
    
      • not confirming does not delete the data
    
  
    
      export PLASH_DATA=$(mktemp -d)
    
  
    
      plash init
    
  
    
      set +ex
    
  
    
      echo 'no' | plash purge
    
  
    
      set -ex
    
  
    
      out=$(ls -A $PLASH_DATA)
    
  
    
      test -n "$out"
    
  
    
      
    
  
    
      • Bad confirmation leads to bad exit satus
    
  
    
      ! echo no | plash purge
    
  
Source Code
#define USAGE "usage: plash purge [ --yes ]\n"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <plash.h>
int confirm_via_input() {
  printf("Delete all build data? [y/N] ");
  char inp[sizeof("y\n")];
  fgets(inp, sizeof(inp), stdin);
  return (strcmp(inp, "y\n") == 0);
}
int is_confirmed_via_argv(char **argv) {
  return (argv[1] && (strcmp(argv[1], "--yes") == 0));
}
int purge_main(int argc, char *argv[]) {
  if (!(is_confirmed_via_argv(argv) || confirm_via_input())) {
    fputs("Action not confirmed\n", stderr);
    return EXIT_FAILURE;
  }
  pl_unshare_user();
  char *plash_data = pl_call("data");
  if (chdir(plash_data) == -1)
    pl_fatal("chdir %s", plash_data);
  // this one first
  pl_run("rm", "rm", "-rf", "id_counter", NULL);
  pl_run("rm", "-rf", "index");
  pl_run("rm", "-rf", "layer");
  pl_run("rm", "-rf", "map");
  pl_run("rm", "-rf", "mnt");
  pl_run("rm", "-rf", "tmp");
  fputs("All deleted.\n", stderr);
  return EXIT_SUCCESS;
}