1/*
2 * Copyright (C) 2015 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "NetworkCacheBlobStorage.h"
28
29#include "Logging.h"
30#include "NetworkCacheFileSystem.h"
31#include <fcntl.h>
32#include <wtf/FileSystem.h>
33#include <wtf/RunLoop.h>
34#include <wtf/SHA1.h>
35
36#if !OS(WINDOWS)
37#include <sys/stat.h>
38#endif
39
40namespace WebKit {
41namespace NetworkCache {
42
43BlobStorage::BlobStorage(const String& blobDirectoryPath, Salt salt)
44 : m_blobDirectoryPath(blobDirectoryPath)
45 , m_salt(salt)
46{
47}
48
49String BlobStorage::blobDirectoryPath() const
50{
51 return m_blobDirectoryPath.isolatedCopy();
52}
53
54void BlobStorage::synchronize()
55{
56 ASSERT(!RunLoop::isMain());
57
58 FileSystem::makeAllDirectories(blobDirectoryPath());
59
60 m_approximateSize = 0;
61 auto blobDirectory = blobDirectoryPath();
62 traverseDirectory(blobDirectory, [this, &blobDirectory](const String& name, DirectoryEntryType type) {
63 if (type != DirectoryEntryType::File)
64 return;
65 auto path = FileSystem::pathByAppendingComponent(blobDirectory, name);
66 auto filePath = FileSystem::fileSystemRepresentation(path);
67 struct stat stat;
68 ::stat(filePath.data(), &stat);
69 // No clients left for this blob.
70 if (stat.st_nlink == 1)
71 unlink(filePath.data());
72 else
73 m_approximateSize += stat.st_size;
74 });
75
76 LOG(NetworkCacheStorage, "(NetworkProcess) blob synchronization completed approximateSize=%zu", approximateSize());
77}
78
79String BlobStorage::blobPathForHash(const SHA1::Digest& hash) const
80{
81 auto hashAsString = SHA1::hexDigest(hash);
82 return FileSystem::pathByAppendingComponent(blobDirectoryPath(), String::fromUTF8(hashAsString));
83}
84
85BlobStorage::Blob BlobStorage::add(const String& path, const Data& data)
86{
87 ASSERT(!RunLoop::isMain());
88
89 auto hash = computeSHA1(data, m_salt);
90 if (data.isEmpty())
91 return { data, hash };
92
93 String blobPath = blobPathForHash(hash);
94
95 FileSystem::deleteFile(path);
96
97 bool blobExists = FileSystem::fileExists(blobPath);
98 if (blobExists) {
99 FileSystem::makeSafeToUseMemoryMapForPath(blobPath);
100 auto existingData = mapFile(blobPath);
101 if (bytesEqual(existingData, data)) {
102 if (!FileSystem::hardLink(blobPath, path))
103 WTFLogAlways("Failed to create hard link from %s to %s", blobPath.utf8().data(), path.utf8().data());
104 return { existingData, hash };
105 }
106 FileSystem::deleteFile(blobPath);
107 }
108
109 auto mappedData = data.mapToFile(blobPath);
110 if (mappedData.isNull())
111 return { };
112
113 FileSystem::makeSafeToUseMemoryMapForPath(blobPath);
114
115 if (!FileSystem::hardLink(blobPath, path))
116 WTFLogAlways("Failed to create hard link from %s to %s", blobPath.utf8().data(), path.utf8().data());
117
118 m_approximateSize += mappedData.size();
119
120 return { mappedData, hash };
121}
122
123BlobStorage::Blob BlobStorage::get(const String& path)
124{
125 ASSERT(!RunLoop::isMain());
126
127 auto linkPath = FileSystem::fileSystemRepresentation(path);
128 auto data = mapFile(linkPath.data());
129
130 return { data, computeSHA1(data, m_salt) };
131}
132
133void BlobStorage::remove(const String& path)
134{
135 ASSERT(!RunLoop::isMain());
136
137 FileSystem::deleteFile(path);
138}
139
140unsigned BlobStorage::shareCount(const String& path)
141{
142 ASSERT(!RunLoop::isMain());
143
144 auto linkPath = FileSystem::fileSystemRepresentation(path);
145 struct stat stat;
146 if (::stat(linkPath.data(), &stat) < 0)
147 return 0;
148 // Link count is 2 in the single client case (the blob file and a link).
149 return stat.st_nlink - 1;
150}
151
152}
153}
154