44
55use Symfony \Component \Filesystem \Exception \IOException ;
66use Symfony \Component \Filesystem \Filesystem ;
7+ use PSFS \base \types \traits \Helper \FileAtomicTrait ;
78
89/**
910 * @package PSFS\base\types\helpers
1011 */
1112class FileHelper
1213{
14+ use FileAtomicTrait;
1315 /**
1416 * @param mixed $data
1517 * @param string $path
@@ -29,36 +31,14 @@ public static function writeFile(string $path, mixed $data): int|bool
2931 */
3032 public static function writeFileAtomic (string $ path , mixed $ data , int $ flags = 0 ): bool
3133 {
32- $ dir = dirname ($ path );
33- if (file_exists ($ dir ) && !is_dir ($ dir )) {
34- return false ;
35- }
36- if (!is_dir ($ dir ) && mkdir ($ dir , 0775 , true ) === false && !is_dir ($ dir )) {
34+ if (!self ::ensureParentDirectory ($ path )) {
3735 return false ;
3836 }
3937 if (is_dir ($ path )) {
4038 return false ;
4139 }
4240 $ existingMode = file_exists ($ path ) ? (fileperms ($ path ) & 0777 ) : 0644 ;
43- $ tmpPath = tempnam ($ dir , '.tmp-psfs- ' );
44- if (false === $ tmpPath ) {
45- return false ;
46- }
47- $ bytes = file_put_contents ($ tmpPath , $ data , $ flags | LOCK_EX );
48- if (false === $ bytes ) {
49- if (file_exists ($ tmpPath )) {
50- unlink ($ tmpPath );
51- }
52- return false ;
53- }
54- if (rename ($ tmpPath , $ path ) === false ) {
55- if (file_exists ($ tmpPath )) {
56- unlink ($ tmpPath );
57- }
58- return false ;
59- }
60- chmod ($ path , $ existingMode > 0 ? $ existingMode : 0644 );
61- return true ;
41+ return self ::writeTempAndSwap ($ path , $ data , $ flags , $ existingMode );
6242 }
6343
6444 /**
@@ -71,35 +51,14 @@ public static function copyFileAtomic(string $source, string $target): bool
7151 if (!file_exists ($ source )) {
7252 return false ;
7353 }
74- $ dir = dirname ($ target );
75- if (file_exists ($ dir ) && !is_dir ($ dir )) {
76- return false ;
77- }
78- if (!is_dir ($ dir ) && mkdir ($ dir , 0775 , true ) === false && !is_dir ($ dir )) {
54+ if (!self ::ensureParentDirectory ($ target )) {
7955 return false ;
8056 }
8157 if (is_dir ($ target )) {
8258 return false ;
8359 }
8460 $ mode = fileperms ($ source ) & 0777 ;
85- $ tmpPath = tempnam ($ dir , '.tmp-psfs- ' );
86- if (false === $ tmpPath ) {
87- return false ;
88- }
89- if (copy ($ source , $ tmpPath ) === false ) {
90- if (file_exists ($ tmpPath )) {
91- unlink ($ tmpPath );
92- }
93- return false ;
94- }
95- if (rename ($ tmpPath , $ target ) === false ) {
96- if (file_exists ($ tmpPath )) {
97- unlink ($ tmpPath );
98- }
99- return false ;
100- }
101- chmod ($ target , $ mode > 0 ? $ mode : 0644 );
102- return true ;
61+ return self ::copyTempAndSwap ($ source , $ target , $ mode );
10362 }
10463
10564 /**
@@ -125,26 +84,10 @@ public static function deleteFile(string $path): bool
12584 */
12685 public static function withExclusiveLock (string $ lockPath , callable $ callback ): mixed
12786 {
128- $ dir = dirname ($ lockPath );
129- if (file_exists ($ dir ) && !is_dir ($ dir )) {
87+ if (!self ::ensureParentDirectory ($ lockPath )) {
13088 return null ;
13189 }
132- if (!is_dir ($ dir ) && mkdir ($ dir , 0775 , true ) === false && !is_dir ($ dir )) {
133- return null ;
134- }
135- $ handle = fopen ($ lockPath , 'c+ ' );
136- if (false === $ handle ) {
137- return null ;
138- }
139- try {
140- if (!flock ($ handle , LOCK_EX )) {
141- return null ;
142- }
143- return $ callback ();
144- } finally {
145- flock ($ handle , LOCK_UN );
146- fclose ($ handle );
147- }
90+ return self ::withExclusiveFileLock ($ lockPath , $ callback );
14891 }
14992
15093 /**
0 commit comments