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 ShouldFollowSymbolicLinks { No, Yes };
100
101WTF_EXPORT_PRIVATE bool fileExists(const String&);
102WTF_EXPORT_PRIVATE bool deleteFile(const String&);
103WTF_EXPORT_PRIVATE bool deleteEmptyDirectory(const String&);
104WTF_EXPORT_PRIVATE bool moveFile(const String& oldPath, const String& newPath);
105WTF_EXPORT_PRIVATE bool getFileSize(const String&, long long& result);
106WTF_EXPORT_PRIVATE bool getFileSize(PlatformFileHandle, long long& result);
107WTF_EXPORT_PRIVATE Optional<WallTime> getFileModificationTime(const String&);
108WTF_EXPORT_PRIVATE Optional<WallTime> getFileCreationTime(const String&); // Not all platforms store file creation time.
109WTF_EXPORT_PRIVATE Optional<FileMetadata> fileMetadata(const String& path);
110WTF_EXPORT_PRIVATE Optional<FileMetadata> fileMetadataFollowingSymlinks(const String& path);
111WTF_EXPORT_PRIVATE bool fileIsDirectory(const String&, ShouldFollowSymbolicLinks);
112WTF_EXPORT_PRIVATE String pathByAppendingComponent(const String& path, const String& component);
113WTF_EXPORT_PRIVATE String pathByAppendingComponents(StringView path, const Vector<StringView>& components);
114WTF_EXPORT_PRIVATE String lastComponentOfPathIgnoringTrailingSlash(const String& path);
115WTF_EXPORT_PRIVATE bool makeAllDirectories(const String& path);
116WTF_EXPORT_PRIVATE String homeDirectoryPath();
117WTF_EXPORT_PRIVATE String pathGetFileName(const String&);
118WTF_EXPORT_PRIVATE String directoryName(const String&);
119WTF_EXPORT_PRIVATE bool getVolumeFreeSpace(const String&, uint64_t&);
120WTF_EXPORT_PRIVATE Optional<int32_t> getFileDeviceId(const CString&);
121WTF_EXPORT_PRIVATE bool createSymbolicLink(const String& targetPath, const String& symbolicLinkPath);
122
123WTF_EXPORT_PRIVATE void setMetadataURL(const String& path, const String& urlString, const String& referrer = { });
124
125bool canExcludeFromBackup(); // Returns true if any file can ever be excluded from backup.
126bool excludeFromBackup(const String&); // Returns true if successful.
127
128WTF_EXPORT_PRIVATE Vector<String> listDirectory(const String& path, const String& filter = String());
129
130WTF_EXPORT_PRIVATE CString fileSystemRepresentation(const String&);
131String stringFromFileSystemRepresentation(const char*);
132
133inline bool isHandleValid(const PlatformFileHandle& handle) { return handle != invalidPlatformFileHandle; }
134
135// Prefix is what the filename should be prefixed with, not the full path.
136WTF_EXPORT_PRIVATE String openTemporaryFile(const String& prefix, PlatformFileHandle&);
137WTF_EXPORT_PRIVATE PlatformFileHandle openFile(const String& path, FileOpenMode);
138WTF_EXPORT_PRIVATE void closeFile(PlatformFileHandle&);
139// Returns the resulting offset from the beginning of the file if successful, -1 otherwise.
140WTF_EXPORT_PRIVATE long long seekFile(PlatformFileHandle, long long offset, FileSeekOrigin);
141bool truncateFile(PlatformFileHandle, long long offset);
142// Returns number of bytes actually read if successful, -1 otherwise.
143WTF_EXPORT_PRIVATE int writeToFile(PlatformFileHandle, const char* data, int length);
144// Returns number of bytes actually written if successful, -1 otherwise.
145WTF_EXPORT_PRIVATE int readFromFile(PlatformFileHandle, char* data, int length);
146
147WTF_EXPORT_PRIVATE PlatformFileHandle openAndLockFile(const String&, FileOpenMode, OptionSet<FileLockMode> = FileLockMode::Exclusive);
148WTF_EXPORT_PRIVATE void unlockAndCloseFile(PlatformFileHandle);
149
150// Appends the contents of the file found at 'path' to the open PlatformFileHandle.
151// Returns true if the write was successful, false if it was not.
152WTF_EXPORT_PRIVATE bool appendFileContentsToFileHandle(const String& path, PlatformFileHandle&);
153
154WTF_EXPORT_PRIVATE bool hardLink(const String& source, const String& destination);
155// Hard links a file if possible, copies it if not.
156WTF_EXPORT_PRIVATE bool hardLinkOrCopyFile(const String& source, const String& destination);
157
158#if USE(FILE_LOCK)
159WTF_EXPORT_PRIVATE bool lockFile(PlatformFileHandle, OptionSet<FileLockMode>);
160WTF_EXPORT_PRIVATE bool unlockFile(PlatformFileHandle);
161#endif
162
163// Encode a string for use within a file name.
164WTF_EXPORT_PRIVATE String encodeForFileName(const String&);
165WTF_EXPORT_PRIVATE String decodeFromFilename(const String&);
166
167WTF_EXPORT_PRIVATE bool filesHaveSameVolume(const String&, const String&);
168
169#if USE(CF)
170WTF_EXPORT_PRIVATE RetainPtr<CFURLRef> pathAsURL(const String&);
171#endif
172
173#if USE(GLIB)
174String filenameForDisplay(const String&);
175#endif
176
177#if OS(WINDOWS)
178WTF_EXPORT_PRIVATE String localUserSpecificStorageDirectory();
179WTF_EXPORT_PRIVATE String roamingUserSpecificStorageDirectory();
180WTF_EXPORT_PRIVATE String createTemporaryDirectory();
181WTF_EXPORT_PRIVATE bool deleteNonEmptyDirectory(const String&);
182#endif
183
184#if PLATFORM(COCOA)
185WTF_EXPORT_PRIVATE NSString *createTemporaryDirectory(NSString *directoryPrefix);
186WTF_EXPORT_PRIVATE bool deleteNonEmptyDirectory(const String&);
187#endif
188
189WTF_EXPORT_PRIVATE String realPath(const String&);
190
191WTF_EXPORT_PRIVATE bool isSafeToUseMemoryMapForPath(const String&);
192WTF_EXPORT_PRIVATE void makeSafeToUseMemoryMapForPath(const String&);
193
194class MappedFileData {
195public:
196 MappedFileData() { }
197 MappedFileData(MappedFileData&&);
198 WTF_EXPORT_PRIVATE MappedFileData(const String& filePath, bool& success);
199 WTF_EXPORT_PRIVATE ~MappedFileData();
200 MappedFileData& operator=(MappedFileData&&);
201
202 explicit operator bool() const { return !!m_fileData; }
203 const void* data() const { return m_fileData; }
204 unsigned size() const { return m_fileSize; }
205
206private:
207 void* m_fileData { nullptr };
208 unsigned m_fileSize { 0 };
209};
210
211inline MappedFileData::MappedFileData(MappedFileData&& other)
212 : m_fileData(std::exchange(other.m_fileData, nullptr))
213 , m_fileSize(std::exchange(other.m_fileSize, 0))
214{
215}
216
217inline MappedFileData& MappedFileData::operator=(MappedFileData&& other)
218{
219 m_fileData = std::exchange(other.m_fileData, nullptr);
220 m_fileSize = std::exchange(other.m_fileSize, 0);
221 return *this;
222}
223
224} // namespace FileSystemImpl
225} // namespace WTF
226
227namespace FileSystem = WTF::FileSystemImpl;
228