summaryrefslogtreecommitdiff
path: root/hel/recv-media.nix
blob: e0306b9aa08e35b74524ba0f20903260f63895d2 (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
{ stdenv, coreutils, writeScriptBin, eject }:

writeScriptBin "recv-media" ''
  #!${stdenv.shell}

  pid=$?

  exec 1> >(${eject}/bin/logger --id=$pid -t recv-media -p user.notice)
  exec 2> >(${eject}/bin/logger --id=$pid -t recv-media -p user.error)

  [[ -z "$1" || -z "$2" ]] && exit 2

  PATH=${coreutils}/bin

  dir=/var/media
  tmpFile="''${dir}"/.tmp/$(basename "$1")
  target="''${dir}"/$(basename "$1")

  if ! cp -lnv --preserve=all "$1" "''${target}"; then
    mkdir -pv $(dirname "''${tmpFile}")

    ${eject}/bin/ionice -c 3 -t cp -vn --preserve=all "$1" "''${tmpFile}" && mv -v "''${tmpFile}" "''${target}"
    touch -c -m -t "$2" "''${target}"
  fi
''