webif/var/mongoose/lib/system.class
hummypkg 9fb84443cd add rmdir_if_empty
git-svn-id: file:///root/webif/svn/humax/pkg/src/webif/trunk@1654 2a923420-c742-0410-a762-8d5b09965624
2013-09-05 18:52:13 +00:00

272 lines
5.7 KiB
Plaintext

if {![exists -proc class]} { package require oo }
if {![exists -proc sqlite3.open]} { package require sqlite3 }
class system {}
proc {system model} {} {
if {[catch {set fp [open /etc/model r]}]} {
set model {HD[R]}
} else {
set model [string trim [read $fp]]
close $fp
}
return $model
}
proc {system hostname} {} {
if {[catch {set hostname [string trim [exec hostname]]}]} {
set hostname "humax"
}
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"
} else {
set modver [string trim [read $fp]]
close $fp
}
if {$short} { return $modver }
lassign [split $modver ""] a b c
return [format "%d.%d%d" $a $b $c]
}
proc {system modbuild} {} {
if {[catch {set fp [open /etc/modbuild r]}]} {
return 0
} else {
set modbuild [string trim [read $fp]]
close $fp
}
return $modbuild
}
proc {system fhtcpversion} {} {
set file "/etc/fhtcpversion"
if {![file exists $file]} { set file "/root/fhtcpversion" }
if {[catch {set fp [open $file r]}]} {
set ver "?.??.??"
} else {
set ver [string trim [read $fp]]
close $fp
}
return $ver
}
proc {system pkgver} {{pkg webif}} {
return [lrange [split [exec opkg list-installed $pkg] " "] 2 end]
}
proc {system pkginst} {pkg} {{cache {}} {ncache {}}} {
if {$pkg in $cache} { return 1 }
if {$pkg in $ncache} { return 0 }
# It may not be possible to get an opkg lock immediately so
# try several times and throw an error if not.
set status unknown
loop i 0 5 {
if {[catch {set status [exec opkg list-installed $pkg]}]} {
sleep 1
} else break
}
if {$status eq "unknown"} {
error "Could not get opkg lock after 5 seconds."
}
if {$status ne ""} {
lappend $cache $pkg
return 1
}
lappend $ncache $pkg
return 0
}
proc {system mediaroot} {} {
switch [system model] {
HDR { return "/media/My Video" }
HD { return "/media/drive1/Video" }
}
return ""
}
proc {system dustbin} {{short 0}} {
set dustbin "\[Deleted Items\]"
if {![catch {set fd [open "/mod/boot/dustbin.name" r]}]} {
set dustbin [lindex [split [read $fd] "\n"] 0]
$fd close
}
if {$short} { return $dustbin }
return "[system mediaroot]/$dustbin"
}
proc {system diskspace} {} {
switch [system model] {
HDR { set part /mnt/hd2 }
HD { set part /media/drive1 }
}
set size 0
set used 0
set free 0
set perc 0
set dev 0
foreach line [split [exec /mod/bin/busybox/df -h $part 2>>/dev/null] "\n\r"] {
if {[string match "/*" $line]} {
regsub -all -- {[[:space:]]+} $line " " line
set fields [split $line]
set dev [lindex $fields 0]
set size [lindex $fields 1]
set used [lindex $fields 2]
set free [lindex $fields 3]
set perc [string trimright [lindex $fields 4] "%"]
set fperc $(100 - $perc)
break
}
}
return [list $size $used $perc $free $fperc $dev]
}
proc {system disk} {} {
return [string range [lindex [system diskspace] 5] 0 end-1]
}
proc {system diskfree} {} {
switch [system model] {
HDR { set part /mnt/hd2 }
HD { set part /media/drive1 }
}
set free 0
foreach line [split [exec /mod/bin/busybox/df -k $part 2>>/dev/null] "\n\r"] {
if {[string match "/*" $line]} {
regsub -all -- {[[:space:]]+} $line " " line
set fields [split $line]
set free [lindex $fields 3]
set free $($free * 1024)
break
}
}
return $free
}
proc {system busy} {} {
# Is humaxtv doing anything important?
if {[catch {set pid [exec /mod/bin/busybox/pgrep humaxtv]}]} {
return 0
}
set c 0
foreach line [split [exec /mod/bin/lsof -p $pid] "\n"] {
if {[string match {*Video*.ts} $line]} { incr c }
}
if {$c > 0} { return 1 }
return 0
}
proc {system inuse} {file} {
# Is anything using the file?
set file [file rootname [file tail $file]]
set c 0
foreach line [split [exec /mod/bin/lsof] "\n"] {
if {[string first $file $line] >= 0} { incr c }
}
if {$c > 0} { return 1 }
return 0
}
proc {system reboot} {} {
exec /etc/init.d/S90settop shut
exec /sbin/reboot
}
proc {system restartpending} {} {
close [open /tmp/.restartpending w]
}
proc {system param} {param {type Value} {tbl MENUCONFIG}} {
if {[catch {set db [sqlite3.open /var/lib/humaxtv/setup.db]} msg]} {
return 0
}
set val 0
set ret [$db query "
select item$type
from TBL_$tbl
where itemName = '$param'
"]
if {[llength $ret] == 1} {
lassign [lindex $ret 0] x val
}
$db close
return $val
}
proc {system padding} {} {
return [list \
[system param START_PADDING_TIME] \
[system param STOP_PADDING_TIME] \
]
}
proc {system instandby} {} {
return [system param LAST_STANDBY Value USERCONFIG]
}
proc {system mkdir_p} {dir} {
exec /mod/bin/busybox/mkdir -p $dir
}
proc {system rmdir_if_empty} {dir} {
foreach e [readdir -nocomplain $dir] {
if {$e eq "." || $e eq ".."} continue
if {[file isdirectory "$dir/$e"]} {
return 0
} elseif {[string index $e 0] ne "."} {
return 0
}
}
file delete -force $dir
return 1
}
proc {system listening} {{mport 0}} {
set ret {}
foreach line [split [exec /mod/bin/busybox/netstat -lntpw] "\n"] {
# Remove repeated spaces
regsub -all -- {[[:space:]]+} $line " " line
# tcp 0 0 0.0.0.0:9955 0.0.0.0:* LISTEN 169/humaxtv
lassign [split $line " "] x x x port x type app
if {$type ne "LISTEN"} { continue }
lassign [split $port ":"] x port
if {$mport && $mport != $port} { continue }
lappend ret [list $port [split $app "/"]]
}
return $ret
}
proc {system is_listening} {mport} {
return [llength [system listening $mport]]
}
proc {system notify} {msg} {
set logfd [open "/mod/tmp/notify.log" "a+"]
puts $logfd "[\
clock format [clock seconds] -format "%d/%m/%Y %H:%M"\
] - $msg"
$logfd close
}