r69285 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69284‎ | r69285 | r69286 >
Date:10:04, 12 July 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Work on porting WP filesystem asbtraction
Modified paths:
  • /trunk/extensions/Deployment/includes/filesystems/Ssh2Filesystem.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Deployment/includes/filesystems/Ssh2Filesystem.php
@@ -155,132 +155,152 @@
156156 * @see Filesystem::changeFileGroup
157157 */
158158 public function changeFileGroup( $file, $group, $recursive = false ) {
159 - if ( !$this->exists( $file ) ) {
160 - return false;
161 - }
162 -
163 - if ( !$recursive || !$this->isDir( $file ) ) {
164 - return $this->runCommand( sprintf( 'chgrp %o %s', $mode, escapeshellarg( $file ) ) );
165 - }
166 -
167 - return $this->runCommand( sprintf( 'chgrp -R %o %s', $mode, escapeshellarg( $file ) ) );
 159+ // FIXME?
 160+ return $this->runCommandRecursivly( 'chgrp', $file, $recursive );
168161 }
169162
170163 /**
171164 * @see Filesystem::chmod
172165 */
173166 public function chmod( $file, $mode = false, $recursive = false ) {
174 - if ( !$this->exists( $file ) ) {
175 - return false;
176 - }
177 -
178 - if ( !$mode ) {
179 - if ( $this->is_file($file) ) {
180 - $mode = FS_CHMOD_FILE;
181 - }
182 - elseif ( $this->is_dir($file) ) {
183 - $mode = FS_CHMOD_DIR;
184 - }
185 - else {
186 - return false;
187 - }
188 - }
189 -
190 - if ( !$recursive || !$this->isDir( $file ) ) {
191 - return $this->runCommand( sprintf( 'chmod %o %s', $mode, escapeshellarg( $file ) ) );
192 - }
193 -
194 - return $this->runCommand( sprintf( 'chmod -R %o %s', $mode, escapeshellarg( $file ) ) );
 167+ return $this->runCommandRecursivly( 'chmod', $file, $recursive, $mode );
195168 }
196169
197170 /**
198171 * @see Filesystem::chown
199172 */
200173 public function chown( $file, $owner, $recursive = false ) {
201 - if ( !$this->exists( $file ) ) {
202 - return false;
203 - }
204 -
205 - if ( ! $recursive || ! $this->is_dir($file) )
206 - return $this->run_command(sprintf('chown %o %s', $mode, escapeshellarg($file)), true);
207 - return $this->run_command(sprintf('chown -R %o %s', $mode, escapeshellarg($file)), true);
 174+ return $this->runCommandRecursivly( 'chown', $file, $recursive, $mode );
208175 }
209176
210177 /**
211178 * @see Filesystem::delete
212179 */
213180 public function delete( $path, $recursive = false ) {
 181+ if ( $this->isFile( $path ) ) {
 182+ return ssh2_sftp_unlink( $this->sftp_link, $path );
 183+ }
 184+
 185+ if ( !$recursive ) {
 186+ return ssh2_sftp_rmdir( $this->sftp_link, $path );
 187+ }
 188+
 189+ $filelist = $this->listDir( $path );
214190
 191+ if ( is_array( $filelist ) ) {
 192+ foreach ( $filelist as $filename => $fileinf ) {
 193+ $this->delete( $path . '/' . $filename, $recursive );
 194+ }
 195+ }
 196+
 197+ return ssh2_sftp_rmdir( $this->sftpConnection, $path );
215198 }
216199
217200 /**
218201 * @see Filesystem::doCopy
219202 */
220 - protected function doCopy( $from, $to ) {
 203+ protected function doCopy( $source, $destination ) {
 204+ $content = $this->get_contents( $source );
221205
 206+ if ( false === $content ) {
 207+ return false;
 208+ }
 209+
 210+ return $this->writeToFile( $destination, $content );
222211 }
223212
224213 /**
225214 * @see Filesystem::doMove
226215 */
227 - protected function doMove( $from, $to ) {
228 -
 216+ protected function doMove( $source, $destination ) {
 217+ wfSuppressWarnings();
 218+ $ssh2_sftp_rename = ssh2_sftp_rename( $this->connection, $source, $destination );
 219+ wfRestoreWarnings();
 220+ return $ssh2_sftp_rename;
229221 }
230222
231223 /**
232224 * @see Filesystem::exists
233225 */
234226 public function exists( $file ) {
235 -
 227+ return file_exists( 'ssh2.sftp://' . $this->sftpConnection . '/' . ltrim( $file, '/' ) );
236228 }
237229
238230 /**
239231 * @see Filesystem::getChmod
240232 */
241233 public function getChmod( $file ) {
242 -
 234+ wfSuppressWarnings();
 235+ $fileperms = fileperms( 'ssh2.sftp://' . $this->sftpConnection . '/' . ltrim( $file, '/' );
 236+ wfRestoreWarnings();
 237+ return substr( decoct( $fileperms ) ), 3 );
243238 }
244239
245240 /**
246241 * @see Filesystem::getContents
247242 */
248243 public function getContents() {
249 -
 244+ return file_get_contents( 'ssh2.sftp://' . $this->sftpConnection . '/' . ltrim( $file, '/' ) );
250245 }
251246
252247 /**
253 - * @see Filesystem::getCreationTime
254 - */
255 - public function getCreationTime( $file ) {
256 -
257 - }
258 -
259 - /**
260248 * @see Filesystem::getCurrentWorkingDir
261249 */
262250 public function getCurrentWorkingDir() {
 251+ $cwd = $this->runCommand( 'pwd' );
263252
 253+ if ( $cwd ) {
 254+ $cwd = rtrim( $cwd, '/' ) . '/';
 255+ }
 256+
 257+ return $cwd;
264258 }
265259
266260 /**
267261 * @see Filesystem::getGroup
268262 */
269263 public function getGroup( $file ) {
 264+ wfSuppressWarnings();
 265+ $gid = filegroup( 'ssh2.sftp://' . $this->sftpConnection . '/' . ltrim( $file, '/' ) );
 266+ wfRestoreWarnings();
270267
 268+ if ( !$gid ) {
 269+ return false;
 270+ }
 271+
 272+ if ( !function_exists( 'posix_getgrgid' ) ) {
 273+ return $gid;
 274+ }
 275+
 276+ $groupArray = posix_getgrgid( $gid );
 277+ return $groupArray['name'];
271278 }
272279
273280 /**
274281 * @see Filesystem::getModificationTime
275282 */
276283 public function getModificationTime( $file ) {
277 -
 284+ return filemtime( 'ssh2.sftp://' . $this->sftpConnection . '/' . ltrim( $file, '/' ) );
278285 }
279286
280287 /**
281288 * @see Filesystem::getOwner
282289 */
283290 public function getOwner( $file ) {
 291+ wfSuppressWarnings();
 292+ $owneruid = filegroup( 'ssh2.sftp://' . $this->sftpConnection . '/' . ltrim( $file, '/' ) );
 293+ wfRestoreWarnings();
284294
 295+ if ( !$owneruid ) {
 296+ return false;
 297+ }
 298+
 299+ if ( !function_exists( 'posix_getpwuid' ) ) {
 300+ return $owneruid;
 301+ }
 302+
 303+ $ownerArray = posix_getpwuid( $owneruid );
 304+ return $ownerArray['name'];
285305 }
286306
287307 /**
@@ -346,6 +366,30 @@
347367
348368 }
349369
 370+ protected function runCommandRecursivly( $command, $file, $recursive, $mode = false ) {
 371+ if ( !$this->exists( $file ) ) {
 372+ return false;
 373+ }
 374+
 375+ if ( !$mode ) {
 376+ if ( $this->is_file($file) ) {
 377+ $mode = FS_CHMOD_FILE;
 378+ }
 379+ elseif ( $this->is_dir($file) ) {
 380+ $mode = FS_CHMOD_DIR;
 381+ }
 382+ else {
 383+ return false;
 384+ }
 385+ }
 386+
 387+ if ( !$recursive || !$this->isDir( $file ) ) {
 388+ return $this->runCommand( $command . sprintf( 'chmod %o %s', $mode, escapeshellarg( $file ) ) );
 389+ }
 390+
 391+ return $this->runCommand( $command .sprintf( 'chmod -R %o %s', $mode, escapeshellarg( $file ) ) );
 392+ }
 393+
350394 /**
351395 * Executes a command.
352396 *

Status & tagging log