Changeset 257518 in webkit


Ignore:
Timestamp:
Feb 26, 2020 2:41:40 PM (4 years ago)
Author:
chris.reid@sony.com
Message:

[Win] Implement NetworkCache::Data by using FileSystem::MappedFileData
https://bugs.webkit.org/show_bug.cgi?id=197684
<rdar://problem/59467397>

Reviewed by Yusuke Suzuki.

Source/WebKit:

  • NetworkProcess/NetworkProcess.cpp:

Ensure that the CacheStorage directory is actually being created.

  • NetworkProcess/cache/NetworkCacheData.cpp:
  • NetworkProcess/cache/NetworkCacheData.h:
  • NetworkProcess/cache/NetworkCacheDataCocoa.mm:
  • NetworkProcess/cache/NetworkCacheDataSoup.cpp:
  • NetworkProcess/cache/NetworkCacheFileSystem.cpp:

Use more FileSystem functionality to share code across platforms.

  • NetworkProcess/cache/NetworkCacheDataCurl.cpp:

Use Optional<Vector> for m_buffer since we need to differentiate isEmpty and isNull.

Source/WTF:

  • wtf/FileSystem.cpp:
  • wtf/FileSystem.h:

Added FileAccessPermission flag when opening files.
Remove default argument for the listDirectory filter since the defaut
String() filter doesn't match all files on Mac and Windows.

Added FileOpenMode::ReadWrite to be used with ReadWrite MappedFileData.
Added failIfFileExists flag to openFile.

  • wtf/glib/FileSystemGlib.cpp:
  • wtf/posix/FileSystemPOSIX.cpp:

Added (S_IRUSR | S_IWUSR) file open modes.

  • wtf/win/FileSystemWin.cpp:

Implement getVolumeFreeSpace since some of the tests use it when toggling cache.

  • wtf/win/PathWalker.cpp:

Tools:

Add test for FileSystem::createFile

  • TestWebKitAPI/Tests/WTF/FileSystem.cpp:

LayoutTests:

  • platform/wincairo/TestExpectations:
