summaryrefslogtreecommitdiffstats
path: root/packaging/macos/jhb/usr/bin/archive
blob: f84f0c807ff557e5dee3e17b0496f724b09daf6d (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env bash
#
# SPDX-FileCopyrightText: 2021 René de Hesselle <dehesselle@web.de>
#
# SPDX-License-Identifier: GPL-2.0-or-later

### description ################################################################

# This is a helper to create release archives and mount them.

### shellcheck #################################################################

# Nothing here.

### dependencies ###############################################################

#------------------------------------------------------ source jhb configuration

source "$(dirname "${BASH_SOURCE[0]}")"/../../etc/jhb.conf.sh

#------------------------------------------- source common functions from bash_d

# bash_d is already available (it's part of jhb configuration)

bash_d_include error

### variables ##################################################################

# Nothing here.

### functions ##################################################################

function create_tar
{
  local file=$ARTIFACT_DIR/$RELEASE_ARCHIVE

  echo_i "creating $file"

  tar -C "$WRK_DIR" -cp "$(basename "$VER_DIR")" |
    XZ_OPT=-T0 "$BIN_DIR"/xz > "$file"

  shasum -a 256 "$file" > "$file".sha256
  cat "$file".sha256
}

function create_dmg
{
  local file=$ARTIFACT_DIR/$RELEASE_ARCHIVE

  ( # create dmg
    local vol_name
    vol_name=$(basename "$VER_DIR")

    cd "$WRK_DIR" || exit 1
    hdiutil create -fs HFS+ -ov -format UDBZ \
      -srcfolder "$vol_name" \
      -volname "$vol_name" \
      "$file"
  )

  ( # create and print checksum
    cd "$(dirname "$file")" || exit 1
    shasum -a 256 "$(basename "$file")" > "$file".sha256
    cat "$file".sha256
  )
}

function mount_dmg
{
  local file=$REP_DIR/$RELEASE_ARCHIVE
  local mountpoint=$VER_DIR

  if [ ! -d "$mountpoint" ]; then
    mkdir -p "$mountpoint"
  fi

  echo_i "mounting $(basename "$file") may take some time"
  local device
  device=$(hdiutil attach -nomount "$file" | grep "^/dev/disk" |
    grep "Apple_HFS" | awk '{ print $1 }')
  echo_i "$file attached to $device"
  mount -o nobrowse,noowners,noquarantine,ro -t hfs "$device" "$mountpoint"
  echo_i "$device mounted to $mountpoint"
}

function unmount_dmg
{
  local mountpoint=$VER_DIR

  while : ; do   # unmount everything (in reverse order)
    local disk
    disk=$(mount | grep "$mountpoint" | tail -n1 | awk '{ print $1 }')
    disk=${disk%s[0-9]}  # cut off slice specification

    if [ ${#disk} -eq 0 ]; then
      break   # nothing to do here
    else
      # We loop over the 'eject' since it occasionally timeouts.
      while ! diskutil eject "$disk" > /dev/null; do
        echo_w "retrying to eject $disk in 5 seconds"
        sleep 5
      done

      echo_i "ejected $disk"
    fi
  done
}

function download_dmg
{
  if [ ! -d "$REP_DIR" ]; then
    mkdir -p "$REP_DIR"
  fi

  for url in "${RELEASE_URLS[@]}"; do
    local partial_download=$REP_DIR/${FUNCNAME[0]}  # TMP_DIR not available yet
    # download (at least) 100 kb of data
    curl -L "$url" 2>/dev/null | head -c 100000 > "$partial_download"
    # if we got 100 kb, it's not a "404 file not found"
    if [ "$(stat -f%z "$partial_download")" -eq 100000 ]; then
      echo_i "downloading $url"
      curl -o "$REP_DIR/$RELEASE_ARCHIVE" -L -C - "$url"
      break
    fi
  done

  rm "$partial_download"
}

function install_dmg
{
  local overlay_size=${1:-1}   # unit GiB, default 1

  local file=$REP_DIR/$RELEASE_ARCHIVE

  # download and mount read-only disk image
  if [ -f "$file" ]; then
    echo_i "using $file"
  else
    download_dmg
  fi
  mount_dmg

  # create writable overlay
  local device
  device=$(hdiutil attach -nomount ram://$((overlay_size * 1024 * 2048)) | xargs)
  newfs_hfs -v "$RELEASE_OVERLAY" "$device" >/dev/null
  echo_i "$overlay_size GiB ram attached to $device"
  mount -o nobrowse,rw,union -t hfs "$device" "$VER_DIR"
  echo_i "$device union-mounted at $VER_DIR"

  # Sadly, there are some limitations involved with union-mounting:
  #   - Files are not visible to macOS' versions of 'ls' or 'find'.
  #     (The GNU versions do work though.)
  #   - You cannot write in a location without having written to its
  #     parent location first. That's why we need to pre-create all directories
  #     below.
  #
  # (Shadow-mounting a dmg is not a feasible alternative due to its
  # bad write-performance.)

  # Create and run a script for mass-creating directories.
  echo_i "setting up directories in overlay"
  gfind "$VER_DIR" -mindepth 1 -type d \
    ! -path "$BLD_DIR/*" ! -path "$SRC_DIR/*" \
    -exec echo "mkdir {}" \; > "$VER_DIR"/create_dirs.sh
  chmod 755 "$VER_DIR"/create_dirs.sh
  "$VER_DIR"/create_dirs.sh
  rm "$VER_DIR"/create_dirs.sh
}

function uninstall_dmg
{
  unmount_dmg
}

function main
{
  local command=$1
  local option=$2

  case "$command" in
    create_dmg)
      create_dmg
      ;;
    create_tar)
      create_tar
      ;;
    install_dmg)
      install_dmg "$option"   # option: overlay size in GiB, defaul is 1
      ;;
    uninstall_dmg)
      uninstall_dmg
      ;;
    *)
      echo_e "usage: $0 {create_dmg|create_tar|install_dmg|uninstall_dmg}"
      ;;
  esac
}

### main #######################################################################

error_trace_enable

main "$@"