LayoutTests/ChangeLog

 12020-02-18 Christopher Reid <chris.reid@sony.com>
 2
 3 [Win] Implement NetworkCache::Data by using FileSystem::MappedFileData
 4 https://bugs.webkit.org/show_bug.cgi?id=197684
 5 <rdar://problem/59467397>
 6
 7 Reviewed by NOBODY (OOPS!).
 8
 9 * platform/wincairo/TestExpectations:
 10
1112020-02-17 Dean Jackson <dino@apple.com>
212
313 [WebGL] Update WebGL2 results with ANGLE backend

LayoutTests/platform/wincairo/TestExpectations

@@crypto/subtle/wrap-key-malformed-parameters.html [ Skip ]
11611161
11621162http/tests/appcache [ Skip ]
11631163http/tests/blink/sendbeacon [ Skip ]
1164 http/tests/cache [ Skip ]
 1164
 1165# needs more investigation
 1166http/tests/cache/cancel-multiple-post-xhrs.html [ Failure ]
 1167
 1168# fails because of conversion to epoch behavior specific to Mac
 1169http/tests/cache/disk-cache/disk-cache-last-modified.html [ Failure ]
 1170
 1171# needs more investigation
 1172http/tests/cache/iframe-304-crash.html [ Failure ]
 1173http/tests/cache/network-error-during-revalidation.html [ Failure ]
 1174http/tests/cache/partitioned-cache-iframe.html [ Failure ]
 1175http/tests/cache/partitioned-cache.html [ Failure ]
 1176http/tests/cache/willsendrequest-returns-null-for-memory-cache-load.html [ Failure ]
 1177
11651178http/tests/cache-storage [ Skip ]
11661179http/tests/canvas [ Skip ]
11671180http/tests/contentdispositionattachmentsandbox [ Skip ]

Source/WTF/ChangeLog

 12020-02-18 Christopher Reid <chris.reid@sony.com>
 2
 3 [Win] Implement NetworkCache::Data by using FileSystem::MappedFileData
 4 https://bugs.webkit.org/show_bug.cgi?id=197684
 5 <rdar://problem/59467397>
 6
 7 Reviewed by NOBODY (OOPS!).
 8
 9 * wtf/FileSystem.cpp:
 10 * wtf/FileSystem.h:
 11 Added FileAccessPermission flag when opening files.
 12 Remove default argument for the listDirectory filter since the defaut
 13 String() filter doesn't match all files on Mac and Windows.
 14
 15 Added FileOpenMode::ReadWrite to be used with ReadWrite MappedFileData.
 16
 17 * wtf/glib/FileSystemGlib.cpp:
 18 * wtf/posix/FileSystemPOSIX.cpp:
 19 Added (S_IRUSR | S_IWUSR) file open modes.
 20
 21 * wtf/win/FileSystemWin.cpp:
 22 Implement getVolumeFreeSpace since some of the tests use it when toggling cache.
 23
 24 * wtf/win/PathWalker.cpp:
 25
1262020-02-18 Simon Fraser <simon.fraser@apple.com>
227
328 Move from "layer flush" terminology to "rendering update"

Source/WTF/wtf/FileSystem.cpp

@@MappedFileData::~MappedFileData()
287287 unmapViewOfFile(m_fileData, m_fileSize);
288288}
289289
290 #if HAVE(MMAP)
291 
292 MappedFileData::MappedFileData(const String& filePath, MappedFileMode mode, bool& success)
 290MappedFileData::MappedFileData(const String& filePath, MappedFileMode mapMode, bool& success)
293291{
294  auto fd = openFile(filePath, FileOpenMode::Read);
 292 auto fd = openFile(filePath, FileSystem::FileOpenMode::Read);
295293
296  success = mapFileHandle(fd, mode);
 294 success = mapFileHandle(fd, FileSystem::FileOpenMode::Read, mapMode);
297295 closeFile(fd);
298296}
299297
300 bool MappedFileData::mapFileHandle(PlatformFileHandle handle, MappedFileMode mode)
 298#if HAVE(MMAP)
 299
 300bool MappedFileData::mapFileHandle(PlatformFileHandle handle, FileOpenMode openMode, MappedFileMode mapMode)
