8585#include < type_traits>
8686#include < wtf/Box.h>
8787#include < wtf/CommaPrinter.h>
88+ #include < wtf/FileSystem.h>
8889#include < wtf/MainThread.h>
8990#include < wtf/MemoryPressureHandler.h>
9091#include < wtf/MonotonicTime.h>
@@ -1003,7 +1004,6 @@ class ShellSourceProvider : public StringSourceProvider {
10031004
10041005 void commitCachedBytecode () const override
10051006 {
1006- #if OS(DARWIN)
10071007 if (!cacheEnabled () || !m_cachedBytecode || !m_cachedBytecode->hasUpdates ())
10081008 return ;
10091009
@@ -1012,32 +1012,33 @@ class ShellSourceProvider : public StringSourceProvider {
10121012 });
10131013
10141014 String filename = cachePath ();
1015- int fd = open (filename. utf8 (). data (), O_CREAT | O_WRONLY | O_TRUNC | O_EXLOCK | O_NONBLOCK , 0666 );
1016- if (fd == - 1 )
1015+ auto fd = FileSystem::openAndLockFile (filename, FileSystem::FileOpenMode::Write, {FileSystem::FileLockMode::Exclusive, FileSystem::FileLockMode::Nonblocking} );
1016+ if (! FileSystem::isHandleValid (fd) )
10171017 return ;
10181018
10191019 auto closeFD = makeScopeExit ([&] {
1020- close (fd);
1020+ FileSystem::unlockAndCloseFile (fd);
10211021 });
10221022
1023- struct stat sb;
1024- int res = fstat (fd, &sb);
1025- size_t size = static_cast <size_t >(sb.st_size );
1026- if (res || size != m_cachedBytecode->size ()) {
1023+ long long fileSize;
1024+ if (!FileSystem::getFileSize (fd, fileSize))
1025+ return ;
1026+
1027+ size_t cacheFileSize;
1028+ if (!WTF::convertSafely (fileSize, cacheFileSize) || cacheFileSize != m_cachedBytecode->size ()) {
10271029 // The bytecode cache has already been updated
10281030 return ;
10291031 }
10301032
1031- if (ftruncate (fd, m_cachedBytecode->sizeForUpdate ()))
1033+ if (! FileSystem::truncateFile (fd, m_cachedBytecode->sizeForUpdate ()))
10321034 return ;
10331035
10341036 m_cachedBytecode->commitUpdates ([&] (off_t offset, const void * data, size_t size) {
1035- off_t result = lseek (fd, offset, SEEK_SET );
1037+ long long result = FileSystem::seekFile (fd, offset, FileSystem::FileSeekOrigin::Beginning );
10361038 ASSERT_UNUSED (result, result != -1 );
1037- size_t bytesWritten = static_cast <size_t >(write (fd, data, size));
1039+ size_t bytesWritten = static_cast <size_t >(FileSystem::writeToFile (fd, static_cast < const char *>( data) , size));
10381040 ASSERT_UNUSED (bytesWritten, bytesWritten == size);
10391041 });
1040- #endif
10411042 }
10421043
10431044private:
@@ -1046,40 +1047,34 @@ class ShellSourceProvider : public StringSourceProvider {
10461047 if (!cacheEnabled ())
10471048 return static_cast <const char *>(nullptr );
10481049 const char * cachePath = Options::diskCachePath ();
1049- String filename = sourceOrigin ().string ();
1050- filename.replace (' /' , ' _' );
1051- return makeString (cachePath, ' /' , source ().toString ().hash (), ' -' , filename, " .bytecode-cache" );
1050+ String filename = FileSystem::encodeForFileName (sourceOrigin ().string ());
1051+ return FileSystem::pathByAppendingComponent (cachePath, makeString (source ().toString ().hash (), ' -' , filename, " .bytecode-cache" ));
10521052 }
10531053
10541054 void loadBytecode () const
10551055 {
1056- #if OS(DARWIN)
10571056 if (!cacheEnabled ())
10581057 return ;
10591058
10601059 String filename = cachePath ();
10611060 if (filename.isNull ())
10621061 return ;
10631062
1064- int fd = open (filename. utf8 (). data (), O_RDONLY | O_SHLOCK | O_NONBLOCK );
1065- if (fd == - 1 )
1063+ auto fd = FileSystem::openAndLockFile (filename, FileSystem::FileOpenMode::Read, {FileSystem::FileLockMode::Shared, FileSystem::FileLockMode::Nonblocking} );
1064+ if (! FileSystem::isHandleValid (fd) )
10661065 return ;
10671066
10681067 auto closeFD = makeScopeExit ([&] {
1069- close (fd);
1068+ FileSystem::unlockAndCloseFile (fd);
10701069 });
10711070
1072- struct stat sb;
1073- int res = fstat (fd, &sb);
1074- size_t size = static_cast <size_t >(sb.st_size );
1075- if (res || !size)
1076- return ;
1071+ bool success;
1072+ FileSystem::MappedFileData mappedFileData (fd, FileSystem::MappedFileMode::Private, success);
10771073
1078- void * buffer = mmap (nullptr , size, PROT_READ , MAP_PRIVATE , fd, 0 );
1079- if (buffer == MAP_FAILED )
1074+ if (!success)
10801075 return ;
1081- m_cachedBytecode = CachedBytecode::create (buffer, size);
1082- # endif
1076+
1077+ m_cachedBytecode = CachedBytecode::create ( WTFMove (mappedFileData));
10831078 }
10841079
10851080 ShellSourceProvider (const String& source, const SourceOrigin& sourceOrigin, URL && url, const TextPosition& startPosition, SourceProviderSourceType sourceType)
0 commit comments