1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
/*
* Copyright (C) 2015 Igalia S.L.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "NetworkCacheIOChannel.h"
#include "NetworkCacheFileSystem.h"
#include <wtf/MainThread.h>
#include <wtf/RunLoop.h>
#include <wtf/glib/GUniquePtr.h>
#include <wtf/glib/RunLoopSourcePriority.h>
namespace WebKit {
namespace NetworkCache {
IOChannel::IOChannel(const String& filePath, Type type, std::optional<WorkQueue::QOS> qos)
{
auto path = FileSystem::fileSystemRepresentation(filePath);
GRefPtr<GFile> file = adoptGRef(g_file_new_for_path(path.data()));
Locker locker { m_lock };
switch (type) {
case Type::Create: {
g_file_delete(file.get(), nullptr, nullptr);
m_outputStream = adoptGRef(G_OUTPUT_STREAM(g_file_create(file.get(), static_cast<GFileCreateFlags>(G_FILE_CREATE_PRIVATE), nullptr, nullptr)));
#if !HAVE(STAT_BIRTHTIME)
GUniquePtr<char> birthtimeString(g_strdup_printf("%" G_GUINT64_FORMAT, WallTime::now().secondsSinceEpoch().secondsAs<uint64_t>()));
g_file_set_attribute_string(file.get(), "xattr::birthtime", birthtimeString.get(), G_FILE_QUERY_INFO_NONE, nullptr, nullptr);
#endif
m_qos = qos.value_or(WorkQueue::QOS::Background);
break;
}
case Type::Write: {
auto ioStream = adoptGRef(g_file_open_readwrite(file.get(), nullptr, nullptr));
m_outputStream = g_io_stream_get_output_stream(G_IO_STREAM(ioStream.get()));
m_qos = qos.value_or(WorkQueue::QOS::Background);
break;
}
case Type::Read:
m_inputStream = adoptGRef(G_INPUT_STREAM(g_file_read(file.get(), nullptr, nullptr)));
m_qos = qos.value_or(WorkQueue::QOS::Default);
break;
}
}
IOChannel::~IOChannel()
{
RELEASE_ASSERT(!m_wasDeleted.exchange(true));
}
void IOChannel::read(size_t offset, size_t size, Ref<WTF::WorkQueueBase>&& queue, Function<void(Data&&, int error)>&& completionHandler)
{
Locker locker { m_lock };
if (!m_inputStream) {
queue->dispatch([protectedThis = Ref { *this }, completionHandler = WTFMove(completionHandler)] {
Data data;
completionHandler(WTFMove(data), -1);
});
return;
}
Thread::create("IOChannel::read"_s, [this, protectedThis = Ref { *this }, offset, size, queue = Ref { queue }, completionHandler = WTFMove(completionHandler)]() mutable {
Locker locker { m_lock };
GRefPtr<GFileInfo> info = adoptGRef(g_file_input_stream_query_info(G_FILE_INPUT_STREAM(m_inputStream.get()), G_FILE_ATTRIBUTE_STANDARD_SIZE, nullptr, nullptr));
if (info) {
auto fileSize = g_file_info_get_size(info.get());
if (fileSize && static_cast<guint64>(fileSize) <= std::numeric_limits<size_t>::max()) {
if (G_IS_SEEKABLE(m_inputStream.get()) && g_seekable_can_seek(G_SEEKABLE(m_inputStream.get())))
g_seekable_seek(G_SEEKABLE(m_inputStream.get()), offset, G_SEEK_SET, nullptr, nullptr);
size_t bufferSize = std::min<size_t>(size, fileSize - offset);
uint8_t* bufferData = static_cast<uint8_t*>(fastMalloc(bufferSize));
GRefPtr<GBytes> buffer = adoptGRef(g_bytes_new_with_free_func(bufferData, bufferSize, fastFree, bufferData));
gsize bytesRead;
if (g_input_stream_read_all(m_inputStream.get(), bufferData, bufferSize, &bytesRead, nullptr, nullptr)) {
GRefPtr<GBytes> bytes = bufferSize == bytesRead ? buffer : adoptGRef(g_bytes_new_from_bytes(buffer.get(), 0, bytesRead));
queue->dispatch([protectedThis = WTFMove(protectedThis), bytes = WTFMove(bytes), completionHandler = WTFMove(completionHandler)]() mutable {
Data data(WTFMove(bytes));
completionHandler(WTFMove(data), 0);
});
return;
}
}
}
queue->dispatch([protectedThis = Ref { *this }, completionHandler = WTFMove(completionHandler)] {
completionHandler(Data { }, -1);
});
}, ThreadType::Unknown, m_qos)->detach();
}
void IOChannel::write(size_t offset, const Data& data, Ref<WTF::WorkQueueBase>&& queue, Function<void(int error)>&& completionHandler)
{
Locker locker { m_lock };
if (!m_outputStream) {
queue->dispatch([protectedThis = Ref { *this }, completionHandler = WTFMove(completionHandler)] {
completionHandler(-1);
});
return;
}
GRefPtr<GBytes> buffer = offset ? adoptGRef(g_bytes_new_from_bytes(data.bytes(), offset, data.size() - offset)) : data.bytes();
Thread::create("IOChannel::write"_s, [this, protectedThis = Ref { *this }, buffer = WTFMove(buffer), queue = WTFMove(queue), completionHandler = WTFMove(completionHandler)]() mutable {
Locker locker { m_lock };
gsize buffersize;
const auto* bufferData = g_bytes_get_data(buffer.get(), &buffersize);
auto success = g_output_stream_write_all(m_outputStream.get(), bufferData, buffersize, nullptr, nullptr, nullptr);
queue->dispatch([protectedThis = Ref { *this }, success, completionHandler = WTFMove(completionHandler)] {
completionHandler(success ? 0 : -1);
});
}, ThreadType::Unknown, m_qos)->detach();
}
} // namespace NetworkCache
} // namespace WebKit
|