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/mman.h> |
38 | #include <sys/stat.h> |
39 | #include <unistd.h> |
40 | #endif |
41 | |
42 | namespace WebKit { |
43 | namespace NetworkCache { |
44 | |
45 | BlobStorage::BlobStorage(const String& blobDirectoryPath, Salt salt) |
46 | : m_blobDirectoryPath(blobDirectoryPath) |
47 | , m_salt(salt) |
48 | { |
49 | } |
50 | |
51 | String BlobStorage::blobDirectoryPath() const |
52 | { |
53 | return m_blobDirectoryPath.isolatedCopy(); |
54 | } |
55 | |
56 | void BlobStorage::synchronize() |
57 | { |
58 | ASSERT(!RunLoop::isMain()); |
59 | |
60 | FileSystem::makeAllDirectories(blobDirectoryPath()); |
61 | |
62 | m_approximateSize = 0; |
63 | auto blobDirectory = blobDirectoryPath(); |
64 | traverseDirectory(blobDirectory, [this, &blobDirectory](const String& name, DirectoryEntryType type) { |
65 | if (type != DirectoryEntryType::File) |
66 | return; |
67 | auto path = FileSystem::pathByAppendingComponent(blobDirectory, name); |
68 | auto filePath = FileSystem::fileSystemRepresentation(path); |
69 | struct stat stat; |
70 | ::stat(filePath.data(), &stat); |
71 | // No clients left for this blob. |
72 | if (stat.st_nlink == 1) |
73 | unlink(filePath.data()); |
74 | else |
75 | m_approximateSize += stat.st_size; |
76 | }); |
77 | |
78 | LOG(NetworkCacheStorage, "(NetworkProcess) blob synchronization completed approximateSize=%zu" , approximateSize()); |
79 | } |
80 | |
81 | String BlobStorage::blobPathForHash(const SHA1::Digest& hash) const |
82 | { |
83 | auto hashAsString = SHA1::hexDigest(hash); |
84 | return FileSystem::pathByAppendingComponent(blobDirectoryPath(), String::fromUTF8(hashAsString)); |
85 | } |
86 | |
87 | BlobStorage::Blob BlobStorage::add(const String& path, const Data& data) |
88 | { |
89 | ASSERT(!RunLoop::isMain()); |
90 | |
91 | auto hash = computeSHA1(data, m_salt); |
92 | if (data.isEmpty()) |
93 | return { data, hash }; |
94 | |
95 | String blobPath = blobPathForHash(hash); |
96 | |
97 | FileSystem::deleteFile(path); |
98 | |
99 | bool blobExists = FileSystem::fileExists(blobPath); |
100 | if (blobExists) { |
101 | FileSystem::makeSafeToUseMemoryMapForPath(blobPath); |
102 | auto existingData = mapFile(blobPath); |
103 | if (bytesEqual(existingData, data)) { |
104 | if (!FileSystem::hardLink(blobPath, path)) |
105 | WTFLogAlways("Failed to create hard link from %s to %s" , blobPath.utf8().data(), path.utf8().data()); |
106 | return { existingData, hash }; |
107 | } |
108 | FileSystem::deleteFile(blobPath); |
109 | } |
110 | |
111 | auto mappedData = data.mapToFile(blobPath); |
112 | if (mappedData.isNull()) |
113 | return { }; |
114 | |
115 | FileSystem::makeSafeToUseMemoryMapForPath(blobPath); |
116 | |
117 | if (!FileSystem::hardLink(blobPath, path)) |
118 | WTFLogAlways("Failed to create hard link from %s to %s" , blobPath.utf8().data(), path.utf8().data()); |
119 | |
120 | m_approximateSize += mappedData.size(); |
121 | |
122 | return { mappedData, hash }; |
123 | } |
124 | |
125 | BlobStorage::Blob BlobStorage::get(const String& path) |
126 | { |
127 | ASSERT(!RunLoop::isMain()); |
128 | |
129 | auto linkPath = FileSystem::fileSystemRepresentation(path); |
130 | auto data = mapFile(linkPath.data()); |
131 | |
132 | return { data, computeSHA1(data, m_salt) }; |
133 | } |
134 | |
135 | void BlobStorage::remove(const String& path) |
136 | { |
137 | ASSERT(!RunLoop::isMain()); |
138 | |
139 | FileSystem::deleteFile(path); |
140 | } |
141 | |
142 | unsigned BlobStorage::shareCount(const String& path) |
143 | { |
144 | ASSERT(!RunLoop::isMain()); |
145 | |
146 | auto linkPath = FileSystem::fileSystemRepresentation(path); |
147 | struct stat stat; |
148 | if (::stat(linkPath.data(), &stat) < 0) |
149 | return 0; |
150 | // Link count is 2 in the single client case (the blob file and a link). |
151 | return stat.st_nlink - 1; |
152 | } |
153 | |
154 | } |
155 | } |
156 | |