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)
46typedef const struct __CFData* CFDataRef;
47#endif
48
49OBJC_CLASS NSString;
50
51#if OS(WINDOWS)
52typedef void *HANDLE;
53#endif
54
55#if USE(GLIB)
56typedef struct _GFileIOStream GFileIOStream;
57#endif
58
59namespace WTF {
60
61struct FileMetadata;
62
63namespace FileSystemImpl {
64
65// PlatformFileHandle
66#if USE(GLIB) && !OS(WINDOWS)
67typedef GFileIOStream* PlatformFileHandle;
68const PlatformFileHandle invalidPlatformFileHandle = 0;
69#elif OS(WINDOWS)
70typedef 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.
73const PlatformFileHandle invalidPlatformFileHandle = reinterpret_cast<HANDLE>(-1);
74#else
75typedef int PlatformFileHandle;
76const PlatformFileHandle invalidPlatformFileHandle = -1;
77#endif
78
79enum class FileOpenMode {
80 Read,
81 Write,
82#if OS(DARWIN)
83 EventsOnly,
84#endif
85};
86
87enum class FileSeekOrigin {
88 Beginning,
89 Current,
90 End,
91};
92
93enum class FileLockMode {
94 Shared = 1 << 0,
95 Exclusive = 1 << 1,
96 Nonblocking = 1 << 2,
97};
98
99enum class MappedFileMode {
100 Shared,
101 Private,
102};
103
104enum class ShouldFollowSymbolicLinks { No, Yes };
105
106WTF_EXPORT_PRIVATE bool fileExists(const String&);
107WTF_EXPORT_PRIVATE bool deleteFile(const String&);
108WTF_EXPORT_PRIVATE bool deleteEmptyDirectory(const String&);
109WTF_EXPORT_PRIVATE bool moveFile(const String& oldPath, const String& newPath);
110WTF_EXPORT_PRIVATE bool getFileSize(const String&, long long& result);
111WTF_EXPORT_PRIVATE bool getFileSize(PlatformFileHandle, long long& result);
112WTF_EXPORT_PRIVATE Optional<WallTime> getFileModificationTime(const String&);
113WTF_EXPORT_PRIVATE Optional<WallTime> getFileCreationTime(const String&); // Not all platforms store file creation time.
114WTF_EXPORT_PRIVATE Optional<FileMetadata> fileMetadata(const String& path);
115WTF_EXPORT_PRIVATE Optional<FileMetadata> fileMetadataFollowingSymlinks(const String& path);
116WTF_EXPORT_PRIVATE bool fileIsDirectory(const String&, ShouldFollowSymbolicLinks);
117WTF_EXPORT_PRIVATE String pathByAppendingComponent(const String& path, const String& component);
118WTF_EXPORT_PRIVATE String pathByAppendingComponents(StringView path, const Vector<StringView>& components);
119WTF_EXPORT_PRIVATE String lastComponentOfPathIgnoringTrailingSlash(const String& path);
120WTF_EXPORT_PRIVATE bool makeAllDirectories(const String& path);
121WTF_EXPORT_PRIVATE String homeDirectoryPath();
122WTF_EXPORT_PRIVATE String pathGetFileName(const String&);
123WTF_EXPORT_PRIVATE String directoryName(const String&);
124WTF_EXPORT_PRIVATE bool getVolumeFreeSpace(const String&, uint64_t&);
125WTF_EXPORT_PRIVATE Optional<int32_t> getFileDeviceId(const CString&);
126WTF_EXPORT_PRIVATE bool createSymbolicLink(const String& targetPath, const String& symbolicLinkPath);
127WTF_EXPORT_PRIVATE String createTemporaryZipArchive(const String& directory);
128
129WTF_EXPORT_PRIVATE void setMetadataURL(const String& path, const String& urlString, const String& referrer = { });
130
131bool canExcludeFromBackup(); // Returns true if any file can ever be excluded from backup.
132bool excludeFromBackup(const String&); // Returns true if successful.
133
134WTF_EXPORT_PRIVATE Vector<String> listDirectory(const String& path, const String& filter = String());
135
136WTF_EXPORT_PRIVATE CString fileSystemRepresentation(const String&);
137String stringFromFileSystemRepresentation(const char*);
138
139inline bool isHandleValid(const PlatformFileHandle& handle) { return handle != invalidPlatformFileHandle; }
140
141// Prefix is what the filename should be prefixed with, not the full path.
142WTF_EXPORT_PRIVATE String openTemporaryFile(const String& prefix, PlatformFileHandle&);
143WTF_EXPORT_PRIVATE PlatformFileHandle openFile(const String& path, FileOpenMode);
144WTF_EXPORT_PRIVATE void closeFile(PlatformFileHandle&);
145// Returns the resulting offset from the beginning of the file if successful, -1 otherwise.
146WTF_EXPORT_PRIVATE long long seekFile(PlatformFileHandle, long long offset, FileSeekOrigin);
147WTF_EXPORT_PRIVATE bool truncateFile(PlatformFileHandle, long long offset);
148// Returns number of bytes actually read if successful, -1 otherwise.
149WTF_EXPORT_PRIVATE int writeToFile(PlatformFileHandle, const char* data, int length);
150// Returns number of bytes actually written if successful, -1 otherwise.
151WTF_EXPORT_PRIVATE int readFromFile(PlatformFileHandle, char* data, int length);
152
153WTF_EXPORT_PRIVATE PlatformFileHandle openAndLockFile(const String&, FileOpenMode, OptionSet<FileLockMode> = FileLockMode::Exclusive);
154WTF_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.
158WTF_EXPORT_PRIVATE bool appendFileContentsToFileHandle(const String& path, PlatformFileHandle&);
159
160WTF_EXPORT_PRIVATE bool hardLink(const String& source, const String& destination);
161// Hard links a file if possible, copies it if not.
162WTF_EXPORT_PRIVATE bool hardLinkOrCopyFile(const String& source, const String& destination);
163
164#if USE(FILE_LOCK)
165WTF_EXPORT_PRIVATE bool lockFile(PlatformFileHandle, OptionSet<FileLockMode>);
166WTF_EXPORT_PRIVATE bool unlockFile(PlatformFileHandle);
167#endif
168
169// Encode a string for use within a file name.
170WTF_EXPORT_PRIVATE String encodeForFileName(const String&);
171WTF_EXPORT_PRIVATE String decodeFromFilename(const String&);
172
173WTF_EXPORT_PRIVATE bool filesHaveSameVolume(const String&, const String&);
174
175#if USE(CF)
176WTF_EXPORT_PRIVATE RetainPtr<CFURLRef> pathAsURL(const String&);
177#endif
178
179#if USE(GLIB)
180String filenameForDisplay(const String&);
181#endif
182
183#if OS(WINDOWS)
184WTF_EXPORT_PRIVATE String localUserSpecificStorageDirectory();
185WTF_EXPORT_PRIVATE String roamingUserSpecificStorageDirectory();
186WTF_EXPORT_PRIVATE String createTemporaryDirectory();
187WTF_EXPORT_PRIVATE bool deleteNonEmptyDirectory(const String&);
188#endif
189
190#if PLATFORM(COCOA)
191WTF_EXPORT_PRIVATE NSString *createTemporaryDirectory(NSString *directoryPrefix);
192WTF_EXPORT_PRIVATE bool deleteNonEmptyDirectory(const String&);
193#endif
194
195WTF_EXPORT_PRIVATE String realPath(const String&);
196
197WTF_EXPORT_PRIVATE bool isSafeToUseMemoryMapForPath(const String&);
198WTF_EXPORT_PRIVATE void makeSafeToUseMemoryMapForPath(const String&);
199
200WTF_EXPORT_PRIVATE bool unmapViewOfFile(void* buffer, size_t);
201
202class MappedFileData {
203 WTF_MAKE_FAST_ALLOCATED;
204public:
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
218private:
219 WTF_EXPORT_PRIVATE bool mapFileHandle(PlatformFileHandle, MappedFileMode);
220
221 void* m_fileData { nullptr };
222 unsigned m_fileSize { 0 };
223};
224
225inline MappedFileData::MappedFileData(PlatformFileHandle handle, MappedFileMode mode, bool& success)
226{
227 success = mapFileHandle(handle, mode);
228}
229
230inline 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
236inline 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
246namespace FileSystem = WTF::FileSystemImpl;
247