Changeset 256633 in webkit


Ignore:
Timestamp:
Feb 14, 2020 12:06:28 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

Reviewed by Fujii Hironori.

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.

  • 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:

LayoutTests:

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

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r256628 r256633  
     12020-02-14  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
     6        Reviewed by Fujii Hironori.
     7
     8        * platform/wincairo/TestExpectations:
     9
    1102020-02-14  Antoine Quint  <graouts@webkit.org>
    211
  • trunk/LayoutTests/platform/wincairo/TestExpectations

    r256477 r256633  
    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

    r256629 r256633  
     12020-02-14  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
     6        Reviewed by Fujii Hironori.
     7
     8        * wtf/FileSystem.cpp:
     9        * wtf/FileSystem.h:
     10        Added FileAccessPermission flag when opening files.
     11        Remove default argument for the listDirectory filter since the defaut
     12        String() filter doesn't match all files on Mac and Windows.
     13
     14        * wtf/glib/FileSystemGlib.cpp:
     15        * wtf/posix/FileSystemPOSIX.cpp:
     16        Added (S_IRUSR | S_IWUSR) file open modes.
     17
     18        * wtf/win/FileSystemWin.cpp:
     19        Implement getVolumeFreeSpace since some of the tests use it when toggling cache.
     20       
     21        * wtf/win/PathWalker.cpp:
     22
    1232020-02-14  Alex Christensen  <achristensen@webkit.org>
    224
  • trunk/Source/WTF/wtf/FileSystem.cpp

    r254046 r256633  
    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#if OS(DARWIN)
     336    case FileOpenMode::EventsOnly:
     337        ASSERT_NOT_REACHED();
     338#endif
     339    }
     340
     341    void* data = mmap(0, size, pageProtection, MAP_FILE | (mapMode == MappedFileMode::Shared ? MAP_SHARED : MAP_PRIVATE), fd, 0);
    328342
    329343    if (data == MAP_FAILED) {
  • trunk/Source/WTF/wtf/FileSystem.h

    r248546 r256633  
    8585};
    8686
     87enum class FileAccessPermission : bool {
     88    User,
     89    All
     90};
     91
    8792enum class FileSeekOrigin {
    8893    Beginning,
     
    132137bool excludeFromBackup(const String&); // Returns true if successful.
    133138
    134 WTF_EXPORT_PRIVATE Vector<String> listDirectory(const String& path, const String& filter = String());
     139WTF_EXPORT_PRIVATE Vector<String> listDirectory(const String& path, const String& filter);
    135140
    136141WTF_EXPORT_PRIVATE CString fileSystemRepresentation(const String&);
     
    141146// Prefix is what the filename should be prefixed with, not the full path.
    142147WTF_EXPORT_PRIVATE String openTemporaryFile(const String& prefix, PlatformFileHandle&);
    143 WTF_EXPORT_PRIVATE PlatformFileHandle openFile(const String& path, FileOpenMode);
     148WTF_EXPORT_PRIVATE PlatformFileHandle openFile(const String& path, FileOpenMode, FileAccessPermission = FileAccessPermission::All);
    144149WTF_EXPORT_PRIVATE void closeFile(PlatformFileHandle&);
    145150// Returns the resulting offset from the beginning of the file if successful, -1 otherwise.
     
    207212    WTF_EXPORT_PRIVATE MappedFileData(const String& filePath, MappedFileMode, bool& success);
    208213    WTF_EXPORT_PRIVATE MappedFileData(PlatformFileHandle, MappedFileMode, bool& success);
     214    WTF_EXPORT_PRIVATE MappedFileData(PlatformFileHandle, FileOpenMode, MappedFileMode, bool& success);
    209215    WTF_EXPORT_PRIVATE ~MappedFileData();
    210216    MappedFileData& operator=(MappedFileData&&);
     
    217223
    218224private:
    219     WTF_EXPORT_PRIVATE bool mapFileHandle(PlatformFileHandle, MappedFileMode);
     225    WTF_EXPORT_PRIVATE bool mapFileHandle(PlatformFileHandle, FileOpenMode, MappedFileMode);
    220226
    221227    void* m_fileData { nullptr };
     
    223229};
    224230
    225 inline MappedFileData::MappedFileData(PlatformFileHandle handle, MappedFileMode mode, bool& success)
    226 {
    227     success = mapFileHandle(handle, mode);
     231inline MappedFileData::MappedFileData(PlatformFileHandle handle, MappedFileMode mapMode, bool& success)
     232{
     233    success = mapFileHandle(handle, FileOpenMode::Read, mapMode);
     234}
     235
     236inline MappedFileData::MappedFileData(PlatformFileHandle handle, FileOpenMode openMode, MappedFileMode mapMode, bool& success)
     237{
     238    success = mapFileHandle(handle, openMode, mapMode);
    228239}
    229240
  • trunk/Source/WTF/wtf/glib/FileSystemGlib.cpp

    r247542 r256633  
    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);
     
    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
  • trunk/Source/WTF/wtf/posix/FileSystemPOSIX.cpp

    r249027 r256633  
    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);
     
    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;
    9497#if OS(DARWIN)
    95     else if (mode == FileOpenMode::EventsOnly)
     98    case FileOpenMode::EventsOnly:
    9699        platformFlag |= O_EVTONLY;
     100        break;
    97101#endif
    98 
    99     return open(fsRep.data(), platformFlag, 0666);
     102    }
     103
     104    int permissionFlag = 0;
     105    if (permission == FileAccessPermission::User)
     106        permissionFlag |= (S_IRUSR | S_IWUSR);
     107    else if (permission == FileAccessPermission::All)
     108        permissionFlag |= (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
     109
     110    return open(fsRep.data(), platformFlag, permissionFlag);
    100111}
    101112
  • trunk/Source/WTF/wtf/win/FileSystemWin.cpp

    r256063 r256633  
    422422}
    423423
    424 PlatformFileHandle openFile(const String& path, FileOpenMode mode)
     424PlatformFileHandle openFile(const String& path, FileOpenMode mode, FileAccessPermission)
    425425{
    426426    DWORD desiredAccess = 0;
     
    437437        creationDisposition = CREATE_ALWAYS;
    438438        break;
    439     default:
    440         ASSERT_NOT_REACHED();
    441439    }
    442440
     
    550548}
    551549
    552 bool getVolumeFreeSpace(const String&, uint64_t&)
    553 {
    554     return false;
     550bool getVolumeFreeSpace(const String& path, uint64_t& freeSpace)
     551{
     552    ULARGE_INTEGER freeBytesAvailableToCaller;
     553    if (!GetDiskFreeSpaceExW(path.wideCharacters().data(), &freeBytesAvailableToCaller, nullptr, nullptr))
     554        return false;
     555
     556    freeSpace = freeBytesAvailableToCaller.QuadPart;
     557    return true;
    555558}
    556559
     
    604607}
    605608
    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)
     609bool MappedFileData::mapFileHandle(PlatformFileHandle handle, FileOpenMode openMode, MappedFileMode)
    615610{
    616611    if (!isHandleValid(handle))
     
    626621    }
    627622
    628     auto mapping = CreateFileMapping(handle, nullptr, PAGE_READONLY, 0, 0, nullptr);
     623    DWORD pageProtection = PAGE_READONLY;
     624    DWORD desiredAccess = FILE_MAP_READ;
     625    switch (openMode) {
     626    case FileOpenMode::Read:
     627        pageProtection = PAGE_READONLY;
     628        desiredAccess = FILE_MAP_READ;
     629        break;
     630    case FileOpenMode::Write:
     631        pageProtection = PAGE_READWRITE;
     632        desiredAccess = FILE_MAP_WRITE;
     633        break;
     634    }
     635
     636    auto mapping = CreateFileMapping(handle, nullptr, pageProtection, 0, 0, nullptr);
    629637    if (!mapping)
    630638        return false;
    631639
    632     m_fileData = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, size);
     640    m_fileData = MapViewOfFile(mapping, desiredAccess, 0, 0, size);
    633641    CloseHandle(mapping);
    634642    if (!m_fileData)
  • trunk/Source/WTF/wtf/win/PathWalker.cpp

    r242592 r256633  
    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

    r256632 r256633  
     12020-02-14  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
     6        Reviewed by Fujii Hironori.
     7
     8        * NetworkProcess/NetworkProcess.cpp:
     9        Ensure that the CacheStorage directory is actually being created.
     10
     11        * NetworkProcess/cache/NetworkCacheData.cpp:
     12        * NetworkProcess/cache/NetworkCacheData.h:
     13        * NetworkProcess/cache/NetworkCacheDataCocoa.mm:
     14        * NetworkProcess/cache/NetworkCacheDataSoup.cpp:
     15        * NetworkProcess/cache/NetworkCacheFileSystem.cpp:
     16        Use more FileSystem functionality to share code across platforms.
     17
     18        * NetworkProcess/cache/NetworkCacheDataCurl.cpp:
     19        Use Optional<Vector> for m_buffer since we need to differentiate isEmpty and isNull.
     20
    1212020-02-14  Yusuke Suzuki  <ysuzuki@apple.com>
    222
  • trunk/Source/WebKit/NetworkProcess/NetworkProcess.cpp

    r255681 r256633  
    454454        return makeUnique<SessionStorageQuotaManager>(cacheRootPath, defaultQuota, defaultThirdPartyQuota);
    455455    }).isNewEntry;
    456     if (isNewEntry)
     456    if (isNewEntry) {
    457457        SandboxExtension::consumePermanently(cacheRootPathHandle);
     458        if (!cacheRootPath.isEmpty())
     459            postStorageTask(createCrossThreadTask(*this, &NetworkProcess::ensurePathExists, cacheRootPath));
     460    }
    458461}
    459462
  • trunk/Source/WebKit/NetworkProcess/cache/NetworkCacheData.cpp

    r248973 r256633  
    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    auto handle = FileSystem::openFile(path, FileSystem::FileOpenMode::Write, 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::Write, 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) {
     
    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);
     
    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)
     77    return Data::adoptMap(WTFMove(mappedFile), handle);
     78}
     79
    9080Data mapFile(const char* path)
    9181{
    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
    11582    auto file = FileSystem::openFile(path, FileSystem::FileOpenMode::Read);
    11683    if (!FileSystem::isHandleValid(file))
     
    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)
     
    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();
     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())
     152            return { };
    190153
    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 { };
     154        return salt;
    197155    }
     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();
     160    FileSystem::closeFile(file);
     161    if (!success)
     162        return { };
     163
    198164    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();
    206 
    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     }
    214     return salt;
    215 #endif
    216165}
    217166
  • trunk/Source/WebKit/NetworkProcess/cache/NetworkCacheData.h

    r245186 r256633  
    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

    r239260 r256633  
    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

    r245186 r256633  
    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

    r241283 r256633  
    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

    r245301 r256633  
    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
Note: See TracChangeset for help on using the changeset viewer.