1 | /* |
2 | * Copyright (C) 2007-2017 Apple Inc. All rights reserved. |
3 | * Copyright (C) 2008 Collabora, Ltd. All rights reserved. |
4 | * Copyright (C) 2015 Canon Inc. All rights reserved. |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
9 | * |
10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
15 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
16 | * its contributors may be used to endorse or promote products derived |
17 | * from this software without specific prior written permission. |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
20 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
22 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
23 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
26 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
30 | |
31 | #pragma once |
32 | |
33 | #include <time.h> |
34 | #include <utility> |
35 | #include <wtf/Forward.h> |
36 | #include <wtf/OptionSet.h> |
37 | #include <wtf/Vector.h> |
38 | #include <wtf/WallTime.h> |
39 | #include <wtf/text/WTFString.h> |
40 | |
41 | #if USE(CF) |
42 | #include <wtf/RetainPtr.h> |
43 | #endif |
44 | |
45 | #if USE(CF) |
46 | typedef const struct __CFData* CFDataRef; |
47 | #endif |
48 | |
49 | OBJC_CLASS NSString; |
50 | |
51 | #if OS(WINDOWS) |
52 | typedef void *HANDLE; |
53 | #endif |
54 | |
55 | #if USE(GLIB) |
56 | typedef struct _GFileIOStream GFileIOStream; |
57 | #endif |
58 | |
59 | namespace WTF { |
60 | |
61 | struct FileMetadata; |
62 | |
63 | namespace FileSystemImpl { |
64 | |
65 | // PlatformFileHandle |
66 | #if USE(GLIB) && !OS(WINDOWS) |
67 | typedef GFileIOStream* PlatformFileHandle; |
68 | const PlatformFileHandle invalidPlatformFileHandle = 0; |
69 | #elif OS(WINDOWS) |
70 | typedef HANDLE PlatformFileHandle; |
71 | // FIXME: -1 is INVALID_HANDLE_VALUE, defined in <winbase.h>. Chromium tries to |
72 | // avoid using Windows headers in headers. We'd rather move this into the .cpp. |
73 | const PlatformFileHandle invalidPlatformFileHandle = reinterpret_cast<HANDLE>(-1); |
74 | #else |
75 | typedef int PlatformFileHandle; |
76 | const PlatformFileHandle invalidPlatformFileHandle = -1; |
77 | #endif |
78 | |
79 | enum class FileOpenMode { |
80 | Read, |
81 | Write, |
82 | #if OS(DARWIN) |
83 | EventsOnly, |
84 | #endif |
85 | }; |
86 | |
87 | enum class FileSeekOrigin { |
88 | Beginning, |
89 | Current, |
90 | End, |
91 | }; |
92 | |
93 | enum class FileLockMode { |
94 | Shared = 1 << 0, |
95 | Exclusive = 1 << 1, |
96 | Nonblocking = 1 << 2, |
97 | }; |
98 | |
99 | enum class MappedFileMode { |
100 | Shared, |
101 | Private, |
102 | }; |
103 | |
104 | enum class ShouldFollowSymbolicLinks { No, Yes }; |
105 | |
106 | WTF_EXPORT_PRIVATE bool fileExists(const String&); |
107 | WTF_EXPORT_PRIVATE bool deleteFile(const String&); |
108 | WTF_EXPORT_PRIVATE bool deleteEmptyDirectory(const String&); |
109 | WTF_EXPORT_PRIVATE bool moveFile(const String& oldPath, const String& newPath); |
110 | WTF_EXPORT_PRIVATE bool getFileSize(const String&, long long& result); |
111 | WTF_EXPORT_PRIVATE bool getFileSize(PlatformFileHandle, long long& result); |
112 | WTF_EXPORT_PRIVATE Optional<WallTime> getFileModificationTime(const String&); |
113 | WTF_EXPORT_PRIVATE Optional<WallTime> getFileCreationTime(const String&); // Not all platforms store file creation time. |
114 | WTF_EXPORT_PRIVATE Optional<FileMetadata> fileMetadata(const String& path); |
115 | WTF_EXPORT_PRIVATE Optional<FileMetadata> fileMetadataFollowingSymlinks(const String& path); |
116 | WTF_EXPORT_PRIVATE bool fileIsDirectory(const String&, ShouldFollowSymbolicLinks); |
117 | WTF_EXPORT_PRIVATE String pathByAppendingComponent(const String& path, const String& component); |
118 | WTF_EXPORT_PRIVATE String pathByAppendingComponents(StringView path, const Vector<StringView>& components); |
119 | WTF_EXPORT_PRIVATE String lastComponentOfPathIgnoringTrailingSlash(const String& path); |
120 | WTF_EXPORT_PRIVATE bool makeAllDirectories(const String& path); |
121 | WTF_EXPORT_PRIVATE String homeDirectoryPath(); |
122 | WTF_EXPORT_PRIVATE String pathGetFileName(const String&); |
123 | WTF_EXPORT_PRIVATE String directoryName(const String&); |
124 | WTF_EXPORT_PRIVATE bool getVolumeFreeSpace(const String&, uint64_t&); |
125 | WTF_EXPORT_PRIVATE Optional<int32_t> getFileDeviceId(const CString&); |
126 | WTF_EXPORT_PRIVATE bool createSymbolicLink(const String& targetPath, const String& symbolicLinkPath); |
127 | WTF_EXPORT_PRIVATE String createTemporaryZipArchive(const String& directory); |
128 | |
129 | WTF_EXPORT_PRIVATE void setMetadataURL(const String& path, const String& urlString, const String& referrer = { }); |
130 | |
131 | bool canExcludeFromBackup(); // Returns true if any file can ever be excluded from backup. |
132 | bool excludeFromBackup(const String&); // Returns true if successful. |
133 | |
134 | WTF_EXPORT_PRIVATE Vector<String> listDirectory(const String& path, const String& filter = String()); |
135 | |
136 | WTF_EXPORT_PRIVATE CString fileSystemRepresentation(const String&); |
137 | String stringFromFileSystemRepresentation(const char*); |
138 | |
139 | inline bool isHandleValid(const PlatformFileHandle& handle) { return handle != invalidPlatformFileHandle; } |
140 | |
141 | // Prefix is what the filename should be prefixed with, not the full path. |
142 | WTF_EXPORT_PRIVATE String openTemporaryFile(const String& prefix, PlatformFileHandle&); |
143 | WTF_EXPORT_PRIVATE PlatformFileHandle openFile(const String& path, FileOpenMode); |
144 | WTF_EXPORT_PRIVATE void closeFile(PlatformFileHandle&); |
145 | // Returns the resulting offset from the beginning of the file if successful, -1 otherwise. |
146 | WTF_EXPORT_PRIVATE long long seekFile(PlatformFileHandle, long long offset, FileSeekOrigin); |
147 | WTF_EXPORT_PRIVATE bool truncateFile(PlatformFileHandle, long long offset); |
148 | // Returns number of bytes actually read if successful, -1 otherwise. |
149 | WTF_EXPORT_PRIVATE int writeToFile(PlatformFileHandle, const char* data, int length); |
150 | // Returns number of bytes actually written if successful, -1 otherwise. |
151 | WTF_EXPORT_PRIVATE int readFromFile(PlatformFileHandle, char* data, int length); |
152 | |
153 | WTF_EXPORT_PRIVATE PlatformFileHandle openAndLockFile(const String&, FileOpenMode, OptionSet<FileLockMode> = FileLockMode::Exclusive); |
154 | WTF_EXPORT_PRIVATE void unlockAndCloseFile(PlatformFileHandle); |
155 | |
156 | // Appends the contents of the file found at 'path' to the open PlatformFileHandle. |
157 | // Returns true if the write was successful, false if it was not. |
158 | WTF_EXPORT_PRIVATE bool appendFileContentsToFileHandle(const String& path, PlatformFileHandle&); |
159 | |
160 | WTF_EXPORT_PRIVATE bool hardLink(const String& source, const String& destination); |
161 | // Hard links a file if possible, copies it if not. |
162 | WTF_EXPORT_PRIVATE bool hardLinkOrCopyFile(const String& source, const String& destination); |
163 | |
164 | #if USE(FILE_LOCK) |
165 | WTF_EXPORT_PRIVATE bool lockFile(PlatformFileHandle, OptionSet<FileLockMode>); |
166 | WTF_EXPORT_PRIVATE bool unlockFile(PlatformFileHandle); |
167 | #endif |
168 | |
169 | // Encode a string for use within a file name. |
170 | WTF_EXPORT_PRIVATE String encodeForFileName(const String&); |
171 | WTF_EXPORT_PRIVATE String decodeFromFilename(const String&); |
172 | |
173 | WTF_EXPORT_PRIVATE bool filesHaveSameVolume(const String&, const String&); |
174 | |
175 | #if USE(CF) |
176 | WTF_EXPORT_PRIVATE RetainPtr<CFURLRef> pathAsURL(const String&); |
177 | #endif |
178 | |
179 | #if USE(GLIB) |
180 | String filenameForDisplay(const String&); |
181 | #endif |
182 | |
183 | #if OS(WINDOWS) |
184 | WTF_EXPORT_PRIVATE String localUserSpecificStorageDirectory(); |
185 | WTF_EXPORT_PRIVATE String roamingUserSpecificStorageDirectory(); |
186 | WTF_EXPORT_PRIVATE String createTemporaryDirectory(); |
187 | WTF_EXPORT_PRIVATE bool deleteNonEmptyDirectory(const String&); |
188 | #endif |
189 | |
190 | #if PLATFORM(COCOA) |
191 | WTF_EXPORT_PRIVATE NSString *createTemporaryDirectory(NSString *directoryPrefix); |
192 | WTF_EXPORT_PRIVATE bool deleteNonEmptyDirectory(const String&); |
193 | #endif |
194 | |
195 | WTF_EXPORT_PRIVATE String realPath(const String&); |
196 | |
197 | WTF_EXPORT_PRIVATE bool isSafeToUseMemoryMapForPath(const String&); |
198 | WTF_EXPORT_PRIVATE void makeSafeToUseMemoryMapForPath(const String&); |
199 | |
200 | WTF_EXPORT_PRIVATE bool unmapViewOfFile(void* buffer, size_t); |
201 | |
202 | class MappedFileData { |
203 | WTF_MAKE_FAST_ALLOCATED; |
204 | public: |
205 | MappedFileData() { } |
206 | MappedFileData(MappedFileData&&); |
207 | WTF_EXPORT_PRIVATE MappedFileData(const String& filePath, MappedFileMode, bool& success); |
208 | WTF_EXPORT_PRIVATE MappedFileData(PlatformFileHandle, MappedFileMode, bool& success); |
209 | WTF_EXPORT_PRIVATE ~MappedFileData(); |
210 | MappedFileData& operator=(MappedFileData&&); |
211 | |
212 | explicit operator bool() const { return !!m_fileData; } |
213 | const void* data() const { return m_fileData; } |
214 | unsigned size() const { return m_fileSize; } |
215 | |
216 | void* leakHandle() { return std::exchange(m_fileData, nullptr); } |
217 | |
218 | private: |
219 | WTF_EXPORT_PRIVATE bool mapFileHandle(PlatformFileHandle, MappedFileMode); |
220 | |
221 | void* m_fileData { nullptr }; |
222 | unsigned m_fileSize { 0 }; |
223 | }; |
224 | |
225 | inline MappedFileData::MappedFileData(PlatformFileHandle handle, MappedFileMode mode, bool& success) |
226 | { |
227 | success = mapFileHandle(handle, mode); |
228 | } |
229 | |
230 | inline MappedFileData::MappedFileData(MappedFileData&& other) |
231 | : m_fileData(std::exchange(other.m_fileData, nullptr)) |
232 | , m_fileSize(std::exchange(other.m_fileSize, 0)) |
233 | { |
234 | } |
235 | |
236 | inline MappedFileData& MappedFileData::operator=(MappedFileData&& other) |
237 | { |
238 | m_fileData = std::exchange(other.m_fileData, nullptr); |
239 | m_fileSize = std::exchange(other.m_fileSize, 0); |
240 | return *this; |
241 | } |
242 | |
243 | } // namespace FileSystemImpl |
244 | } // namespace WTF |
245 | |
246 | namespace FileSystem = WTF::FileSystemImpl; |
247 | |