Location:
trunk
Files:
19 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r257517 r257518  
     12020-02-26  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 Yusuke Suzuki.
     8
     9        * platform/wincairo/TestExpectations:
     10
    1112020-02-26  Jason Lawrence  <lawrence.j@apple.com>
    212
  • trunk/LayoutTests/platform/wincairo/TestExpectations

    r256700 r257518  
    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 ]
  • trunk/Source/WTF/ChangeLog

    r257410 r257518  
     12020-02-26  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 Yusuke Suzuki.
     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        Added failIfFileExists flag to openFile.
     17
     18        * wtf/glib/FileSystemGlib.cpp:
     19        * wtf/posix/FileSystemPOSIX.cpp:
     20        Added (S_IRUSR | S_IWUSR) file open modes.
     21
     22        * wtf/win/FileSystemWin.cpp:
     23        Implement getVolumeFreeSpace since some of the tests use it when toggling cache.
     24       
     25        * wtf/win/PathWalker.cpp:
     26
    1272020-02-25  Devin Rousso  <drousso@apple.com>
    228
  • trunk/Source/WTF/wtf/FileSystem.cpp

    r256700 r257518  
    288288}
    289289
     290MappedFileData::MappedFileData(const String& filePath, MappedFileMode mapMode, bool& success)
     291{
     292    auto fd = openFile(filePath, FileSystem::FileOpenMode::Read);
     293
     294    success = mapFileHandle(fd, FileSystem::FileOpenMode::Read, mapMode);
     295    closeFile(fd);
     296}
     297
    290298#if HAVE(MMAP)
    291299
    292 MappedFileData::MappedFileData(const String& filePath, MappedFileMode mode, bool& success)
    293 {
    294     auto fd = openFile(filePath, FileOpenMode::Read);
    295 
    296     success = mapFileHandle(fd, mode);
    297     closeFile(fd);
    298 }
    299 
    300 bool MappedFileData::mapFileHandle(PlatformFileHandle handle, MappedFileMode mode)
     300bool MappedFileData::mapFileHandle(PlatformFileHandle handle, FileOpenMode openMode, MappedFileMode mapMode)
    301301{
    302302    if (!isHandleValid(handle))
     
    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) {
  • trunk/Source/WTF/wtf/FileSystem.h

    r256700 r257518  
    8080    Read,
    8181    Write,
     82    ReadWrite,
    8283#if OS(DARWIN)
    8384    EventsOnly,
    8485#endif
     86};
     87
     88enum class FileAccessPermission : bool {
     89    User,
     90    All
    8591};
    8692
     
    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&);
     
    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, bool failIfFileExists = false);
    144150WTF_EXPORT_PRIVATE void closeFile(PlatformFileHandle&);
    145151// Returns the resulting offset from the beginning of the file if successful, -1 otherwise.
     
    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&&);
     
    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 };
     
    223230};
    224231
    225 inline MappedFileData::MappedFileData(PlatformFileHandle handle, MappedFileMode mode, bool& success)
    226 {
    227     success = mapFileHandle(handle, mode);
     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)
     238{
     239    success = mapFileHandle(handle, openMode, mapMode);
    228240}
    229241
  • trunk/Source/WTF/wtf/glib/FileSystemGlib.cpp

    r256700 r257518  
    339339}
    340340
    341 PlatformFileHandle openFile(const String& path, FileOpenMode mode)
     341PlatformFileHandle openFile(const String& path, FileOpenMode mode, FileAccessPermission permission, bool failIfFileExists)
    342342{
    343343    auto filename = fileSystemRepresentation(path);
     
    347347    GRefPtr<GFile> file = adoptGRef(g_file_new_for_path(filename.data()));
    348348    GRefPtr<GFileIOStream> ioStream;
     349    GFileCreateFlags permissionFlag = (permission == FileAccessPermission::All) ? G_FILE_CREATE_NONE : G_FILE_CREATE_PRIVATE;
     350
     351    if (failIfFileExists) {
     352        ioStream = adoptGRef(g_file_create_readwrite(file.get(), permissionFlag, nullptr, nullptr));
     353        return ioStream.leakRef();
     354    }
     355
    349356    if (mode == FileOpenMode::Read)
    350357        ioStream = adoptGRef(g_file_open_readwrite(file.get(), nullptr, nullptr));
    351     else if (mode == FileOpenMode::Write) {
     358    else if (mode == FileOpenMode::Write || mode == FileOpenMode::ReadWrite) {
    352359        if (g_file_test(filename.data(), static_cast<GFileTest>(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)))
    353360            ioStream = adoptGRef(g_file_open_readwrite(file.get(), nullptr, nullptr));
    354361        else
    355             ioStream = adoptGRef(g_file_create_readwrite(file.get(), G_FILE_CREATE_NONE, nullptr, nullptr));
     362            ioStream = adoptGRef(g_file_create_readwrite(file.get(), permissionFlag, nullptr, nullptr));
    356363    }
    357364
  • trunk/Source/WTF/wtf/posix/FileSystemPOSIX.cpp

    r256700 r257518  
    8080}
    8181
    82 PlatformFileHandle openFile(const String& path, FileOpenMode mode)
     82PlatformFileHandle openFile(const String& path, FileOpenMode mode, FileAccessPermission permission, bool failIfFileExists)
    8383{
    8484    CString fsRep = fileSystemRepresentation(path);
     
    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
    98 
    99     return open(fsRep.data(), platformFlag, 0666);
     105    }
     106
     107    if (failIfFileExists)
     108        platformFlag |= (O_CREAT | O_EXCL);
     109
     110    int permissionFlag = 0;
     111    if (permission == FileAccessPermission::User)
     112        permissionFlag |= (S_IRUSR | S_IWUSR);
     113    else if (permission == FileAccessPermission::All)
     114        permissionFlag |= (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
     115
     116    return open(fsRep.data(), platformFlag, permissionFlag);
    100117}
    101118
  • trunk/Source/WTF/wtf/win/FileSystemWin.cpp

    r256700 r257518  
    422422}
    423423
    424 PlatformFileHandle openFile(const String& path, FileOpenMode mode)
     424PlatformFileHandle openFile(const String& path, FileOpenMode mode, FileAccessPermission, bool failIfFileExists)
    425425{
    426426    DWORD desiredAccess = 0;
     
    437437        creationDisposition = CREATE_ALWAYS;
    438438        break;
    439     default:
    440         ASSERT_NOT_REACHED();
    441     }
     439    case FileOpenMode::ReadWrite:
     440        desiredAccess = GENERIC_READ | GENERIC_WRITE;
     441        creationDisposition = OPEN_ALWAYS;
     442        break;
     443    }
     444
     445    if (failIfFileExists)
     446        creationDisposition = CREATE_NEW;
    442447
    443448    String destination = path;
     
    550555}
    551556
    552 bool getVolumeFreeSpace(const String&, uint64_t&)
    553 {
    554     return false;
     557bool getVolumeFreeSpace(const String& path, uint64_t& freeSpace)
     558{
     559    ULARGE_INTEGER freeBytesAvailableToCaller;
     560    if (!GetDiskFreeSpaceExW(path.wideCharacters().data(), &freeBytesAvailableToCaller, nullptr, nullptr))
     561        return false;
     562
     563    freeSpace = freeBytesAvailableToCaller.QuadPart;
     564    return true;
    555565}
    556566
     
    604614}
    605615
    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)
     616bool MappedFileData::mapFileHandle(PlatformFileHandle handle, FileOpenMode openMode, MappedFileMode)
    615617{
    616618    if (!isHandleValid(handle))
     
    626628    }
    627629
    628     auto mapping = CreateFileMapping(handle, nullptr, PAGE_READONLY, 0, 0, nullptr);
     630    DWORD pageProtection = PAGE_READONLY;
     631    DWORD desiredAccess = FILE_MAP_READ;
     632    switch (openMode) {
     633    case FileOpenMode::Read:
     634        pageProtection = PAGE_READONLY;
     635        desiredAccess = FILE_MAP_READ;
     636        break;
     637    case FileOpenMode::Write:
     638        pageProtection = PAGE_READWRITE;
     639        desiredAccess = FILE_MAP_WRITE;
     640        break;
     641    case FileOpenMode::ReadWrite:
     642        pageProtection = PAGE_READWRITE;
     643        desiredAccess = FILE_MAP_WRITE | FILE_MAP_READ;
     644        break;
     645    }
     646
     647    auto mapping = CreateFileMapping(handle, nullptr, pageProtection, 0, 0, nullptr);
    629648    if (!mapping)
    630649        return false;
    631650
    632     m_fileData = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, size);
     651    m_fileData = MapViewOfFile(mapping, desiredAccess, 0, 0, size);
    633652    CloseHandle(mapping);
    634653    if (!m_fileData)
  • trunk/Source/WTF/wtf/win/PathWalker.cpp

    r256700 r257518  
    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}
  • trunk/Source/WebKit/ChangeLog

    r257508 r257518  
     12020-02-26  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 Yusuke Suzuki.
     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-26  Per Arne Vollan  <pvollan@apple.com>
    223
  • trunk/Source/WebKit/NetworkProcess/NetworkProcess.cpp

    r257206 r257518  
    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
  • trunk/Source/WebKit/NetworkProcess/cache/NetworkCacheData.cpp

    r256700 r257518  
    2929#include <fcntl.h>
    3030#include <wtf/CryptographicallyRandomNumber.h>
    31 #include <wtf/FileSystem.h>
    3231
    3332#if !OS(WINDOWS)
     
    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    constexpr bool failIfFileExists = true;
     44    auto handle = FileSystem::openFile(path, FileSystem::FileOpenMode::ReadWrite, FileSystem::FileAccessPermission::User, failIfFileExists);
     45    if (!FileSystem::isHandleValid(handle) || !FileSystem::truncateFile(handle, m_size)) {
     46        FileSystem::closeFile(handle);
    5147        return { };
    5248    }
    5349   
    5450    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);
     51    bool success;
     52    FileSystem::MappedFileData mappedFile(handle, FileSystem::FileOpenMode::ReadWrite, FileSystem::MappedFileMode::Shared, success);
     53    if (!success) {
     54        FileSystem::closeFile(handle);
    5955        return { };
    6056    }
    6157
     58    void* map = const_cast<void*>(mappedFile.data());
    6259    uint8_t* mapData = static_cast<uint8_t*>(map);
    6360    apply([&mapData](const uint8_t* bytes, size_t bytesSize) {
     
    6764    });
    6865
     66#if OS(WINDOWS)
     67    DWORD oldProtection;
     68    VirtualProtect(map, m_size, FILE_MAP_READ, &oldProtection);
     69    FlushViewOfFile(map, m_size);
     70#else
    6971    // Drop the write permission.
    7072    mprotect(map, m_size, PROT_READ);
     
    7274    // Flush (asynchronously) to file, turning this into clean memory.
    7375    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 }
    8776#endif
    8877
    89 #if !OS(WINDOWS)
     78    return Data::adoptMap(WTFMove(mappedFile), handle);
     79}
     80
    9081Data mapFile(const char* path)
    9182{
    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);
    107 }
    108 #endif
    109 
    110 Data mapFile(const String& path)
    111 {
    112 #if !OS(WINDOWS)
    113     return mapFile(FileSystem::fileSystemRepresentation(path).data());
    114 #else
    11583    auto file = FileSystem::openFile(path, FileSystem::FileOpenMode::Read);
    11684    if (!FileSystem::isHandleValid(file))
     
    12088        return { };
    12189    return adoptAndMapFile(file, 0, size);
    122 #endif
    12390}
    12491
    125 #if !OS(WINDOWS)
    126 Data adoptAndMapFile(int fd, size_t offset, size_t size)
     92Data mapFile(const String& path)
     93{
     94    return mapFile(FileSystem::fileSystemRepresentation(path).data());
     95}
     96
     97Data adoptAndMapFile(FileSystem::PlatformFileHandle handle, size_t offset, size_t size)
    12798{
    12899    if (!size) {
    129         close(fd);
     100        FileSystem::closeFile(handle);
    130101        return Data::empty();
    131102    }
    132 
    133     void* map = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, offset);
    134     if (map == MAP_FAILED) {
    135         close(fd);
     103    bool success;
     104    FileSystem::MappedFileData mappedFile(handle, FileSystem::FileOpenMode::Read, FileSystem::MappedFileMode::Private, success);
     105    if (!success) {
     106        FileSystem::closeFile(handle);
    136107        return { };
    137108    }
    138109
    139     return Data::adoptMap(map, size, fd);
     110    return Data::adoptMap(WTFMove(mappedFile), handle);
    140111}
    141 #else
    142 Data adoptAndMapFile(FileSystem::PlatformFileHandle file, size_t offset, size_t size)
    143 {
    144     return Data(file, offset, size);
    145 }
    146 #endif
    147112
    148113SHA1::Digest computeSHA1(const Data& data, const Salt& salt)
     
    180145Optional<Salt> readOrMakeSalt(const String& path)
    181146{
    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)
    196             return { };
    197     }
    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());
    203     FileSystem::closeFile(file);
    204     if (bytesRead != salt.size()) {
    205         salt = makeSalt();
     147    if (fileExists(path)) {
     148        auto file = FileSystem::openFile(path, FileSystem::FileOpenMode::Read);
     149        Salt salt;
     150        auto bytesRead = FileSystem::readFromFile(file, reinterpret_cast<char*>(salt.data()), salt.size());
     151        FileSystem::closeFile(file);
     152        if (bytesRead == salt.size())
     153            return salt;
    206154
    207155        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 { };
    213156    }
     157
     158    Salt salt = makeSalt();
     159    auto file = FileSystem::openFile(path, FileSystem::FileOpenMode::Write, FileSystem::FileAccessPermission::User);
     160    bool success = FileSystem::writeToFile(file, reinterpret_cast<char*>(salt.data()), salt.size()) == salt.size();
     161    FileSystem::closeFile(file);
     162    if (!success)
     163        return { };
     164
    214165    return salt;
    215 #endif
    216166}
    217167
  • trunk/Source/WebKit/NetworkProcess/cache/NetworkCacheData.h

    r256700 r257518  
    4040#endif
    4141
     42#if USE(CURL)
     43#include <wtf/Box.h>
     44#include <wtf/Variant.h>
     45#endif
     46
    4247namespace WebKit {
    4348
     
    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)
     
    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;
     
    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 };
     
    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
  • trunk/Source/WebKit/NetworkProcess/cache/NetworkCacheDataCocoa.mm

    r256700 r257518  
    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);
  • trunk/Source/WebKit/NetworkProcess/cache/NetworkCacheDataCurl.cpp

    r256700 r257518  
    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
     
    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());
     
    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
  • trunk/Source/WebKit/NetworkProcess/cache/NetworkCacheDataSoup.cpp

    r256700 r257518  
    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}
     
    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
     
    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);
     
    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{
     
    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
  • trunk/Source/WebKit/NetworkProcess/cache/NetworkCacheFileSystem.cpp

    r256700 r257518  
    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
  • trunk/Tools/ChangeLog

    r257498 r257518  
     12020-02-26  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 Yusuke Suzuki.
     8
     9        Add test for FileSystem::createFile
     10
     11        * TestWebKitAPI/Tests/WTF/FileSystem.cpp:
     12
    1132020-02-26  Aakash Jain  <aakash_jain@apple.com>
    214
  • trunk/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp

    r247542 r257518  
    146146}
    147147
     148TEST_F(FileSystemTest, openExistingFileAndFailIfFileExists)
     149{
     150    auto handle = FileSystem::openFile(tempFilePath(), FileSystem::FileOpenMode::ReadWrite, FileSystem::FileAccessPermission::All, true);
     151    EXPECT_FALSE(FileSystem::isHandleValid(handle));
    148152}
     153
     154}
Note: See TracChangeset for help on using the changeset viewer.