98_update.pm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. ################################################################
  2. # $Id: 98_update.pm 12801 2016-12-17 16:52:31Z rudolfkoenig $
  3. package main;
  4. use strict;
  5. use warnings;
  6. use HttpUtils;
  7. use File::Copy qw(mv copy);
  8. use Blocking;
  9. sub CommandUpdate($$);
  10. sub upd_getUrl($);
  11. sub upd_initRestoreDirs($);
  12. sub upd_mkDir($$$);
  13. sub upd_rmTree($);
  14. sub upd_writeFile($$$$);
  15. sub upd_mv($$);
  16. sub upd_metainit($);
  17. sub upd_metacmd($@);
  18. sub upd_saveConfig($$$);
  19. my $updateInBackground;
  20. my $updRet;
  21. my %updDirs;
  22. my $updArg;
  23. my $mainPgm = "/fhem.pl\$";
  24. my %upd_connecthash;
  25. my $upd_needJoin;
  26. my $upd_nChanged;
  27. ########################################
  28. sub
  29. update_Initialize($$)
  30. {
  31. my %hash = (
  32. Fn => "CommandUpdate",
  33. Hlp => "[<fileName>|all|check|force] [http://.../controlfile],update FHEM",
  34. );
  35. $cmds{update} = \%hash;
  36. }
  37. ########################################
  38. sub
  39. CommandUpdate($$)
  40. {
  41. my ($cl,$param) = @_;
  42. my @args = split(/ +/,$param);
  43. my $err = upd_metainit(0);
  44. return $err if($err);
  45. if($args[0] &&
  46. ($args[0] eq "list" ||
  47. $args[0] eq "add" ||
  48. $args[0] eq "delete" ||
  49. $args[0] eq "reset")) {
  50. return upd_metacmd($cl, @args);
  51. }
  52. my $arg = (defined($args[0]) ? $args[0] : "all");
  53. my $src = (defined($args[1]) ? $args[1] : "");
  54. my $ret = eval { "Hello" =~ m/$arg/ };
  55. return "first argument must be a valid regexp, all, force or check"
  56. if($arg =~ m/^[-\?\*]/ || $ret);
  57. $arg = lc($arg) if($arg =~ m/^(check|all|force)$/i);
  58. $updateInBackground = AttrVal("global","updateInBackground",1);
  59. $updateInBackground = 0 if($arg ne "all");
  60. $updArg = $arg;
  61. if($updateInBackground) {
  62. CallFn($cl->{NAME}, "ActivateInformFn", $cl, "log");
  63. BlockingCall("doUpdateInBackground", {src=>$src,arg=>$arg});
  64. return "Executing the update the background.";
  65. } else {
  66. doUpdateLoop($src, $arg);
  67. my $ret = $updRet; $updRet = "";
  68. return $ret;
  69. }
  70. }
  71. sub
  72. upd_metainit($)
  73. {
  74. my $force = shift;
  75. my $mpath = $attr{global}{modpath}."/FHEM/controls.txt";
  76. if($force || ! -f $mpath || -s $mpath == 0) {
  77. if(!open(FH, ">$mpath")) {
  78. my $msg = "Can't open $mpath: $!";
  79. Log 1, $msg;
  80. return $msg;
  81. }
  82. print FH "http://fhem.de/fhemupdate/controls_fhem.txt\n";
  83. close(FH);
  84. }
  85. return undef;
  86. }
  87. sub
  88. upd_metacmd($@)
  89. {
  90. my ($cl, @args) = @_;
  91. my $mpath = $attr{global}{modpath}."/FHEM/controls.txt";
  92. if($args[0] eq "list") {
  93. open(FH, $mpath) || return "Can't open $mpath: $!";
  94. my $ret = join("", <FH>);
  95. close(FH);
  96. return $ret;
  97. }
  98. if($args[0] eq "add") {
  99. return "Usage: update add http://.../controls_.*.txt"
  100. if(int(@args) != 2 || $args[1] !~ m,^http.*/(controls_.*.txt)$,);
  101. my $fname = $1;
  102. open(FH, $mpath) || return "Can't open $mpath: $!";
  103. my (%fulls, %parts);
  104. map {chomp($_);$fulls{$_}=1; my $x=$_; $x =~ s,^.*/,,; $parts{$x}=$_;} <FH>;
  105. close(FH);
  106. return "$args[1] is already in the list" if($fulls{$args[1]});
  107. return "$fname is already present in $parts{$fname}" if($parts{$fname});
  108. open(FH, ">>$mpath") || return "Can't write $mpath: $!";
  109. print FH $args[1],"\n";
  110. close(FH);
  111. return undef;
  112. }
  113. if($args[0] eq "delete") {
  114. return "Usage: update delete http://.../controls_.*.txt"
  115. if(int(@args) != 2 || $args[1] !~ m,^http.*/(controls_.*.txt)$,);
  116. open(FH, $mpath) || return "Can't open $mpath: $!";
  117. my @list = grep { $_ ne $args[1]."\n"; } <FH>;
  118. close(FH);
  119. open(FH, ">$mpath") || return "Can't write $mpath: $!";
  120. print FH join("", @list);
  121. close(FH);
  122. return undef;
  123. }
  124. if($args[0] eq "reset") {
  125. return upd_metainit(1);
  126. }
  127. }
  128. sub
  129. uLog($$)
  130. {
  131. my ($loglevel, $arg) = @_;
  132. return if($loglevel > $attr{global}{verbose} || !defined($arg));
  133. if($updateInBackground) {
  134. Log 1, $arg;
  135. } else {
  136. Log $loglevel, $arg if($updArg ne "check");
  137. $updRet .= "$arg\n";
  138. }
  139. }
  140. my $inLog = 0;
  141. sub
  142. update_Log2Event($$)
  143. {
  144. my ($level, $text) = @_;
  145. return if($inLog || $level > $attr{global}{verbose});
  146. $inLog = 1;
  147. $text =~ s/\n/ /g; # Multiline text causes havoc in Analyze
  148. BlockingInformParent("Log", [$level, $text], 0);
  149. $inLog = 0;
  150. }
  151. sub
  152. doUpdateInBackground($)
  153. {
  154. my ($h) = @_;
  155. no warnings 'redefine'; # The main process is not affected
  156. *Log = \&update_Log2Event;
  157. sleep(2); # Give time for ActivateInform / FHEMWEB / JavaScript
  158. doUpdateLoop($h->{src}, $h->{arg});
  159. }
  160. sub
  161. doUpdateLoop($$)
  162. {
  163. my ($src, $arg) = @_;
  164. $upd_needJoin = 0;
  165. $upd_nChanged = 0;
  166. if($src =~ m/^http.*/) {
  167. doUpdate(1,1, $src, $arg);
  168. HttpUtils_Close(\%upd_connecthash);
  169. return;
  170. }
  171. my $mpath = $attr{global}{modpath}."/FHEM/controls.txt";
  172. if(!open(LFH, $mpath)) {
  173. my $msg = "Can't open $mpath: $!";
  174. uLog 1, $msg;
  175. return $msg;
  176. }
  177. my @list = <LFH>;
  178. close(LFH);
  179. chomp @list;
  180. my ($max,$curr) = (0,0);
  181. foreach my $srcLine (@list) {
  182. next if($src && $srcLine !~ m/controls_${src}.txt/);
  183. $max++;
  184. }
  185. uLog 1, "No source file named controls_$src found" if($src && !$max);
  186. foreach my $srcLine (@list) {
  187. next if($src && $srcLine !~ m/controls_${src}.txt/);
  188. doUpdate(++$curr, $max, $srcLine, $arg);
  189. HttpUtils_Close(\%upd_connecthash);
  190. }
  191. if($upd_nChanged) {
  192. if($updateInBackground) {
  193. BlockingInformParent("DoTrigger", ["global", "UPDATE", 0 ], 0)
  194. } else {
  195. DoTrigger("global","UPDATE", 0);
  196. }
  197. }
  198. }
  199. sub
  200. doUpdate($$$$)
  201. {
  202. my ($curr, $max, $src, $arg) = @_;
  203. my ($basePath, $ctrlFileName);
  204. if($src !~ m,^(.*)/([^/]*)$,) {
  205. uLog 1, "Cannot parse $src, probably not a valid http control file";
  206. return;
  207. }
  208. $basePath = $1;
  209. $ctrlFileName = $2;
  210. $ctrlFileName =~ m/controls_(.*).txt/;
  211. my $srcName = $1;
  212. if(AttrVal("global", "backup_before_update", 0) &&
  213. $arg ne "check" && $curr==1) {
  214. my $cmdret = AnalyzeCommand(undef, "backup");
  215. if ($cmdret !~ m/backup done.*/) {
  216. uLog 1, "Something went wrong during backup: $cmdret";
  217. uLog 1, "update was canceled. Please check manually!";
  218. return;
  219. }
  220. }
  221. if($max != 1) {
  222. uLog 1, "";
  223. uLog 1, $srcName;
  224. }
  225. my $remCtrlFile = upd_getUrl($src);
  226. return if(!$remCtrlFile);
  227. my @remList = split(/\R/, $remCtrlFile);
  228. uLog 4, "Got remote $ctrlFileName with ".int(@remList)." entries.";
  229. ###########################
  230. # read in & digest the local control file
  231. my $root = $attr{global}{modpath};
  232. my $restoreDir = ($arg eq "check" ? "" : upd_initRestoreDirs($root));
  233. my @locList;
  234. if(($arg eq "check" || $arg eq "all") &&
  235. open(FD, "$root/FHEM/$ctrlFileName")) {
  236. @locList = map { $_ =~ s/[\r\n]//; $_ } <FD>;
  237. close(FD);
  238. uLog 4, "Got local $ctrlFileName with ".int(@locList)." entries.";
  239. }
  240. my %lh;
  241. foreach my $l (@locList) {
  242. my @l = split(" ", $l, 4);
  243. next if($l[0] ne "UPD");
  244. $lh{$l[3]}{TS} = $l[1];
  245. $lh{$l[3]}{LEN} = $l[2];
  246. }
  247. my $canJoin;
  248. my $cmod = AttrVal('global', 'commandref', 'full');
  249. my $cj = "$root/contrib/commandref_".
  250. ($cmod eq "full" ? "join":"modular").".pl";
  251. if(-f $cj &&
  252. -f "$root/docs/commandref_frame.html" &&
  253. -w "$root/docs/commandref.html" &&
  254. (AttrVal('global','exclude_from_update','') !~ m/commandref/) ) {
  255. $canJoin = 1;
  256. }
  257. my @excl = split(" ", AttrVal("global", "exclude_from_update", ""));
  258. my $noSzCheck = AttrVal("global", "updateNoFileCheck", configDBUsed());
  259. my @rl = upd_getChanges($root, $basePath);
  260. ###########################
  261. # process the remote controlfile
  262. my ($nChanged,$nSkipped) = (0,0);
  263. my $isSingle = ($arg ne "all" && $arg ne "force" && $arg ne "check");
  264. foreach my $r (@remList) {
  265. my @r = split(" ", $r, 4);
  266. if($r[0] eq "MOV" && ($arg eq "all" || $arg eq "force")) {
  267. if($r[1] =~ m+\.\.+ || $r[2] =~ m+\.\.+) {
  268. uLog 1, "Suspicious line $r, aborting";
  269. return 1;
  270. }
  271. upd_mkDir($root, $r[2], 0);
  272. my $mvret = upd_mv("$root/$r[1]", "$root/$r[2]");
  273. uLog 4, "mv $root/$r[1] $root/$r[2]". ($mvret ? " FAILED:$mvret":"");
  274. }
  275. next if($r[0] ne "UPD");
  276. my $fName = $r[3];
  277. if($fName =~ m+\.\.+) {
  278. uLog 1, "Suspicious line $r, aborting";
  279. return 1;
  280. }
  281. if($isSingle) {
  282. next if($fName !~ m/$arg/);
  283. } else {
  284. my $isExcl;
  285. foreach my $ex (@excl) {
  286. $isExcl = 1 if($fName =~ m/$ex/);
  287. }
  288. my $fPath = "$root/$fName";
  289. $fPath = $0 if($fPath =~ m/$mainPgm/);
  290. my $fileOk = ($lh{$fName} &&
  291. $lh{$fName}{TS} eq $r[1] &&
  292. $lh{$fName}{LEN} eq $r[2]);
  293. if($isExcl && !$fileOk) {
  294. uLog 1, "update: skipping $fName, matches exclude_from_update";
  295. $nSkipped++;
  296. next;
  297. }
  298. if($noSzCheck) {
  299. next if($isExcl || $fileOk);
  300. } else {
  301. my $sz = -s $fPath;
  302. next if($isExcl || ($fileOk && defined($sz) && $sz eq $r[2]));
  303. }
  304. }
  305. $upd_needJoin = 1 if($fName =~ m/commandref_frame/ || $fName=~ m/\d+.*.pm/);
  306. next if($fName =~ m/commandref.*html/ && $fName !~ m/frame/ && $canJoin);
  307. uLog 1, "List of new / modified files since last update:"
  308. if($arg eq "check" && $nChanged == 0);
  309. $nChanged++;
  310. uLog 1, "$r[0] $fName";
  311. next if($arg eq "check");
  312. my $remFile = upd_getUrl("$basePath/$fName");
  313. return if(!$remFile); # Error already reported
  314. if(length($remFile) ne $r[2]) {
  315. uLog 1, "Got ".length($remFile)." bytes for $fName, expected $r[2]";
  316. if($attr{global}{verbose} == 5) {
  317. upd_writeFile($root, $restoreDir, "$fName.corrupt", $remFile);
  318. uLog 1, "saving it to $fName.corrupt .";
  319. next;
  320. } else {
  321. uLog 1, "aborting.";
  322. return;
  323. }
  324. }
  325. return if(!upd_writeFile($root, $restoreDir, $fName, $remFile));
  326. }
  327. if($nChanged) {
  328. for my $f ($attr{global}{configfile}, $attr{global}{statefile}) {
  329. upd_saveConfig($root, $restoreDir, $f) if($f && $f !~ m,(^/|\.\.),);
  330. }
  331. }
  332. uLog 1, "nothing to do..." if($nChanged == 0 && $nSkipped == 0);
  333. if(@rl && ($nChanged || $nSkipped)) {
  334. uLog(1, "");
  335. uLog 1, "New entries in the CHANGED file:";
  336. map { uLog 1, $_ } @rl;
  337. }
  338. return if($arg eq "check");
  339. if(($arg eq "all" || $arg eq "force") && ($nChanged || $nSkipped)) {
  340. return if(!upd_writeFile($root, $restoreDir,
  341. "FHEM/$ctrlFileName", $remCtrlFile));
  342. }
  343. if($canJoin && $upd_needJoin && $curr == $max) {
  344. chdir($root);
  345. $cj .= " -noWarnings" if($cmod eq "full");
  346. uLog(1, "Calling $^X $cj, this may take a while");
  347. my $ret = `$^X $cj`;
  348. foreach my $l (split(/[\r\n]+/, $ret)) {
  349. uLog(1, $l);
  350. }
  351. }
  352. $upd_nChanged += $nChanged;
  353. return "" if(!$upd_nChanged);
  354. uLog(1, "");
  355. if($curr == $max) {
  356. uLog 1,
  357. 'update finished, "shutdown restart" is needed to activate the changes.';
  358. my $ss = AttrVal("global","sendStatistics",undef);
  359. if(!defined($ss)) {
  360. uLog(1, "");
  361. uLog(1, "Please consider using the global attribute sendStatistics");
  362. } elsif(defined($ss) && lc($ss) eq "onupdate") {
  363. uLog(1, "");
  364. my $ret = AnalyzeCommandChain(undef, "fheminfo send");
  365. $ret =~ s/.*server response:/server response:/ms;
  366. uLog(1, "fheminfo $ret");
  367. }
  368. }
  369. }
  370. sub
  371. upd_mv($$)
  372. {
  373. my ($src, $dest) = @_;
  374. if($src =~ m/\*/) {
  375. $src =~ m,^(.*)/([^/]+)$,;
  376. my ($dir, $pat) = ($1, $2);
  377. $pat = "^$pat\$";
  378. opendir(my $dh, $dir) || return "$dir: $!";
  379. while(my $r = readdir($dh)) {
  380. next if($r !~ m/$pat/ || "$dir/$r" eq $dest);
  381. my $mvret = mv("$dir/$r", $dest);
  382. return "MV $dir/$r $dest: $!" if(!$mvret);
  383. Log 3, "MV $dir/$r $dest";
  384. }
  385. closedir($dh);
  386. } else {
  387. return "MV $src $dest: $!" if(mv($src, $dest));
  388. }
  389. return undef;
  390. }
  391. sub
  392. upd_mkDir($$$)
  393. {
  394. my ($root, $dir, $isFile) = @_;
  395. if($isFile) { # Delete the file Component
  396. $dir =~ m,^(.*)/([^/]*)$,;
  397. $dir = $1;
  398. }
  399. return if($updDirs{$dir});
  400. $updDirs{$dir} = 1;
  401. my @p = split("/", $dir);
  402. for(my $i = 0; $i < int(@p); $i++) {
  403. my $path = "$root/".join("/", @p[0..$i]);
  404. if(!-d $path) {
  405. mkdir $path;
  406. uLog 4, "MKDIR $root/".join("/", @p[0..$i]);
  407. }
  408. }
  409. }
  410. sub
  411. upd_getChanges($$)
  412. {
  413. my ($root, $basePath) = @_;
  414. my $lFile = "";
  415. if(open(FH, "$root/CHANGED")) {
  416. foreach my $l (<FH>) { # first non-comment line
  417. next if($l =~ m/^#/);
  418. chomp $l;
  419. $lFile = $l;
  420. last;
  421. }
  422. close(FH);
  423. }
  424. my @lines = split(/[\r\n]/,upd_getUrl("$basePath/CHANGED"));
  425. my $maxLines = 25;
  426. my @ret;
  427. foreach my $line (@lines) {
  428. next if($line =~ m/^#/);
  429. last if($line eq "" || $line eq $lFile);
  430. push @ret, $line;
  431. if($maxLines-- < 1) {
  432. push @ret, "... rest of lines skipped.";
  433. last;
  434. }
  435. }
  436. return @ret;
  437. }
  438. sub
  439. upd_getUrl($)
  440. {
  441. my ($url) = @_;
  442. $url =~ s/%/%25/g;
  443. $upd_connecthash{url} = $url;
  444. $upd_connecthash{keepalive} = ($url =~ m/localUpdate/ ? 0 : 1); # Forum #49798
  445. # $upd_connecthash{compress} = 1; # fhem.de does not support compression
  446. my ($err, $data) = HttpUtils_BlockingGet(\%upd_connecthash);
  447. if($err) {
  448. uLog 1, $err;
  449. return "";
  450. }
  451. if(!$data) {
  452. uLog 1, "$url: empty file received";
  453. return "";
  454. }
  455. return $data;
  456. }
  457. sub
  458. upd_saveConfig($$$)
  459. {
  460. my($root, $restoreDir, $fName) = @_;
  461. return if(!$fName || !$restoreDir || configDBUsed() || !-r "$root/$fName");
  462. upd_mkDir($root, "$restoreDir/$fName", 1);
  463. Log 1, "saving $fName";
  464. if(!copy("$root/$fName", "$root/$restoreDir/$fName")) {
  465. uLog 1, "copy $root/$fName $root/$restoreDir/$fName failed:$!, ".
  466. "aborting the update";
  467. return 0;
  468. }
  469. }
  470. sub
  471. upd_writeFile($$$$)
  472. {
  473. my($root, $restoreDir, $fName, $content) = @_;
  474. # copy the old file and save the new
  475. upd_mkDir($root, $fName, 1);
  476. upd_mkDir($root, "$restoreDir/$fName", 1) if($restoreDir);
  477. if($restoreDir && -f "$root/$fName" &&
  478. ! copy("$root/$fName", "$root/$restoreDir/$fName")) {
  479. uLog 1, "copy $root/$fName $root/$restoreDir/$fName failed:$!, ".
  480. "aborting the update";
  481. return 0;
  482. }
  483. my $rest = ($restoreDir ? "trying to restore the previous version and ":"").
  484. "aborting the update";
  485. my $fPath = "$root/$fName";
  486. $fPath = $0 if($fPath =~ m/$mainPgm/);
  487. if(!open(FD, ">$fPath")) {
  488. uLog 1, "open $fPath failed: $!, $rest";
  489. copy "$root/$restoreDir/$fName", "$root/$fName" if($restoreDir);
  490. return 0;
  491. }
  492. binmode(FD);
  493. print FD $content;
  494. close(FD);
  495. my $written = -s "$fPath";
  496. if($written != length($content)) {
  497. uLog 1, "writing $fPath failed: $!, $rest";
  498. copy "$root/$restoreDir/$fName", "$fPath" if($restoreDir);
  499. return;
  500. }
  501. cfgDB_FileUpdate("$fPath") if(configDBUsed());
  502. return 1;
  503. }
  504. sub
  505. upd_rmTree($)
  506. {
  507. my ($dir) = @_;
  508. my $dh;
  509. if(!opendir($dh, $dir)) {
  510. uLog 1, "opendir $dir: $!";
  511. return;
  512. }
  513. my @files = grep { $_ ne "." && $_ ne ".." } readdir($dh);
  514. closedir($dh);
  515. foreach my $f (@files) {
  516. if(-d "$dir/$f") {
  517. upd_rmTree("$dir/$f");
  518. } else {
  519. uLog 4, "rm $dir/$f";
  520. if(!unlink("$dir/$f")) {
  521. uLog 1, "rm $dir/$f failed: $!";
  522. }
  523. }
  524. }
  525. uLog 4, "rmdir $dir";
  526. if(!rmdir($dir)) {
  527. uLog 1, "rmdir $dir failed: $!";
  528. }
  529. }
  530. sub
  531. upd_initRestoreDirs($)
  532. {
  533. my ($root) = @_;
  534. my $nDirs = AttrVal("global","restoreDirs", 3);
  535. if($nDirs !~ m/^\d+$/ || $nDirs < 0) {
  536. uLog 1, "invalid restoreDirs value $nDirs, setting it to 3";
  537. $nDirs = 3;
  538. }
  539. return "" if($nDirs == 0);
  540. my $rdName = "restoreDir";
  541. my @t = localtime;
  542. my $restoreDir = sprintf("$rdName/%04d-%02d-%02d",
  543. $t[5]+1900, $t[4]+1, $t[3]);
  544. Log 1, "MKDIR $restoreDir" if(! -d "$root/restoreDir");
  545. upd_mkDir($root, $restoreDir, 0);
  546. if(!opendir(DH, "$root/$rdName")) {
  547. uLog 1, "opendir $root/$rdName: $!";
  548. return "";
  549. }
  550. my @oldDirs = sort grep { $_ !~ m/^\./ && $_ ne $restoreDir } readdir(DH);
  551. closedir(DH);
  552. while(int(@oldDirs) > $nDirs) {
  553. my $dir = "$root/$rdName/". shift(@oldDirs);
  554. next if($dir =~ m/$restoreDir/); # Just in case
  555. uLog 1, "RMDIR: $dir";
  556. upd_rmTree($dir);
  557. }
  558. return $restoreDir;
  559. }
  560. 1;
  561. =pod
  562. =item command
  563. =item summary update FHEM program files from the central repository
  564. =item summary_DE FHEM Programmdateien aktualisieren
  565. =begin html
  566. <a name="update"></a>
  567. <h3>update</h3>
  568. <ul>
  569. <code>update [&lt;fileName&gt;|all|check|force]
  570. [http://.../controlfile]</code>
  571. <br>or<br>
  572. <code>update [add source|delete source|list|reset]</code>
  573. <br>
  574. <br>
  575. Update the FHEM installation. Technically this means update will download
  576. the controlfile(s) first, compare it to the local version of the file in the
  577. moddir/FHEM directory, and download each file where the attributes (timestamp
  578. and filelength) are different. Upon completion it triggers the global:UPDATE
  579. event.
  580. <br>
  581. With the commands add/delete/list/reset you can manage the list of
  582. controlfiles, e.g. for thirdparty packages.
  583. Notes:
  584. <ul>
  585. <li>The contrib directory will not be updated.</li>
  586. <li>The files are automatically transferred from the source repository
  587. (SVN) to the web site once a day, at 7:45 CET / CEST.</li>
  588. <li>The all argument is default.</li>
  589. <li>The force argument will disregard the local file.</li>
  590. <li>The check argument will only display the files it would download, and
  591. the last section of the CHANGED file.</li>
  592. <li>Specifying a filename will only download matching files (regexp).</li>
  593. </ul>
  594. See also the restore command.<br>
  595. <br>
  596. Examples:<br>
  597. <ul>
  598. <li>update check</li>
  599. <li>update</li>
  600. <li>update force</li>
  601. <li>update check http://fhem.de/fhemupdate/controls_fhem.txt</li>
  602. </ul>
  603. <a name="updateattr"></a>
  604. <br>
  605. <b>Attributes</b> (use attr global ...)
  606. <ul>
  607. <a name="updateInBackground"></a>
  608. <li>updateInBackground<br>
  609. If this attribute is set (to 1), the update will be executed in a
  610. background process. The return message is communicated via events, and
  611. in telnet the inform command is activated, in FHEMWEB the Event
  612. Monitor. Default is set. Set it to 0 to switch it off.
  613. </li><br>
  614. <a name="updateNoFileCheck"></a>
  615. <li>updateNoFileCheck<br>
  616. If set, the command won't compare the local file size with the expected
  617. size. This attribute was introduced to satisfy some experienced FHEM
  618. user, its default value is 0.
  619. </li><br>
  620. <a name="backup_before_update"></a>
  621. <li>backup_before_update<br>
  622. If this attribute is set, an update will back up your complete
  623. installation via the <a href="#backup">backup</a> command. The default
  624. is not set as update relies on the restore feature (see below).<br>
  625. Example:<br>
  626. <ul>
  627. attr global backup_before_update
  628. </ul>
  629. </li><br>
  630. <a name="exclude_from_update"></a>
  631. <li>exclude_from_update<br>
  632. Contains a space separated list of fileNames (regexps) which will be
  633. excluded by an update. The special value commandref will disable calling
  634. commandref_join at the end, i.e commandref.html will be out of date.
  635. The module-only documentation is not affected and is up-to-date.<br>
  636. Example:<br>
  637. <ul>
  638. attr global exclude_from_update 21_OWTEMP.pm FS20.off.png
  639. </ul>
  640. </li><br>
  641. <a name="restoreDirs"></a>
  642. <li>restoreDirs<br>
  643. update saves each file before overwriting it with the new version from
  644. the Web. For this purpose update creates a directory restoreDir in the
  645. global modpath directory, then a subdirectory with the current date,
  646. where the old version of the currently replaced file is stored.
  647. The default value of this attribute is 3, meaning that 3 old versions
  648. (i.e. date-directories) are kept, and the older ones are deleted. If
  649. the attribute is set to 0, the feature is deactivated.
  650. </li><br>
  651. </ul>
  652. </ul>
  653. =end html
  654. =begin html_DE
  655. <a name="update"></a>
  656. <h3>update</h3>
  657. <ul>
  658. <code>update [&lt;fileName&gt;|all|check|force]
  659. [http://.../controlfile]</code>
  660. <br>oder<br>
  661. <code>update [add source|delete source|list|reset]</code>
  662. <br>
  663. <br>
  664. Erneuert die FHEM Installation. D.h. es wird (werden) zuerst die
  665. Kontroll-Datei(en) heruntergeladen, und mit der lokalen Version dieser Datei
  666. in moddir/FHEM verglichen. Danach werden alle in der Kontroll-Datei
  667. spezifizierten Dateien heruntergeladen, deren Gr&ouml;&szlig;e oder
  668. Zeitstempel sich unterscheidet. Wenn dieser Ablauf abgeschlossen ist, wird
  669. das globale UPDATE Ereignis ausgel&ouml;st.
  670. <br>
  671. Mit den Befehlen add/delete/list/reset kann man die Liste der Kontrolldateien
  672. pflegen.
  673. <br>
  674. <br>
  675. Zu beachten:
  676. <ul>
  677. <li>Das contrib Verzeichnis wird nicht heruntergeladen.</li>
  678. <li>Die Dateien werden auf der Webseite einmal am Tag um 07:45 MET/MEST aus
  679. der Quell-Verwaltungssystem (SVN) bereitgestellt.</li>
  680. <li>Das all Argument ist die Voreinstellung.</li>
  681. <li>Das force Argument beachtet die lokale controls_fhem.txt Datei
  682. nicht.</li>
  683. <li>Das check Argument zeigt die neueren Dateien an, und den letzten
  684. Abschnitt aus der CHANGED Datei</li>
  685. <li>Falls man &lt;fileName&gt; spezifiziert, dann werden nur die Dateien
  686. heruntergeladen, die diesem Regexp entsprechen.</li>
  687. </ul>
  688. Siehe also das restore Befehl.<br>
  689. <br>
  690. Beispiele:<br>
  691. <ul>
  692. <li>update check</li>
  693. <li>update</li>
  694. <li>update force</li>
  695. <li>update check http://fhem.de/fhemupdate/controls_fhem.txt</li>
  696. </ul>
  697. <a name="updateattr"></a>
  698. <br>
  699. <b>Attribute</b> (sind mit attr global zu setzen)
  700. <ul>
  701. <a name="updateInBackground"></a>
  702. <li>updateInBackground<br>
  703. Wenn dieses Attribut gesetzt ist, wird das update Befehl in einem
  704. separaten Prozess ausgef&uuml;hrt, und alle Meldungen werden per Event
  705. &uuml;bermittelt. In der telnet Sitzung wird inform, in FHEMWEB wird
  706. das Event Monitor aktiviert. Die Voreinstellung ist an, zum
  707. Deaktivieren bitte Attribut auf 0 setzen.
  708. </li><br>
  709. <a name="updateNoFileCheck"></a>
  710. <li>updateNoFileCheck<br>
  711. Wenn dieses Attribut gesetzt ist, wird die Gr&ouml;&szlig;e der bereits
  712. vorhandenen, lokalen Datei nicht mit der Sollgr&ouml;&szlig;e
  713. verglichen. Dieses Attribut wurde nach nicht genau spezifizierten Wnsch
  714. erfahrener FHEM Benutzer eingefuehrt, die Voreinstellung ist 0.
  715. </li><br>
  716. <a name="backup_before_update"></a>
  717. <li>backup_before_update<br>
  718. Wenn dieses Attribut gesetzt ist, erstellt FHEM eine Sicherheitskopie
  719. der FHEM Installation vor dem update mit dem backup Befehl. Die
  720. Voreinstellung is "nicht gesetzt", da update sich auf das restore
  721. Feature verl&auml;sst, s.u.<br>
  722. Beispiel:<br>
  723. <ul>
  724. attr global backup_before_update
  725. </ul>
  726. </li><br>
  727. <a name="exclude_from_update"></a>
  728. <li>exclude_from_update<br>
  729. Enth&auml;lt eine Liste durch Leerzeichen getrennter Dateinamen
  730. (regexp), welche nicht im update ber&uuml;cksichtigt werden.<br>
  731. Falls der Wert commandref enth&auml;lt, dann wird commandref_join.pl
  732. nach dem update nicht aufgerufen, d.h. die Gesamtdokumentation ist
  733. nicht mehr aktuell. Die Moduldokumentation bleibt weiterhin aktuell.
  734. <br>
  735. Beispiel:<br>
  736. <ul>
  737. attr global exclude_from_update 21_OWTEMP.pm temp4hum4.gplot
  738. </ul>
  739. </li><br>
  740. <li><a href="#restoreDirs">restoreDirs</a>
  741. update sichert jede Datei vor dem &Uuml;berschreiben mit der neuen
  742. Version aus dem Web. F&uuml;r diesen Zweck wird zuerst ein restoreDir
  743. Verzeichnis in der global modpath Verzeichnis angelegt, und danach
  744. ein Unterverzeichnis mit dem aktuellen Datum. In diesem Verzeichnis
  745. werden vor dem &Uuml;berschreiben die alten Versionen der Dateien
  746. gerettet. Die Voreinstellung ist 3, d.h. die letzten 3
  747. Datums-Verzeichnisse werden aufgehoben, und die &auml;lteren entfernt.
  748. Falls man den Wert auf 0 setzt, dann ist dieses Feature deaktiviert.
  749. </li><br>
  750. </ul>
  751. </ul>
  752. =end html_DE
  753. =cut