summaryrefslogtreecommitdiff
path: root/src/store-child-permanently
blob: 463f776c62e73fadd152f35ba58740068b47c3d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/sh

. samizdat-paths.sh

error()
{
  printf 'Error: %s\n' "${*:-command failed}" >&2
  exit 1
}

warning()
{
  printf 'Warning: %s\n' "${*:-something went wrong}" >&2
}

store_tmpfs()
{
  local dir="$1"
  mountpoint -q "$dir"       || return 0
  [ ! -d "$dir".backup ]     || error "existing child backup found: '$dir.backup'.  Refusing to continue."
  cp -a "$dir" "$dir".backup || error "failed to make copy of child."
  umount "$dir"              || error "failed to unmount child tmpfs"
  rmdir "$dir"               || mv -Tb "$dir" "$dir".unlikely
  mv -T "$dir".backup "$dir" || error "failed to move copied child dir into place.  Child will not function!"
}

assert_root()
{
  [ "$(id -u)" = 0 ] || error "you are not root"
}

main()
{
  assert_root
  child=$1
  [ "$child" ] || usage
  if [ -d "$samizdat_child_dir"/child."$child" ]; then
    store_tmpfs "$samizdat_child_dir"/child."$child"
  else
    error "not a valid child: $child"
  fi
}

main "$@"