Fork me on GitHub

Usage

plash mkdtemp

Description

Create a temporary directory in the plash data.

Tested behaviour

Preface

#!/bin/bash

    
  
    
      
set -eux

    
  
    
      
function has_tmpdir {
  tmproot="$(plash data realpath tmp)"
  [[ "$(ls $tmproot)" ]]
}  

    
  
    
      
function has_not_tmpdir {
  ! has_tmpdir
}  

    
  
    
      

`plash clean` cleans tmpdir only if calling proccess died

has_not_tmpdir
sh -c "plash mkdtemp" # from a child process
has_tmpdir
plash clean
has_not_tmpdir
plash mkdtemp
has_tmpdir
plash clean
has_tmpdir

    
  
    
      

Created tmpdir is in plash_data

tmpdir="$(plash mkdtemp)"
[[ "$tmpdir" = "$PLASH_DATA"* ]]

    
  
    
      

Created tmpdir is writable

tmpdir="$(plash mkdtemp)"
touch "$tmpdir/file"
mkdir "$tmpdir/dir"

    
  


Source Code

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <plash.h>

int main() {
  char *tmpdir, *tmpdir_templ;
  if (asprintf(&tmpdir_templ, "%s/tmp/plashtmp_%d_%d_XXXXXX", pl_call("data"),
               getsid(0), getppid()) == -1)
    pl_fatal("asprintf");
  tmpdir = mkdtemp(tmpdir_templ);
  if (tmpdir == NULL)
    pl_fatal("mkdtemp: %s", tmpdir_templ);
  puts(tmpdir);
}