-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymlink.php
More file actions
483 lines (370 loc) · 12.6 KB
/
Symlink.php
File metadata and controls
483 lines (370 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/base/blob/master/LICENSE
*/
namespace Quid\Base;
// symlink
// class with static methods to manage symlinks
final class Symlink extends Finder
{
// config
protected static array $config = [];
// is
// retourne vrai si le chemin est un lien symbolique
final public static function is($path,bool $makePath=true):bool
{
if($makePath === true)
$path = self::path($path);
return is_string($path) && is_link($path);
}
// isReadable
// retourne vrai si le chemin est un lien symbolique existant et lisible
final public static function isReadable($path,bool $makePath=true):bool
{
if($makePath === true)
$path = self::path($path);
return self::is($path,false) && self::isPermission('readable',$path);
}
// isWritable
// retourne vrai si le chemin est un lien symbolique existant et accessible en écriture
final public static function isWritable($path,bool $makePath=true):bool
{
if($makePath === true)
$path = self::path($path);
return self::is($path,false) && self::isPermission('writable',$path);
}
// isExecutable
// retourne vrai le chemin est un lien symbolique existant et éxécutable
final public static function isExecutable($path,bool $makePath=true):bool
{
if($makePath === true)
$path = self::path($path);
return self::is($path,false) && self::isPermission('executable',$path);
}
// inode
// retourne le numéro d'inode du symlink
final public static function inode($path):?int
{
$return = null;
$ino = self::statValue('ino',$path);
if(is_int($ino))
$return = $ino;
return $return;
}
// permission
// retourne la permission du symlink
final public static function permission($path,bool $format=false)
{
$return = null;
$mode = self::statValue('mode',$path);
if(is_int($mode))
$return = ($format === true)? self::formatValue('permission',$mode):$mode;
return $return;
}
// owner
// retourne l'identifiant du propriétaire du symlink
final public static function owner($path,bool $format=false)
{
$return = null;
$owner = self::statValue('uid',$path);
if(is_int($owner))
$return = ($format === true)? self::formatValue('owner',$owner):$owner;
return $return;
}
// ownerChange
// change le owner du symlink
final public static function ownerChange($user,$path):bool
{
$path = self::path($path);
return is_scalar($user) && self::isWritable($path,false) && lchown($path,$user);
}
// group
// retourne l'identifiant du groupe du symlink
final public static function group($path,bool $format=false)
{
$return = null;
$group = self::statValue('gid',$path);
if(is_int($group))
$return = ($format === true)? self::formatValue('group',$group):$group;
return $return;
}
// groupChange
// change le groupe du symlink
final public static function groupChange($group,$path):bool
{
$path = self::path($path);
return is_scalar($group) && self::isWritable($path,false) && lchgrp($path,$group);
}
// size
// retourne la taille du symlink
final public static function size($path,bool $format=false)
{
$return = null;
$size = self::statValue('size',$path);
if(is_int($size))
$return = ($format === true)? self::formatValue('size',$size):$size;
return $return;
}
// dateAccess
// retourne la dernière date d'accès du symlink
final public static function dateAccess($path,bool $format=false)
{
$return = null;
$atime = self::statValue('atime',$path);
if(is_int($atime))
$return = ($format === true)? self::formatValue('dateAccess',$atime):$atime;
return $return;
}
// dateModify
// retourne la date de modification du symlink
final public static function dateModify($path,bool $format=false)
{
$return = null;
$mtime = self::statValue('mtime',$path);
if(is_int($mtime))
$return = ($format === true)? self::formatValue('dateModify',$mtime):$mtime;
return $return;
}
// dateInodeModify
// retourne la date de modification de l'inode du symlink
final public static function dateInodeModify($path,bool $format=false)
{
$return = null;
$ctime = self::statValue('ctime',$path);
if(is_int($ctime))
$return = ($format === true)? self::formatValue('dateInodeModify',$ctime):$ctime;
return $return;
}
// stat
// retourne les informations stat du symlink
// le chemin de symlink n'est pas suivi
final public static function stat($path,bool $formatKey=false,bool $formatValue=false):?array
{
$return = null;
$path = self::path($path);
if(self::is($path,false))
{
$stat = lstat($path);
if(is_array($stat))
{
$return = $stat;
if($formatKey === true)
$return = self::statReformat($return,$formatValue);
}
}
return $return;
}
// info
// étend la méthode info de finder
// ajoute la target, enlève size
final public static function info($path,bool $clearStatCache=false,bool $format=true):?array
{
$return = null;
$path = self::path($path);
$is = self::isReadable($path,false);
if($is === true)
{
$return = [];
if($clearStatCache === true)
self::clearStatCache();
$return['path'] = $path;
$return['target'] = self::get($path);
$return['type'] = filetype($path);
$return['readable'] = $is;
$return['writable'] = self::isWritable($path,false);
$return['executable'] = self::isExecutable($path,false);
$return['dir'] = is_dir($path);
$return['file'] = is_file($path);
$return['link'] = is_link($path);
$return['pathinfo'] = Path::info($path);
$return['stat'] = (array) self::stat($path,$format);
}
return $return;
}
// get
// retourne le contenu d'un symlink
final public static function get($path):?string
{
$return = null;
$path = self::path($path);
if(self::isReadable($path,false))
{
$link = readlink($path);
if(is_string($link) && !empty($link))
$return = self::normalize($link,false);
}
return $return;
}
// getStat
// retourne le tableau stat du fichier ou directoire référencé, si existant
// sinon retourne null
final public static function getStat($path,bool $format=true):?array
{
$return = null;
$path = self::get($path);
if(!empty($path))
$return = Finder::stat($path,$format);
return $return;
}
// getInfo
// retourne le tableau info du fichier ou directoire référencé, si le symlink existe
// sinon retourne null
final public static function getInfo($path,bool $format=true,bool $clearStatCache=false):?array
{
$return = null;
$path = self::get($path);
if(!empty($path))
$return = Finder::info($path,$format,$clearStatCache);
return $return;
}
// set
// créer un nouveau lien symbolique si la target est lisible et que la destination est créable
// ne remplace pas si existant par défaut (mais on peut mettre true)
final public static function set($to,$from,bool $replace=false):bool
{
$return = false;
$to = self::path($to);
$from = self::path($from);
if(is_string($to) && is_string($from))
{
if($replace === true && Finder::is($to,false))
{
if(is_link($to))
self::unset($to);
else
self::unlink($to);
}
if(Finder::isReadable($from,false) && !Finder::is($to,false))
{
$dirname = Path::dirname($to);
if(!empty($dirname) && Dir::setOrWritable($dirname))
$return = symlink($from,$to);
}
}
return $return;
}
// sets
// permet de créer plusieurs symlinks, un tableau from->to doit être fourni
// retourne un tableau multidimensionnel avec status et from
// un status a null signifie que le symlink existe déjà (vers le bon chemin)
// support pour catchAll, et dig si from et to sont des directoires
final public static function sets(array $array,bool $replace=false,bool $dig=false):array
{
$return = [];
foreach ($array as $from => $to)
{
$r = ['status'=>null,'from'=>$from];
$from = self::normalize($from);
$to = self::normalize($to);
$get = self::get($to);
$go = true;
// symlink impossible, mais dig et deux directoires
if($dig === true && $get === null && !self::is($to) && Dir::is($to) && Dir::is($from))
{
$go = false;
$from = Path::append($from,'*');
$catchAll = Dir::fromToCatchAll([$from=>$to]);
if(!empty($catchAll))
{
$sets = self::sets($catchAll,$replace);
$return = Arr::merge($return,$sets);
}
}
// mauvais symlink
elseif(is_string($get) && !Finder::is($get))
{
self::unset($to);
$get = null;
}
if($go === true)
{
if($get !== $from)
$r['status'] = self::set($to,$from,$replace);
$return[$to] = $r;
}
}
return $return;
}
// touch
// touche un symlink, pour ce faire il faut le détruire et le recréer
final public static function touch($path):bool
{
$return = false;
$path = self::path($path);
if(self::isWritable($path,false))
{
$target = self::get($path);
if(!empty($target) && self::unset($path))
$return = self::set($path,$target);
}
return $return;
}
// rename
// renome un symlink
// le symlink garde le même target après avoir été déplacé
final public static function rename($target,$path):bool
{
$return = false;
$target = self::path($target);
$path = self::path($path);
if(self::isWritable($path,false) && self::isCreatable($target))
{
$dirname = Path::dirname($target);
if(!empty($dirname) && Dir::setOrWritable($dirname))
$return = rename($path,$target);
}
return $return;
}
// copy
// copy un symlink, pour ce faire il faut le recréer
// le symlink garde la même target, il fait juste se créer à un nouvel endroit
final public static function copy($to,$path):bool
{
$return = false;
$to = self::path($to);
$path = self::path($path);
if(is_string($to) && is_string($path) && self::isReadable($path,false))
{
$target = self::get($path);
if(!empty($target))
$return = self::set($to,$target);
}
return $return;
}
// unset
// efface un symlink, n'efface pas le fichier vers lequel il pointe
// gestion d'un problème sous windows ou il faut utiliser rmdir si le symlink pointe vers un directoire
final public static function unset($path):bool
{
$return = false;
$path = self::path($path);
if(self::isWritable($path,false))
{
if(Server::isWindows() && Dir::is($path))
$return = rmdir($path);
else
$return = unlink($path);
}
return $return;
}
// reset
// efface et recrée un symlink avec une nouvelle target
final public static function reset($target,$path):bool
{
$return = false;
$target = self::path($target);
$path = self::path($path);
if(is_string($target) && is_string($path))
{
self::unset($path);
$return = (self::set($path,$target));
}
return $return;
}
}
// init
Symlink::__init();
?>