add auto + better inuse checking

git-svn-id: file:///root/webif/svn/humax/pkg/src/webif/trunk@1066 2a923420-c742-0410-a762-8d5b09965624
This commit is contained in:
hummypkg 2012-06-15 21:34:07 +00:00
parent 101f5a7946
commit 9d8d630b07
18 changed files with 390 additions and 37 deletions

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -33,18 +33,17 @@ if {$order eq "-"} {
set model [system model]
set dustbin [system dustbin 1]
proc icon {img {hover ""} {extra ""}} {
puts -nonewline "<img src=$img class=va height=21 $extra"
proc icon {img {hover ""} {extra ""} {class "va"}} {
puts -nonewline "<img src=$img class=\"$class\" height=21 $extra"
if {$hover ne ""} {
puts -nonewline " alt=\"$hover\" title=\"$hover\""
}
puts ">"
}
proc directory {file bfile} {
proc directory {file bfile tbfile} {
global flatten
regsub -all " +" $bfile "" tbfile
puts "<div class=va>"
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 "
<a href=#>
<img class=\"dopt va\" border=0 width=45 "
puts -nonewline "autosqueeze=$autosqueeze "
puts -nonewline "autoshrink=$autoshrink "
puts -nonewline "autodedup=$autodedup "
puts -nonewline "autodecrypt=$autodecrypt "
if $flatten { puts -nonewline "noflat=$noflat " }
@ -101,11 +100,12 @@ proc directory {file bfile} {
proc entry {file} {{i 0}} {
set bfile [file tail $file]
regsub -all " +" $bfile "" tbfile
if {[string index $bfile 0] == "\025"} {
set bfile [string range $bfile 1 end]
}
if [file isdirectory "$file"] {
directory $file $bfile
directory $file $bfile $tbfile
continue
}
set ext [file extension $file]
@ -211,6 +211,10 @@ proc entry {file} {{i 0}} {
set dlna 1
}
# Shrunk (shown when necessary via Ajax)
icon "/img/compress.png" "Shrunk" \
"id=\"sp_$tbfile\"" "va hidden"
set bx [$ts get bookmarks]
}

View File

@ -14,10 +14,10 @@ puts {
}
if {$model eq "HDR"} {
puts { <li class="separator"><a href=#decrypt>Decrypt</a></li> }
puts { <li class="compress"><a href=#strip>Squeeze</a></li> }
puts { <li class="separator decrypt"><a href=#decrypt>Decrypt</a></li> }
puts { <li class="compress"><a href=#strip>Shrink</a></li> }
} else {
puts { <li class="compress separator"><a href=#strip>Squeeze</a></li> }
puts { <li class="compress separator"><a href=#strip>Shrink</a></li> }
}
if {[system pkginst ffmpeg]} {
puts {
@ -51,11 +51,12 @@ puts {
<li class=pwcopy><a href=#copy>Copy to clipboard</a></li>
<li class=separator><a href=#rename>Rename</a></li>
<li class=separator><a href=#resetnew>Reset new flag</a></li>
<li class="separator compress"><a href=#squeeze>Auto-Squeeze</a></li>
<li class="separator compress"><a href=#shrink>Auto-Shrink</a></li>
<li class=dedup><a href=#dedup>Auto-Dedup</a></li>
<li class=decrypt><a href=#decrypt>Auto-Decrypt</a></li>
}
if {$model eq "HDR"} {
puts { <li class=decrypt><a href=#decrypt>Auto-Decrypt</a></li> }
}
if $flatten {
puts { <li class="separator"><a href=#flat>No-Flatten</a></li> }
}

View File

@ -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);

View File

@ -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]

View File

@ -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]

View File

@ -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..."
}

View File

@ -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 "}"

View File

@ -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"

View File

@ -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"

212
var/mongoose/lib/bin/auto Executable file
View File

@ -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

40
var/mongoose/lib/lock Normal file
View File

@ -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
}

View File

@ -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 }

View File

@ -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