Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

dfs.php
Go to the documentation of this file.
1 <?php
12 class eZDFSFileHandlerDFSBackend implements eZDFSFileHandlerDFSBackendInterface
13 {
14 public function __construct()
15 {
16 $mountPointPath = eZINI::instance( 'file.ini' )->variable( 'eZDFSClusteringSettings', 'MountPointPath' );
17
18 if ( !$mountPointPath = realpath( $mountPointPath ) )
19 throw new eZDFSFileHandlerNFSMountPointNotFoundException( $mountPointPath );
20
21 if ( !is_writeable( $mountPointPath ) )
22 throw new eZDFSFileHandlerNFSMountPointNotWriteableException( $mountPointPath );
23
24 if ( substr( $mountPointPath, -1 ) != '/' )
25 $mountPointPath = "$mountPointPath/";
26
27 $this->mountPointPath = $mountPointPath;
28
29 $this->filePermissionMask = octdec( eZINI::instance()->variable( 'FileSettings', 'StorageFilePermissions' ) );
30 }
31
35 public function supports( $path )
36 {
37 return true;
38 }
39
48 public function copyFromDFSToDFS( $srcFilePath, $dstFilePath )
49 {
50 $this->accumulatorStart();
51
52 $srcFilePath = $this->makeDFSPath( $srcFilePath );
53 $dstFilePath = $this->makeDFSPath( $dstFilePath );
54 if ( file_exists( dirname( $dstFilePath ) ) )
55 {
56 $ret = copy( $srcFilePath, $dstFilePath );
57 if ( $ret )
58 $this->fixPermissions( $dstFilePath );
59 }
60 else
61 {
62 $ret = $this->createFile( $dstFilePath, fopen( $srcFilePath, 'rb' ), false );
63 }
64
65 $this->accumulatorStop();
66
67 return $ret;
68 }
69
78 public function copyFromDFS( $srcFilePath, $dstFilePath = false )
79 {
80 $this->accumulatorStart();
81
82 if ( $dstFilePath === false )
83 {
84 $dstFilePath = $srcFilePath;
85 }
86 $srcFilePath = $this->makeDFSPath( $srcFilePath );
87
88 if ( file_exists( dirname( $dstFilePath ) ) )
89 {
90 $ret = copy( $srcFilePath, $dstFilePath );
91 if ( $ret )
92 $this->fixPermissions( $dstFilePath );
93 }
94 else
95 {
96 $ret = $this->createFile( $dstFilePath, fopen( $srcFilePath, 'rb' ) );
97 }
98
99 if ( $ret )
100 {
101 $ret = $this->copyTimestamp( $srcFilePath, $dstFilePath );
102 }
103
104 if ( !$ret && file_exists( $dstFilePath ) )
105 {
106 unlink( $dstFilePath );
107 }
108
109 $this->accumulatorStop();
110
111 return $ret;
112 }
113
123 public function copyToDFS( $srcFilePath, $dstFilePath = false )
124 {
125 $this->accumulatorStart();
126
127 $srcFileSize = filesize( $srcFilePath );
128 $srcFileHandle = fopen( $srcFilePath, 'rb' );
129 if ( $srcFileHandle === false )
130 {
131 $this->accumulatorStop();
132 eZDebug::writeError( "Error getting contents of file 'FS://$srcFilePath'.", __METHOD__ );
133 return false;
134 }
135
136 if ( $dstFilePath === false )
137 {
138 $dstFilePath = $srcFilePath;
139 }
140
141 $dstFilePath = $this->makeDFSPath( $dstFilePath );
142 $ret = $this->createFile( $dstFilePath, $srcFileHandle, true );
143 if ( $ret )
144 {
145 // Double checking if the file has been correctly created
146 clearstatcache( true, $dstFilePath );
147 $dstFileSize = filesize( $dstFilePath );
148 if ( $dstFileSize != $srcFileSize )
149 {
150 eZDebug::writeError( "Size ($dstFileSize) of written data for file FS://$dstFilePath does not match expected size of original DFS file ($srcFileSize)", __METHOD__ );
151 $ret = false;
152 }
153 }
154
155 $this->accumulatorStop();
156
157 return $ret;
158 }
159
168 public function delete( $filePath )
169 {
170 $this->accumulatorStart();
171
172 if ( is_array( $filePath ) )
173 {
174 $ret = true;
175 foreach( $filePath as $file )
176 {
177 $dfsPath = $this->makeDFSPath( $file );
178 $locRet = @unlink( $dfsPath );
179 $ret = $ret and $locRet;
180
181 if ( $locRet )
182 eZClusterFileHandler::cleanupEmptyDirectories( $dfsPath );
183 }
184 }
185 else
186 {
187 $dfsPath = $this->makeDFSPath( $filePath );
188 $ret = @unlink( $dfsPath );
189
190 if ( $ret )
191 eZClusterFileHandler::cleanupEmptyDirectories( $dfsPath );
192 }
193
194 $this->accumulatorStop();
195
196 return $ret;
197 }
198
207 public function passthrough( $filePath, $startOffset = 0, $length = false )
208 {
209 return eZFile::downloadContent(
210 $this->makeDFSPath( $filePath ),
211 $startOffset,
212 $length
213 );
214 }
215
223 public function getContents( $filePath )
224 {
225 $this->accumulatorStart();
226
227 // @todo Throw an exception if it fails
228 // (FileNotFound, or FileNotReadable, depends on testing)
229 $ret = @file_get_contents( $this->makeDFSPath( $filePath ) );
230
231 $this->accumulatorStop();
232
233 return $ret;
234 }
235
244 public function createFileOnDFS( $filePath, $contents )
245 {
246 $this->accumulatorStart();
247
248 $filePath = $this->makeDFSPath( $filePath );
249 $ret = $this->createFile( $filePath, $contents, false );
250
251 $this->accumulatorStop();
252
253 return $ret;
254 }
255
263 public function renameOnDFS( $oldPath, $newPath )
264 {
265 $this->accumulatorStart();
266
267 $oldPath = $this->makeDFSPath( $oldPath );
268 $newPath = $this->makeDFSPath( $newPath );
269
270 $ret = eZFile::rename( $oldPath, $newPath, true );
271
272 if ( $ret )
273 eZClusterFileHandler::cleanupEmptyDirectories( $oldPath );
274
275 $this->accumulatorStop();
276
277 return $ret;
278 }
279
286 public function existsOnDFS( $filePath )
287 {
288 if ( file_exists( $this->makeDFSPath( $filePath ) ) )
289 {
290 return true;
291 }
292
293 // Verify that mount point is still there
294 $filePathDir = substr( $filePath, 0, strpos( $filePath, '/' ) + 1 );
295 $path = realpath( $this->getMountPoint() ). '/' . $filePathDir;
296 if ( !file_exists( $path ) || !is_dir( $path ) )
297 {
298 throw new eZDFSFileHandlerBackendException( "NFS mount root $path not found" );
299 }
300
301 return false;
302 }
303
309 protected function getMountPoint()
310 {
311 return $this->mountPointPath;
312 }
313
319 protected function makeDFSPath( $filePath )
320 {
321 return $this->mountPointPath . $filePath;
322 }
323
324 protected function accumulatorStart()
325 {
326 eZDebug::accumulatorStart( 'mysql_cluster_dfs_operations', 'MySQL Cluster', 'DFS operations' );
327 }
328
329 protected function accumulatorStop()
330 {
331 eZDebug::accumulatorStop( 'mysql_cluster_dfs_operations' );
332 }
333
334 protected function fixPermissions( $filePath )
335 {
336 chmod( $filePath, $this->filePermissionMask );
337 }
338
345 protected function copyTimestamp( $srcFilePath, $dstFilePath )
346 {
347 clearstatcache( true, $srcFilePath );
348 $originalTimestamp = filemtime( $srcFilePath );
349 if ( !$originalTimestamp )
350 {
351 return false;
352 }
353
354 return touch( $dstFilePath, $originalTimestamp);
355 }
356
357 protected function createFile( $filePath, $contents, $atomic = true )
358 {
359 // $contents can result from a failed file_get_contents(). In this case
360 if ( $contents === false )
361 return false;
362
363 $createResult = eZFile::create( basename( $filePath ), dirname( $filePath ), $contents, $atomic );
364
365 if ( $createResult )
366 $this->fixPermissions( $filePath );
367
368 return $createResult;
369 }
370
377 public function getDfsFileSize( $filePath )
378 {
379 return filesize( $this->makeDFSPath( $filePath ) );
380 }
381
389 public function getFilesList( $basePath )
390 {
391 // The custom iterator filters out the file path in order to get a relative one
392 return new eZDFSFileHandlerDFSBackendFilterIterator(
393 new RecursiveIteratorIterator(
394 new RecursiveDirectoryIterator(
395 $this->mountPointPath . '/' . $basePath,
396 FilesystemIterator::SKIP_DOTS|FilesystemIterator::UNIX_PATHS
397 )
398 ),
399 $this->mountPointPath
400 );
401 }
402
406 public function applyServerUri( $filePath )
407 {
408 return $filePath;
409 }
410
415 protected $mountPointPath;
416
421 private $filePermissionMask;
422 }
423 ?>
eZClusterFileHandler\cleanupEmptyDirectories
static cleanupEmptyDirectories( $path)
Definition: ezclusterfilehandler.php:103
eZDFSFileHandlerBackendException
Definition: ezdfsfilehandlerbackendexception.php:11
eZDFSFileHandlerDFSBackendFilterIterator
Definition: dfs_filter_iterator.php:16
eZDFSFileHandlerDFSBackend
Definition: dfs.php:13
eZDFSFileHandlerDFSBackend\getContents
getContents( $filePath)
Definition: dfs.php:223
eZDFSFileHandlerDFSBackend\renameOnDFS
renameOnDFS( $oldPath, $newPath)
Definition: dfs.php:263
eZDFSFileHandlerDFSBackend\fixPermissions
fixPermissions( $filePath)
Definition: dfs.php:334
eZDFSFileHandlerDFSBackend\copyToDFS
copyToDFS( $srcFilePath, $dstFilePath=false)
Definition: dfs.php:123
eZDFSFileHandlerDFSBackend\createFileOnDFS
createFileOnDFS( $filePath, $contents)
Definition: dfs.php:244
eZDFSFileHandlerDFSBackend\passthrough
passthrough( $filePath, $startOffset=0, $length=false)
Definition: dfs.php:207
eZDFSFileHandlerDFSBackend\getDfsFileSize
getDfsFileSize( $filePath)
Definition: dfs.php:377
eZDFSFileHandlerDFSBackend\getMountPoint
getMountPoint()
Definition: dfs.php:309
eZDFSFileHandlerDFSBackend\$filePermissionMask
$filePermissionMask
Definition: dfs.php:421
eZDFSFileHandlerDFSBackend\applyServerUri
applyServerUri( $filePath)
Definition: dfs.php:406
eZDFSFileHandlerDFSBackend\accumulatorStart
accumulatorStart()
Definition: dfs.php:324
eZDFSFileHandlerDFSBackend\$mountPointPath
$mountPointPath
Definition: dfs.php:415
eZDFSFileHandlerDFSBackend\supports
supports( $path)
Definition: dfs.php:35
eZDFSFileHandlerDFSBackend\createFile
createFile( $filePath, $contents, $atomic=true)
Definition: dfs.php:357
eZDFSFileHandlerDFSBackend\copyTimestamp
copyTimestamp( $srcFilePath, $dstFilePath)
Definition: dfs.php:345
eZDFSFileHandlerDFSBackend\existsOnDFS
existsOnDFS( $filePath)
Definition: dfs.php:286
eZDFSFileHandlerDFSBackend\makeDFSPath
makeDFSPath( $filePath)
Definition: dfs.php:319
eZDFSFileHandlerDFSBackend\__construct
__construct()
Definition: dfs.php:14
eZDFSFileHandlerDFSBackend\getFilesList
getFilesList( $basePath)
Definition: dfs.php:389
eZDFSFileHandlerDFSBackend\copyFromDFS
copyFromDFS( $srcFilePath, $dstFilePath=false)
Definition: dfs.php:78
eZDFSFileHandlerDFSBackend\copyFromDFSToDFS
copyFromDFSToDFS( $srcFilePath, $dstFilePath)
Definition: dfs.php:48
eZDFSFileHandlerDFSBackend\accumulatorStop
accumulatorStop()
Definition: dfs.php:329
eZDFSFileHandlerNFSMountPointNotFoundException
Definition: mount_point_not_found.php:20
eZDFSFileHandlerNFSMountPointNotWriteableException
Definition: mount_point_not_writeable.php:20
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:546
eZDebug\accumulatorStart
static accumulatorStart( $key, $inGroup=false, $name=false, $recursive=false)
Definition: ezdebug.php:1362
eZDebug\accumulatorStop
static accumulatorStop( $key, $recursive=false)
Definition: ezdebug.php:1390
eZFile\downloadContent
static downloadContent( $file, $startOffset=0, $length=false)
Definition: ezfile.php:278
eZFile\rename
static rename( $srcFile, $destFile, $mkdir=false, $flags=0)
Definition: ezfile.php:153
eZFile\create
static create( $filename, $directory=false, $data=false, $atomic=false)
Definition: ezfile.php:42
file_exists
static file_exists( $inifilename)
Definition: eZPBINIFile.php:195
$contents
$contents
Definition: ezarticle/user/extendedsearch.php:52
$file
$file
Definition: ezfilemanager/user/filedownload.php:35
$filePath
$filePath
Definition: ezfilemanager/user/filedownload.php:50
$ret
$ret
Definition: ezsitemanager/admin/filelist.php:88
eZDFSFileHandlerDFSBackendInterface
Definition: ezdfsfilehandlerdfsbackendinterface.php:18
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.