darwin-shell 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,322 @@
1
+ #!/usr/bin/env bash
2
+ # darwin-shell env: deterministic parsing of an explicit .env file.
3
+ # No variable expansion, no multiline values, no external commands.
4
+
5
+ # Depend on core before setting the guard: a failed dependency must be
6
+ # retryable on the next source attempt.
7
+ if ! declare -F dsh::core::fail >/dev/null 2>&1; then
8
+ if declare -F dsh::use >/dev/null 2>&1; then
9
+ dsh::use core || return $?
10
+ else
11
+ printf 'dsh::env: core module is required; source bootstrap.sh first\n' >&2
12
+ return 2
13
+ fi
14
+ fi
15
+ if ! declare -F dsh::core::fail >/dev/null 2>&1; then
16
+ printf 'dsh::env: core module failed to load\n' >&2
17
+ return 2
18
+ fi
19
+
20
+ if [[ -n "${_dsh_private_env_sourced:-}" ]]; then
21
+ return 0
22
+ fi
23
+ _dsh_private_env_sourced=1
24
+ readonly _dsh_private_env_sourced
25
+
26
+ declare -gA _dsh_private_env_keys=()
27
+ declare -gA _dsh_private_env_values=()
28
+ declare -gA _dsh_private_env_denied=(
29
+ [BASH_ENV]=1
30
+ [ENV]=1
31
+ [SHELLOPTS]=1
32
+ [BASHOPTS]=1
33
+ [IFS]=1
34
+ [CDPATH]=1
35
+ [PROMPT_COMMAND]=1
36
+ [LD_PRELOAD]=1
37
+ )
38
+
39
+ # Results of the last value parse. Module-internal and process-local: do not
40
+ # call parse or load inside $(...), which would discard the parsed state.
41
+ _dsh_private_env_value=''
42
+ _dsh_private_env_error=''
43
+
44
+ _dsh_private_env_help() {
45
+ case "${1:-}" in
46
+ parse)
47
+ dsh::i18n::t 'Usage: dsh::env::parse <file>'
48
+ printf '\n'
49
+ dsh::i18n::t 'Parse KEY=VALUE from <file> into an internal map. No exports.'
50
+ dsh::i18n::t 'First duplicate wins. Key must match [A-Za-z_][A-Za-z0-9_]*.'
51
+ dsh::i18n::t 'No ${VAR} expansion and no multiline values.'
52
+ ;;
53
+ load)
54
+ dsh::i18n::t 'Usage: dsh::env::load <file> [--override]'
55
+ printf '\n'
56
+ dsh::i18n::t 'Parse <file> and export its keys. Existing variables are kept'
57
+ dsh::i18n::t 'unless --override is given. Under dry-run, print key names only.'
58
+ ;;
59
+ get)
60
+ dsh::i18n::t 'Usage: dsh::env::get <key> [default]'
61
+ printf '\n'
62
+ dsh::i18n::t 'Print the value of <key> from the environment, or <default>.'
63
+ ;;
64
+ has)
65
+ dsh::i18n::t 'Usage: dsh::env::has <key>'
66
+ printf '\n'
67
+ dsh::i18n::t 'Return 0 when <key> is set, even if its value is empty.'
68
+ ;;
69
+ require)
70
+ dsh::i18n::t 'Usage: dsh::env::require <key>...'
71
+ printf '\n'
72
+ dsh::i18n::t 'Return 0 when every key is set; otherwise fail with code 2.'
73
+ dsh::i18n::t 'Key names may be reported; values are never printed.'
74
+ ;;
75
+ *)
76
+ dsh::i18n::t 'dsh::env: no help for %1$s' "${1:-<empty>}" >&2
77
+ return "$DSH_RC_USAGE"
78
+ ;;
79
+ esac
80
+ }
81
+
82
+ _dsh_private_env_trim() {
83
+ local s="$1"
84
+ s="${s#"${s%%[![:space:]]*}"}"
85
+ s="${s%"${s##*[![:space:]]}"}"
86
+ printf '%s' "$s"
87
+ }
88
+
89
+ # Parse the text after '=' into _dsh_private_env_value. Quoted values keep
90
+ # inner whitespace; only \" and \\ are unescaped in double quotes. Every
91
+ # other backslash is literal because no interpolation is supported.
92
+ _dsh_private_env_parse_value() {
93
+ local raw="$1" line_no="$2" i=1 ch next rest tail value='' out='' prev=''
94
+ _dsh_private_env_value=''
95
+ _dsh_private_env_error=''
96
+
97
+ raw="${raw#"${raw%%[![:space:]]*}"}"
98
+ [[ -n "$raw" ]] || return 0
99
+
100
+ if [[ "${raw:0:1}" == "'" ]]; then
101
+ while (( i < ${#raw} )); do
102
+ ch="${raw:i:1}"
103
+ if [[ "$ch" == "'" ]]; then
104
+ value="${raw:1:i-1}"
105
+ rest="${raw:i+1}"
106
+ tail="${rest#"${rest%%[![:space:]]*}"}"
107
+ if [[ -n "$tail" && "$tail" != \#* ]]; then
108
+ _dsh_private_env_error="$(dsh::i18n::t -n 'line %1$s: unexpected text after quoted value' "$line_no")"
109
+ return "$DSH_RC_USAGE"
110
+ fi
111
+ _dsh_private_env_value="$value"
112
+ return 0
113
+ fi
114
+ ((++i))
115
+ done
116
+ _dsh_private_env_error="$(dsh::i18n::t -n 'line %1$s: unterminated single-quoted value' "$line_no")"
117
+ return "$DSH_RC_USAGE"
118
+ fi
119
+
120
+ if [[ "${raw:0:1}" == '"' ]]; then
121
+ while (( i < ${#raw} )); do
122
+ ch="${raw:i:1}"
123
+ if [[ "$ch" == '"' ]]; then
124
+ rest="${raw:i+1}"
125
+ tail="${rest#"${rest%%[![:space:]]*}"}"
126
+ if [[ -n "$tail" && "$tail" != \#* ]]; then
127
+ _dsh_private_env_error="$(dsh::i18n::t -n 'line %1$s: unexpected text after quoted value' "$line_no")"
128
+ return "$DSH_RC_USAGE"
129
+ fi
130
+ _dsh_private_env_value="$value"
131
+ return 0
132
+ fi
133
+ if [[ "$ch" == '\' ]]; then
134
+ if (( i + 1 >= ${#raw} )); then
135
+ _dsh_private_env_error="$(dsh::i18n::t -n 'line %1$s: trailing backslash in quoted value' "$line_no")"
136
+ return "$DSH_RC_USAGE"
137
+ fi
138
+ next="${raw:i+1:1}"
139
+ case "$next" in
140
+ '"') value+='"' ;;
141
+ '\') value+='\' ;;
142
+ *) value+="$ch$next" ;;
143
+ esac
144
+ ((i += 2))
145
+ continue
146
+ fi
147
+ value+="$ch"
148
+ ((++i))
149
+ done
150
+ _dsh_private_env_error="$(dsh::i18n::t -n 'line %1$s: unterminated double-quoted value' "$line_no")"
151
+ return "$DSH_RC_USAGE"
152
+ fi
153
+
154
+ # Unquoted values end at a comment introduced by whitespace + '#'.
155
+ i=0
156
+ while (( i < ${#raw} )); do
157
+ ch="${raw:i:1}"
158
+ if [[ "$ch" == '#' && ( -z "$prev" || "$prev" == [[:space:]] ) ]]; then
159
+ break
160
+ fi
161
+ out+="$ch"
162
+ prev="$ch"
163
+ ((++i))
164
+ done
165
+ out="${out%"${out##*[![:space:]]}"}"
166
+ _dsh_private_env_value="$out"
167
+ return 0
168
+ }
169
+
170
+ _dsh_private_env_check_key() {
171
+ local key="$1" location="$2"
172
+ if [[ ! "$key" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]; then
173
+ dsh::core::fail "$DSH_RC_USAGE" '%1$s: invalid key: %2$s' "$location" "$key"
174
+ return $?
175
+ fi
176
+ if [[ -n "${_dsh_private_env_denied[$key]+set}" ]]; then
177
+ dsh::core::fail "$DSH_RC_USAGE" '%1$s: key is not allowed: %2$s' "$location" "$key"
178
+ return $?
179
+ fi
180
+ return 0
181
+ }
182
+
183
+ # Public: parse an explicit file. The result lives in process-local maps and
184
+ # is replaced on every call; no environment variable is changed.
185
+ dsh::env::parse() {
186
+ if [[ "${1:-}" == "--help" ]]; then
187
+ _dsh_private_env_help parse
188
+ return $?
189
+ fi
190
+ local file="${1:-}"
191
+ if [[ -z "$file" ]]; then
192
+ dsh::core::fail "$DSH_RC_USAGE" 'env::parse: file required'
193
+ return $?
194
+ fi
195
+ if [[ ! -r "$file" ]]; then
196
+ dsh::core::fail "$DSH_RC_USAGE" 'env::parse: cannot read: %1$s' "$file"
197
+ return $?
198
+ fi
199
+
200
+ _dsh_private_env_keys=()
201
+ _dsh_private_env_values=()
202
+
203
+ local line='' line_no=0 trimmed key raw location
204
+ while IFS= read -r line || [[ -n "$line" ]]; do
205
+ ((++line_no))
206
+ line="${line%$'\r'}"
207
+ if (( line_no == 1 )); then
208
+ line="${line#$'\xEF\xBB\xBF'}"
209
+ fi
210
+ location="$file:$line_no"
211
+
212
+ trimmed="$(_dsh_private_env_trim "$line")"
213
+ [[ -z "$trimmed" || "$trimmed" == \#* ]] && continue
214
+
215
+ if [[ "$trimmed" == export[[:space:]]* ]]; then
216
+ trimmed="${trimmed#export}"
217
+ trimmed="$(_dsh_private_env_trim "$trimmed")"
218
+ fi
219
+ if [[ "$trimmed" != *=* ]]; then
220
+ dsh::core::fail "$DSH_RC_USAGE" '%1$s: expected KEY=VALUE' "$location"
221
+ return $?
222
+ fi
223
+
224
+ key="$(_dsh_private_env_trim "${trimmed%%=*}")"
225
+ _dsh_private_env_check_key "$key" "$location" || return $?
226
+
227
+ raw="${trimmed#*=}"
228
+ if ! _dsh_private_env_parse_value "$raw" "$line_no"; then
229
+ dsh::core::fail "$DSH_RC_USAGE" '%1$s: %2$s' "$location" "$_dsh_private_env_error"
230
+ return $?
231
+ fi
232
+
233
+ # First duplicate wins, matching the common dotenv precedence rule.
234
+ if [[ -n "${_dsh_private_env_values[$key]+set}" ]]; then
235
+ continue
236
+ fi
237
+ _dsh_private_env_keys[$key]=1
238
+ _dsh_private_env_values[$key]="$_dsh_private_env_value"
239
+ done < "$file"
240
+ return 0
241
+ }
242
+
243
+ # Public: parse and export. Under dry-run only key names are printed.
244
+ dsh::env::load() {
245
+ if [[ "${1:-}" == "--help" ]]; then
246
+ _dsh_private_env_help load
247
+ return $?
248
+ fi
249
+ local file="${1:-}" override=0 arg key
250
+ if [[ -z "$file" ]]; then
251
+ dsh::core::fail "$DSH_RC_USAGE" 'env::load: file required'
252
+ return $?
253
+ fi
254
+ shift
255
+ for arg in "$@"; do
256
+ case "$arg" in
257
+ --override) override=1 ;;
258
+ *) dsh::core::fail "$DSH_RC_USAGE" 'env::load: unknown option: %1$s' "$arg"; return $? ;;
259
+ esac
260
+ done
261
+
262
+ dsh::env::parse "$file" || return $?
263
+ for key in "${!_dsh_private_env_values[@]}"; do
264
+ if (( override == 0 )) && [[ -n "${!key+x}" ]]; then
265
+ continue
266
+ fi
267
+ if dsh::core::dry_run; then
268
+ printf '%s\n' "$key"
269
+ continue
270
+ fi
271
+ export "$key=${_dsh_private_env_values[$key]}"
272
+ done
273
+ return 0
274
+ }
275
+
276
+ # Public: read a key from the environment without touching any file.
277
+ dsh::env::get() {
278
+ if [[ "${1:-}" == "--help" ]]; then
279
+ _dsh_private_env_help get
280
+ return $?
281
+ fi
282
+ local key="${1:-}"
283
+ _dsh_private_env_check_key "$key" 'env::get' || return $?
284
+ if [[ -n "${!key+x}" ]]; then
285
+ printf '%s\n' "${!key}"
286
+ return 0
287
+ fi
288
+ printf '%s\n' "${2:-}"
289
+ return 0
290
+ }
291
+
292
+ # Public: distinguish an unset key from an explicitly empty one.
293
+ dsh::env::has() {
294
+ if [[ "${1:-}" == "--help" ]]; then
295
+ _dsh_private_env_help has
296
+ return $?
297
+ fi
298
+ local key="${1:-}"
299
+ _dsh_private_env_check_key "$key" 'env::has' || return $?
300
+ [[ -n "${!key+x}" ]]
301
+ }
302
+
303
+ # Public: fail fast when required keys are unset. Values are never printed.
304
+ dsh::env::require() {
305
+ if [[ "${1:-}" == "--help" ]]; then
306
+ _dsh_private_env_help require
307
+ return $?
308
+ fi
309
+ if (( $# == 0 )); then
310
+ dsh::core::fail "$DSH_RC_USAGE" 'env::require: at least one key required'
311
+ return $?
312
+ fi
313
+ local key
314
+ for key in "$@"; do
315
+ _dsh_private_env_check_key "$key" 'env::require' || return $?
316
+ if [[ -z "${!key+x}" ]]; then
317
+ dsh::core::fail "$DSH_RC_USAGE" 'env::require: missing %1$s' "$key"
318
+ return $?
319
+ fi
320
+ done
321
+ return 0
322
+ }
@@ -0,0 +1,303 @@
1
+ #!/usr/bin/env bash
2
+ # darwin-shell file: filesystem mechanics shared by scripts.
3
+ # Public functions print only the documented data (size, count, path, prune
4
+ # result) and return a status. No function exits, installs a trap or changes
5
+ # the caller's shell options. Temporary state lives next to the target so a
6
+ # rename stays on the same filesystem.
7
+
8
+ # Dependencies sit below this module. Load them before the guard so a failed
9
+ # dependency is retryable on the next source attempt.
10
+ if ! declare -F dsh::i18n::t >/dev/null 2>&1; then
11
+ if declare -F dsh::use >/dev/null 2>&1; then
12
+ dsh::use i18n || return $?
13
+ else
14
+ _dsh_private_file_self="${BASH_SOURCE[0]}"
15
+ source "${_dsh_private_file_self%/*}/i18n.sh" || return $?
16
+ unset _dsh_private_file_self
17
+ fi
18
+ fi
19
+ if ! declare -F dsh::core::fail >/dev/null 2>&1; then
20
+ if declare -F dsh::use >/dev/null 2>&1; then
21
+ dsh::use core || return $?
22
+ else
23
+ _dsh_private_file_self="${BASH_SOURCE[0]}"
24
+ source "${_dsh_private_file_self%/*}/core.sh" || return $?
25
+ unset _dsh_private_file_self
26
+ fi
27
+ fi
28
+
29
+ if [[ -n "${_dsh_private_file_sourced:-}" ]]; then
30
+ return 0
31
+ fi
32
+ _dsh_private_file_sourced=1
33
+ readonly _dsh_private_file_sourced
34
+
35
+ _dsh_private_file_help() {
36
+ case "${1:-}" in
37
+ size)
38
+ dsh::i18n::t 'Usage: dsh::file::size <file>'
39
+ printf '\n'
40
+ dsh::i18n::t 'Print the size of <file> in bytes. Returns 1 when missing.'
41
+ ;;
42
+ count)
43
+ dsh::i18n::t 'Usage: dsh::file::count <dir> [pattern]'
44
+ printf '\n'
45
+ dsh::i18n::t 'Count files under <dir>, optionally filtered by name pattern.'
46
+ ;;
47
+ is_empty)
48
+ dsh::i18n::t 'Usage: dsh::file::is_empty <dir>'
49
+ printf '\n'
50
+ dsh::i18n::t 'Return 0 when <dir> has no entries, 2 when unreadable.'
51
+ ;;
52
+ atomic_write)
53
+ dsh::i18n::t 'Usage: dsh::file::atomic_write <target>'
54
+ printf '\n'
55
+ dsh::i18n::t 'Write stdin to <target> via a same-directory temp file, then rename.'
56
+ ;;
57
+ temp_dir)
58
+ dsh::i18n::t 'Usage: dsh::file::temp_dir [prefix]'
59
+ printf '\n'
60
+ dsh::i18n::t 'Create a temp directory under DARWIN_SH_TMPDIR and print its path.'
61
+ ;;
62
+ prune_stale)
63
+ dsh::i18n::t 'Usage: dsh::file::prune_stale <dir> <days> [--name PATTERN] [--dry-run]'
64
+ printf '\n'
65
+ dsh::i18n::t 'Delete entries under <dir> older than <days>, optionally by name pattern.'
66
+ ;;
67
+ *)
68
+ dsh::i18n::t 'dsh::file: no help for %1$s' "${1:-<empty>}" >&2
69
+ return "$DSH_RC_USAGE"
70
+ ;;
71
+ esac
72
+ }
73
+
74
+ _dsh_private_file_readable_dir() {
75
+ local dir="$1"
76
+ [[ -d "$dir" && -r "$dir" && -x "$dir" ]]
77
+ }
78
+
79
+ # Public: print the size of <file> in bytes. Missing files are a status, not
80
+ # the string "N/A", so callers can branch without parsing output.
81
+ dsh::file::size() {
82
+ if [[ "${1:-}" == "--help" ]]; then
83
+ _dsh_private_file_help size
84
+ return $?
85
+ fi
86
+ if (( $# != 1 )); then
87
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::file::%1$s: missing argument' size
88
+ return $?
89
+ fi
90
+ local file="$1"
91
+ if [[ ! -e "$file" ]]; then
92
+ dsh::core::fail "$DSH_RC_FAILURE" 'dsh::file::size: not found: %1$s' "$file"
93
+ return $?
94
+ fi
95
+
96
+ local out=''
97
+ if dsh::core::have_cmd stat; then
98
+ # GNU and BSD stat disagree on the format flag; try both before falling
99
+ # back to wc, which is the portable but slower byte counter.
100
+ out="$(stat -c %s "$file" 2>/dev/null || true)"
101
+ if [[ ! "$out" =~ ^[0-9]+$ ]]; then
102
+ out="$(stat -f %z "$file" 2>/dev/null || true)"
103
+ fi
104
+ fi
105
+ if [[ ! "$out" =~ ^[0-9]+$ ]] && dsh::core::have_cmd wc; then
106
+ out="$(wc -c < "$file" 2>/dev/null || true)"
107
+ out="${out//[[:space:]]/}"
108
+ fi
109
+ if [[ ! "$out" =~ ^[0-9]+$ ]]; then
110
+ dsh::core::fail "$DSH_RC_FAILURE" 'dsh::file::size: cannot stat: %1$s' "$file"
111
+ return $?
112
+ fi
113
+ printf '%s\n' "$out"
114
+ }
115
+
116
+ # Public: count regular files under <dir>, optionally matching a name pattern.
117
+ # An unreadable directory is a usage error, never a misleading zero.
118
+ dsh::file::count() {
119
+ if [[ "${1:-}" == "--help" ]]; then
120
+ _dsh_private_file_help count
121
+ return $?
122
+ fi
123
+ if (( $# < 1 || $# > 2 )); then
124
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::file::%1$s: expected 1 or 2 arguments' count
125
+ return $?
126
+ fi
127
+ local dir="$1" pattern="${2:-}"
128
+ if ! _dsh_private_file_readable_dir "$dir"; then
129
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::file::%1$s: cannot read directory: %2$s' count "$dir"
130
+ return $?
131
+ fi
132
+
133
+ local -a find_args=("$dir" -mindepth 1 -type f)
134
+ [[ -n "$pattern" ]] && find_args+=(-name "$pattern")
135
+ local count=0 entry
136
+ # NUL-delimited reads keep unusual file names from being counted twice.
137
+ while IFS= read -r -d '' entry; do
138
+ count=$((count + 1))
139
+ done < <(find "${find_args[@]}" -print0 2>/dev/null)
140
+ printf '%s\n' "$count"
141
+ }
142
+
143
+ # Public: return 0 when <dir> has no entries, 2 when it cannot be read.
144
+ # The glob runs in a subshell so dotglob/nullglob never leak to the caller.
145
+ dsh::file::is_empty() {
146
+ if [[ "${1:-}" == "--help" ]]; then
147
+ _dsh_private_file_help is_empty
148
+ return $?
149
+ fi
150
+ if (( $# != 1 )); then
151
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::file::%1$s: missing argument' is_empty
152
+ return $?
153
+ fi
154
+ local dir="$1"
155
+ if ! _dsh_private_file_readable_dir "$dir"; then
156
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::file::%1$s: cannot read directory: %2$s' is_empty "$dir"
157
+ return $?
158
+ fi
159
+ (
160
+ shopt -s nullglob dotglob
161
+ local -a entries=("$dir"/*)
162
+ (( ${#entries[@]} == 0 ))
163
+ )
164
+ }
165
+
166
+ # Public: atomically replace <target> with stdin. The temp file is created in
167
+ # the target's own directory so mv cannot cross a filesystem boundary. On any
168
+ # failure the temp file is removed explicitly: installed libraries never set
169
+ # a trap on behalf of the calling script.
170
+ dsh::file::atomic_write() {
171
+ if [[ "${1:-}" == "--help" ]]; then
172
+ _dsh_private_file_help atomic_write
173
+ return $?
174
+ fi
175
+ if (( $# != 1 )) || [[ -z "${1:-}" ]]; then
176
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::file::%1$s: missing argument' atomic_write
177
+ return $?
178
+ fi
179
+ local target="$1" dir base tmp
180
+ base="${target##*/}"
181
+ if [[ -z "$base" ]]; then
182
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::file::atomic_write: invalid target: %1$s' "$target"
183
+ return $?
184
+ fi
185
+ dir="${target%/*}"
186
+ [[ "$dir" != "$target" ]] || dir='.'
187
+ if [[ ! -d "$dir" || ! -w "$dir" ]]; then
188
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::file::%1$s: cannot write directory: %2$s' atomic_write "$dir"
189
+ return $?
190
+ fi
191
+
192
+ tmp="$(mktemp "$dir/.${base}.tmp.XXXXXX" 2>/dev/null)" || {
193
+ dsh::core::fail "$DSH_RC_FAILURE" 'dsh::file::%1$s: cannot create temp file in: %2$s' atomic_write "$dir"
194
+ return $?
195
+ }
196
+ if ! cat > "$tmp"; then
197
+ rm -f -- "$tmp"
198
+ dsh::core::fail "$DSH_RC_FAILURE" 'dsh::file::%1$s: failed to write temp file for: %2$s' atomic_write "$target"
199
+ return $?
200
+ fi
201
+ if ! mv -f -- "$tmp" "$target"; then
202
+ rm -f -- "$tmp"
203
+ dsh::core::fail "$DSH_RC_FAILURE" 'dsh::file::%1$s: failed to replace: %2$s' atomic_write "$target"
204
+ return $?
205
+ fi
206
+ return 0
207
+ }
208
+
209
+ # Public: create a temp directory and print its path. DARWIN_SH_TMPDIR wins
210
+ # over TMPDIR; the prefix is restricted to a safe filename character set.
211
+ dsh::file::temp_dir() {
212
+ if [[ "${1:-}" == "--help" ]]; then
213
+ _dsh_private_file_help temp_dir
214
+ return $?
215
+ fi
216
+ if (( $# > 1 )); then
217
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::file::%1$s: too many arguments' temp_dir
218
+ return $?
219
+ fi
220
+ local prefix="${1:-darwin-shell}"
221
+ if [[ ! "$prefix" =~ ^[A-Za-z0-9_.-]+$ ]]; then
222
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::file::temp_dir: invalid prefix: %1$s' "$prefix"
223
+ return $?
224
+ fi
225
+ local root="${DARWIN_SH_TMPDIR:-${TMPDIR:-/tmp}}"
226
+ root="${root%/}"
227
+ [[ -n "$root" ]] || root='/'
228
+ local path
229
+ path="$(mktemp -d "$root/${prefix}.XXXXXX" 2>/dev/null)" || {
230
+ dsh::core::fail "$DSH_RC_FAILURE" 'dsh::file::temp_dir: cannot create under: %1$s' "$root"
231
+ return $?
232
+ }
233
+ printf '%s\n' "$path"
234
+ }
235
+
236
+ # Public: remove entries under <dir> older than <days>. Only mtime and an
237
+ # optional name pattern decide; lock ownership belongs to lock.sh. Dry-run
238
+ # prints the candidates instead of deleting, honouring DARWIN_SH_DRY_RUN too.
239
+ # Normal mode prints the number of removed entries.
240
+ dsh::file::prune_stale() {
241
+ if [[ "${1:-}" == "--help" ]]; then
242
+ _dsh_private_file_help prune_stale
243
+ return $?
244
+ fi
245
+ if (( $# < 2 )); then
246
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::file::prune_stale: expected <dir> <days> [options]'
247
+ return $?
248
+ fi
249
+ local dir="$1" days="$2" pattern='' dry=0
250
+ if [[ ! "$days" =~ ^[0-9]+$ ]]; then
251
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::file::prune_stale: days must be a non-negative integer: %1$s' "$days"
252
+ return $?
253
+ fi
254
+ shift 2
255
+ while (( $# )); do
256
+ case "$1" in
257
+ --name)
258
+ if (( $# < 2 )); then
259
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::file::prune_stale: --name requires a value'
260
+ return $?
261
+ fi
262
+ pattern="$2"
263
+ shift 2
264
+ ;;
265
+ --dry-run)
266
+ dry=1
267
+ shift
268
+ ;;
269
+ *)
270
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::file::prune_stale: unknown option: %1$s' "$1"
271
+ return $?
272
+ ;;
273
+ esac
274
+ done
275
+ if ! _dsh_private_file_readable_dir "$dir"; then
276
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::file::%1$s: cannot read directory: %2$s' prune_stale "$dir"
277
+ return $?
278
+ fi
279
+
280
+ local -a find_args=("$dir" -mindepth 1 -mtime "+$days")
281
+ [[ -n "$pattern" ]] && find_args+=(-name "$pattern")
282
+ find_args+=(-print0)
283
+ local -a entries=()
284
+ local entry
285
+ while IFS= read -r -d '' entry; do
286
+ entries+=("$entry")
287
+ done < <(find "${find_args[@]}" 2>/dev/null)
288
+
289
+ if (( dry )) || dsh::core::dry_run; then
290
+ for entry in "${entries[@]}"; do
291
+ printf '%s\n' "$entry"
292
+ done
293
+ return 0
294
+ fi
295
+
296
+ local removed=0
297
+ for entry in "${entries[@]}"; do
298
+ if rm -rf -- "$entry"; then
299
+ removed=$((removed + 1))
300
+ fi
301
+ done
302
+ printf '%s\n' "$removed"
303
+ }