From 9d8d630b0771b645b847031bbabe885451fc8221 Mon Sep 17 00:00:00 2001 From: hummypkg Date: Fri, 15 Jun 2012 21:34:07 +0000 Subject: [PATCH] add auto + better inuse checking git-svn-id: file:///root/webif/svn/humax/pkg/src/webif/trunk@1066 2a923420-c742-0410-a762-8d5b09965624 --- CONTROL/control | 4 +- CONTROL/postinst | 12 + CONTROL/prerm | 7 + var/mongoose/cgi-bin/browse.jim | 24 +- var/mongoose/cgi-bin/browse/assets.jim | 13 +- var/mongoose/cgi-bin/browse/browse.js | 38 +++- var/mongoose/cgi-bin/browse/crop/execute.jim | 8 +- .../cgi-bin/browse/decrypt/execute.jim | 7 +- var/mongoose/cgi-bin/browse/ffmpeg.jim | 2 +- var/mongoose/cgi-bin/browse/shrunk.jim | 33 +++ var/mongoose/cgi-bin/opkg.jim | 2 +- var/mongoose/html/dedup/normalise.jim | 1 + var/mongoose/lib/bin/auto | 212 ++++++++++++++++++ var/mongoose/lib/{ => bin}/ffmpeg | 0 var/mongoose/lib/{ => bin}/opkg | 0 var/mongoose/lib/lock | 40 ++++ var/mongoose/lib/system.class | 20 +- var/mongoose/lib/ts.class | 4 +- 18 files changed, 390 insertions(+), 37 deletions(-) create mode 100755 var/mongoose/cgi-bin/browse/shrunk.jim create mode 100755 var/mongoose/lib/bin/auto rename var/mongoose/lib/{ => bin}/ffmpeg (100%) rename var/mongoose/lib/{ => bin}/opkg (100%) create mode 100644 var/mongoose/lib/lock diff --git a/CONTROL/control b/CONTROL/control index d56d619..66a77b2 100644 --- a/CONTROL/control +++ b/CONTROL/control @@ -1,9 +1,9 @@ Package: webif Priority: optional Section: web -Version: 0.9.11-2 +Version: 0.9.12 Architecture: mipsel Maintainer: af123@hummypkg.org.uk -Depends: webif-channelicons(>=1.0.1),mongoose(>=3.0-7),jim(>=0.73-1),jim-oo,jim-sqlite3(>=0.73),jim-cgi(>=0.5),service-control(>=1.2),busybox(>=1.19.3-1),lsof,epg(>=1.0.9),hmt(>=1.1.6),ssmtp,anacron,trm,openssl-command,nicesplice,id3v2,file,rsvsync(>=1.0.2),webif-charts(>=1.2),stripts(>=1.0.2) +Depends: webif-channelicons(>=1.0.1),mongoose(>=3.0-7),jim(>=0.73-1),jim-oo,jim-sqlite3(>=0.73),jim-cgi(>=0.5),service-control(>=1.2),busybox(>=1.19.3-1),lsof,epg(>=1.0.9),hmt(>=1.1.6),ssmtp,anacron,trm,openssl-command,nicesplice,id3v2,file,rsvsync(>=1.0.2),webif-charts(>=1.2),stripts(>=1.0.3) Suggests: ffmpeg,webif-iphone Description: An evolving web interface for the Humax. diff --git a/CONTROL/postinst b/CONTROL/postinst index 25b2ead..33a7b5e 100755 --- a/CONTROL/postinst +++ b/CONTROL/postinst @@ -2,6 +2,18 @@ export tmpf=/tmp/cronf.$$ +# Add cron jobs + +crond=$PKG_ROOT/var/spool/cron/crontabs +[ -d $crond ] || exit 1 + +cronf=$crond/root +grep -v webif/lib/bin/auto $cronf > $tmpf +( + cat $tmpf + echo '*/10 * * * * /mod/webif/lib/bin/auto >> /tmp/webif_auto.log 2>&1' +) > $cronf + # Add anacron jobs ana=$PKG_ROOT/etc/anacrontab diff --git a/CONTROL/prerm b/CONTROL/prerm index 924be78..29bc89e 100755 --- a/CONTROL/prerm +++ b/CONTROL/prerm @@ -2,6 +2,13 @@ export tmpf=/tmp/cronf.$$ +cronf=$PKG_ROOT/var/spool/cron/crontabs/root +if [ -f $cronf ]; then + grep -v webif/lib/bin/auto $cronf > $tmpf + cp $tmpf $cronf + [ -s $cronf ] || rm -f $cronf +fi + ana=/mod/etc/anacrontab grep -v 'backup/backup.jim' $ana > $tmpf cp $tmpf $ana diff --git a/var/mongoose/cgi-bin/browse.jim b/var/mongoose/cgi-bin/browse.jim index ab4e555..db2b56e 100755 --- a/var/mongoose/cgi-bin/browse.jim +++ b/var/mongoose/cgi-bin/browse.jim @@ -33,18 +33,17 @@ if {$order eq "-"} { set model [system model] set dustbin [system dustbin 1] -proc icon {img {hover ""} {extra ""}} { - puts -nonewline "\"$hover\"" } -proc directory {file bfile} { +proc directory {file bfile tbfile} { global flatten - regsub -all " +" $bfile "" tbfile puts "
" set img "/images/711_1_09_Media_Folder.png" if {$bfile eq $::dustbin} { set img "/img/Dustbin_Folder.png" } @@ -63,10 +62,10 @@ proc directory {file bfile} { if $noflat { icon "/img/flat-tyre.png" "No-flatten" } } - set autosqueeze 0 - if {[file exists "$file/.autosqueeze"]} { - set autosqueeze 1 - icon "/img/compress.png" "Auto-squeeze" + set autoshrink 0 + if {[file exists "$file/.autoshrink"]} { + set autoshrink 1 + icon "/img/compress.png" "Auto-shrink" } set autodedup 0 @@ -84,7 +83,7 @@ proc directory {file bfile} { puts -nonewline " Decrypt } - puts {
  • Squeeze
  • } + puts {
  • Decrypt
  • } + puts {
  • Shrink
  • } } else { - puts {
  • Squeeze
  • } + puts {
  • Shrink
  • } } if {[system pkginst ffmpeg]} { puts { @@ -51,11 +51,12 @@ puts {
  • Copy to clipboard
  • Rename
  • Reset new flag
  • -
  • Auto-Squeeze
  • +
  • Auto-Shrink
  • Auto-Dedup
  • -
  • Auto-Decrypt
  • } - +if {$model eq "HDR"} { + puts {
  • Auto-Decrypt
  • } +} if $flatten { puts {
  • No-Flatten
  • } } diff --git a/var/mongoose/cgi-bin/browse/browse.js b/var/mongoose/cgi-bin/browse/browse.js index c5e566f..6f8822b 100755 --- a/var/mongoose/cgi-bin/browse/browse.js +++ b/var/mongoose/cgi-bin/browse/browse.js @@ -112,6 +112,24 @@ function new_folder_callback(data, status, xhr) $.each(data, set_folder_new); } +function insert_shrunk(file, perc) +{ + if (perc == 0) + { + file = file.replace(/[ ]/g, ''); + file = file.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1'); + //console.log("File: (%s) = (%s)", file, perc); + $('#sp_' + file).show(); + } +} + +function shrunk_callback(data, status, xhr) +{ + //console.log("Status: %s", status); + //console.dir(data); + $.each(data, insert_shrunk); +} + function delete_callback(file, dir, id) { var el = 'div.bf#' + id; @@ -285,14 +303,14 @@ function preparedmenu(el, menu) $(menu).changeContextMenuItem('#flat', 'Prevent Flatten'); } - if (el.attr('autosqueeze') != undefined) + if (el.attr('autoshrink') != undefined) { - if (el.attr('autosqueeze') > 0) - $(menu).changeContextMenuItem('#squeeze', - 'Disable Auto-squeeze'); + if (el.attr('autoshrink') > 0) + $(menu).changeContextMenuItem('#shrink', + 'Disable Auto-shrink'); else - $(menu).changeContextMenuItem('#squeeze', - 'Enable Auto-squeeze'); + $(menu).changeContextMenuItem('#shrink', + 'Enable Auto-shrink'); } if (el.attr('autodedup') != undefined) { @@ -477,9 +495,9 @@ var dmenuclick = function(action, el, pos) $.get(url, function() { window.location.reload(true); }); break; - case 'squeeze': + case 'shrink': var url = '/cgi-bin/browse/flagdir.jim?dir=' + file + - '&flag=autosqueeze'; + '&flag=autoshrink'; $.get(url, function() { window.location.reload(true); }); break; @@ -637,6 +655,10 @@ var dmenuclick = function(action, el, pos) $.getJSON('/cgi-bin/browse/sizes.jim?dir=' + encodeURIComponent(dir), folder_size_callback); + // Flag shrunk recordings + $.getJSON('/cgi-bin/browse/shrunk.jim?dir=' + encodeURIComponent(dir), + shrunk_callback); + // Flag folders with unwatched items $.getJSON('/cgi-bin/browse/newdir.jim?dir=' + encodeURIComponent(dir), new_folder_callback); diff --git a/var/mongoose/cgi-bin/browse/crop/execute.jim b/var/mongoose/cgi-bin/browse/crop/execute.jim index 40465d1..beec903 100755 --- a/var/mongoose/cgi-bin/browse/crop/execute.jim +++ b/var/mongoose/cgi-bin/browse/crop/execute.jim @@ -2,7 +2,7 @@ package require cgi source /mod/webif/lib/setup -require ts.class pretty_size +require ts.class pretty_size system.class puts "Content-Type: text/html\r\n\r\n" @@ -10,6 +10,12 @@ cgi_input #cgi_dump set rfile [cgi_get file] + +if {[system inuse $rfile]} { + puts "This file is in use. Cannot process at the moment." + exit +} + set ts [ts fetch $rfile] set dir [file dirname $rfile] diff --git a/var/mongoose/cgi-bin/browse/decrypt/execute.jim b/var/mongoose/cgi-bin/browse/decrypt/execute.jim index 489fe24..652af5e 100755 --- a/var/mongoose/cgi-bin/browse/decrypt/execute.jim +++ b/var/mongoose/cgi-bin/browse/decrypt/execute.jim @@ -3,7 +3,7 @@ package require sqlite3 package require cgi source /mod/webif/lib/setup -require ts.class +require ts.class system.class puts "Content-Type: text/html\r\n\r\n" @@ -22,6 +22,11 @@ if {$url eq ""} { exit } +if {[system inuse $rfile]} { + puts "This file is in use. Cannot decrypt at the moment." + exit +} + set xstart [clock milliseconds] set base [file rootname $rfile] diff --git a/var/mongoose/cgi-bin/browse/ffmpeg.jim b/var/mongoose/cgi-bin/browse/ffmpeg.jim index a3ff1cd..b7029b8 100755 --- a/var/mongoose/cgi-bin/browse/ffmpeg.jim +++ b/var/mongoose/cgi-bin/browse/ffmpeg.jim @@ -10,7 +10,7 @@ cgi_input if [file exists /mod/bin/ffmpeg] { set file [dict get $_cgi file] - puts [exec /mod/webif/lib/ffmpeg -i $file] + puts [exec /mod/webif/lib/bin/ffmpeg -i $file] } else { puts "Install ffmpeg package for more information..." } diff --git a/var/mongoose/cgi-bin/browse/shrunk.jim b/var/mongoose/cgi-bin/browse/shrunk.jim new file mode 100755 index 0000000..bc92b49 --- /dev/null +++ b/var/mongoose/cgi-bin/browse/shrunk.jim @@ -0,0 +1,33 @@ +#!/mod/bin/jimsh + +package require cgi + +#puts "Content-Type: text/plain" +puts "Content-Type: application/json" +puts "" + +cgi_input +#cgi_dump + +set dir [cgi_get dir "/media/My Video/Children"] + +puts "{" +regsub -all {([\\["$])} $dir {\\\1} xdir +foreach file [readdir -nocomplain $dir] { + if {![string match {*.ts} $file]} { continue } + + regsub -all {([\\["$])} [file rootname $file] {\\\1} xfile + if {[catch {set perc [exec /mod/bin/stripts -Aq "$xdir/$xfile"]}]} { + continue + } + if {[string match {*%} $perc]} { + set perc [string range $perc 0 end-1] + } else { + set perc 0 + } + + puts " \"$file\": $perc," +} +puts " \"dummy\": 0" +puts "}" + diff --git a/var/mongoose/cgi-bin/opkg.jim b/var/mongoose/cgi-bin/opkg.jim index 6d32da6..7fb713b 100755 --- a/var/mongoose/cgi-bin/opkg.jim +++ b/var/mongoose/cgi-bin/opkg.jim @@ -11,7 +11,7 @@ set cmd [cgi_get cmd update] proc opkg {cmd} { chunk ">>> opkg $cmd\r\n" - set bcmd "|/mod/webif/lib/opkg $cmd" + set bcmd "|/mod/webif/lib/bin/opkg $cmd" set fd [open $bcmd r] while {[gets $fd line] >= 0} { chunk "$line\r\n" diff --git a/var/mongoose/html/dedup/normalise.jim b/var/mongoose/html/dedup/normalise.jim index a40d553..1a786e9 100755 --- a/var/mongoose/html/dedup/normalise.jim +++ b/var/mongoose/html/dedup/normalise.jim @@ -66,6 +66,7 @@ proc dedupnormalise {title {reserve ""}} { set ntitle "$seriesmap($title): $title" set title $ntitle } else { + # Try lower case without spaces regsub -all -- {[[:space:]]+} [string tolower $title] "" ntitle if {[dict exists $seriesmap $ntitle]} { set title "$seriesmap($ntitle): $title" diff --git a/var/mongoose/lib/bin/auto b/var/mongoose/lib/bin/auto new file mode 100755 index 0000000..2824b57 --- /dev/null +++ b/var/mongoose/lib/bin/auto @@ -0,0 +1,212 @@ +#!/mod/bin/jimsh + +source /mod/webif/lib/setup +require lock system.class ts.class tdelete + +if {![acquire_lock webif_auto]} { + puts "Cannot acquire exclusive lock, terminating." + exit +} +puts "Got lock." + +set tmp "/mod/tmp/webif_auto" +if {![file exists $tmp]} { + if {[catch {file mkdir $tmp} msg]} { + puts "Cannot create temporary directory - $tmp ($msg)" + exit + } +} elseif {![file isdirectory $tmp]} { + puts "Cannot create temporary directory - $tmp (file exists)" + exit +} + +# Clean-up the temporary directory +foreach file [readdir -nocomplain $tmp] { file delete -force "$tmp/$file" } + +if {[system pkginst undelete]} { + set dustbin "[system dustbin]" +} else { + set dustbin "" +} + +proc bindir {file binroot} { + set dir [file dirname $file] + regsub "^[system mediaroot]" $dir $binroot ndir + if {$dir eq $ndir} { set ndir $binroot } + system mkdir_p $ndir + return $ndir +} + +proc escape {str} { + regsub -all {([\\["$])} $str {\\\1} str + return $str +} + +proc dedup {dir} { + puts "DEDUP: \[$dir]" + puts [exec /mod/webif/html/dedup/dedup -yes [escape $dir]] + exec /mod/webif/html/dedup/dedup -yes [escape $dir] +} + +proc do_shrink {ts} { + global tmp dustbin + + set file [file rootname [$ts get file]] + puts " SHRINK: $file" + if {[catch { + set perc [exec /mod/bin/stripts -aq [escape $file]] + } msg]} { + puts " Error: $msg" + return + } + if {[string match {*%} $perc]} { + set perc [string range $perc 0 end-1] + } else { + set perc 0 + } + + if {$perc == 0} { + puts " Already shrunk." + return + } + puts " Estimate $perc% saving." + puts " Shrinking..." + if {[catch { + puts [exec /mod/bin/stripts -q [escape $file] $tmp/shrunk] + } msg]} { + puts "Error during shrink: $msg" + return + } + + # The following steps are structured to minimise the risk of + # things being left in an inconsistent state if the system goes + # into standby. Renames within the same filesystem are very + # quick so the risk is small, but even so... + + # Move the shrunken version back to the local directory. + foreach f [glob "$tmp/shrunk.*"] { + set ext [file extension $f] + file rename $f "${file}_shrunk${ext}" + } + + # Move the old recording to the bin if undelete is installed. + if {$dustbin ne ""} { + $ts move [bindir $file "$dustbin/webif_autoshrink"] 1 1 + } else { + # Delete otherwise. + if {[$ts delete]} { + puts "Successfully deleted $file." + } else { + puts "Problem deleting $file, [$ts get error]" + return + } + } + + # Finally, rename the shrunken recording again. + foreach f [glob "${file}_shrunk.*"] { + file rename $f "${file}[file extension $f]" + } +} + +proc do_decrypt {ts} { + global tmp dustbin + + set file [$ts get file] + set rfile [file rootname $file] + set bfile [file tail $file] + puts " DECRYPT: $rfile" + + if {![$ts flag "ODEncrypted"]} { + puts " Already decrypted." + return + } + + lassign [$ts dlnaloc] url + if {$url eq ""} { + puts " Not yet indexed." + return + } + + puts " DLNA: $url" + exec wget -O "$tmp/$bfile" $url + + # Move the encrypted file out of the way. + file rename $file "$rfile.encrypted" + # Move the decrypted copy into place. + file rename "$tmp/$bfile" $file + # Patch the HMT - quickest way to get back to a playable file. + exec /mod/bin/hmt -encrypted [escape "$rfile.hmt"] + + puts " Removing/binning old copy." + # Move the old recording to the bin if undelete is installed. + set bin [bindir $file "$dustbin/webif_autodecrypt"] + if {$dustbin ne ""} { + set tail [file tail $rfile] + file rename "$rfile.encrypted" "$bin/$tail.ts" + foreach ext {nts hmt thm} { + if {[file exists "$rfile.$ext"]} { + file copy $rfile.$ext "$bin/$tail.$ext" + if {$ext eq "hmt"} { + # Patch the binned HMT back + exec /mod/bin/hmt +encrypted \ + [escape "$bin/$tail.hmt"] + } + } + } + } else { + file delete "$rfile.encrypted" + } + puts " Done." +} + +proc entries {dir callback} { + foreach entry [readdir -nocomplain $dir] { + if {![string match {*.ts} $entry} continue + if {[catch {set ts [ts fetch "$dir/$entry"]}]} continue + if {$ts == 0} continue + if {[system inuse [file rootname "$dir/$entry"]]} { + puts "$entry - in use\n" + continue + } + $callback $ts + } +} + +proc shrink {dir} { + puts "SHRINK: \[$dir]" + entries $dir do_shrink +} + +proc decrypt {dir} { + puts "DECRYPT: \[$dir]" + entries $dir do_decrypt +} + +proc scan {dir attr} {{indent 0}} { + incr indent 2 + + #puts "[string repeat " " $indent]\[$dir]" + + if {[file exists "$dir/.auto$attr"]} { $attr $dir } + + foreach entry [readdir -nocomplain $dir] { + if {[file isdirectory "$dir/$entry"]} { + scan "$dir/$entry" $attr + } + } + + incr indent -2 +} + +set root [system mediaroot] + +if {[llength $argv] > 0} { + foreach arg $argv { scan $root $arg } +} else { + foreach arg {dedup decrypt shrink} { + scan $root $arg + } +} + +release_lock webif_auto + diff --git a/var/mongoose/lib/ffmpeg b/var/mongoose/lib/bin/ffmpeg similarity index 100% rename from var/mongoose/lib/ffmpeg rename to var/mongoose/lib/bin/ffmpeg diff --git a/var/mongoose/lib/opkg b/var/mongoose/lib/bin/opkg similarity index 100% rename from var/mongoose/lib/opkg rename to var/mongoose/lib/bin/opkg diff --git a/var/mongoose/lib/lock b/var/mongoose/lib/lock new file mode 100644 index 0000000..efc281f --- /dev/null +++ b/var/mongoose/lib/lock @@ -0,0 +1,40 @@ +#!/mod/bin/jimsh + +proc _lock_to_port {id} { + set len [string length $id] + set hash $len + + for {set i 0} {$i < $len} {incr i} { + scan [string index $id $i] "%c" asc + set hash [expr (($hash<<5)^($hash>>27))^$asc]; + } + + return [expr (65535 - ($hash % 101))] +} + +proc _lock_dummy {newsock addr port} { } + +proc acquire_lock {id} { + global _locks + + set port [_lock_to_port $id] + + if {[catch {set socket [socket stream.server "127.0.0.1:$port"]} msg]} { + return 0 + } + + set _locks($id) $socket + return 1 +} + +proc release_lock {id} { + global _locks + + if {[catch {close $_locks($id)} msg]} { + puts "Error releasing lock: $msg" + return 0 + } + unset _locks($id) + return 1 +} + diff --git a/var/mongoose/lib/system.class b/var/mongoose/lib/system.class index 4ba7e66..686732f 100644 --- a/var/mongoose/lib/system.class +++ b/var/mongoose/lib/system.class @@ -21,6 +21,16 @@ proc {system hostname} {} { return $hostname } +proc {system ip} {} { + if {[catch {set fp [open /etc/hosts r]}]} { + set ip "127.0.0.1" + } else { + set ipl [lindex [split [$fp read] "\n"] 1] + regsub -- {[[:space:]].*} $ipl "" ip + } + return $ip +} + proc {system modversion} {{short 0}} { if {[catch {set fp [open /etc/modversion r]}]} { set modver "102" @@ -112,13 +122,13 @@ proc {system busy} {} { } proc {system inuse} {file} { - # Is humaxtv using the file? - if {[catch {set pid [exec /mod/bin/busybox/pgrep humaxtv]}]} { - return 0 - } + # Is anything using the file (used to only check the Humax binary)? +# if {[catch {set pid [exec /mod/bin/busybox/pgrep humaxtv]}]} { +# return 0 +# } regsub -all {([\\["$])} [file rootname [file tail $file]] {\\\1} file set c 0 - foreach line [split [exec /mod/bin/lsof -p $pid] "\n"] { + foreach line [split [exec /mod/bin/lsof] "\n"] { if {[string match "*$file*" $line]} { incr c } } if {$c > 0} { return 1 } diff --git a/var/mongoose/lib/ts.class b/var/mongoose/lib/ts.class index 4a92c87..92fcbd5 100644 --- a/var/mongoose/lib/ts.class +++ b/var/mongoose/lib/ts.class @@ -1,7 +1,7 @@ if {![exists -proc class]} { package require oo } source /mod/webif/lib/setup -require tdelete +require tdelete system.class set dmsfile /mnt/hd2/dms_cds.db @@ -179,7 +179,7 @@ ts method dlnaloc {} { set rec [lindex $muri 0] set xuri [lindex $rec 3] set mime [lindex $rec 1] - set url "http://$::env(HTTP_HOST):9000/web/$xuri" + set url "http://[system ip]:9000/web/$xuri" } $db close