Skip to content

Commit

Permalink
Merge pull request #420 from nextcloud/updateDir
Browse files Browse the repository at this point in the history
Introduce Update Directory
  • Loading branch information
blizzz authored Sep 2, 2022
2 parents e7915bc + 6cf29c7 commit 121af7e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 40 deletions.
43 changes: 21 additions & 22 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function __construct($baseDir) {
return;
}

$dataDir = $this->getDataDirectoryLocation();
$dataDir = $this->getUpdateDirectoryLocation();
if (empty($dataDir) || !is_string($dataDir)) {
throw new \Exception('Could not read data directory from config.php.');
}
Expand All @@ -115,10 +115,7 @@ public function __construct($baseDir) {
$buildTime = $OC_Build;
}

if ($version === null) {
return;
}
if ($buildTime === null) {
if ($version === null || $buildTime === null) {
return;
}

Expand Down Expand Up @@ -216,8 +213,8 @@ public function getConfigOption(string $key) {
*
* @return string
*/
private function getDataDirectoryLocation() {
return $this->configValues['datadirectory'];
private function getUpdateDirectoryLocation() {
return $this->configValues['updatedirectory'] ?? $this->configValues['datadirectory'];
}

/**
Expand Down Expand Up @@ -398,7 +395,7 @@ public function createBackup() {
];

// Create new folder for the backup
$backupFolderLocation = $this->getDataDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid').'/backups/nextcloud-'.$this->getConfigOption('version') . '-' . time() . '/';
$backupFolderLocation = $this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid').'/backups/nextcloud-'.$this->getConfigOption('version') . '-' . time() . '/';
$this->silentLog('[info] backup folder location: ' . $backupFolderLocation);

$state = mkdir($backupFolderLocation, 0750, true);
Expand Down Expand Up @@ -550,7 +547,8 @@ public function downloadUpdate() {
$this->silentLog('[info] downloadUpdate()');

$response = $this->getUpdateServerResponse();
$storageLocation = $this->getDataDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/downloads/';

$storageLocation = $this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/downloads/';
if (file_exists($storageLocation)) {
$this->silentLog('[info] storage location exists');
$this->recursiveDelete($storageLocation);
Expand Down Expand Up @@ -618,7 +616,7 @@ public function downloadUpdate() {
* @throws \Exception
*/
private function getDownloadedFilePath() {
$storageLocation = $this->getDataDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/downloads/';
$storageLocation = $this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/downloads/';
$this->silentLog('[info] storage location: ' . $storageLocation);

$filesInStorageLocation = scandir($storageLocation);
Expand Down Expand Up @@ -839,7 +837,7 @@ public function deleteOldFiles() {
throw new \Exception('core/shipped.json is not available');
}

$newShippedAppsFile = $this->getDataDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/downloads/nextcloud/core/shipped.json';
$newShippedAppsFile = $this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/downloads/nextcloud/core/shipped.json';
if (!file_exists($newShippedAppsFile)) {
throw new \Exception('core/shipped.json is not available in the new release');
}
Expand Down Expand Up @@ -997,7 +995,7 @@ public function moveNewVersionInPlace() {
'ocs/v1.php',
'ocs/v2.php',
];
$storageLocation = $this->getDataDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/downloads/nextcloud/';
$storageLocation = $this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/downloads/nextcloud/';
$this->silentLog('[info] storage location: ' . $storageLocation);
$this->moveWithExclusions($storageLocation, $excludedElements);

Expand All @@ -1013,14 +1011,15 @@ public function moveNewVersionInPlace() {
public function finalize() {
$this->silentLog('[info] finalize()');

$storageLocation = $this->getDataDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/downloads/nextcloud/';
$storageLocation = $this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/downloads/nextcloud/';
$this->silentLog('[info] storage location: ' . $storageLocation);
$this->moveWithExclusions($storageLocation, []);
$state = rmdir($storageLocation);
if ($state === false) {
throw new \Exception('Could not rmdir $storagelocation');
}
$state = unlink($this->getDataDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/.step');

$state = unlink($this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/.step');
if ($state === false) {
throw new \Exception('Could not rmdir .step');
}
Expand All @@ -1039,7 +1038,7 @@ public function finalize() {
* @throws \Exception
*/
private function writeStep($state, $step) {
$updaterDir = $this->getDataDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid');
$updaterDir = $this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid');
if (!file_exists($updaterDir . '/.step')) {
if (!file_exists($updaterDir)) {
$result = mkdir($updaterDir);
Expand Down Expand Up @@ -1084,7 +1083,7 @@ public function endStep($step) {
public function currentStep() {
$this->silentLog('[info] currentStep()');

$updaterDir = $this->getDataDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid');
$updaterDir = $this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid');
$jsonData = [];
if (file_exists($updaterDir. '/.step')) {
$state = file_get_contents($updaterDir . '/.step');
Expand All @@ -1109,7 +1108,7 @@ public function currentStep() {
public function rollbackChanges($step) {
$this->silentLog('[info] rollbackChanges("' . $step . '")');

$updaterDir = $this->getDataDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid');
$updaterDir = $this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid');
if (file_exists($updaterDir . '/.step')) {
$this->silentLog('[info] unlink .step');
$state = unlink($updaterDir . '/.step');
Expand Down Expand Up @@ -1153,7 +1152,7 @@ public function logException(\Exception $e) {
* @throws LogException
*/
public function log($message) {
$updaterLogPath = $this->getDataDirectoryLocation() . '/updater.log';
$updaterLogPath = $this->getUpdateDirectoryLocation() . '/updater.log';

$fh = fopen($updaterLogPath, 'a');
if ($fh === false) {
Expand Down Expand Up @@ -1831,13 +1830,13 @@ public function isAuthenticated() {
</div>
</li>
</ul>
<?php else: ?>
<?php else : ?>
<div id="login" class="section">
<h2>Authentication</h2>
<p>To login you need to provide the unhashed value of "updater.secret" in your config file.</p>
<p>If you don't know that value, you can access this updater directly via the Nextcloud admin screen or generate
your own secret:</p>
<code>php -r '$password = trim(shell_exec("openssl rand -base64 48"));if(strlen($password) === 64) {$hash = password_hash($password, PASSWORD_DEFAULT) . "\n"; echo "Insert as \"updater.secret\": ".$hash; echo "The plaintext value is: ".$password."\n";}else{echo "Could not execute OpenSSL.\n";};'</code>
<code>php -r '$password = trim(shell_exec("openssl rand -base64 48")); if (strlen($password) === 64) {$hash = password_hash($password, PASSWORD_DEFAULT) . "\n"; echo "Insert as \"updater.secret\": ".$hash; echo "The plaintext value is: ".$password."\n";} else {echo "Could not execute OpenSSL.\n";};'</code>
<form method="post" name="login">
<fieldset>
<input type="password" name="updater-secret-input" value=""
Expand All @@ -1856,7 +1855,8 @@ public function isAuthenticated() {
</div>

</body>
<?php if ($auth->isAuthenticated()): ?>

<?php if ($auth->isAuthenticated()) : ?>
<script>
function escapeHTML(s) {
return s.toString().split('&').join('&amp;').split('<').join('&lt;').split('>').join('&gt;').split('"').join('&quot;').split('\'').join('&#039;');
Expand Down Expand Up @@ -2244,4 +2244,3 @@ function confirmExit() {
<?php endif; ?>

</html>

35 changes: 17 additions & 18 deletions lib/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function __construct($baseDir) {
return;
}

$dataDir = $this->getDataDirectoryLocation();
$dataDir = $this->getUpdateDirectoryLocation();
if (empty($dataDir) || !is_string($dataDir)) {
throw new \Exception('Could not read data directory from config.php.');
}
Expand All @@ -85,10 +85,7 @@ public function __construct($baseDir) {
$buildTime = $OC_Build;
}

if ($version === null) {
return;
}
if ($buildTime === null) {
if ($version === null || $buildTime === null) {
return;
}

Expand Down Expand Up @@ -186,8 +183,8 @@ public function getConfigOption(string $key) {
*
* @return string
*/
private function getDataDirectoryLocation() {
return $this->configValues['datadirectory'];
private function getUpdateDirectoryLocation() {
return $this->configValues['updatedirectory'] ?? $this->configValues['datadirectory'];
}

/**
Expand Down Expand Up @@ -368,7 +365,7 @@ public function createBackup() {
];

// Create new folder for the backup
$backupFolderLocation = $this->getDataDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid').'/backups/nextcloud-'.$this->getConfigOption('version') . '-' . time() . '/';
$backupFolderLocation = $this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid').'/backups/nextcloud-'.$this->getConfigOption('version') . '-' . time() . '/';
$this->silentLog('[info] backup folder location: ' . $backupFolderLocation);

$state = mkdir($backupFolderLocation, 0750, true);
Expand Down Expand Up @@ -520,7 +517,8 @@ public function downloadUpdate() {
$this->silentLog('[info] downloadUpdate()');

$response = $this->getUpdateServerResponse();
$storageLocation = $this->getDataDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/downloads/';

$storageLocation = $this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/downloads/';
if (file_exists($storageLocation)) {
$this->silentLog('[info] storage location exists');
$this->recursiveDelete($storageLocation);
Expand Down Expand Up @@ -588,7 +586,7 @@ public function downloadUpdate() {
* @throws \Exception
*/
private function getDownloadedFilePath() {
$storageLocation = $this->getDataDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/downloads/';
$storageLocation = $this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/downloads/';
$this->silentLog('[info] storage location: ' . $storageLocation);

$filesInStorageLocation = scandir($storageLocation);
Expand Down Expand Up @@ -809,7 +807,7 @@ public function deleteOldFiles() {
throw new \Exception('core/shipped.json is not available');
}

$newShippedAppsFile = $this->getDataDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/downloads/nextcloud/core/shipped.json';
$newShippedAppsFile = $this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/downloads/nextcloud/core/shipped.json';
if (!file_exists($newShippedAppsFile)) {
throw new \Exception('core/shipped.json is not available in the new release');
}
Expand Down Expand Up @@ -967,7 +965,7 @@ public function moveNewVersionInPlace() {
'ocs/v1.php',
'ocs/v2.php',
];
$storageLocation = $this->getDataDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/downloads/nextcloud/';
$storageLocation = $this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/downloads/nextcloud/';
$this->silentLog('[info] storage location: ' . $storageLocation);
$this->moveWithExclusions($storageLocation, $excludedElements);

Expand All @@ -983,14 +981,15 @@ public function moveNewVersionInPlace() {
public function finalize() {
$this->silentLog('[info] finalize()');

$storageLocation = $this->getDataDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/downloads/nextcloud/';
$storageLocation = $this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/downloads/nextcloud/';
$this->silentLog('[info] storage location: ' . $storageLocation);
$this->moveWithExclusions($storageLocation, []);
$state = rmdir($storageLocation);
if ($state === false) {
throw new \Exception('Could not rmdir $storagelocation');
}
$state = unlink($this->getDataDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/.step');

$state = unlink($this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid') . '/.step');
if ($state === false) {
throw new \Exception('Could not rmdir .step');
}
Expand All @@ -1009,7 +1008,7 @@ public function finalize() {
* @throws \Exception
*/
private function writeStep($state, $step) {
$updaterDir = $this->getDataDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid');
$updaterDir = $this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid');
if (!file_exists($updaterDir . '/.step')) {
if (!file_exists($updaterDir)) {
$result = mkdir($updaterDir);
Expand Down Expand Up @@ -1054,7 +1053,7 @@ public function endStep($step) {
public function currentStep() {
$this->silentLog('[info] currentStep()');

$updaterDir = $this->getDataDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid');
$updaterDir = $this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid');
$jsonData = [];
if (file_exists($updaterDir. '/.step')) {
$state = file_get_contents($updaterDir . '/.step');
Expand All @@ -1079,7 +1078,7 @@ public function currentStep() {
public function rollbackChanges($step) {
$this->silentLog('[info] rollbackChanges("' . $step . '")');

$updaterDir = $this->getDataDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid');
$updaterDir = $this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOption('instanceid');
if (file_exists($updaterDir . '/.step')) {
$this->silentLog('[info] unlink .step');
$state = unlink($updaterDir . '/.step');
Expand Down Expand Up @@ -1123,7 +1122,7 @@ public function logException(\Exception $e) {
* @throws LogException
*/
public function log($message) {
$updaterLogPath = $this->getDataDirectoryLocation() . '/updater.log';
$updaterLogPath = $this->getUpdateDirectoryLocation() . '/updater.log';

$fh = fopen($updaterLogPath, 'a');
if ($fh === false) {
Expand Down

0 comments on commit 121af7e

Please sign in to comment.