301301{
302302 if (!isHandleValid(handle))
303303 return false;

@@bool MappedFileData::mapFileHandle(PlatformFileHandle handle, MappedFileMode mod
324324 return true;
325325 }
326326
327  void* data = mmap(0, size, PROT_READ, MAP_FILE | (mode == MappedFileMode::Shared ? MAP_SHARED : MAP_PRIVATE), fd, 0);
 327 int pageProtection = PROT_READ;
 328 switch (openMode) {
 329 case FileOpenMode::Read:
 330 pageProtection = PROT_READ;
 331 break;
 332 case FileOpenMode::Write:
 333 pageProtection = PROT_WRITE;
 334 break;
 335 case FileOpenMode::ReadWrite:
 336 pageProtection = PROT_READ | PROT_WRITE;
 337 break;
 338#if OS(DARWIN)
 339 case FileOpenMode::EventsOnly:
 340 ASSERT_NOT_REACHED();
 341#endif
 342 }
 343
 344 void* data = mmap(0, size, pageProtection, MAP_FILE | (mapMode == MappedFileMode::Shared ? MAP_SHARED : MAP_PRIVATE), fd, 0);
328345
329346 if (data == MAP_FAILED) {
330347 return false;

Source/WTF/wtf/FileSystem.h

@@const PlatformFileHandle invalidPlatformFileHandle = -1;
7979enum class FileOpenMode {
8080 Read,
8181 Write,
 82 ReadWrite,
8283#if OS(DARWIN)
8384 EventsOnly,
8485#endif
8586};
8687
 88enum class FileAccessPermission : bool {
 89 User,
 90 All
 91};
 92
8793enum class FileSeekOrigin {
8894 Beginning,
8995 Current,

@@WTF_EXPORT_PRIVATE void setMetadataURL(const String& path, const String& urlStri
131137bool canExcludeFromBackup(); // Returns true if any file can ever be excluded from backup.
132138bool excludeFromBackup(const String&); // Returns true if successful.
133139
134 WTF_EXPORT_PRIVATE Vector<String> listDirectory(const String& path, const String& filter = String());
 140WTF_EXPORT_PRIVATE Vector<String> listDirectory(const String& path, const String& filter);
135141
136142WTF_EXPORT_PRIVATE CString fileSystemRepresentation(const String&);
137143String stringFromFileSystemRepresentation(const char*);

@@inline bool isHandleValid(const PlatformFileHandle& handle) { return handle != i
140146
141147// Prefix is what the filename should be prefixed with, not the full path.
142148WTF_EXPORT_PRIVATE String openTemporaryFile(const String& prefix, PlatformFileHandle&);
143 WTF_EXPORT_PRIVATE PlatformFileHandle openFile(const String& path, FileOpenMode);
 149WTF_EXPORT_PRIVATE PlatformFileHandle openFile(const String& path, FileOpenMode, FileAccessPermission = FileAccessPermission::All);
144150WTF_EXPORT_PRIVATE void closeFile(PlatformFileHandle&);
145151// Returns the resulting offset from the beginning of the file if successful, -1 otherwise.
146152WTF_EXPORT_PRIVATE long long seekFile(PlatformFileHandle, long long offset, FileSeekOrigin);

@@public:
206212 MappedFileData(MappedFileData&&);
207213 WTF_EXPORT_PRIVATE MappedFileData(const String& filePath, MappedFileMode, bool& success);
208214 WTF_EXPORT_PRIVATE MappedFileData(PlatformFileHandle, MappedFileMode, bool& success);
 215 WTF_EXPORT_PRIVATE MappedFileData(PlatformFileHandle, FileOpenMode, MappedFileMode, bool& success);
209216 WTF_EXPORT_PRIVATE ~MappedFileData();
210217 MappedFileData& operator=(MappedFileData&&);
211218

@@public:
216223 void* leakHandle() { return std::exchange(m_fileData, nullptr); }
217224
218225private:
219  WTF_EXPORT_PRIVATE bool mapFileHandle(PlatformFileHandle, MappedFileMode);
 226 WTF_EXPORT_PRIVATE bool mapFileHandle(PlatformFileHandle, FileOpenMode, MappedFileMode);
220227
221228 void* m_fileData { nullptr };
222229 unsigned m_fileSize { 0 };
223230};
224231
225 inline MappedFileData::MappedFileData(PlatformFileHandle handle, MappedFileMode mode, bool& success)
 232inline MappedFileData::MappedFileData(PlatformFileHandle handle, MappedFileMode mapMode, bool& success)
 233{
 234 success = mapFileHandle(handle, FileOpenMode::Read, mapMode);
 235}
 236
 237inline MappedFileData::MappedFileData(PlatformFileHandle handle, FileOpenMode openMode, MappedFileMode mapMode, bool& success)
226238{
227  success = mapFileHandle(handle, mode);
 239 success = mapFileHandle(handle, openMode, mapMode);
228240}
229241
230242inline MappedFileData::MappedFileData(MappedFileData&& other)

Source/WTF/wtf/glib/FileSystemGlib.cpp

@@String openTemporaryFile(const String& prefix, PlatformFileHandle& handle)
338338 return String::fromUTF8(tempPath.get());
339339}
340340
341 PlatformFileHandle openFile(const String& path, FileOpenMode mode)
 341PlatformFileHandle openFile(const String& path, FileOpenMode mode, FileAccessPermission permission)
342342{
343343 auto filename = fileSystemRepresentation(path);
344344 if (!validRepresentation(filename))

@@PlatformFileHandle openFile(const String& path, FileOpenMode mode)
348348 GRefPtr<GFileIOStream> ioStream;
349349 if (mode == FileOpenMode::Read)
350350 ioStream = adoptGRef(g_file_open_readwrite(file.get(), nullptr, nullptr));
351  else if (mode == FileOpenMode::Write) {
 351 else if (mode == FileOpenMode::Write || mode == FileOpenMode::ReadWrite) {
352352 if (g_file_test(filename.data(), static_cast<GFileTest>(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)))
353353 ioStream = adoptGRef(g_file_open_readwrite(file.get(), nullptr, nullptr));
354  else
355  ioStream = adoptGRef(g_file_create_readwrite(file.get(), G_FILE_CREATE_NONE, nullptr, nullptr));
 354 else {
 355 GFileCreateFlags permissionFlag = (permission == FileAccessPermission::All) ? G_FILE_CREATE_NONE : G_FILE_CREATE_PRIVATE;
 356 ioStream = adoptGRef(g_file_create_readwrite(file.get(), permissionFlag, nullptr, nullptr));
 357 }
356358 }
357359
358360 return ioStream.leakRef();

Source/WTF/wtf/posix/FileSystemPOSIX.cpp

@@bool deleteFile(const String& path)
7979 return unlinked;
8080}
8181
82 PlatformFileHandle openFile(const String& path, FileOpenMode mode)
 82PlatformFileHandle openFile(const String& path, FileOpenMode mode, FileAccessPermission permission)
8383{
8484 CString fsRep = fileSystemRepresentation(path);
8585

@@PlatformFileHandle openFile(const String& path, FileOpenMode mode)
8787 return invalidPlatformFileHandle;
8888
8989 int platformFlag = 0;
90  if (mode == FileOpenMode::Read)
 90 switch (mode) {
 91 case FileOpenMode::Read:
9192 platformFlag |= O_RDONLY;
92  else if (mode == FileOpenMode::Write)
 93 break;
 94 case FileOpenMode::Write:
9395 platformFlag |= (O_WRONLY | O_CREAT | O_TRUNC);
 96 break;
 97 case FileOpenMode::ReadWrite:
 98 platformFlag |= (O_RDWR | O_CREAT);
 99 break;
94100#if OS(DARWIN)
95  else if (mode == FileOpenMode::EventsOnly)
 101 case FileOpenMode::EventsOnly:
96102 platformFlag |= O_EVTONLY;
 103 break;
97104#endif
 105 }
 106
 107 int permissionFlag = 0;
 108 if (permission == FileAccessPermission::User)
 109 permissionFlag |= (S_IRUSR | S_IWUSR);
 110 else if (permission == FileAccessPermission::All)
 111 permissionFlag |= (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
98112
99  return open(fsRep.data(), platformFlag, 0666);
 113 return open(fsRep.data(), platformFlag, permissionFlag);
100114}
101115
102116void closeFile(PlatformFileHandle& handle)

Source/WTF/wtf/win/FileSystemWin.cpp

@@String openTemporaryFile(const String&, PlatformFileHandle& handle)
421421 return proposedPath;
422422}
423423
424 PlatformFileHandle openFile(const String& path, FileOpenMode mode)
 424PlatformFileHandle openFile(const String& path, FileOpenMode mode, FileAccessPermission)
425425{
426426 DWORD desiredAccess = 0;
427427 DWORD creationDisposition = 0;

@@PlatformFileHandle openFile(const String& path, FileOpenMode mode)
436436 desiredAccess = GENERIC_WRITE;
437437 creationDisposition = CREATE_ALWAYS;
438438 break;
439  default:
440  ASSERT_NOT_REACHED();
 439 case FileOpenMode::ReadWrite:
 440 desiredAccess = GENERIC_READ | GENERIC_WRITE;
 441 creationDisposition = OPEN_ALWAYS;
 442 break;
441443 }
442444
443445 String destination = path;

@@Vector<String> listDirectory(const String& directory, const String& filter)
549551 return entries;
550552}
551553
552 bool getVolumeFreeSpace(const String&, uint64_t&)
 554bool getVolumeFreeSpace(const String& path, uint64_t& freeSpace)
553555{
554  return false;
 556 ULARGE_INTEGER freeBytesAvailableToCaller;
 557 if (!GetDiskFreeSpaceExW(path.wideCharacters().data(), &freeBytesAvailableToCaller, nullptr, nullptr))
 558 return false;
 559
 560 freeSpace = freeBytesAvailableToCaller.QuadPart;
 561 return true;
555562}
556563
557564Optional<int32_t> getFileDeviceId(const CString& fsFile)

@@bool unmapViewOfFile(void* buffer, size_t)
603610 return UnmapViewOfFile(buffer);
604611}
605612
606 MappedFileData::MappedFileData(const String& filePath, MappedFileMode mode, bool& success)
607 {
608  auto file = CreateFile(filePath.wideCharacters().data(), GENERIC_READ, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
609 
610  success = mapFileHandle(file, mode);
611  closeFile(file);
612 }
613 
614 bool MappedFileData::mapFileHandle(PlatformFileHandle handle, MappedFileMode)
 613bool MappedFileData::mapFileHandle(PlatformFileHandle handle, FileOpenMode openMode, MappedFileMode)
615614{
616615 if (!isHandleValid(handle))
617616 return false;

@@bool MappedFileData::mapFileHandle(PlatformFileHandle handle, MappedFileMode)
625624 return true;
626625 }
627626
628  auto mapping = CreateFileMapping(handle, nullptr, PAGE_READONLY, 0, 0, nullptr);
 627 DWORD pageProtection = PAGE_READONLY;
 628 DWORD desiredAccess = FILE_MAP_READ;
 629 switch (openMode) {
 630 case FileOpenMode::Read:
 631 pageProtection = PAGE_READONLY;
 632 desiredAccess = FILE_MAP_READ;
 633 break;
 634 case FileOpenMode::Write:
 635 pageProtection = PAGE_READWRITE;
 636 desiredAccess = FILE_MAP_WRITE;
 637 break;
 638 case FileOpenMode::ReadWrite:
 639 pageProtection = PAGE_READWRITE;
 640 desiredAccess = FILE_MAP_WRITE | FILE_MAP_READ;
 641 break;
 642 }
 643
 644 auto mapping = CreateFileMapping(handle, nullptr, pageProtection, 0, 0, nullptr);
629645 if (!mapping)
630646 return false;
631647
632  m_fileData = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, size);
 648 m_fileData = MapViewOfFile(mapping, desiredAccess, 0, 0, size);
633649 CloseHandle(mapping);
634650 if (!m_fileData)
635651 return false;

Source/WTF/wtf/win/PathWalker.cpp

@@namespace WTF {
3232
3333PathWalker::PathWalker(const String& directory, const String& pattern)
3434{
35  String path = directory + "\\" + pattern;
 35 String path = makeString(directory, '\\', pattern);
3636 m_handle = ::FindFirstFileW(path.wideCharacters().data(), &m_data);
3737}
3838

Source/WebKit/ChangeLog

 12020-02-18 Christopher Reid <chris.reid@sony.com>
 2
 3 [Win] Implement NetworkCache::Data by using FileSystem::MappedFileData
 4 https://bugs.webkit.org/show_bug.cgi?id=197684
 5 <rdar://problem/59467397>
 6
 7 Reviewed by NOBODY (OOPS!).
 8
 9 * NetworkProcess/NetworkProcess.cpp:
 10 Ensure that the CacheStorage directory is actually being created.
 11
 12 * NetworkProcess/cache/NetworkCacheData.cpp:
 13 * NetworkProcess/cache/NetworkCacheData.h:
 14 * NetworkProcess/cache/NetworkCacheDataCocoa.mm:
 15 * NetworkProcess/cache/NetworkCacheDataSoup.cpp:
 16 * NetworkProcess/cache/NetworkCacheFileSystem.cpp:
 17 Use more FileSystem functionality to share code across platforms.
 18
 19 * NetworkProcess/cache/NetworkCacheDataCurl.cpp:
 20 Use Optional<Vector> for m_buffer since we need to differentiate isEmpty and isNull.
 21
1222020-02-18 Simon Fraser <simon.fraser@apple.com>
223
324 Move from "layer flush" terminology to "rendering update"

Source/WebKit/NetworkProcess/NetworkProcess.cpp

@@void NetworkProcess::addSessionStorageQuotaManager(PAL::SessionID sessionID, uin
454454 auto isNewEntry = m_sessionStorageQuotaManagers.ensure(sessionID, [defaultQuota, defaultThirdPartyQuota, &cacheRootPath] {
455455 return makeUnique<SessionStorageQuotaManager>(cacheRootPath, defaultQuota, defaultThirdPartyQuota);
456456 }).isNewEntry;
457  if (isNewEntry)
 457 if (isNewEntry) {
458458 SandboxExtension::consumePermanently(cacheRootPathHandle);
 459 if (!cacheRootPath.isEmpty())
 460 postStorageTask(createCrossThreadTask(*this, &NetworkProcess::ensurePathExists, cacheRootPath));
 461 }
459462}
460463
461464void NetworkProcess::removeSessionStorageQuotaManager(PAL::SessionID sessionID)

Source/WebKit/NetworkProcess/cache/NetworkCacheData.cpp

2828
2929#include <fcntl.h>
3030#include <wtf/CryptographicallyRandomNumber.h>
31 #include <wtf/FileSystem.h>
3231
3332#if !OS(WINDOWS)
3433#include <sys/mman.h>

3938namespace WebKit {
4039namespace NetworkCache {
4140
42 #if !OS(WINDOWS)
4341Data Data::mapToFile(const String& path) const
4442{
45  int fd = open(FileSystem::fileSystemRepresentation(path).data(), O_CREAT | O_EXCL | O_RDWR , S_IRUSR | S_IWUSR);
46  if (fd < 0)
47  return { };
48 
49  if (ftruncate(fd, m_size) < 0) {
50  close(fd);
 43 auto handle = FileSystem::openFile(path, FileSystem::FileOpenMode::ReadWrite, FileSystem::FileAccessPermission::User);
 44 if (!FileSystem::truncateFile(handle, m_size)) {
 45 FileSystem::closeFile(handle);
5146 return { };
5247 }
5348
5449 FileSystem::makeSafeToUseMemoryMapForPath(path);
55 
56  void* map = mmap(nullptr, m_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
57  if (map == MAP_FAILED) {
58  close(fd);
 50 bool success;
 51 FileSystem::MappedFileData mappedFile(handle, FileSystem::FileOpenMode::ReadWrite, FileSystem::MappedFileMode::Shared, success);
 52 if (!success) {
 53 FileSystem::closeFile(handle);
5954 return { };
6055 }
6156
 57 void* map = const_cast<void*>(mappedFile.data());
6258 uint8_t* mapData = static_cast<uint8_t*>(map);
6359 apply([&mapData](const uint8_t* bytes, size_t bytesSize) {
6460 memcpy(mapData, bytes, bytesSize);

@@Data Data::mapToFile(const String& path) const
6662 return true;
6763 });
6864
 65#if OS(WINDOWS)
 66 DWORD oldProtection;
 67 VirtualProtect(map, m_size, FILE_MAP_READ, &oldProtection);
 68 FlushViewOfFile(map, m_size);
 69#else
6970 // Drop the write permission.
7071 mprotect(map, m_size, PROT_READ);
7172
7273 // Flush (asynchronously) to file, turning this into clean memory.
7374 msync(map, m_size, MS_ASYNC);
74 
75  return Data::adoptMap(map, m_size, fd);
76 }
77 #else
78 Data Data::mapToFile(const String& path) const
79 {
80  auto file = FileSystem::openFile(path, FileSystem::FileOpenMode::Write);
81  if (!FileSystem::isHandleValid(file))
82  return { };
83  if (FileSystem::writeToFile(file, reinterpret_cast<const char*>(data()), size()) < 0)
84  return { };
85  return Data(Vector<uint8_t>(m_buffer));
86 }
8775#endif
8876
89 #if !OS(WINDOWS)
90 Data mapFile(const char* path)
91 {
92  int fd = open(path, O_RDONLY, 0);
93  if (fd < 0)
94  return { };
95  struct stat stat;
96  if (fstat(fd, &stat) < 0) {
97  close(fd);
98  return { };
99  }
100  size_t size = stat.st_size;
101  if (!size) {
102  close(fd);
103  return Data::empty();
104  }
105 
106  return adoptAndMapFile(fd, 0, size);
 77 return Data::adoptMap(WTFMove(mappedFile), handle);
10778}
108 #endif
10979
110 Data mapFile(const String& path)
 80Data mapFile(const char* path)
11181{
112 #if !OS(WINDOWS)
113  return mapFile(FileSystem::fileSystemRepresentation(path).data());
114 #else
11582 auto file = FileSystem::openFile(path, FileSystem::FileOpenMode::Read);
11683 if (!FileSystem::isHandleValid(file))
11784 return { };

@@Data mapFile(const String& path)
11986 if (!FileSystem::getFileSize(file, size))
12087 return { };
12188 return adoptAndMapFile(file, 0, size);
122 #endif
12389}
12490
125 #if !OS(WINDOWS)
126 Data adoptAndMapFile(int fd, size_t offset, size_t size)
 91Data mapFile(const String& path)
 92{
 93 return mapFile(FileSystem::fileSystemRepresentation(path).data());
 94}
 95
 96Data adoptAndMapFile(FileSystem::PlatformFileHandle handle, size_t offset, size_t size)
12797{
12898 if (!size) {
129  close(fd);
 99 FileSystem::closeFile(handle);
130100 return Data::empty();
131101 }
132 
133  void* map = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, offset);
134  if (map == MAP_FAILED) {
135  close(fd);
 102 bool success;
 103 FileSystem::MappedFileData mappedFile(handle, FileSystem::FileOpenMode::Read, FileSystem::MappedFileMode::Private, success);
 104 if (!success) {
 105 FileSystem::closeFile(handle);
136106 return { };
137107 }
138108
139  return Data::adoptMap(map, size, fd);
 109 return Data::adoptMap(WTFMove(mappedFile), handle);
140110}
141 #else
142 Data adoptAndMapFile(FileSystem::PlatformFileHandle file, size_t offset, size_t size)
143 {
144  return Data(file, offset, size);
145 }
146 #endif
147111
148112SHA1::Digest computeSHA1(const Data& data, const Salt& salt)
149113{

@@static Salt makeSalt()
179143
180144Optional<Salt> readOrMakeSalt(const String& path)
181145{
182 #if !OS(WINDOWS)
183  auto cpath = FileSystem::fileSystemRepresentation(path);
184  auto fd = open(cpath.data(), O_RDONLY, 0);
185  Salt salt;
186  auto bytesRead = read(fd, salt.data(), salt.size());
187  close(fd);
188  if (bytesRead != static_cast<ssize_t>(salt.size())) {
189  salt = makeSalt();
190 
191  unlink(cpath.data());
192  fd = open(cpath.data(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
193  bool success = write(fd, salt.data(), salt.size()) == static_cast<ssize_t>(salt.size());
194  close(fd);
195  if (!success)
 146 if (fileExists(path)) {
 147 auto file = FileSystem::openFile(path, FileSystem::FileOpenMode::Read);
 148 Salt salt;
 149 auto bytesRead = FileSystem::readFromFile(file, reinterpret_cast<char*>(salt.data()), salt.size());
 150 FileSystem::closeFile(file);
 151 if (bytesRead != salt.size())
196152 return { };
 153
 154 return salt;
197155 }
198  return salt;
199 #else
200  auto file = FileSystem::openFile(path, FileSystem::FileOpenMode::Read);
201  Salt salt;
202  auto bytesRead = FileSystem::readFromFile(file, reinterpret_cast<char*>(salt.data()), salt.size());
 156
 157 Salt salt = makeSalt();
 158 auto file = FileSystem::openFile(path, FileSystem::FileOpenMode::Write, FileSystem::FileAccessPermission::User);
 159 bool success = FileSystem::writeToFile(file, reinterpret_cast<char*>(salt.data()), salt.size()) == salt.size();
203160 FileSystem::closeFile(file);
204  if (bytesRead != salt.size()) {
205  salt = makeSalt();
 161 if (!success)
 162 return { };
206163
207  FileSystem::deleteFile(path);
208  file = FileSystem::openFile(path, FileSystem::FileOpenMode::Write);
209  bool success = FileSystem::writeToFile(file, reinterpret_cast<char*>(salt.data()), salt.size()) == salt.size();
210  FileSystem::closeFile(file);
211  if (!success)
212  return { };
213  }
214164 return salt;
215 #endif
216165}
217166
218167} // namespace NetworkCache

Source/WebKit/NetworkProcess/cache/NetworkCacheData.h

3939#include <WebCore/GRefPtrSoup.h>
4040#endif
4141
 42#if USE(CURL)
 43#include <wtf/Box.h>
 44#include <wtf/Variant.h>
 45#endif
 46
4247namespace WebKit {
4348
4449class SharedMemory;

@@public:
5358 ~Data() { }
5459
5560 static Data empty();
56 #if !OS(WINDOWS)
57  static Data adoptMap(void* map, size_t, int fd);
58 #endif
 61 static Data adoptMap(FileSystem::MappedFileData&&, FileSystem::PlatformFileHandle);
5962
6063#if PLATFORM(COCOA)
6164 enum class Backing { Buffer, Map };
6265 Data(OSObjectPtr<dispatch_data_t>&&, Backing = Backing::Buffer);
6366#endif
6467#if USE(SOUP)
65  Data(GRefPtr<SoupBuffer>&&, int fd = -1);
66 #elif OS(WINDOWS)
67  explicit Data(Vector<uint8_t>&&);
68  Data(FileSystem::PlatformFileHandle, size_t offset, size_t);
 68 Data(GRefPtr<SoupBuffer>&&, FileSystem::PlatformFileHandle fd = FileSystem::invalidPlatformFileHandle);
 69#elif USE(CURL)
 70 Data(Variant<Vector<uint8_t>, FileSystem::MappedFileData>&&);
6971#endif
7072 bool isNull() const;
7173 bool isEmpty() const { return !m_size; }

@@private:
9496#endif
9597#if USE(SOUP)
9698 mutable GRefPtr<SoupBuffer> m_buffer;
97  int m_fileDescriptor { -1 };
 99 FileSystem::PlatformFileHandle m_fileDescriptor { FileSystem::invalidPlatformFileHandle };
98100#endif
99 #if OS(WINDOWS)
100  Vector<uint8_t> m_buffer;
 101#if USE(CURL)
 102 Box<Variant<Vector<uint8_t>, FileSystem::MappedFileData>> m_buffer;
101103#endif
102104 mutable const uint8_t* m_data { nullptr };
103105 size_t m_size { 0 };

@@private:
106108
107109Data concatenate(const Data&, const Data&);
108110bool bytesEqual(const Data&, const Data&);
109 #if !OS(WINDOWS)
110 Data adoptAndMapFile(int, size_t offset, size_t);
111 #else
112111Data adoptAndMapFile(FileSystem::PlatformFileHandle, size_t offset, size_t);
113 #endif
114 #if USE(GLIB) && !PLATFORM(WIN)
115 Data adoptAndMapFile(GFileIOStream*, size_t offset, size_t);
116 #endif
117 #if !OS(WINDOWS)
118112Data mapFile(const char* path);
119 #endif
120113Data mapFile(const String& path);
121114
122115using Salt = std::array<uint8_t, 8>;

Source/WebKit/NetworkProcess/cache/NetworkCacheDataCocoa.mm

@@Data concatenate(const Data& a, const Data& b)
9292 return { adoptOSObject(dispatch_data_create_concat(a.dispatchData(), b.dispatchData())) };
9393}
9494
95 Data Data::adoptMap(void* map, size_t size, int fd)
 95Data Data::adoptMap(FileSystem::MappedFileData&& mappedFile, FileSystem::PlatformFileHandle fd)
9696{
 97 size_t size = mappedFile.size();
 98 void* map = mappedFile.leakHandle();
9799 ASSERT(map);
98100 ASSERT(map != MAP_FAILED);
99  close(fd);
 101 FileSystem::closeFile(fd);
100102 auto bodyMap = adoptOSObject(dispatch_data_create(map, size, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), [map, size] {
101103 munmap(map, size);
102104 }));

Source/WebKit/NetworkProcess/cache/NetworkCacheDataCurl.cpp

@@namespace WebKit {
3030namespace NetworkCache {
3131
3232Data::Data(const uint8_t* data, size_t size)
 33 : m_buffer(Box<Variant<Vector<uint8_t>, FileSystem::MappedFileData>>::create(Vector<uint8_t>(size)))
 34 , m_size(size)
3335{
34  m_buffer.resize(size);
35  m_size = size;
36  memcpy(m_buffer.data(), data, size);
 36 memcpy(WTF::get<Vector<uint8_t>>(*m_buffer).data(), data, size);
3737}
3838
39 Data::Data(FileSystem::PlatformFileHandle file, size_t offset, size_t size)
 39Data::Data(Variant<Vector<uint8_t>, FileSystem::MappedFileData>&& data)
 40 : m_buffer(Box<Variant<Vector<uint8_t>, FileSystem::MappedFileData>>::create(WTFMove(data)))
 41 , m_isMap(WTF::holds_alternative<FileSystem::MappedFileData>(*m_buffer))
4042{
41  m_buffer.resize(size);
42  m_size = size;
43  FileSystem::seekFile(file, offset, FileSystem::FileSeekOrigin::Beginning);
44  FileSystem::readFromFile(file, reinterpret_cast<char*>(m_buffer.data()), size);
45  FileSystem::closeFile(file);
46 }
47 
48 Data::Data(Vector<uint8_t>&& buffer)
49  : m_buffer(WTFMove(buffer))
50 {
51  m_size = m_buffer.size();
 43 m_size = WTF::switchOn(*m_buffer,
 44 [](const Vector<uint8_t>& buffer) -> size_t { return buffer.size(); },
 45 [](const FileSystem::MappedFileData& mappedFile) -> size_t { return mappedFile.size(); }
 46 );
5247}
5348
5449Data Data::empty()
5550{
56  return { };
 51 Vector<uint8_t> buffer;
 52 return { WTFMove(buffer) };
5753}
5854
5955const uint8_t* Data::data() const
6056{
61  return m_buffer.data();
 57 if (!m_buffer)
 58 return nullptr;
 59
 60 return WTF::switchOn(*m_buffer,
 61 [](const Vector<uint8_t>& buffer) -> const uint8_t* { return buffer.data(); },
 62 [](const FileSystem::MappedFileData& mappedFile) -> const uint8_t* { return static_cast<const uint8_t*>(mappedFile.data()); }
 63 );
6264}
6365
6466bool Data::isNull() const
6567{
66  return m_buffer.isEmpty();
 68 return !m_buffer;
6769}
6870
6971bool Data::apply(const Function<bool(const uint8_t*, size_t)>& applier) const

@@bool Data::apply(const Function<bool(const uint8_t*, size_t)>& applier) const
7173 if (isEmpty())
7274 return false;
7375
74  return applier(reinterpret_cast<const uint8_t*>(m_buffer.data()), m_buffer.size());
 76 return applier(reinterpret_cast<const uint8_t*>(data()), size());
7577}
7678
7779Data Data::subrange(size_t offset, size_t size) const
7880{
79  return { m_buffer.data() + offset, size };
 81 if (!m_buffer)
 82 return { };
 83
 84 return { data() + offset, size };
8085}
8186
8287Data concatenate(const Data& a, const Data& b)
8388{
 89 if (a.isNull())
 90 return b;
 91 if (b.isNull())
 92 return a;
 93
8494 Vector<uint8_t> buffer(a.size() + b.size());
8595 memcpy(buffer.data(), a.data(), a.size());
8696 memcpy(buffer.data() + a.size(), b.data(), b.size());
8797 return Data(WTFMove(buffer));
8898}
8999
 100Data Data::adoptMap(FileSystem::MappedFileData&& mappedFile, FileSystem::PlatformFileHandle fd)
 101{
 102 ASSERT(mappedFile.data());
 103 FileSystem::closeFile(fd);
 104
 105 return { WTFMove(mappedFile) };
 106}
 107
90108} // namespace NetworkCache
91109} // namespace WebKit

Source/WebKit/NetworkProcess/cache/NetworkCacheDataSoup.cpp

@@Data::Data(const uint8_t* data, size_t size)
4848 m_buffer = adoptGRef(soup_buffer_new_with_owner(copiedData, size, copiedData, fastFree));
4949}
5050
51 Data::Data(GRefPtr<SoupBuffer>&& buffer, int fd)
 51Data::Data(GRefPtr<SoupBuffer>&& buffer, FileSystem::PlatformFileHandle fd)
5252 : m_buffer(buffer)
5353 , m_fileDescriptor(fd)
5454 , m_size(buffer ? buffer->length : 0)
55  , m_isMap(m_size && fd != -1)
 55 , m_isMap(m_size && FileSystem::isHandleValid(fd))
5656{
5757}
5858

@@struct MapWrapper {
108108 ~MapWrapper()
109109 {
110110 munmap(map, size);
111  close(fileDescriptor);
 111 FileSystem::closeFile(fileDescriptor);
112112 }
113113
114114 void* map;
115115 size_t size;
116  int fileDescriptor;
 116 FileSystem::PlatformFileHandle fileDescriptor;
117117};
118118
119119static void deleteMapWrapper(MapWrapper* wrapper)

@@static void deleteMapWrapper(MapWrapper* wrapper)
121121 delete wrapper;
122122}
123123
124 Data Data::adoptMap(void* map, size_t size, int fd)
 124Data Data::adoptMap(FileSystem::MappedFileData&& mappedFile, FileSystem::PlatformFileHandle fd)
125125{
 126 size_t size = mappedFile.size();
 127 void* map = mappedFile.leakHandle();
126128 ASSERT(map);
127129 ASSERT(map != MAP_FAILED);
128130 MapWrapper* wrapper = new MapWrapper { map, size, fd };

@@Data Data::adoptMap(void* map, size_t size, int fd)
130132 return { WTFMove(buffer), fd };
131133}
132134
133 #if USE(GLIB) && !PLATFORM(WIN)
134 Data adoptAndMapFile(GFileIOStream* stream, size_t offset, size_t size)
135 {
136  GInputStream* inputStream = g_io_stream_get_input_stream(G_IO_STREAM(stream));
137  int fd = g_file_descriptor_based_get_fd(G_FILE_DESCRIPTOR_BASED(inputStream));
138  return adoptAndMapFile(fd, offset, size);
139 }
140 #endif
141 
142135RefPtr<SharedMemory> Data::tryCreateSharedMemory() const
143136{
144137 if (isNull() || !isMap())
145138 return nullptr;
146139
147  return SharedMemory::wrapMap(const_cast<char*>(m_buffer->data), m_buffer->length, m_fileDescriptor);
 140 GInputStream* inputStream = g_io_stream_get_input_stream(G_IO_STREAM(m_fileDescriptor));
 141 int fd = g_file_descriptor_based_get_fd(G_FILE_DESCRIPTOR_BASED(inputStream));
 142 return SharedMemory::wrapMap(const_cast<char*>(m_buffer->data), m_buffer->length, fd);
148143}
149144
150145} // namespace NetworkCache

Source/WebKit/NetworkProcess/cache/NetworkCacheFileSystem.cpp

5353namespace WebKit {
5454namespace NetworkCache {
5555
56 #if !OS(WINDOWS)
57 static DirectoryEntryType directoryEntryType(uint8_t dtype)
58 {
59  switch (dtype) {
60  case DT_DIR:
61  return DirectoryEntryType::Directory;
62  case DT_REG:
63  return DirectoryEntryType::File;
64  default:
65  ASSERT_NOT_REACHED();
66  return DirectoryEntryType::File;
67  }
68  return DirectoryEntryType::File;
69 }
70 #endif
71 
7256void traverseDirectory(const String& path, const Function<void (const String&, DirectoryEntryType)>& function)
7357{
74 #if !OS(WINDOWS)
75  DIR* dir = opendir(FileSystem::fileSystemRepresentation(path).data());
76  if (!dir)
77  return;
78  dirent* dp;
79  while ((dp = readdir(dir))) {
80  if (dp->d_type != DT_DIR && dp->d_type != DT_REG)
81  continue;
82  const char* name = dp->d_name;
83  if (!strcmp(name, ".") || !strcmp(name, ".."))
84  continue;
85  auto nameString = String::fromUTF8(name);
86  if (nameString.isNull())
87  continue;
88  function(nameString, directoryEntryType(dp->d_type));
89  }
90  closedir(dir);
91 #else
92  auto entries = FileSystem::listDirectory(path);
 58 auto entries = FileSystem::listDirectory(path, "*"_s);
9359 for (auto& entry : entries) {
9460 auto type = FileSystem::fileIsDirectory(entry, FileSystem::ShouldFollowSymbolicLinks::No) ? DirectoryEntryType::Directory : DirectoryEntryType::File;
95  function(entry, type);
 61 function(FileSystem::pathGetFileName(entry), type);
9662 }
97 #endif
9863}
9964
10065void deleteDirectoryRecursively(const String& path)