2012-06-15 21:34:07 +00:00
|
|
|
#!/mod/bin/jimsh
|
|
|
|
|
|
|
|
source /mod/webif/lib/setup
|
2013-04-03 22:42:15 +00:00
|
|
|
require lock system.class ts.class tdelete pretty_size browse.class \
|
|
|
|
safe_delete settings.class
|
2012-06-15 21:34:07 +00:00
|
|
|
|
2013-04-03 22:42:15 +00:00
|
|
|
set loglevel [[settings] autolog]
|
2013-01-28 01:35:14 +00:00
|
|
|
|
|
|
|
if {![acquire_lock webif_auto]} {
|
|
|
|
puts "Cannot acquire exclusive lock, terminating."
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
|
|
|
set logfile "/mod/tmp/auto.log"
|
|
|
|
# Rotate log file if large enough.
|
|
|
|
if {[file exists $logfile] && [file size $logfile] > 2097152} {
|
|
|
|
file copy -force $logfile "/mod/tmp/auto_old.log"
|
|
|
|
file delete $logfile
|
|
|
|
}
|
|
|
|
|
|
|
|
set logfd [open "/mod/tmp/auto.log" "a+"]
|
2013-04-03 22:42:15 +00:00
|
|
|
proc log {msg {level 1}} {
|
|
|
|
if {$level > $::loglevel} return
|
2013-01-28 01:35:14 +00:00
|
|
|
puts $::logfd "[\
|
|
|
|
clock format [clock seconds] -format "%d/%m/%Y %H:%M"\
|
|
|
|
] - $msg"
|
|
|
|
flush $::logfd
|
|
|
|
}
|
|
|
|
|
|
|
|
proc elapsed {start} {
|
|
|
|
return $(([clock milliseconds] - $start) / 1000.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
proc startclock {} {
|
|
|
|
set ::startclock_s [clock milliseconds]
|
|
|
|
}
|
|
|
|
|
|
|
|
proc endclock {size} {
|
|
|
|
set el [elapsed $::startclock_s]
|
|
|
|
set rate $($size / $el)
|
|
|
|
return "[pretty_size $size] in $el seconds - [pretty_size $rate]/s"
|
|
|
|
}
|
|
|
|
|
|
|
|
set scanstart [clock milliseconds]
|
2013-04-03 22:42:15 +00:00
|
|
|
log "-------------------------------------------------------"
|
2013-01-28 01:35:14 +00:00
|
|
|
|
|
|
|
# is_listening is relatively expensive so it is checked once globally at
|
|
|
|
# the start and then if the server is not listening then no decrypt
|
|
|
|
# operations will be attempted for this run, even if the server starts
|
|
|
|
# up halfway through. Otherwise the server is checked for every decryption
|
|
|
|
# and if it goes away then decryption will not be attempted for the rest
|
|
|
|
# of the run.
|
|
|
|
if {[system is_listening 9000]} {
|
|
|
|
set dlnaok 1
|
2013-04-03 22:42:15 +00:00
|
|
|
log "DLNA Server is running." 2
|
2013-01-28 01:35:14 +00:00
|
|
|
} else {
|
|
|
|
set dlnaok 0
|
2013-04-03 22:42:15 +00:00
|
|
|
log "DLNA Server is NOT running." 2
|
2013-01-28 01:35:14 +00:00
|
|
|
}
|
|
|
|
|
2013-04-03 22:42:15 +00:00
|
|
|
log "Media scan starting, DLNA server status: $dlnaok"
|
2013-01-18 19:24:50 +00:00
|
|
|
|
2013-02-04 22:32:21 +00:00
|
|
|
proc dsc {{size 0}} {
|
|
|
|
set free [system diskfree]
|
|
|
|
|
2013-02-12 00:08:54 +00:00
|
|
|
# Required disk space is 1GiB + 3 times the file size.
|
|
|
|
set req $($size * 3 + 1073741824)
|
2013-02-04 22:32:21 +00:00
|
|
|
|
|
|
|
if {$free < $req} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log "Insufficient disk space. Require=$req, Free=$free" 0
|
2012-08-04 21:42:02 +00:00
|
|
|
exit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dsc
|
|
|
|
|
2012-06-15 21:34:07 +00:00
|
|
|
set tmp "/mod/tmp/webif_auto"
|
|
|
|
if {![file exists $tmp]} {
|
|
|
|
if {[catch {file mkdir $tmp} msg]} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log "Cannot create temporary directory - $tmp ($msg)" 0
|
2012-06-15 21:34:07 +00:00
|
|
|
exit
|
|
|
|
}
|
|
|
|
} elseif {![file isdirectory $tmp]} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log "Cannot create temporary directory - $tmp (file exists)" 0
|
2012-06-15 21:34:07 +00:00
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
|
|
|
# Clean-up the temporary directory
|
2012-08-04 21:42:02 +00:00
|
|
|
foreach file [readdir -nocomplain $tmp] { tdelete "$tmp/$file" }
|
2012-06-15 21:34:07 +00:00
|
|
|
|
|
|
|
if {[system pkginst undelete]} {
|
|
|
|
set dustbin "[system dustbin]"
|
|
|
|
} else {
|
|
|
|
set dustbin ""
|
|
|
|
}
|
|
|
|
|
2013-04-03 22:42:15 +00:00
|
|
|
log "Dustbin: $dustbin" 2
|
2013-01-28 01:35:14 +00:00
|
|
|
|
2012-06-15 21:34:07 +00:00
|
|
|
proc dedup {dir} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log "DEDUP: \[$dir]" 2
|
2013-02-25 22:31:54 +00:00
|
|
|
loop i 0 1 {
|
2013-01-28 01:35:14 +00:00
|
|
|
foreach line [split \
|
|
|
|
[exec /mod/webif/html/dedup/dedup -yes -auto $dir] "\n"] {
|
2013-04-03 22:42:15 +00:00
|
|
|
log $line 2
|
2013-01-28 01:35:14 +00:00
|
|
|
}
|
2013-01-18 19:24:50 +00:00
|
|
|
}
|
2012-06-15 21:34:07 +00:00
|
|
|
}
|
|
|
|
|
2013-02-25 22:31:54 +00:00
|
|
|
proc do_expire {ts} {
|
|
|
|
global ax_days
|
|
|
|
set file [$ts get file]
|
|
|
|
|
|
|
|
# Calculate the age of the file in days.
|
|
|
|
set age $(([clock seconds] - [$ts get start]) / 86400.0)
|
2013-04-03 22:42:15 +00:00
|
|
|
log " EXPIRE: $file (age = $age)" 2
|
2013-02-25 22:31:54 +00:00
|
|
|
|
|
|
|
if {$age > $ax_days} {
|
|
|
|
if {[$ts inuse]} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log " EXPIRE: $file ($age > $ax_days)"
|
|
|
|
log " In use."
|
2013-02-25 22:31:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if {[safe_delete $file]} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log " EXPIRE: $file ($age > $ax_days)" 0
|
|
|
|
log " Deleted." 0
|
2013-02-25 22:31:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-15 21:34:07 +00:00
|
|
|
proc do_shrink {ts} {
|
2012-08-07 22:22:30 +00:00
|
|
|
global tmp dustbin tsgroup
|
2013-04-03 22:42:15 +00:00
|
|
|
set file [$ts get file]
|
2012-06-15 21:34:07 +00:00
|
|
|
|
2013-02-25 22:31:54 +00:00
|
|
|
if {[$ts flag "Shrunk"]} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log " $file - already shrunk." 2
|
2013-02-25 22:31:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-06-15 21:34:07 +00:00
|
|
|
set file [file rootname [$ts get file]]
|
2013-03-05 00:10:38 +00:00
|
|
|
|
|
|
|
if {[$ts inuse]} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log " $file - in use." 2
|
2013-03-05 00:10:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-06-15 21:34:07 +00:00
|
|
|
if {[catch {
|
2012-08-04 00:29:57 +00:00
|
|
|
set perc [exec /mod/bin/stripts -aq $file]
|
2012-06-15 21:34:07 +00:00
|
|
|
} msg]} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log " Error: $msg" 0
|
2012-06-15 21:34:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if {[string match {*%} $perc]} {
|
|
|
|
set perc [string range $perc 0 end-1]
|
|
|
|
} else {
|
|
|
|
set perc 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if {$perc == 0} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log " $file - already shrunk." 2
|
2013-02-25 22:31:54 +00:00
|
|
|
$ts set_shrunk
|
|
|
|
return
|
|
|
|
}
|
2013-01-28 01:35:14 +00:00
|
|
|
set size [$ts size]
|
2013-02-12 00:08:54 +00:00
|
|
|
dsc $size
|
2013-01-28 01:35:14 +00:00
|
|
|
startclock
|
2013-04-03 22:42:15 +00:00
|
|
|
log " SHRINK: $file" 0
|
|
|
|
log " Estimate $perc% saving." 0
|
|
|
|
log " Shrinking..." 0
|
2012-06-15 21:34:07 +00:00
|
|
|
if {[catch {
|
2013-01-28 01:35:14 +00:00
|
|
|
foreach line [split \
|
|
|
|
[exec nice -n 19 /mod/bin/stripts -q $file $tmp/shrunk] \
|
|
|
|
"\n"] {
|
2013-04-03 22:42:15 +00:00
|
|
|
log $line 0
|
2013-01-28 01:35:14 +00:00
|
|
|
}
|
2012-06-15 21:34:07 +00:00
|
|
|
} msg]} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log "Error during shrink: $msg" 0
|
2012-06-15 21:34:07 +00:00
|
|
|
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}"
|
|
|
|
}
|
|
|
|
|
2013-02-25 22:31:54 +00:00
|
|
|
# Remove the old recording (-> bin if undelete is installed)
|
|
|
|
safe_delete [$ts get file] "webif_autoshrink"
|
2012-06-15 21:34:07 +00:00
|
|
|
|
|
|
|
# Finally, rename the shrunken recording again.
|
2012-08-07 22:22:30 +00:00
|
|
|
foreach ext $tsgroup {
|
|
|
|
set f "${file}_shrunk.$ext"
|
|
|
|
if {[file exists $f]} {
|
|
|
|
file rename $f "${file}.$ext"
|
|
|
|
}
|
2012-06-15 21:34:07 +00:00
|
|
|
}
|
2013-02-25 22:31:54 +00:00
|
|
|
$ts set_shrunk
|
2013-04-03 22:42:15 +00:00
|
|
|
log "Done... [endclock $size]" 0
|
2012-06-15 21:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
proc do_decrypt {ts} {
|
|
|
|
global tmp dustbin
|
|
|
|
|
|
|
|
set file [$ts get file]
|
|
|
|
set rfile [file rootname $file]
|
|
|
|
set bfile [file tail $file]
|
|
|
|
|
|
|
|
if {![$ts flag "ODEncrypted"]} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log " $file - Already decrypted." 2
|
2012-06-15 21:34:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-03-07 21:41:38 +00:00
|
|
|
lassign [$ts dlnaloc "127.0.0.1"] url
|
2012-06-15 21:34:07 +00:00
|
|
|
if {$url eq ""} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log " $file - Not yet indexed."
|
2012-06-15 21:34:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-01-28 01:35:14 +00:00
|
|
|
if {![system is_listening 9000]} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log " $file - DLNA Server not running." 2
|
2013-01-28 01:35:14 +00:00
|
|
|
set ::dlnaok 0
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-02-25 22:31:54 +00:00
|
|
|
if {[$ts inuse]} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log " $file - In use."
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check that the file is not already decrypted by analysing it.
|
|
|
|
set anencd [exec /mod/bin/stripts -qE $rfile]
|
|
|
|
if {$anencd != "1"} {
|
|
|
|
log " $file - already decrypted but the HMT flag is wrong." 0
|
|
|
|
system notify "$file - auto-decrypt - file is already decrypted but the HMT flag is wrong."
|
2013-02-25 22:31:54 +00:00
|
|
|
return
|
|
|
|
}
|
2013-04-03 22:42:15 +00:00
|
|
|
|
|
|
|
# Perform the decryption by requesting the file from the DLNA server.
|
2013-01-28 01:35:14 +00:00
|
|
|
set size [$ts size]
|
2013-02-12 00:08:54 +00:00
|
|
|
dsc $size
|
2013-03-05 00:10:38 +00:00
|
|
|
set flagfile "$tmp/decrypting.$bfile"
|
|
|
|
exec /mod/bin/busybox/touch $flagfile
|
2013-01-28 01:35:14 +00:00
|
|
|
startclock
|
2013-04-03 22:42:15 +00:00
|
|
|
log " DECRYPT: $rfile" 0
|
|
|
|
log " DLNA: $url" 0
|
2012-06-15 21:34:07 +00:00
|
|
|
exec wget -O "$tmp/$bfile" $url
|
|
|
|
|
2012-09-19 20:44:04 +00:00
|
|
|
if {[file size $file] != [file size "$tmp/$bfile"]} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log " $file - File size mismatch." 0
|
|
|
|
file delete "$tmp/$bfile"
|
|
|
|
file delete $flagfile
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check if the file is in use. It is possible that the file is
|
|
|
|
# now being played even though it was free when decryption started.
|
|
|
|
if {[$ts inuse]} {
|
|
|
|
log " $file - In use."
|
|
|
|
file delete "$tmp/$bfile"
|
|
|
|
file delete $flagfile
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
# Copy the HMT file over for stripts
|
|
|
|
set thmt "$tmp/[file rootname $bfile].hmt"
|
|
|
|
file copy "$rfile.hmt" $thmt
|
|
|
|
# Check that the file is no longer encrypted by analysing it.
|
|
|
|
set anencd [exec /mod/bin/stripts -qE "$tmp/[file rootname $bfile]"]
|
|
|
|
file delete $thmt
|
|
|
|
|
|
|
|
if {$anencd != "0"} {
|
|
|
|
log " $file - File did not decrypt properly." 0
|
|
|
|
system notify "$file - auto-decrypt failed."
|
|
|
|
file delete "$tmp/$bfile"
|
2013-03-05 00:10:38 +00:00
|
|
|
file delete $flagfile
|
2012-09-19 20:44:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-06-15 21:34:07 +00:00
|
|
|
# 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.
|
2012-08-04 21:42:02 +00:00
|
|
|
exec /mod/bin/hmt -encrypted "$rfile.hmt"
|
2012-06-15 21:34:07 +00:00
|
|
|
|
2013-04-03 22:42:15 +00:00
|
|
|
log " Removing/binning old copy." 0
|
2012-06-15 21:34:07 +00:00
|
|
|
# Move the old recording to the bin if undelete is installed.
|
|
|
|
if {$dustbin ne ""} {
|
2013-02-25 22:31:54 +00:00
|
|
|
set bin [_del_bindir $file "webif_autodecrypt"]
|
2012-06-15 21:34:07 +00:00
|
|
|
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 \
|
2012-08-04 21:42:02 +00:00
|
|
|
"$bin/$tail.hmt"
|
2012-06-15 21:34:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2012-08-04 21:42:02 +00:00
|
|
|
tdelete "$rfile.encrypted"
|
2012-06-15 21:34:07 +00:00
|
|
|
}
|
2013-04-03 22:42:15 +00:00
|
|
|
log "Done... [endclock $size]" 0
|
2013-03-05 00:10:38 +00:00
|
|
|
file delete $flagfile
|
2012-06-15 21:34:07 +00:00
|
|
|
}
|
|
|
|
|
2013-01-08 22:43:43 +00:00
|
|
|
proc do_mpg {ts} {
|
|
|
|
global tmp tsgroup
|
|
|
|
|
|
|
|
set file [file rootname [$ts get file]]
|
|
|
|
|
|
|
|
if {[file exists $file.mpg]} {
|
|
|
|
# Already done.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if {[$ts flag "ODEncrypted"]} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log " $file - Not decrypted." 2
|
2013-01-08 22:43:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if {[$ts get definition] eq "HD"} {
|
2013-03-06 22:49:28 +00:00
|
|
|
# Cannot extract a useful MPG from a HD recording.
|
2013-01-08 22:43:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-02-25 22:31:54 +00:00
|
|
|
if {[$ts inuse]} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log " $file - In use."
|
2013-02-25 22:31:54 +00:00
|
|
|
return
|
|
|
|
}
|
2013-02-12 00:08:54 +00:00
|
|
|
dsc [$ts size]
|
|
|
|
|
2013-04-03 22:42:15 +00:00
|
|
|
log " MPG: $file" 0
|
|
|
|
log " Converting..." 0
|
2013-01-08 22:43:43 +00:00
|
|
|
if {[catch {
|
2013-01-28 01:35:14 +00:00
|
|
|
foreach line [split \
|
|
|
|
[exec nice -n 19 /mod/bin/ffmpeg -y -benchmark -v 0 \
|
2013-01-08 22:43:43 +00:00
|
|
|
-i $file.ts \
|
|
|
|
-map 0:0 -map 0:1 \
|
2013-01-28 01:35:14 +00:00
|
|
|
-vcodec copy -acodec copy $tmp/mpg.mpg] "\n"] {
|
2013-04-03 22:42:15 +00:00
|
|
|
log $line 0
|
2013-01-28 01:35:14 +00:00
|
|
|
}
|
2013-01-08 22:43:43 +00:00
|
|
|
} msg]} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log "Error during mpg extract: $msg" 0
|
2013-01-08 22:43:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
# Move the MPG into the local directory
|
|
|
|
file rename $tmp/mpg.mpg $file.mpg
|
|
|
|
}
|
|
|
|
|
2013-03-06 22:49:28 +00:00
|
|
|
proc do_mp3 {ts} {
|
|
|
|
global tmp tsgroup
|
|
|
|
|
|
|
|
set file [file rootname [$ts get file]]
|
|
|
|
|
|
|
|
if {[file exists $file.mp3]} {
|
|
|
|
# Already done.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if {[$ts flag "ODEncrypted"]} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log " $file - Not decrypted." 2
|
2013-03-06 22:49:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if {[$ts get definition] eq "HD"} {
|
|
|
|
# Cannot extract a useful MP3 from a HD recording.
|
2013-04-03 22:42:15 +00:00
|
|
|
log " $file - High definition." 2
|
2013-03-06 22:49:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if {[$ts inuse]} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log " $file - In use."
|
2013-03-06 22:49:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
dsc [$ts size]
|
|
|
|
|
2013-04-03 22:42:15 +00:00
|
|
|
log " MP3: $file" 0
|
|
|
|
log " Converting..." 0
|
2013-03-06 22:49:28 +00:00
|
|
|
if {[catch {
|
|
|
|
foreach line [split \
|
|
|
|
[exec nice -n 19 /mod/bin/ffmpeg -y -benchmark -v 0 \
|
|
|
|
-i $file.ts \
|
|
|
|
-f mp3 -vn -acodec copy $tmp/mp3.mp3] "\n"] {
|
2013-04-03 22:42:15 +00:00
|
|
|
log $line 0
|
2013-03-06 22:49:28 +00:00
|
|
|
}
|
|
|
|
} msg]} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log "Error during mp3 extract: $msg" 0
|
2013-03-06 22:49:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if {[system pkginst id3v2]} {
|
|
|
|
log [exec /mod/bin/id3v2 \
|
|
|
|
--song "[$ts get title]" \
|
|
|
|
--comment "[$ts get synopsis]" \
|
|
|
|
--album "[$ts get channel_name]" \
|
|
|
|
--year "[clock format [$ts get start] -format {%Y}]" \
|
2013-04-03 22:42:15 +00:00
|
|
|
"$tmp/mp3.mp3"] 0
|
2013-03-06 22:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Move the MP3 into the local directory
|
|
|
|
file rename $tmp/mp3.mp3 $file.mp3
|
|
|
|
}
|
|
|
|
|
2012-06-15 21:34:07 +00:00
|
|
|
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
|
|
|
|
$callback $ts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc shrink {dir} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log "SHRINK: \[$dir]" 2
|
2013-01-28 01:35:14 +00:00
|
|
|
entries $dir do_shrink
|
2012-06-15 21:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
proc decrypt {dir} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log "DECRYPT: \[$dir]" 2
|
2013-01-28 01:35:14 +00:00
|
|
|
if {$::dlnaok} { entries $dir do_decrypt }
|
2012-06-15 21:34:07 +00:00
|
|
|
}
|
|
|
|
|
2013-01-08 22:43:43 +00:00
|
|
|
proc mpg {dir} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log "MPG: \[$dir]" 2
|
2013-01-28 01:35:14 +00:00
|
|
|
entries $dir do_mpg
|
2013-01-08 22:43:43 +00:00
|
|
|
}
|
|
|
|
|
2013-03-06 22:49:28 +00:00
|
|
|
proc mp3 {dir} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log "MP3: \[$dir]" 2
|
2013-03-06 22:49:28 +00:00
|
|
|
entries $dir do_mp3
|
|
|
|
}
|
|
|
|
|
2013-02-25 22:31:54 +00:00
|
|
|
proc expire {dir} {
|
|
|
|
global ax_days
|
2013-04-03 22:42:15 +00:00
|
|
|
log "EXPIRE: \[$dir]" 2
|
2013-02-25 22:31:54 +00:00
|
|
|
|
|
|
|
set ax_days [{dir expiry} $dir]
|
|
|
|
entries $dir do_expire
|
|
|
|
}
|
|
|
|
|
2013-01-18 19:24:50 +00:00
|
|
|
proc scan {dir attr {force 0}} {{indent 0}} {
|
2013-01-27 21:23:16 +00:00
|
|
|
global dustbin
|
|
|
|
|
2012-06-15 21:34:07 +00:00
|
|
|
incr indent 2
|
|
|
|
|
2013-04-03 22:42:15 +00:00
|
|
|
log "[string repeat " " $indent]\[$dir]" 2
|
2012-06-15 21:34:07 +00:00
|
|
|
|
2013-01-27 21:26:45 +00:00
|
|
|
if {$dir eq $dustbin} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log "Dustbin, skipping." 2
|
2013-01-27 21:23:16 +00:00
|
|
|
return
|
|
|
|
}
|
2012-06-16 21:57:24 +00:00
|
|
|
|
2013-02-12 00:08:54 +00:00
|
|
|
if {[string match {\[*} [file tail $dir]]} {
|
|
|
|
# Special folder
|
|
|
|
file stat "$dir/" st
|
|
|
|
if {$st(dev) != $::rootdev} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log "Special folder on different device, skipping." 2
|
2013-02-12 00:08:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if {$force} {
|
|
|
|
set force 0
|
2013-04-03 22:42:15 +00:00
|
|
|
log "Special folder, overriding recursion." 2
|
2013-02-12 00:08:54 +00:00
|
|
|
}
|
2013-01-28 01:35:14 +00:00
|
|
|
}
|
2013-01-18 19:24:50 +00:00
|
|
|
|
|
|
|
# Recursion
|
2013-01-28 01:35:14 +00:00
|
|
|
if {!$force && [file exists "$dir/.auto${attr}r"]} {
|
2013-04-03 22:42:15 +00:00
|
|
|
log "[string repeat " " $indent] (R)" 2
|
2013-01-18 19:24:50 +00:00
|
|
|
set force 1
|
|
|
|
}
|
|
|
|
|
2013-01-28 01:35:14 +00:00
|
|
|
dsc
|
|
|
|
|
2013-01-18 19:24:50 +00:00
|
|
|
if {$force || [file exists "$dir/.auto$attr"]} { $attr $dir }
|
2012-06-15 21:34:07 +00:00
|
|
|
|
|
|
|
foreach entry [readdir -nocomplain $dir] {
|
|
|
|
if {[file isdirectory "$dir/$entry"]} {
|
2013-01-18 19:24:50 +00:00
|
|
|
scan "$dir/$entry" $attr $force
|
2012-06-15 21:34:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
incr indent -2
|
|
|
|
}
|
|
|
|
|
|
|
|
set root [system mediaroot]
|
2013-02-12 00:08:54 +00:00
|
|
|
file stat "$root/" rootstat
|
|
|
|
set rootdev $rootstat(dev)
|
2013-04-03 22:42:15 +00:00
|
|
|
log "Root device: $rootdev" 2
|
2012-06-15 21:34:07 +00:00
|
|
|
|
|
|
|
if {[llength $argv] > 0} {
|
2013-04-03 22:42:15 +00:00
|
|
|
set loglevel 2
|
2012-06-15 21:34:07 +00:00
|
|
|
foreach arg $argv { scan $root $arg }
|
|
|
|
} else {
|
2013-03-06 22:49:28 +00:00
|
|
|
foreach arg {dedup decrypt shrink mpg mp3 expire} {
|
2013-01-28 01:35:14 +00:00
|
|
|
set st [clock milliseconds]
|
2012-06-15 21:34:07 +00:00
|
|
|
scan $root $arg
|
2013-04-03 22:42:15 +00:00
|
|
|
log "$arg scan completed in [elapsed $st] seconds."
|
2012-06-15 21:34:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
release_lock webif_auto
|
|
|
|
|
2013-04-03 22:42:15 +00:00
|
|
|
log "Media scan completed in [elapsed $scanstart] seconds."
|
2013-01-28 01:35:14 +00:00
|
|
|
|