@@ -137,7 +137,7 @@ export class NavContextMenu {
137137 } ) ;
138138 }
139139
140- async download ( e ) {
140+ async download ( e ) {
141141 let download_target = "" ;
142142 let result ; // function-scoped so we can reference later
143143
@@ -158,34 +158,85 @@ export class NavContextMenu {
158158 this . nav_window_ref . stop_load ( ) ;
159159 }
160160 }
161- if ( result ?. [ "archive-path" ] ) {
162- const unitName = `nav-clean-on-open-${ Date . now ( ) } -${ Math . random ( ) . toString ( 36 ) . slice ( 2 , 8 ) } ` ;
163- const script = [
164- 'set -euo pipefail' ,
165- 'if ! command -v inotifywait >/dev/null 2>&1; then ' +
166- 'sleep 300; rm -f -- "$ARCHIVE"; ' +
167- '[ -n "${TEMPDIR:-}" ] && [[ "$TEMPDIR" == /tmp/navigator-* ]] && rm -rf -- "$TEMPDIR"; ' +
168- 'exit 0; fi' ,
169- 'inotifywait -q -t 1800 -e open -- "$ARCHIVE" || true' ,
170- 'rm -f -- "$ARCHIVE"' ,
171- 'sleep 3600' ,
172- '[ -n "${TEMPDIR:-}" ] && [[ "$TEMPDIR" == /tmp/navigator-* ]] && rm -rf -- "$TEMPDIR" || :'
173- ] . join ( ' && ' ) ;
174- const cmd = [
175- 'systemd-run' ,
176- '--property=CollectMode=inactive-or-failed' ,
177- '--property=RuntimeMaxSec=90000' ,
178- '--unit' , unitName ,
179- '--setenv=ARCHIVE=' + result [ 'archive-path' ] ,
180- ...( result [ 'temp-dir' ] ? [ '--setenv=TEMPDIR=' + result [ 'temp-dir' ] ] : [ ] ) ,
181- '/bin/bash' , '-lc' , script
182- ] ;
183-
184- await cockpit . spawn ( cmd , { superuser : 'require' , err : 'out' } ) . then ( out => console . log ( out ) ) ;
185- console . log ( "scheduled cleanup:" , unitName ) ;
186- console . log ( "Deleting :" , result [ 'archive-path' ] , "in 30 minutes or after download starts." ) ;
187- }
188-
161+ if ( result ?. [ "archive-path" ] ) {
162+ const unitName = `nav-clean-sweep-on-close-${ Date . now ( ) } -${ Math . random ( ) . toString ( 36 ) . slice ( 2 , 8 ) } ` ;
163+
164+ const script = `
165+ set -euo pipefail
166+
167+ ARCHIVE="$ARCHIVE"
168+ DIR="$(dirname -- "$ARCHIVE")"
169+ BASE="$(basename -- "$ARCHIVE")"
170+ ROOT="/tmp/navigator"
171+ PATTERN="navigator-download*"
172+
173+ if ! command -v inotifywait >/dev/null 2>&1; then
174+ sleep 300
175+ rm -f -- "$ARCHIVE" || true
176+ # Sweep everything matching PATTERN that's not in use
177+ find "$ROOT" -mindepth 1 -maxdepth 1 -name "$PATTERN" -print0 | \
178+ while IFS= read -r -d '' p; do
179+ if command -v lsof >/dev/null 2>&1 && lsof -t -- "$p" >/dev/null 2>&1; then
180+ continue
181+ fi
182+ rm -rf -- "$p"
183+ done
184+ [ -n "\${TEMPDIR:-}" ] && [[ "$TEMPDIR" == /tmp/navigator-* ]] && rm -rf -- "$TEMPDIR" || :
185+ exit 0
186+ fi
187+
188+ if ! timeout 1800 bash -lc '
189+ inotifywait -q -m -e open --format "%e %f" -- "$DIR" |
190+ while read ev f; do
191+ if [ "$f" = "$BASE" ]; then exit 0; fi
192+ done
193+ '; then
194+ find "$ROOT" -mindepth 1 -maxdepth 1 -name "$PATTERN" -print0 | \
195+ while IFS= read -r -d '' p; do
196+ if command -v lsof >/dev/null 2>&1 && lsof -t -- "$p" >/dev/null 2>&1; then
197+ continue
198+ fi
199+ rm -rf -- "$p"
200+ done
201+ exit 0
202+ fi
203+
204+ if command -v lsof >/dev/null 2>&1; then
205+ # Ensure no process holds ARCHIVE open
206+ sleep 1
207+ while lsof -t -- "$ARCHIVE" >/dev/null 2>&1; do sleep 1; done
208+ else
209+ timeout 86400 bash -lc '
210+ inotifywait -q -m -e close --format "%e %f" -- "$DIR" |
211+ while read ev f; do
212+ if [ "$f" = "$BASE" ]; then exit 0; fi
213+ done
214+ ' || true
215+ fi
216+
217+ rm -f -- "$ARCHIVE" || true
218+
219+ # (uncomment -mmin +5 to keep very fresh ones)
220+ find "$ROOT" -mindepth 1 -maxdepth 1 -name "$PATTERN" -print0 | \
221+ while IFS= read -r -d '' p; do
222+ if command -v lsof >/dev/null 2>&1 && lsof -t -- "$p" >/dev/null 2>&1; then
223+ continue
224+ fi
225+ rm -rf -- "$p"
226+ done
227+ [ -n "\${TEMPDIR:-}" ] && [[ "$TEMPDIR" == /tmp/navigator-* ]] && rm -rf -- "$TEMPDIR" || :
228+ ` ;
229+ const cmd = [
230+ 'systemd-run' ,
231+ '--property=CollectMode=inactive-or-failed' ,
232+ '--property=RuntimeMaxSec=90000' ,
233+ '--unit' , unitName ,
234+ '--setenv=ARCHIVE=' + result [ 'archive-path' ] ,
235+ ...( result [ 'temp-dir' ] ? [ '--setenv=TEMPDIR=' + result [ 'temp-dir' ] ] : [ ] ) ,
236+ '/bin/bash' , '-lc' , script
237+ ] ;
238+ await cockpit . spawn ( cmd , { superuser : 'require' , err : 'out' } ) ;
239+ }
189240 const downloader = new NavDownloader ( download_target ) ;
190241 downloader . download ( ) ;
191242 }
0 commit comments